C
C
Quickbooks Query an Invoice
See more QuickBooks Examples
Demonstrates how to query for invoices matching a SELECT statement via the Quickbooks REST API.Chilkat C Downloads
#include <C_CkJsonObject.h>
#include <C_CkRest.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObject jsonToken;
HCkRest rest;
BOOL bTls;
int port;
BOOL bAutoReconnect;
HCkStringBuilder sbAuth;
HCkStringBuilder sbResponseBody;
int respStatusCode;
HCkJsonObject json;
const char *DocNumber;
const char *SyncToken;
const char *domain;
const char *Balance;
const char *BillAddrCity;
const char *BillAddrLine1;
const char *BillAddrPostalCode;
const char *BillAddrLat;
const char *BillAddrLong;
const char *BillAddrCountrySubDivisionCode;
const char *BillAddrId;
const char *TxnDate;
const char *TotalAmt;
const char *CustomerRefName;
const char *CustomerRefValue;
const char *ShipAddrCity;
const char *ShipAddrLine1;
const char *ShipAddrPostalCode;
const char *ShipAddrLat;
const char *ShipAddrLong;
const char *ShipAddrCountrySubDivisionCode;
const char *ShipAddrId;
const char *DueDate;
const char *PrintStatus;
int Deposit;
BOOL sparse;
const char *EmailStatus;
BOOL ApplyTaxAfterDiscount;
const char *Id;
int TxnTaxDetailTotalTax;
const char *MetaDataCreateTime;
const char *MetaDataLastUpdatedTime;
int j;
int count_j;
int LineNum;
const char *Amount;
const char *SalesItemLineDetailTaxCodeRefValue;
const char *SalesItemLineDetailItemRefName;
const char *SalesItemLineDetailItemRefValue;
const char *DetailType;
const char *DefinitionId;
const char *invType;
const char *Name;
int QueryResponseStartPosition;
int QueryResponseTotalCount;
int QueryResponseMaxResults;
const char *time;
int i;
int count_i;
success = FALSE;
// 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();
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 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_RemoveHeader(rest,"Content-Type");
CkRest_AddHeader(rest,"Accept","application/json");
CkRest_putAllowHeaderFolding(rest,FALSE);
// Add a SELECT statement
CkRest_AddQueryParam(rest,"query","select * from Invoice where id = '239'");
sbResponseBody = CkStringBuilder_Create();
success = CkRest_FullRequestNoBodySb(rest,"GET","/v3/company/<realmID>/invoice",sbResponseBody);
if (success != TRUE) {
printf("%s\n",CkRest_lastErrorText(rest));
CkJsonObject_Dispose(jsonToken);
CkRest_Dispose(rest);
CkStringBuilder_Dispose(sbAuth);
CkStringBuilder_Dispose(sbResponseBody);
return;
}
respStatusCode = CkRest_getResponseStatusCode(rest);
if (respStatusCode >= 400) {
printf("Response Status Code = %d\n",respStatusCode);
printf("Response Header:\n");
printf("%s\n",CkRest_responseHeader(rest));
printf("Response Body:\n");
printf("%s\n",CkStringBuilder_getAsString(sbResponseBody));
CkJsonObject_Dispose(jsonToken);
CkRest_Dispose(rest);
CkStringBuilder_Dispose(sbAuth);
CkStringBuilder_Dispose(sbResponseBody);
return;
}
// Success is indicated by a 200 response status.
printf("response status code = %d\n",respStatusCode);
json = CkJsonObject_Create();
CkJsonObject_LoadSb(json,sbResponseBody);
CkJsonObject_putEmitCompact(json,FALSE);
printf("%s\n",CkJsonObject_emit(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"
// }
// 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.
QueryResponseStartPosition = CkJsonObject_IntOf(json,"QueryResponse.startPosition");
QueryResponseTotalCount = CkJsonObject_IntOf(json,"QueryResponse.totalCount");
QueryResponseMaxResults = CkJsonObject_IntOf(json,"QueryResponse.maxResults");
time = CkJsonObject_stringOf(json,"time");
i = 0;
count_i = CkJsonObject_SizeOfArray(json,"QueryResponse.Invoice");
while (i < count_i) {
CkJsonObject_putI(json,i);
DocNumber = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].DocNumber");
SyncToken = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].SyncToken");
domain = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].domain");
Balance = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].Balance");
BillAddrCity = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].BillAddr.City");
BillAddrLine1 = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].BillAddr.Line1");
BillAddrPostalCode = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].BillAddr.PostalCode");
BillAddrLat = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].BillAddr.Lat");
BillAddrLong = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].BillAddr.Long");
BillAddrCountrySubDivisionCode = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].BillAddr.CountrySubDivisionCode");
BillAddrId = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].BillAddr.Id");
TxnDate = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].TxnDate");
TotalAmt = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].TotalAmt");
CustomerRefName = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].CustomerRef.name");
CustomerRefValue = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].CustomerRef.value");
ShipAddrCity = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].ShipAddr.City");
ShipAddrLine1 = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].ShipAddr.Line1");
ShipAddrPostalCode = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].ShipAddr.PostalCode");
ShipAddrLat = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].ShipAddr.Lat");
ShipAddrLong = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].ShipAddr.Long");
ShipAddrCountrySubDivisionCode = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].ShipAddr.CountrySubDivisionCode");
ShipAddrId = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].ShipAddr.Id");
DueDate = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].DueDate");
PrintStatus = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].PrintStatus");
Deposit = CkJsonObject_IntOf(json,"QueryResponse.Invoice[i].Deposit");
sparse = CkJsonObject_BoolOf(json,"QueryResponse.Invoice[i].sparse");
EmailStatus = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].EmailStatus");
ApplyTaxAfterDiscount = CkJsonObject_BoolOf(json,"QueryResponse.Invoice[i].ApplyTaxAfterDiscount");
Id = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].Id");
TxnTaxDetailTotalTax = CkJsonObject_IntOf(json,"QueryResponse.Invoice[i].TxnTaxDetail.TotalTax");
MetaDataCreateTime = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].MetaData.CreateTime");
MetaDataLastUpdatedTime = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].MetaData.LastUpdatedTime");
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"QueryResponse.Invoice[i].LinkedTxn");
while (j < count_j) {
CkJsonObject_putJ(json,j);
j = j + 1;
}
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"QueryResponse.Invoice[i].Line");
while (j < count_j) {
CkJsonObject_putJ(json,j);
LineNum = CkJsonObject_IntOf(json,"QueryResponse.Invoice[i].Line[j].LineNum");
Amount = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].Line[j].Amount");
SalesItemLineDetailTaxCodeRefValue = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.TaxCodeRef.value");
SalesItemLineDetailItemRefName = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.name");
SalesItemLineDetailItemRefValue = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.value");
Id = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].Line[j].Id");
DetailType = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].Line[j].DetailType");
j = j + 1;
}
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"QueryResponse.Invoice[i].CustomField");
while (j < count_j) {
CkJsonObject_putJ(json,j);
DefinitionId = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].CustomField[j].DefinitionId");
invType = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].CustomField[j].Type");
Name = CkJsonObject_stringOf(json,"QueryResponse.Invoice[i].CustomField[j].Name");
j = j + 1;
}
i = i + 1;
}
CkJsonObject_Dispose(jsonToken);
CkRest_Dispose(rest);
CkStringBuilder_Dispose(sbAuth);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(json);
}