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
(Delphi DLL) 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
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, StringBuilder, Rest, JsonObject; ... procedure TForm1.Button1Click(Sender: TObject); var jsonToken: HCkJsonObject; success: Boolean; rest: HCkRest; bTls: Boolean; port: Integer; bAutoReconnect: Boolean; success: Boolean; sbAuth: HCkStringBuilder; jsonReq: HCkJsonObject; sbRequestBody: HCkStringBuilder; sbResponseBody: HCkStringBuilder; respStatusCode: Integer; jsonResponse: HCkJsonObject; LineNum: Integer; Amount: PWideChar; SalesItemLineDetailTaxCodeRefValue: PWideChar; SalesItemLineDetailItemRefName: PWideChar; SalesItemLineDetailItemRefValue: PWideChar; Id: PWideChar; DetailType: PWideChar; DefinitionId: PWideChar; invType: PWideChar; Name: PWideChar; InvoiceTxnDate: PWideChar; InvoiceDomain: PWideChar; InvoicePrintStatus: PWideChar; InvoiceTotalAmt: PWideChar; InvoiceDueDate: PWideChar; InvoiceApplyTaxAfterDiscount: Boolean; InvoiceDocNumber: PWideChar; InvoiceSparse: Boolean; InvoiceCustomerMemoValue: PWideChar; InvoiceDeposit: Integer; InvoiceBalance: PWideChar; InvoiceCustomerRefName: PWideChar; InvoiceCustomerRefValue: PWideChar; InvoiceTxnTaxDetailTotalTax: Integer; InvoiceSyncToken: PWideChar; InvoiceShipAddrCountrySubDivisionCode: PWideChar; InvoiceShipAddrCity: PWideChar; InvoiceShipAddrPostalCode: PWideChar; InvoiceShipAddrId: PWideChar; InvoiceShipAddrLine1: PWideChar; InvoiceEmailStatus: PWideChar; InvoiceBillAddrCountrySubDivisionCode: PWideChar; InvoiceBillAddrCity: PWideChar; InvoiceBillAddrPostalCode: PWideChar; InvoiceBillAddrId: PWideChar; InvoiceBillAddrLine1: PWideChar; InvoiceMetaDataCreateTime: PWideChar; InvoiceMetaDataLastUpdatedTime: PWideChar; InvoiceId: PWideChar; time: PWideChar; i: Integer; count_i: Integer; begin // 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 := CkJsonObject_Create(); success := CkJsonObject_LoadFile(jsonToken,'qa_data/tokens/qb-access-token.json'); rest := CkRest_Create(); // Connect to the REST server. bTls := True; port := 443; bAutoReconnect := True; success := CkRest_Connect(rest,'sandbox-quickbooks.api.intuit.com',port,bTls,bAutoReconnect); sbAuth := CkStringBuilder_Create(); CkStringBuilder_Append(sbAuth,'Bearer '); CkStringBuilder_Append(sbAuth,CkJsonObject__stringOf(jsonToken,'access_token')); CkRest_putAuthorization(rest,CkStringBuilder__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 := CkJsonObject_Create(); CkJsonObject_UpdateString(jsonReq,'DocNumber','1070'); CkJsonObject_UpdateString(jsonReq,'SyncToken','0'); CkJsonObject_UpdateString(jsonReq,'domain','QBO'); CkJsonObject_UpdateNumber(jsonReq,'Balance','150.0'); CkJsonObject_UpdateString(jsonReq,'BillAddr.City','Bayshore'); CkJsonObject_UpdateString(jsonReq,'BillAddr.Line1','4581 Finch St.'); CkJsonObject_UpdateString(jsonReq,'BillAddr.PostalCode','94326'); CkJsonObject_UpdateString(jsonReq,'BillAddr.Lat','INVALID'); CkJsonObject_UpdateString(jsonReq,'BillAddr.Long','INVALID'); CkJsonObject_UpdateString(jsonReq,'BillAddr.CountrySubDivisionCode','CA'); CkJsonObject_UpdateString(jsonReq,'BillAddr.Id','2'); CkJsonObject_UpdateString(jsonReq,'TxnDate','2015-07-24'); CkJsonObject_UpdateNumber(jsonReq,'TotalAmt','150.0'); CkJsonObject_UpdateString(jsonReq,'CustomerRef.name','Amy''s Bird Sanctuary'); CkJsonObject_UpdateString(jsonReq,'CustomerRef.value','1'); CkJsonObject_UpdateString(jsonReq,'CustomerMemo.value','Added customer memo.'); CkJsonObject_UpdateString(jsonReq,'ShipAddr.City','Bayshore'); CkJsonObject_UpdateString(jsonReq,'ShipAddr.Line1','4581 Finch St.'); CkJsonObject_UpdateString(jsonReq,'ShipAddr.PostalCode','94326'); CkJsonObject_UpdateString(jsonReq,'ShipAddr.Lat','INVALID'); CkJsonObject_UpdateString(jsonReq,'ShipAddr.Long','INVALID'); CkJsonObject_UpdateString(jsonReq,'ShipAddr.CountrySubDivisionCode','CA'); CkJsonObject_UpdateString(jsonReq,'ShipAddr.Id','109'); CkJsonObject_UpdateNewArray(jsonReq,'LinkedTxn'); CkJsonObject_UpdateString(jsonReq,'DueDate','2015-08-23'); CkJsonObject_UpdateString(jsonReq,'PrintStatus','NeedToPrint'); CkJsonObject_UpdateString(jsonReq,'EmailStatus','NotSet'); CkJsonObject_UpdateBool(jsonReq,'sparse',False); CkJsonObject_UpdateInt(jsonReq,'Line[0].LineNum',1); CkJsonObject_UpdateNumber(jsonReq,'Line[0].Amount','150.0'); CkJsonObject_UpdateString(jsonReq,'Line[0].SalesItemLineDetail.TaxCodeRef.value','NON'); CkJsonObject_UpdateString(jsonReq,'Line[0].SalesItemLineDetail.ItemRef.name','Services'); CkJsonObject_UpdateString(jsonReq,'Line[0].SalesItemLineDetail.ItemRef.value','1'); CkJsonObject_UpdateString(jsonReq,'Line[0].Id','1'); CkJsonObject_UpdateString(jsonReq,'Line[0].DetailType','SalesItemLineDetail'); CkJsonObject_UpdateString(jsonReq,'Line[1].DetailType','SubTotalLineDetail'); CkJsonObject_UpdateNumber(jsonReq,'Line[1].Amount','150.0'); CkJsonObject_UpdateNewObject(jsonReq,'Line[1].SubTotalLineDetail'); CkJsonObject_UpdateBool(jsonReq,'ApplyTaxAfterDiscount',False); CkJsonObject_UpdateString(jsonReq,'CustomField[0].DefinitionId','1'); CkJsonObject_UpdateString(jsonReq,'CustomField[0].Type','StringType'); CkJsonObject_UpdateString(jsonReq,'CustomField[0].Name','Crew #'); CkJsonObject_UpdateString(jsonReq,'Id','239'); CkJsonObject_UpdateInt(jsonReq,'TxnTaxDetail.TotalTax',0); CkJsonObject_UpdateString(jsonReq,'MetaData.CreateTime','2015-07-24T10:35:08-07:00'); CkJsonObject_UpdateString(jsonReq,'MetaData.LastUpdatedTime','2015-07-24T10:35:08-07:00'); sbRequestBody := CkStringBuilder_Create(); CkJsonObject_EmitSb(jsonReq,sbRequestBody); CkRest_AddHeader(rest,'Content-Type','application/json'); CkRest_AddHeader(rest,'Accept','application/json'); CkRest_putAllowHeaderFolding(rest,False); sbResponseBody := CkStringBuilder_Create(); success := CkRest_FullRequestSb(rest,'POST','/v3/company/<realmID>/invoice',sbRequestBody,sbResponseBody); if (success <> True) then begin Memo1.Lines.Add(CkRest__lastErrorText(rest)); Exit; end; respStatusCode := CkRest_getResponseStatusCode(rest); // Success is indicated by a 200 response status code. Memo1.Lines.Add('response status code = ' + IntToStr(respStatusCode)); jsonResponse := CkJsonObject_Create(); CkJsonObject_LoadSb(jsonResponse,sbResponseBody); CkJsonObject_putEmitCompact(jsonResponse,False); Memo1.Lines.Add(CkJsonObject__emit(jsonResponse)); if (CkRest_getResponseStatusCode(rest) <> 200) then begin Memo1.Lines.Add('Failed.'); Exit; end; // 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" // } // InvoiceTxnDate := CkJsonObject__stringOf(jsonResponse,'Invoice.TxnDate'); InvoiceDomain := CkJsonObject__stringOf(jsonResponse,'Invoice.domain'); InvoicePrintStatus := CkJsonObject__stringOf(jsonResponse,'Invoice.PrintStatus'); InvoiceTotalAmt := CkJsonObject__stringOf(jsonResponse,'Invoice.TotalAmt'); InvoiceDueDate := CkJsonObject__stringOf(jsonResponse,'Invoice.DueDate'); InvoiceApplyTaxAfterDiscount := CkJsonObject_BoolOf(jsonResponse,'Invoice.ApplyTaxAfterDiscount'); InvoiceDocNumber := CkJsonObject__stringOf(jsonResponse,'Invoice.DocNumber'); InvoiceSparse := CkJsonObject_BoolOf(jsonResponse,'Invoice.sparse'); InvoiceCustomerMemoValue := CkJsonObject__stringOf(jsonResponse,'Invoice.CustomerMemo.value'); InvoiceDeposit := CkJsonObject_IntOf(jsonResponse,'Invoice.Deposit'); InvoiceBalance := CkJsonObject__stringOf(jsonResponse,'Invoice.Balance'); InvoiceCustomerRefName := CkJsonObject__stringOf(jsonResponse,'Invoice.CustomerRef.name'); InvoiceCustomerRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.CustomerRef.value'); InvoiceTxnTaxDetailTotalTax := CkJsonObject_IntOf(jsonResponse,'Invoice.TxnTaxDetail.TotalTax'); InvoiceSyncToken := CkJsonObject__stringOf(jsonResponse,'Invoice.SyncToken'); InvoiceShipAddrCountrySubDivisionCode := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.CountrySubDivisionCode'); InvoiceShipAddrCity := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.City'); InvoiceShipAddrPostalCode := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.PostalCode'); InvoiceShipAddrId := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.Id'); InvoiceShipAddrLine1 := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.Line1'); InvoiceEmailStatus := CkJsonObject__stringOf(jsonResponse,'Invoice.EmailStatus'); InvoiceBillAddrCountrySubDivisionCode := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.CountrySubDivisionCode'); InvoiceBillAddrCity := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.City'); InvoiceBillAddrPostalCode := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.PostalCode'); InvoiceBillAddrId := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.Id'); InvoiceBillAddrLine1 := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.Line1'); InvoiceMetaDataCreateTime := CkJsonObject__stringOf(jsonResponse,'Invoice.MetaData.CreateTime'); InvoiceMetaDataLastUpdatedTime := CkJsonObject__stringOf(jsonResponse,'Invoice.MetaData.LastUpdatedTime'); InvoiceId := CkJsonObject__stringOf(jsonResponse,'Invoice.Id'); time := CkJsonObject__stringOf(jsonResponse,'time'); i := 0; count_i := CkJsonObject_SizeOfArray(jsonResponse,'Invoice.Line'); while i < count_i do begin CkJsonObject_putI(jsonResponse,i); LineNum := CkJsonObject_IntOf(jsonResponse,'Invoice.Line[i].LineNum'); Amount := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].Amount'); SalesItemLineDetailTaxCodeRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value'); SalesItemLineDetailItemRefName := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].SalesItemLineDetail.ItemRef.name'); SalesItemLineDetailItemRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].SalesItemLineDetail.ItemRef.value'); Id := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].Id'); DetailType := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].DetailType'); i := i + 1; end; i := 0; count_i := CkJsonObject_SizeOfArray(jsonResponse,'Invoice.LinkedTxn'); while i < count_i do begin CkJsonObject_putI(jsonResponse,i); i := i + 1; end; i := 0; count_i := CkJsonObject_SizeOfArray(jsonResponse,'Invoice.CustomField'); while i < count_i do begin CkJsonObject_putI(jsonResponse,i); DefinitionId := CkJsonObject__stringOf(jsonResponse,'Invoice.CustomField[i].DefinitionId'); invType := CkJsonObject__stringOf(jsonResponse,'Invoice.CustomField[i].Type'); Name := CkJsonObject__stringOf(jsonResponse,'Invoice.CustomField[i].Name'); i := i + 1; end; CkJsonObject_Dispose(jsonToken); CkRest_Dispose(rest); CkStringBuilder_Dispose(sbAuth); CkJsonObject_Dispose(jsonReq); CkStringBuilder_Dispose(sbRequestBody); CkStringBuilder_Dispose(sbResponseBody); CkJsonObject_Dispose(jsonResponse); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.