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
(Tcl) Quickbooks Create an InvoiceDemonstrates how to create an invoice via the Quickbooks REST API. For more information, see https://www.developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/invoice#create-an-invoice
load ./chilkat.dll # 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. set jsonToken [new_CkJsonObject] set success [CkJsonObject_LoadFile $jsonToken "qa_data/tokens/qb-access-token.json"] set rest [new_CkRest] # Connect to the REST server. set bTls 1 set port 443 set bAutoReconnect 1 set success [CkRest_Connect $rest "sandbox-quickbooks.api.intuit.com" $port $bTls $bAutoReconnect] set sbAuth [new_CkStringBuilder] CkStringBuilder_Append $sbAuth "Bearer " CkStringBuilder_Append $sbAuth [CkJsonObject_stringOf $jsonToken "access_token"] CkRest_put_Authorization $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: # { # "Line": [ # { # "DetailType": "SalesItemLineDetail", # "Amount": 100.0, # "SalesItemLineDetail": { # "ItemRef": { # "name": "Services", # "value": "1" # } # } # } # ], # "CustomerRef": { # "value": "1" # } # } # # Use the this online tool to generate the code from sample JSON: # Generate Code to Create JSON set jsonReq [new_CkJsonObject] CkJsonObject_UpdateString $jsonReq "Line[0].DetailType" "SalesItemLineDetail" CkJsonObject_UpdateNumber $jsonReq "Line[0].Amount" "100.0" CkJsonObject_UpdateString $jsonReq "Line[0].SalesItemLineDetail.ItemRef.name" "Services" CkJsonObject_UpdateString $jsonReq "Line[0].SalesItemLineDetail.ItemRef.value" "1" CkJsonObject_UpdateString $jsonReq "CustomerRef.value" "1" set sbRequestBody [new_CkStringBuilder] CkJsonObject_EmitSb $jsonReq $sbRequestBody CkRest_AddHeader $rest "Content-Type" "application/json" CkRest_AddHeader $rest "Accept" "application/json" CkRest_put_AllowHeaderFolding $rest 0 set sbResponseBody [new_CkStringBuilder] set success [CkRest_FullRequestSb $rest "POST" "/v3/company/<realmID>/invoice" $sbRequestBody $sbResponseBody] if {$success != 1} then { puts [CkRest_lastErrorText $rest] delete_CkJsonObject $jsonToken delete_CkRest $rest delete_CkStringBuilder $sbAuth delete_CkJsonObject $jsonReq delete_CkStringBuilder $sbRequestBody delete_CkStringBuilder $sbResponseBody exit } set respStatusCode [CkRest_get_ResponseStatusCode $rest] # Success is indicated by a 200 response status code. puts "response status code = $respStatusCode" set jsonResponse [new_CkJsonObject] CkJsonObject_LoadSb $jsonResponse $sbResponseBody CkJsonObject_put_EmitCompact $jsonResponse 0 puts [CkJsonObject_emit $jsonResponse] if {[CkRest_get_ResponseStatusCode $rest] != 200} then { puts "Failed." delete_CkJsonObject $jsonToken delete_CkRest $rest delete_CkStringBuilder $sbAuth delete_CkJsonObject $jsonReq delete_CkStringBuilder $sbRequestBody delete_CkStringBuilder $sbResponseBody delete_CkJsonObject $jsonResponse exit } # 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": { # "DocNumber": "1069", # "SyncToken": "0", # "domain": "QBO", # "Balance": 100.0, # "BillAddr": { # "City": "Bayshore", # "Line1": "4581 Finch St.", # "PostalCode": "94326", # "Lat": "INVALID", # "Long": "INVALID", # "CountrySubDivisionCode": "CA", # "Id": "2" # }, # "TxnDate": "2015-07-24", # "TotalAmt": 100.0, # "CustomerRef": { # "name": "Amy's Bird Sanctuary", # "value": "1" # }, # "ShipAddr": { # "City": "Bayshore", # "Line1": "4581 Finch St.", # "PostalCode": "94326", # "Lat": "INVALID", # "Long": "INVALID", # "CountrySubDivisionCode": "CA", # "Id": "109" # }, # "LinkedTxn": [ # ], # "DueDate": "2015-08-23", # "PrintStatus": "NeedToPrint", # "Deposit": 0, # "sparse": false, # "EmailStatus": "NotSet", # "Line": [ # { # "LineNum": 1, # "Amount": 100.0, # "SalesItemLineDetail": { # "TaxCodeRef": { # "value": "NON" # }, # "ItemRef": { # "name": "Services", # "value": "1" # } # }, # "Id": "1", # "DetailType": "SalesItemLineDetail" # }, # { # "DetailType": "SubTotalLineDetail", # "Amount": 100.0, # "SubTotalLineDetail": {} # } # ], # "ApplyTaxAfterDiscount": false, # "CustomField": [ # { # "DefinitionId": "1", # "Type": "StringType", # "Name": "Crew #" # } # ], # "Id": "238", # "TxnTaxDetail": { # "TotalTax": 0 # }, # "MetaData": { # "CreateTime": "2015-07-24T10:33:39-07:00", # "LastUpdatedTime": "2015-07-24T10:33:39-07:00" # } # }, # "time": "2015-07-24T10:33:39.11-07:00" # } # set InvoiceDocNumber [CkJsonObject_stringOf $jsonResponse "Invoice.DocNumber"] set InvoiceSyncToken [CkJsonObject_stringOf $jsonResponse "Invoice.SyncToken"] set InvoiceDomain [CkJsonObject_stringOf $jsonResponse "Invoice.domain"] set InvoiceBalance [CkJsonObject_stringOf $jsonResponse "Invoice.Balance"] set InvoiceBillAddrCity [CkJsonObject_stringOf $jsonResponse "Invoice.BillAddr.City"] set InvoiceBillAddrLine1 [CkJsonObject_stringOf $jsonResponse "Invoice.BillAddr.Line1"] set InvoiceBillAddrPostalCode [CkJsonObject_stringOf $jsonResponse "Invoice.BillAddr.PostalCode"] set InvoiceBillAddrLat [CkJsonObject_stringOf $jsonResponse "Invoice.BillAddr.Lat"] set InvoiceBillAddrLong [CkJsonObject_stringOf $jsonResponse "Invoice.BillAddr.Long"] set InvoiceBillAddrCountrySubDivisionCode [CkJsonObject_stringOf $jsonResponse "Invoice.BillAddr.CountrySubDivisionCode"] set InvoiceBillAddrId [CkJsonObject_stringOf $jsonResponse "Invoice.BillAddr.Id"] set InvoiceTxnDate [CkJsonObject_stringOf $jsonResponse "Invoice.TxnDate"] set InvoiceTotalAmt [CkJsonObject_stringOf $jsonResponse "Invoice.TotalAmt"] set InvoiceCustomerRefName [CkJsonObject_stringOf $jsonResponse "Invoice.CustomerRef.name"] set InvoiceCustomerRefValue [CkJsonObject_stringOf $jsonResponse "Invoice.CustomerRef.value"] set InvoiceShipAddrCity [CkJsonObject_stringOf $jsonResponse "Invoice.ShipAddr.City"] set InvoiceShipAddrLine1 [CkJsonObject_stringOf $jsonResponse "Invoice.ShipAddr.Line1"] set InvoiceShipAddrPostalCode [CkJsonObject_stringOf $jsonResponse "Invoice.ShipAddr.PostalCode"] set InvoiceShipAddrLat [CkJsonObject_stringOf $jsonResponse "Invoice.ShipAddr.Lat"] set InvoiceShipAddrLong [CkJsonObject_stringOf $jsonResponse "Invoice.ShipAddr.Long"] set InvoiceShipAddrCountrySubDivisionCode [CkJsonObject_stringOf $jsonResponse "Invoice.ShipAddr.CountrySubDivisionCode"] set InvoiceShipAddrId [CkJsonObject_stringOf $jsonResponse "Invoice.ShipAddr.Id"] set InvoiceDueDate [CkJsonObject_stringOf $jsonResponse "Invoice.DueDate"] set InvoicePrintStatus [CkJsonObject_stringOf $jsonResponse "Invoice.PrintStatus"] set InvoiceDeposit [CkJsonObject_IntOf $jsonResponse "Invoice.Deposit"] set InvoiceSparse [CkJsonObject_BoolOf $jsonResponse "Invoice.sparse"] set InvoiceEmailStatus [CkJsonObject_stringOf $jsonResponse "Invoice.EmailStatus"] set InvoiceApplyTaxAfterDiscount [CkJsonObject_BoolOf $jsonResponse "Invoice.ApplyTaxAfterDiscount"] set InvoiceId [CkJsonObject_stringOf $jsonResponse "Invoice.Id"] set InvoiceTxnTaxDetailTotalTax [CkJsonObject_IntOf $jsonResponse "Invoice.TxnTaxDetail.TotalTax"] set InvoiceMetaDataCreateTime [CkJsonObject_stringOf $jsonResponse "Invoice.MetaData.CreateTime"] set InvoiceMetaDataLastUpdatedTime [CkJsonObject_stringOf $jsonResponse "Invoice.MetaData.LastUpdatedTime"] set time [CkJsonObject_stringOf $jsonResponse "time"] set i 0 set count_i [CkJsonObject_SizeOfArray $jsonResponse "Invoice.LinkedTxn"] while {$i < $count_i} { CkJsonObject_put_I $jsonResponse $i set i [expr $i + 1] } set i 0 set count_i [CkJsonObject_SizeOfArray $jsonResponse "Invoice.Line"] while {$i < $count_i} { CkJsonObject_put_I $jsonResponse $i set LineNum [CkJsonObject_IntOf $jsonResponse "Invoice.Line[i].LineNum"] set Amount [CkJsonObject_stringOf $jsonResponse "Invoice.Line[i].Amount"] set SalesItemLineDetailTaxCodeRefValue [CkJsonObject_stringOf $jsonResponse "Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value"] set SalesItemLineDetailItemRefName [CkJsonObject_stringOf $jsonResponse "Invoice.Line[i].SalesItemLineDetail.ItemRef.name"] set SalesItemLineDetailItemRefValue [CkJsonObject_stringOf $jsonResponse "Invoice.Line[i].SalesItemLineDetail.ItemRef.value"] set Id [CkJsonObject_stringOf $jsonResponse "Invoice.Line[i].Id"] set DetailType [CkJsonObject_stringOf $jsonResponse "Invoice.Line[i].DetailType"] set i [expr $i + 1] } set i 0 set count_i [CkJsonObject_SizeOfArray $jsonResponse "Invoice.CustomField"] while {$i < $count_i} { CkJsonObject_put_I $jsonResponse $i set DefinitionId [CkJsonObject_stringOf $jsonResponse "Invoice.CustomField[i].DefinitionId"] set invType [CkJsonObject_stringOf $jsonResponse "Invoice.CustomField[i].Type"] set Name [CkJsonObject_stringOf $jsonResponse "Invoice.CustomField[i].Name"] set i [expr $i + 1] } delete_CkJsonObject $jsonToken delete_CkRest $rest delete_CkStringBuilder $sbAuth delete_CkJsonObject $jsonReq delete_CkStringBuilder $sbRequestBody delete_CkStringBuilder $sbResponseBody delete_CkJsonObject $jsonResponse |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.