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 Send an InvoiceDemonstrates how to send 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#send-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; sbResponseBody: HCkStringBuilder; respStatusCode: Integer; jsonResponse: HCkJsonObject; Description: PWideChar; DetailType: PWideChar; SalesItemLineDetailTaxCodeRefValue: PWideChar; SalesItemLineDetailQty: Integer; SalesItemLineDetailUnitPrice: Integer; SalesItemLineDetailServiceDate: PWideChar; SalesItemLineDetailItemRefName: PWideChar; SalesItemLineDetailItemRefValue: PWideChar; LineNum: Integer; Amount: PWideChar; Id: PWideChar; DiscountLineDetailDiscountAccountRefName: PWideChar; DiscountLineDetailDiscountAccountRefValue: PWideChar; DiscountLineDetailPercentBased: Boolean; DiscountLineDetailDiscountPercent: Integer; TaxLineDetailNetAmountTaxable: PWideChar; TaxLineDetailTaxPercent: Integer; TaxLineDetailTaxRateRefValue: PWideChar; TaxLineDetailPercentBased: Boolean; StringValue: PWideChar; Type: PWideChar; Name: PWideChar; InvoiceTxnDate: PWideChar; InvoiceDomain: PWideChar; InvoiceCurrencyRefName: PWideChar; InvoiceCurrencyRefValue: PWideChar; InvoiceShipDate: PWideChar; InvoiceTrackingNum: PWideChar; InvoiceClassRefName: PWideChar; InvoiceClassRefValue: PWideChar; InvoicePrintStatus: PWideChar; InvoiceSalesTermRefValue: PWideChar; InvoiceDeliveryInfoDeliveryType: PWideChar; InvoiceDeliveryInfoDeliveryTime: PWideChar; InvoiceTotalAmt: PWideChar; InvoiceDueDate: PWideChar; InvoiceMetaDataCreateTime: PWideChar; InvoiceMetaDataLastUpdatedTime: PWideChar; InvoiceDocNumber: PWideChar; InvoicePrivateNote: PWideChar; InvoiceSparse: Boolean; InvoiceDepositToAccountRefName: PWideChar; InvoiceDepositToAccountRefValue: PWideChar; InvoiceCustomerMemoValue: PWideChar; InvoiceEmailStatus: PWideChar; InvoiceDeposit: PWideChar; InvoiceBalance: PWideChar; InvoiceCustomerRefName: PWideChar; InvoiceCustomerRefValue: PWideChar; InvoiceTxnTaxDetailTxnTaxCodeRefValue: PWideChar; InvoiceTxnTaxDetailTotalTax: PWideChar; InvoiceSyncToken: PWideChar; InvoiceBillEmailAddress: PWideChar; InvoiceShipAddrCity: PWideChar; InvoiceShipAddrCountry: PWideChar; InvoiceShipAddrLine5: PWideChar; InvoiceShipAddrLine4: PWideChar; InvoiceShipAddrLine3: PWideChar; InvoiceShipAddrLine2: PWideChar; InvoiceShipAddrLine1: PWideChar; InvoiceShipAddrPostalCode: PWideChar; InvoiceShipAddrLat: PWideChar; InvoiceShipAddrLong: PWideChar; InvoiceShipAddrCountrySubDivisionCode: PWideChar; InvoiceShipAddrId: PWideChar; InvoiceDepartmentRefName: PWideChar; InvoiceDepartmentRefValue: PWideChar; InvoiceShipMethodRefName: PWideChar; InvoiceShipMethodRefValue: PWideChar; InvoiceBillAddrCity: PWideChar; InvoiceBillAddrCountry: PWideChar; InvoiceBillAddrLine5: PWideChar; InvoiceBillAddrLine4: PWideChar; InvoiceBillAddrLine3: PWideChar; InvoiceBillAddrLine2: PWideChar; InvoiceBillAddrLine1: PWideChar; InvoiceBillAddrPostalCode: PWideChar; InvoiceBillAddrLat: PWideChar; InvoiceBillAddrLong: PWideChar; InvoiceBillAddrCountrySubDivisionCode: PWideChar; InvoiceBillAddrId: PWideChar; InvoiceApplyTaxAfterDiscount: Boolean; 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. // -------------------------------------------------------------------------- // Technically, the POST has an empty request body, but the Quickbooks documentation indicates that // the Content-Type header should be set to "application/octet-stream", which really makes no sense // because there is not content. (How can no content have a type???) CkRest_AddHeader(rest,'Content-Type','application/octet-stream'); CkRest_putAllowHeaderFolding(rest,False); sbResponseBody := CkStringBuilder_Create(); success := CkRest_FullRequestNoBodySb(rest,'POST','/v3/company/<realmID>/invoice/<invoiceId>/send?sendTo=<emailAddr>',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": "2013-03-14", // "domain": "QBO", // "CurrencyRef": { // "name": "United States Dollar", // "value": "USD" // }, // "ShipDate": "2013-03-01", // "TrackingNum": "123456789", // "ClassRef": { // "name": "Class 1", // "value": "200900000000000003901" // }, // "PrintStatus": "NeedToPrint", // "SalesTermRef": { // "value": "4" // }, // "DeliveryInfo": { // "DeliveryType": "Email", // "DeliveryTime": "2014-12-17T11:50:52-08:00" // }, // "TotalAmt": 52.0, // "Line": [ // { // "Description": "Sample invoice create request", // "DetailType": "SalesItemLineDetail", // "SalesItemLineDetail": { // "TaxCodeRef": { // "value": "TAX" // }, // "Qty": 1, // "UnitPrice": 50, // "ServiceDate": "2013-03-04", // "ItemRef": { // "name": "Hours", // "value": "2" // } // }, // "LineNum": 1, // "Amount": 50.0, // "Id": "1" // }, // { // "DetailType": "SubTotalLineDetail", // "Amount": 50.0, // "SubTotalLineDetail": {} // }, // { // "DetailType": "DiscountLineDetail", // "Amount": 5.0, // "DiscountLineDetail": { // "DiscountAccountRef": { // "name": "Discounts given", // "value": "30" // }, // "PercentBased": true, // "DiscountPercent": 10 // } // }, // { // "DetailType": "SalesItemLineDetail", // "Amount": 2.0, // "SalesItemLineDetail": { // "ItemRef": { // "value": "SHIPPING_ITEM_ID" // } // } // } // ], // "DueDate": "2013-05-13", // "MetaData": { // "CreateTime": "2013-03-14T01:42:16-07:00", // "LastUpdatedTime": "2014-12-17T11:50:58-08:00" // }, // "DocNumber": "Sample_Inv#2", // "PrivateNote": "Summary for sample invoice", // "sparse": false, // "DepositToAccountRef": { // "name": "Undeposited Funds", // "value": "4" // }, // "CustomerMemo": { // "value": "This is the customer message" // }, // "EmailStatus": "EmailSent", // "Deposit": 12.0, // "Balance": 40.0, // "CustomerRef": { // "name": "Mr V3 Service Customer Jr2", // "value": "15" // }, // "TxnTaxDetail": { // "TxnTaxCodeRef": { // "value": "5" // }, // "TotalTax": 5.0, // "TaxLine": [ // { // "DetailType": "TaxLineDetail", // "Amount": 5.0, // "TaxLineDetail": { // "NetAmountTaxable": 50.0, // "TaxPercent": 10, // "TaxRateRef": { // "value": "2" // }, // "PercentBased": true // } // } // ] // }, // "SyncToken": "0", // "BillEmail": { // "Address": "test@intuit.com" // }, // "ShipAddr": { // "City": "San Jose", // "Country": "USA", // "Line5": "Cube 999", // "Line4": "Dept 12", // "Line3": "123 street", // "Line2": "Building 1", // "Line1": "Intuit", // "PostalCode": "95123", // "Lat": "37.2374847", // "Long": "-121.8277925", // "CountrySubDivisionCode": "CA", // "Id": "36" // }, // "DepartmentRef": { // "name": "Mountain View", // "value": "1" // }, // "ShipMethodRef": { // "name": "UPS", // "value": "UPS" // }, // "BillAddr": { // "City": "Mountain View", // "Country": "USA", // "Line5": "Cube 999", // "Line4": "Dept 12", // "Line3": "123 street", // "Line2": "Building 1", // "Line1": "Google", // "PostalCode": "95123", // "Lat": "37.2374847", // "Long": "-121.8277925", // "CountrySubDivisionCode": "CA", // "Id": "35" // }, // "ApplyTaxAfterDiscount": false, // "CustomField": [ // { // "StringValue": "Custom1", // "Type": "StringType", // "Name": "Custom 1" // }, // { // "StringValue": "Custom2", // "Type": "StringType", // "Name": "Custom 2" // }, // { // "StringValue": "Custom3", // "Type": "StringType", // "Name": "Custom 3" // } // ], // "Id": "96" // }, // "time": "2013-03-14T13:32:04.895-07:00" // } // InvoiceTxnDate := CkJsonObject__stringOf(jsonResponse,'Invoice.TxnDate'); InvoiceDomain := CkJsonObject__stringOf(jsonResponse,'Invoice.domain'); InvoiceCurrencyRefName := CkJsonObject__stringOf(jsonResponse,'Invoice.CurrencyRef.name'); InvoiceCurrencyRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.CurrencyRef.value'); InvoiceShipDate := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipDate'); InvoiceTrackingNum := CkJsonObject__stringOf(jsonResponse,'Invoice.TrackingNum'); InvoiceClassRefName := CkJsonObject__stringOf(jsonResponse,'Invoice.ClassRef.name'); InvoiceClassRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.ClassRef.value'); InvoicePrintStatus := CkJsonObject__stringOf(jsonResponse,'Invoice.PrintStatus'); InvoiceSalesTermRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.SalesTermRef.value'); InvoiceDeliveryInfoDeliveryType := CkJsonObject__stringOf(jsonResponse,'Invoice.DeliveryInfo.DeliveryType'); InvoiceDeliveryInfoDeliveryTime := CkJsonObject__stringOf(jsonResponse,'Invoice.DeliveryInfo.DeliveryTime'); InvoiceTotalAmt := CkJsonObject__stringOf(jsonResponse,'Invoice.TotalAmt'); InvoiceDueDate := CkJsonObject__stringOf(jsonResponse,'Invoice.DueDate'); InvoiceMetaDataCreateTime := CkJsonObject__stringOf(jsonResponse,'Invoice.MetaData.CreateTime'); InvoiceMetaDataLastUpdatedTime := CkJsonObject__stringOf(jsonResponse,'Invoice.MetaData.LastUpdatedTime'); InvoiceDocNumber := CkJsonObject__stringOf(jsonResponse,'Invoice.DocNumber'); InvoicePrivateNote := CkJsonObject__stringOf(jsonResponse,'Invoice.PrivateNote'); InvoiceSparse := CkJsonObject_BoolOf(jsonResponse,'Invoice.sparse'); InvoiceDepositToAccountRefName := CkJsonObject__stringOf(jsonResponse,'Invoice.DepositToAccountRef.name'); InvoiceDepositToAccountRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.DepositToAccountRef.value'); InvoiceCustomerMemoValue := CkJsonObject__stringOf(jsonResponse,'Invoice.CustomerMemo.value'); InvoiceEmailStatus := CkJsonObject__stringOf(jsonResponse,'Invoice.EmailStatus'); InvoiceDeposit := CkJsonObject__stringOf(jsonResponse,'Invoice.Deposit'); InvoiceBalance := CkJsonObject__stringOf(jsonResponse,'Invoice.Balance'); InvoiceCustomerRefName := CkJsonObject__stringOf(jsonResponse,'Invoice.CustomerRef.name'); InvoiceCustomerRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.CustomerRef.value'); InvoiceTxnTaxDetailTxnTaxCodeRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.TxnTaxDetail.TxnTaxCodeRef.value'); InvoiceTxnTaxDetailTotalTax := CkJsonObject__stringOf(jsonResponse,'Invoice.TxnTaxDetail.TotalTax'); InvoiceSyncToken := CkJsonObject__stringOf(jsonResponse,'Invoice.SyncToken'); InvoiceBillEmailAddress := CkJsonObject__stringOf(jsonResponse,'Invoice.BillEmail.Address'); InvoiceShipAddrCity := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.City'); InvoiceShipAddrCountry := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.Country'); InvoiceShipAddrLine5 := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.Line5'); InvoiceShipAddrLine4 := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.Line4'); InvoiceShipAddrLine3 := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.Line3'); InvoiceShipAddrLine2 := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.Line2'); InvoiceShipAddrLine1 := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.Line1'); InvoiceShipAddrPostalCode := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.PostalCode'); InvoiceShipAddrLat := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.Lat'); InvoiceShipAddrLong := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.Long'); InvoiceShipAddrCountrySubDivisionCode := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.CountrySubDivisionCode'); InvoiceShipAddrId := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipAddr.Id'); InvoiceDepartmentRefName := CkJsonObject__stringOf(jsonResponse,'Invoice.DepartmentRef.name'); InvoiceDepartmentRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.DepartmentRef.value'); InvoiceShipMethodRefName := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipMethodRef.name'); InvoiceShipMethodRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.ShipMethodRef.value'); InvoiceBillAddrCity := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.City'); InvoiceBillAddrCountry := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.Country'); InvoiceBillAddrLine5 := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.Line5'); InvoiceBillAddrLine4 := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.Line4'); InvoiceBillAddrLine3 := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.Line3'); InvoiceBillAddrLine2 := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.Line2'); InvoiceBillAddrLine1 := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.Line1'); InvoiceBillAddrPostalCode := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.PostalCode'); InvoiceBillAddrLat := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.Lat'); InvoiceBillAddrLong := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.Long'); InvoiceBillAddrCountrySubDivisionCode := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.CountrySubDivisionCode'); InvoiceBillAddrId := CkJsonObject__stringOf(jsonResponse,'Invoice.BillAddr.Id'); InvoiceApplyTaxAfterDiscount := CkJsonObject_BoolOf(jsonResponse,'Invoice.ApplyTaxAfterDiscount'); 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); Description := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].Description'); DetailType := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].DetailType'); SalesItemLineDetailTaxCodeRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value'); SalesItemLineDetailQty := CkJsonObject_IntOf(jsonResponse,'Invoice.Line[i].SalesItemLineDetail.Qty'); SalesItemLineDetailUnitPrice := CkJsonObject_IntOf(jsonResponse,'Invoice.Line[i].SalesItemLineDetail.UnitPrice'); SalesItemLineDetailServiceDate := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].SalesItemLineDetail.ServiceDate'); SalesItemLineDetailItemRefName := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].SalesItemLineDetail.ItemRef.name'); SalesItemLineDetailItemRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].SalesItemLineDetail.ItemRef.value'); LineNum := CkJsonObject_IntOf(jsonResponse,'Invoice.Line[i].LineNum'); Amount := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].Amount'); Id := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].Id'); DiscountLineDetailDiscountAccountRefName := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.name'); DiscountLineDetailDiscountAccountRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.value'); DiscountLineDetailPercentBased := CkJsonObject_BoolOf(jsonResponse,'Invoice.Line[i].DiscountLineDetail.PercentBased'); DiscountLineDetailDiscountPercent := CkJsonObject_IntOf(jsonResponse,'Invoice.Line[i].DiscountLineDetail.DiscountPercent'); i := i + 1; end; i := 0; count_i := CkJsonObject_SizeOfArray(jsonResponse,'Invoice.TxnTaxDetail.TaxLine'); while i < count_i do begin CkJsonObject_putI(jsonResponse,i); DetailType := CkJsonObject__stringOf(jsonResponse,'Invoice.TxnTaxDetail.TaxLine[i].DetailType'); Amount := CkJsonObject__stringOf(jsonResponse,'Invoice.TxnTaxDetail.TaxLine[i].Amount'); TaxLineDetailNetAmountTaxable := CkJsonObject__stringOf(jsonResponse,'Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.NetAmountTaxable'); TaxLineDetailTaxPercent := CkJsonObject_IntOf(jsonResponse,'Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxPercent'); TaxLineDetailTaxRateRefValue := CkJsonObject__stringOf(jsonResponse,'Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxRateRef.value'); TaxLineDetailPercentBased := CkJsonObject_BoolOf(jsonResponse,'Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.PercentBased'); i := i + 1; end; i := 0; count_i := CkJsonObject_SizeOfArray(jsonResponse,'Invoice.CustomField'); while i < count_i do begin CkJsonObject_putI(jsonResponse,i); StringValue := CkJsonObject__stringOf(jsonResponse,'Invoice.CustomField[i].StringValue'); Type := 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); CkStringBuilder_Dispose(sbResponseBody); CkJsonObject_Dispose(jsonResponse); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.