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
(PureBasic) Quickbooks Query an InvoiceDemonstrates how to query for invoices matching a SELECT statement via the Quickbooks REST API. For more information, see https://www.developer.intuit.com/app/developer/qbo/docs/api/accounting/most-commonly-used/invoice#query-an-invoice
IncludeFile "CkStringBuilder.pb" IncludeFile "CkRest.pb" IncludeFile "CkJsonObject.pb" Procedure ChilkatExample() ; 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.i = CkJsonObject::ckCreate() If jsonToken.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i = CkJsonObject::ckLoadFile(jsonToken,"qa_data/tokens/qb-access-token.json") rest.i = CkRest::ckCreate() If rest.i = 0 Debug "Failed to create object." ProcedureReturn EndIf bTls.i = 1 port.i = 443 bAutoReconnect.i = 1 success.i = CkRest::ckConnect(rest,"sandbox-quickbooks.api.intuit.com",port,bTls,bAutoReconnect) sbAuth.i = CkStringBuilder::ckCreate() If sbAuth.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkStringBuilder::ckAppend(sbAuth,"Bearer ") CkStringBuilder::ckAppend(sbAuth,CkJsonObject::ckStringOf(jsonToken,"access_token")) CkRest::setCkAuthorization(rest, CkStringBuilder::ckGetAsString(sbAuth)) ; -------------------------------------------------------------------------- ; Note: The above REST connection and setup of the AWS credentials ; can be done once. After connecting, any number of REST calls can be made. ; The "auto reconnect" property passed to rest.Connect indicates that if ; the connection is lost, a REST method call will automatically reconnect ; if needed. ; -------------------------------------------------------------------------- ; This is a GET request, so there should be no Content-Type ; This line of code is just to make sure.. CkRest::ckRemoveHeader(rest,"Content-Type") CkRest::ckAddHeader(rest,"Accept","application/json") CkRest::setCkAllowHeaderFolding(rest, 0) ; Add a SELECT statement CkRest::ckAddQueryParam(rest,"query","select * from Invoice where id = '239'") sbResponseBody.i = CkStringBuilder::ckCreate() If sbResponseBody.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkRest::ckFullRequestNoBodySb(rest,"GET","/v3/company/<realmID>/invoice",sbResponseBody) If success <> 1 Debug CkRest::ckLastErrorText(rest) CkJsonObject::ckDispose(jsonToken) CkRest::ckDispose(rest) CkStringBuilder::ckDispose(sbAuth) CkStringBuilder::ckDispose(sbResponseBody) ProcedureReturn EndIf respStatusCode.i = CkRest::ckResponseStatusCode(rest) If respStatusCode >= 400 Debug "Response Status Code = " + Str(respStatusCode) Debug "Response Header:" Debug CkRest::ckResponseHeader(rest) Debug "Response Body:" Debug CkStringBuilder::ckGetAsString(sbResponseBody) CkJsonObject::ckDispose(jsonToken) CkRest::ckDispose(rest) CkStringBuilder::ckDispose(sbAuth) CkStringBuilder::ckDispose(sbResponseBody) ProcedureReturn EndIf ; Success is indicated by a 200 response status. Debug "response status code = " + Str(respStatusCode) json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckLoadSb(json,sbResponseBody) CkJsonObject::setCkEmitCompact(json, 0) Debug CkJsonObject::ckEmit(json) ; Sample output: ; Use the this online tool to generate parsing code from sample JSON: ; Generate Parsing Code from JSON ; { ; "QueryResponse": { ; "startPosition": 1, ; "totalCount": 1, ; "maxResults": 1, ; "Invoice": [ ; { ; "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" ; }, ; "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": 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" ; } ; } ; ] ; }, ; "time": "2015-07-24T10:38:50.01-07:00" ; } DocNumber.s SyncToken.s domain.s Balance.s BillAddrCity.s BillAddrLine1.s BillAddrPostalCode.s BillAddrLat.s BillAddrLong.s BillAddrCountrySubDivisionCode.s BillAddrId.s TxnDate.s TotalAmt.s CustomerRefName.s CustomerRefValue.s ShipAddrCity.s ShipAddrLine1.s ShipAddrPostalCode.s ShipAddrLat.s ShipAddrLong.s ShipAddrCountrySubDivisionCode.s ShipAddrId.s DueDate.s PrintStatus.s Deposit.i sparse.i EmailStatus.s ApplyTaxAfterDiscount.i Id.s TxnTaxDetailTotalTax.i MetaDataCreateTime.s MetaDataLastUpdatedTime.s j.i count_j.i LineNum.i Amount.s SalesItemLineDetailTaxCodeRefValue.s SalesItemLineDetailItemRefName.s SalesItemLineDetailItemRefValue.s DetailType.s DefinitionId.s invType.s Name.s QueryResponseStartPosition.i = CkJsonObject::ckIntOf(json,"QueryResponse.startPosition") QueryResponseTotalCount.i = CkJsonObject::ckIntOf(json,"QueryResponse.totalCount") QueryResponseMaxResults.i = CkJsonObject::ckIntOf(json,"QueryResponse.maxResults") time.s = CkJsonObject::ckStringOf(json,"time") i.i = 0 count_i.i = CkJsonObject::ckSizeOfArray(json,"QueryResponse.Invoice") While i < count_i CkJsonObject::setCkI(json, i) DocNumber = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].DocNumber") SyncToken = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].SyncToken") domain = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].domain") Balance = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].Balance") BillAddrCity = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].BillAddr.City") BillAddrLine1 = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].BillAddr.Line1") BillAddrPostalCode = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].BillAddr.PostalCode") BillAddrLat = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].BillAddr.Lat") BillAddrLong = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].BillAddr.Long") BillAddrCountrySubDivisionCode = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].BillAddr.CountrySubDivisionCode") BillAddrId = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].BillAddr.Id") TxnDate = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].TxnDate") TotalAmt = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].TotalAmt") CustomerRefName = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].CustomerRef.name") CustomerRefValue = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].CustomerRef.value") ShipAddrCity = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].ShipAddr.City") ShipAddrLine1 = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].ShipAddr.Line1") ShipAddrPostalCode = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].ShipAddr.PostalCode") ShipAddrLat = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].ShipAddr.Lat") ShipAddrLong = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].ShipAddr.Long") ShipAddrCountrySubDivisionCode = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].ShipAddr.CountrySubDivisionCode") ShipAddrId = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].ShipAddr.Id") DueDate = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].DueDate") PrintStatus = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].PrintStatus") Deposit = CkJsonObject::ckIntOf(json,"QueryResponse.Invoice[i].Deposit") sparse = CkJsonObject::ckBoolOf(json,"QueryResponse.Invoice[i].sparse") EmailStatus = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].EmailStatus") ApplyTaxAfterDiscount = CkJsonObject::ckBoolOf(json,"QueryResponse.Invoice[i].ApplyTaxAfterDiscount") Id = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].Id") TxnTaxDetailTotalTax = CkJsonObject::ckIntOf(json,"QueryResponse.Invoice[i].TxnTaxDetail.TotalTax") MetaDataCreateTime = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].MetaData.CreateTime") MetaDataLastUpdatedTime = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].MetaData.LastUpdatedTime") j = 0 count_j = CkJsonObject::ckSizeOfArray(json,"QueryResponse.Invoice[i].LinkedTxn") While j < count_j CkJsonObject::setCkJ(json, j) j = j + 1 Wend j = 0 count_j = CkJsonObject::ckSizeOfArray(json,"QueryResponse.Invoice[i].Line") While j < count_j CkJsonObject::setCkJ(json, j) LineNum = CkJsonObject::ckIntOf(json,"QueryResponse.Invoice[i].Line[j].LineNum") Amount = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].Line[j].Amount") SalesItemLineDetailTaxCodeRefValue = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.TaxCodeRef.value") SalesItemLineDetailItemRefName = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.name") SalesItemLineDetailItemRefValue = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.value") Id = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].Line[j].Id") DetailType = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].Line[j].DetailType") j = j + 1 Wend j = 0 count_j = CkJsonObject::ckSizeOfArray(json,"QueryResponse.Invoice[i].CustomField") While j < count_j CkJsonObject::setCkJ(json, j) DefinitionId = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].CustomField[j].DefinitionId") invType = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].CustomField[j].Type") Name = CkJsonObject::ckStringOf(json,"QueryResponse.Invoice[i].CustomField[j].Name") j = j + 1 Wend i = i + 1 Wend CkJsonObject::ckDispose(jsonToken) CkRest::ckDispose(rest) CkStringBuilder::ckDispose(sbAuth) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(json) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.