Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Quickbooks Update an InvoiceDemonstrates how to update an invoice using the Quickbooks REST API. For more information, see https://www.developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/invoice#full-update-an-invoice
#include <C_CkJsonObjectW.h> #include <C_CkRestW.h> #include <C_CkStringBuilderW.h> void ChilkatSample(void) { HCkJsonObjectW jsonToken; BOOL success; HCkRestW rest; BOOL bTls; int port; BOOL bAutoReconnect; BOOL success; HCkStringBuilderW sbAuth; HCkJsonObjectW jsonReq; HCkStringBuilderW sbRequestBody; HCkStringBuilderW sbResponseBody; int respStatusCode; HCkJsonObjectW jsonResponse; int LineNum; const wchar_t *Amount; const wchar_t *SalesItemLineDetailTaxCodeRefValue; const wchar_t *SalesItemLineDetailItemRefName; const wchar_t *SalesItemLineDetailItemRefValue; const wchar_t *Id; const wchar_t *DetailType; const wchar_t *DefinitionId; const wchar_t *invType; const wchar_t *Name; const wchar_t *InvoiceTxnDate; const wchar_t *InvoiceDomain; const wchar_t *InvoicePrintStatus; const wchar_t *InvoiceTotalAmt; const wchar_t *InvoiceDueDate; BOOL InvoiceApplyTaxAfterDiscount; const wchar_t *InvoiceDocNumber; BOOL InvoiceSparse; const wchar_t *InvoiceCustomerMemoValue; int InvoiceDeposit; const wchar_t *InvoiceBalance; const wchar_t *InvoiceCustomerRefName; const wchar_t *InvoiceCustomerRefValue; int InvoiceTxnTaxDetailTotalTax; const wchar_t *InvoiceSyncToken; const wchar_t *InvoiceShipAddrCountrySubDivisionCode; const wchar_t *InvoiceShipAddrCity; const wchar_t *InvoiceShipAddrPostalCode; const wchar_t *InvoiceShipAddrId; const wchar_t *InvoiceShipAddrLine1; const wchar_t *InvoiceEmailStatus; const wchar_t *InvoiceBillAddrCountrySubDivisionCode; const wchar_t *InvoiceBillAddrCity; const wchar_t *InvoiceBillAddrPostalCode; const wchar_t *InvoiceBillAddrId; const wchar_t *InvoiceBillAddrLine1; const wchar_t *InvoiceMetaDataCreateTime; const wchar_t *InvoiceMetaDataLastUpdatedTime; const wchar_t *InvoiceId; const wchar_t *time; int i; int count_i; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // First get our previously obtained OAuth2 access token. jsonToken = CkJsonObjectW_Create(); success = CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/qb-access-token.json"); rest = CkRestW_Create(); // Connect to the REST server. bTls = TRUE; port = 443; bAutoReconnect = TRUE; success = CkRestW_Connect(rest,L"sandbox-quickbooks.api.intuit.com",port,bTls,bAutoReconnect); sbAuth = CkStringBuilderW_Create(); CkStringBuilderW_Append(sbAuth,L"Bearer "); CkStringBuilderW_Append(sbAuth,CkJsonObjectW_stringOf(jsonToken,L"access_token")); CkRestW_putAuthorization(rest,CkStringBuilderW_getAsString(sbAuth)); // -------------------------------------------------------------------------- // Note: The above code to setup the initial REST connection // can be done once. After connecting, any number of REST calls can be made. // If the connection is lost, the next REST method call will automatically // reconnect if needed. // -------------------------------------------------------------------------- // Create the following JSON: // { // "DocNumber": "1070", // "SyncToken": "0", // "domain": "QBO", // "Balance": 150.0, // "BillAddr": { // "City": "Bayshore", // "Line1": "4581 Finch St.", // "PostalCode": "94326", // "Lat": "INVALID", // "Long": "INVALID", // "CountrySubDivisionCode": "CA", // "Id": "2" // }, // "TxnDate": "2015-07-24", // "TotalAmt": 150.0, // "CustomerRef": { // "name": "Amy's Bird Sanctuary", // "value": "1" // }, // "CustomerMemo": { // "value": "Added customer memo." // }, // "ShipAddr": { // "City": "Bayshore", // "Line1": "4581 Finch St.", // "PostalCode": "94326", // "Lat": "INVALID", // "Long": "INVALID", // "CountrySubDivisionCode": "CA", // "Id": "109" // }, // "LinkedTxn": [ // ], // "DueDate": "2015-08-23", // "PrintStatus": "NeedToPrint", // "EmailStatus": "NotSet", // "sparse": false, // "Line": [ // { // "LineNum": 1, // "Amount": 150.0, // "SalesItemLineDetail": { // "TaxCodeRef": { // "value": "NON" // }, // "ItemRef": { // "name": "Services", // "value": "1" // } // }, // "Id": "1", // "DetailType": "SalesItemLineDetail" // }, // { // "DetailType": "SubTotalLineDetail", // "Amount": 150.0, // "SubTotalLineDetail": {} // } // ], // "ApplyTaxAfterDiscount": false, // "CustomField": [ // { // "DefinitionId": "1", // "Type": "StringType", // "Name": "Crew #" // } // ], // "Id": "239", // "TxnTaxDetail": { // "TotalTax": 0 // }, // "MetaData": { // "CreateTime": "2015-07-24T10:35:08-07:00", // "LastUpdatedTime": "2015-07-24T10:35:08-07:00" // } // } // // Use the this online tool to generate the code from sample JSON: // Generate Code to Create JSON jsonReq = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(jsonReq,L"DocNumber",L"1070"); CkJsonObjectW_UpdateString(jsonReq,L"SyncToken",L"0"); CkJsonObjectW_UpdateString(jsonReq,L"domain",L"QBO"); CkJsonObjectW_UpdateNumber(jsonReq,L"Balance",L"150.0"); CkJsonObjectW_UpdateString(jsonReq,L"BillAddr.City",L"Bayshore"); CkJsonObjectW_UpdateString(jsonReq,L"BillAddr.Line1",L"4581 Finch St."); CkJsonObjectW_UpdateString(jsonReq,L"BillAddr.PostalCode",L"94326"); CkJsonObjectW_UpdateString(jsonReq,L"BillAddr.Lat",L"INVALID"); CkJsonObjectW_UpdateString(jsonReq,L"BillAddr.Long",L"INVALID"); CkJsonObjectW_UpdateString(jsonReq,L"BillAddr.CountrySubDivisionCode",L"CA"); CkJsonObjectW_UpdateString(jsonReq,L"BillAddr.Id",L"2"); CkJsonObjectW_UpdateString(jsonReq,L"TxnDate",L"2015-07-24"); CkJsonObjectW_UpdateNumber(jsonReq,L"TotalAmt",L"150.0"); CkJsonObjectW_UpdateString(jsonReq,L"CustomerRef.name",L"Amy's Bird Sanctuary"); CkJsonObjectW_UpdateString(jsonReq,L"CustomerRef.value",L"1"); CkJsonObjectW_UpdateString(jsonReq,L"CustomerMemo.value",L"Added customer memo."); CkJsonObjectW_UpdateString(jsonReq,L"ShipAddr.City",L"Bayshore"); CkJsonObjectW_UpdateString(jsonReq,L"ShipAddr.Line1",L"4581 Finch St."); CkJsonObjectW_UpdateString(jsonReq,L"ShipAddr.PostalCode",L"94326"); CkJsonObjectW_UpdateString(jsonReq,L"ShipAddr.Lat",L"INVALID"); CkJsonObjectW_UpdateString(jsonReq,L"ShipAddr.Long",L"INVALID"); CkJsonObjectW_UpdateString(jsonReq,L"ShipAddr.CountrySubDivisionCode",L"CA"); CkJsonObjectW_UpdateString(jsonReq,L"ShipAddr.Id",L"109"); CkJsonObjectW_UpdateNewArray(jsonReq,L"LinkedTxn"); CkJsonObjectW_UpdateString(jsonReq,L"DueDate",L"2015-08-23"); CkJsonObjectW_UpdateString(jsonReq,L"PrintStatus",L"NeedToPrint"); CkJsonObjectW_UpdateString(jsonReq,L"EmailStatus",L"NotSet"); CkJsonObjectW_UpdateBool(jsonReq,L"sparse",FALSE); CkJsonObjectW_UpdateInt(jsonReq,L"Line[0].LineNum",1); CkJsonObjectW_UpdateNumber(jsonReq,L"Line[0].Amount",L"150.0"); CkJsonObjectW_UpdateString(jsonReq,L"Line[0].SalesItemLineDetail.TaxCodeRef.value",L"NON"); CkJsonObjectW_UpdateString(jsonReq,L"Line[0].SalesItemLineDetail.ItemRef.name",L"Services"); CkJsonObjectW_UpdateString(jsonReq,L"Line[0].SalesItemLineDetail.ItemRef.value",L"1"); CkJsonObjectW_UpdateString(jsonReq,L"Line[0].Id",L"1"); CkJsonObjectW_UpdateString(jsonReq,L"Line[0].DetailType",L"SalesItemLineDetail"); CkJsonObjectW_UpdateString(jsonReq,L"Line[1].DetailType",L"SubTotalLineDetail"); CkJsonObjectW_UpdateNumber(jsonReq,L"Line[1].Amount",L"150.0"); CkJsonObjectW_UpdateNewObject(jsonReq,L"Line[1].SubTotalLineDetail"); CkJsonObjectW_UpdateBool(jsonReq,L"ApplyTaxAfterDiscount",FALSE); CkJsonObjectW_UpdateString(jsonReq,L"CustomField[0].DefinitionId",L"1"); CkJsonObjectW_UpdateString(jsonReq,L"CustomField[0].Type",L"StringType"); CkJsonObjectW_UpdateString(jsonReq,L"CustomField[0].Name",L"Crew #"); CkJsonObjectW_UpdateString(jsonReq,L"Id",L"239"); CkJsonObjectW_UpdateInt(jsonReq,L"TxnTaxDetail.TotalTax",0); CkJsonObjectW_UpdateString(jsonReq,L"MetaData.CreateTime",L"2015-07-24T10:35:08-07:00"); CkJsonObjectW_UpdateString(jsonReq,L"MetaData.LastUpdatedTime",L"2015-07-24T10:35:08-07:00"); sbRequestBody = CkStringBuilderW_Create(); CkJsonObjectW_EmitSb(jsonReq,sbRequestBody); CkRestW_AddHeader(rest,L"Content-Type",L"application/json"); CkRestW_AddHeader(rest,L"Accept",L"application/json"); CkRestW_putAllowHeaderFolding(rest,FALSE); sbResponseBody = CkStringBuilderW_Create(); success = CkRestW_FullRequestSb(rest,L"POST",L"/v3/company/<realmID>/invoice",sbRequestBody,sbResponseBody); if (success != TRUE) { wprintf(L"%s\n",CkRestW_lastErrorText(rest)); CkJsonObjectW_Dispose(jsonToken); CkRestW_Dispose(rest); CkStringBuilderW_Dispose(sbAuth); CkJsonObjectW_Dispose(jsonReq); CkStringBuilderW_Dispose(sbRequestBody); CkStringBuilderW_Dispose(sbResponseBody); return; } respStatusCode = CkRestW_getResponseStatusCode(rest); // Success is indicated by a 200 response status code. wprintf(L"response status code = %d\n",respStatusCode); jsonResponse = CkJsonObjectW_Create(); CkJsonObjectW_LoadSb(jsonResponse,sbResponseBody); CkJsonObjectW_putEmitCompact(jsonResponse,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(jsonResponse)); if (CkRestW_getResponseStatusCode(rest) != 200) { wprintf(L"Failed.\n"); CkJsonObjectW_Dispose(jsonToken); CkRestW_Dispose(rest); CkStringBuilderW_Dispose(sbAuth); CkJsonObjectW_Dispose(jsonReq); CkStringBuilderW_Dispose(sbRequestBody); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jsonResponse); return; } // Sample output... // (See the parsing code below..) // // Use the this online tool to generate parsing code from sample JSON: // Generate Parsing Code from JSON // { // "Invoice": { // "TxnDate": "2015-07-24", // "domain": "QBO", // "PrintStatus": "NeedToPrint", // "TotalAmt": 150.0, // "Line": [ // { // "LineNum": 1, // "Amount": 150.0, // "SalesItemLineDetail": { // "TaxCodeRef": { // "value": "NON" // }, // "ItemRef": { // "name": "Services", // "value": "1" // } // }, // "Id": "1", // "DetailType": "SalesItemLineDetail" // }, // { // "DetailType": "SubTotalLineDetail", // "Amount": 150.0, // "SubTotalLineDetail": {} // } // ], // "DueDate": "2015-08-23", // "ApplyTaxAfterDiscount": false, // "DocNumber": "1070", // "sparse": false, // "CustomerMemo": { // "value": "Added customer memo." // }, // "Deposit": 0, // "Balance": 150.0, // "CustomerRef": { // "name": "Amy's Bird Sanctuary", // "value": "1" // }, // "TxnTaxDetail": { // "TotalTax": 0 // }, // "SyncToken": "1", // "LinkedTxn": [ // ], // "ShipAddr": { // "CountrySubDivisionCode": "CA", // "City": "Bayshore", // "PostalCode": "94326", // "Id": "118", // "Line1": "4581 Finch St." // }, // "EmailStatus": "NotSet", // "BillAddr": { // "CountrySubDivisionCode": "CA", // "City": "Bayshore", // "PostalCode": "94326", // "Id": "117", // "Line1": "4581 Finch St." // }, // "MetaData": { // "CreateTime": "2015-07-24T10:35:08-07:00", // "LastUpdatedTime": "2015-07-24T10:53:39-07:00" // }, // "CustomField": [ // { // "DefinitionId": "1", // "Type": "StringType", // "Name": "Crew #" // } // ], // "Id": "239" // }, // "time": "2015-07-24T10:53:39.287-07:00" // } // // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat. // See this example explaining how this memory should be used: const char * functions. InvoiceTxnDate = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.TxnDate"); InvoiceDomain = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.domain"); InvoicePrintStatus = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.PrintStatus"); InvoiceTotalAmt = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.TotalAmt"); InvoiceDueDate = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.DueDate"); InvoiceApplyTaxAfterDiscount = CkJsonObjectW_BoolOf(jsonResponse,L"Invoice.ApplyTaxAfterDiscount"); InvoiceDocNumber = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.DocNumber"); InvoiceSparse = CkJsonObjectW_BoolOf(jsonResponse,L"Invoice.sparse"); InvoiceCustomerMemoValue = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.CustomerMemo.value"); InvoiceDeposit = CkJsonObjectW_IntOf(jsonResponse,L"Invoice.Deposit"); InvoiceBalance = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.Balance"); InvoiceCustomerRefName = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.CustomerRef.name"); InvoiceCustomerRefValue = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.CustomerRef.value"); InvoiceTxnTaxDetailTotalTax = CkJsonObjectW_IntOf(jsonResponse,L"Invoice.TxnTaxDetail.TotalTax"); InvoiceSyncToken = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.SyncToken"); InvoiceShipAddrCountrySubDivisionCode = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.ShipAddr.CountrySubDivisionCode"); InvoiceShipAddrCity = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.ShipAddr.City"); InvoiceShipAddrPostalCode = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.ShipAddr.PostalCode"); InvoiceShipAddrId = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.ShipAddr.Id"); InvoiceShipAddrLine1 = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.ShipAddr.Line1"); InvoiceEmailStatus = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.EmailStatus"); InvoiceBillAddrCountrySubDivisionCode = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.BillAddr.CountrySubDivisionCode"); InvoiceBillAddrCity = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.BillAddr.City"); InvoiceBillAddrPostalCode = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.BillAddr.PostalCode"); InvoiceBillAddrId = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.BillAddr.Id"); InvoiceBillAddrLine1 = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.BillAddr.Line1"); InvoiceMetaDataCreateTime = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.MetaData.CreateTime"); InvoiceMetaDataLastUpdatedTime = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.MetaData.LastUpdatedTime"); InvoiceId = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.Id"); time = CkJsonObjectW_stringOf(jsonResponse,L"time"); i = 0; count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"Invoice.Line"); while (i < count_i) { CkJsonObjectW_putI(jsonResponse,i); LineNum = CkJsonObjectW_IntOf(jsonResponse,L"Invoice.Line[i].LineNum"); Amount = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.Line[i].Amount"); SalesItemLineDetailTaxCodeRefValue = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value"); SalesItemLineDetailItemRefName = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.Line[i].SalesItemLineDetail.ItemRef.name"); SalesItemLineDetailItemRefValue = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.Line[i].SalesItemLineDetail.ItemRef.value"); Id = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.Line[i].Id"); DetailType = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.Line[i].DetailType"); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"Invoice.LinkedTxn"); while (i < count_i) { CkJsonObjectW_putI(jsonResponse,i); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jsonResponse,L"Invoice.CustomField"); while (i < count_i) { CkJsonObjectW_putI(jsonResponse,i); DefinitionId = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.CustomField[i].DefinitionId"); invType = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.CustomField[i].Type"); Name = CkJsonObjectW_stringOf(jsonResponse,L"Invoice.CustomField[i].Name"); i = i + 1; } CkJsonObjectW_Dispose(jsonToken); CkRestW_Dispose(rest); CkStringBuilderW_Dispose(sbAuth); CkJsonObjectW_Dispose(jsonReq); CkStringBuilderW_Dispose(sbRequestBody); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jsonResponse); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.