C
C
Quickbooks Create a New Customer
See more QuickBooks Examples
Demonstrates how to create a new customer 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;
HCkJsonObject jsonReq;
HCkStringBuilder sbRequestBody;
HCkStringBuilder sbResponseBody;
int respStatusCode;
HCkJsonObject jsonResponse;
const char *CustomerDomain;
const char *CustomerPrimaryEmailAddrAddress;
const char *CustomerDisplayName;
const char *CustomerCurrencyRefName;
const char *CustomerCurrencyRefValue;
const char *CustomerDefaultTaxCodeRefValue;
const char *CustomerPreferredDeliveryMethod;
const char *CustomerGivenName;
const char *CustomerFullyQualifiedName;
BOOL CustomerBillWithParent;
const char *CustomerTitle;
BOOL CustomerJob;
int CustomerBalanceWithJobs;
const char *CustomerPrimaryPhoneFreeFormNumber;
BOOL CustomerTaxable;
const char *CustomerMetaDataCreateTime;
const char *CustomerMetaDataLastUpdatedTime;
const char *CustomerBillAddrCity;
const char *CustomerBillAddrCountry;
const char *CustomerBillAddrLine1;
const char *CustomerBillAddrPostalCode;
const char *CustomerBillAddrCountrySubDivisionCode;
const char *CustomerBillAddrId;
const char *CustomerMiddleName;
const char *CustomerNotes;
BOOL CustomerActive;
int CustomerBalance;
const char *CustomerSyncToken;
const char *CustomerSuffix;
const char *CustomerCompanyName;
const char *CustomerFamilyName;
const char *CustomerPrintOnCheckName;
BOOL CustomerSparse;
const char *CustomerId;
const char *time;
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();
// 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:
// {
// "FullyQualifiedName": "King Groceries",
// "PrimaryEmailAddr": {
// "Address": "jdrew@myemail.com"
// },
// "DisplayName": "King's Groceries",
// "Suffix": "Jr",
// "Title": "Mr",
// "MiddleName": "B",
// "Notes": "Here are other details.",
// "FamilyName": "King",
// "PrimaryPhone": {
// "FreeFormNumber": "(555) 555-5555"
// },
// "CompanyName": "King Groceries",
// "BillAddr": {
// "CountrySubDivisionCode": "CA",
// "City": "Mountain View",
// "PostalCode": "94042",
// "Line1": "123 Main Street",
// "Country": "USA"
// },
// "GivenName": "James"
// }
//
// Use the this online tool to generate the code from sample JSON:
// Generate Code to Create JSON
jsonReq = CkJsonObject_Create();
CkJsonObject_UpdateString(jsonReq,"FullyQualifiedName","King Groceries");
CkJsonObject_UpdateString(jsonReq,"PrimaryEmailAddr.Address","jdrew@myemail.com");
CkJsonObject_UpdateString(jsonReq,"DisplayName","King's Groceries");
CkJsonObject_UpdateString(jsonReq,"Suffix","Jr");
CkJsonObject_UpdateString(jsonReq,"Title","Mr");
CkJsonObject_UpdateString(jsonReq,"MiddleName","B");
CkJsonObject_UpdateString(jsonReq,"Notes","Here are other details.");
CkJsonObject_UpdateString(jsonReq,"FamilyName","King");
CkJsonObject_UpdateString(jsonReq,"PrimaryPhone.FreeFormNumber","(555) 555-5555");
CkJsonObject_UpdateString(jsonReq,"CompanyName","King Groceries");
CkJsonObject_UpdateString(jsonReq,"BillAddr.CountrySubDivisionCode","CA");
CkJsonObject_UpdateString(jsonReq,"BillAddr.City","Mountain View");
CkJsonObject_UpdateString(jsonReq,"BillAddr.PostalCode","94042");
CkJsonObject_UpdateString(jsonReq,"BillAddr.Line1","123 Main Street");
CkJsonObject_UpdateString(jsonReq,"BillAddr.Country","USA");
CkJsonObject_UpdateString(jsonReq,"GivenName","James");
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>/customer",sbRequestBody,sbResponseBody);
if (success != TRUE) {
printf("%s\n",CkRest_lastErrorText(rest));
CkJsonObject_Dispose(jsonToken);
CkRest_Dispose(rest);
CkStringBuilder_Dispose(sbAuth);
CkJsonObject_Dispose(jsonReq);
CkStringBuilder_Dispose(sbRequestBody);
CkStringBuilder_Dispose(sbResponseBody);
return;
}
respStatusCode = CkRest_getResponseStatusCode(rest);
// Success is indicated by a 200 response status code.
printf("response status code = %d\n",respStatusCode);
jsonResponse = CkJsonObject_Create();
CkJsonObject_LoadSb(jsonResponse,sbResponseBody);
CkJsonObject_putEmitCompact(jsonResponse,FALSE);
printf("%s\n",CkJsonObject_emit(jsonResponse));
if (CkRest_getResponseStatusCode(rest) != 200) {
printf("Failed.\n");
CkJsonObject_Dispose(jsonToken);
CkRest_Dispose(rest);
CkStringBuilder_Dispose(sbAuth);
CkJsonObject_Dispose(jsonReq);
CkStringBuilder_Dispose(sbRequestBody);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jsonResponse);
return;
}
// Sample output...
// (See the parsing code below..)
//
// Use the this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// {
// "Customer": {
// "domain": "QBO",
// "PrimaryEmailAddr": {
// "Address": "jdrew@myemail.com"
// },
// "DisplayName": "King's Groceries",
// "CurrencyRef": {
// "name": "United States Dollar",
// "value": "USD"
// },
// "DefaultTaxCodeRef": {
// "value": "2"
// },
// "PreferredDeliveryMethod": "Print",
// "GivenName": "James",
// "FullyQualifiedName": "King's Groceries",
// "BillWithParent": false,
// "Title": "Mr",
// "Job": false,
// "BalanceWithJobs": 0,
// "PrimaryPhone": {
// "FreeFormNumber": "(555) 555-5555"
// },
// "Taxable": true,
// "MetaData": {
// "CreateTime": "2015-07-23T10:58:12-07:00",
// "LastUpdatedTime": "2015-07-23T10:58:12-07:00"
// },
// "BillAddr": {
// "City": "Mountain View",
// "Country": "USA",
// "Line1": "123 Main Street",
// "PostalCode": "94042",
// "CountrySubDivisionCode": "CA",
// "Id": "112"
// },
// "MiddleName": "B",
// "Notes": "Here are other details.",
// "Active": true,
// "Balance": 0,
// "SyncToken": "0",
// "Suffix": "Jr",
// "CompanyName": "King Groceries",
// "FamilyName": "King",
// "PrintOnCheckName": "King Groceries",
// "sparse": false,
// "Id": "67"
// },
// "time": "2015-07-23T10:58:12.099-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.
CustomerDomain = CkJsonObject_stringOf(jsonResponse,"Customer.domain");
CustomerPrimaryEmailAddrAddress = CkJsonObject_stringOf(jsonResponse,"Customer.PrimaryEmailAddr.Address");
CustomerDisplayName = CkJsonObject_stringOf(jsonResponse,"Customer.DisplayName");
CustomerCurrencyRefName = CkJsonObject_stringOf(jsonResponse,"Customer.CurrencyRef.name");
CustomerCurrencyRefValue = CkJsonObject_stringOf(jsonResponse,"Customer.CurrencyRef.value");
CustomerDefaultTaxCodeRefValue = CkJsonObject_stringOf(jsonResponse,"Customer.DefaultTaxCodeRef.value");
CustomerPreferredDeliveryMethod = CkJsonObject_stringOf(jsonResponse,"Customer.PreferredDeliveryMethod");
CustomerGivenName = CkJsonObject_stringOf(jsonResponse,"Customer.GivenName");
CustomerFullyQualifiedName = CkJsonObject_stringOf(jsonResponse,"Customer.FullyQualifiedName");
CustomerBillWithParent = CkJsonObject_BoolOf(jsonResponse,"Customer.BillWithParent");
CustomerTitle = CkJsonObject_stringOf(jsonResponse,"Customer.Title");
CustomerJob = CkJsonObject_BoolOf(jsonResponse,"Customer.Job");
CustomerBalanceWithJobs = CkJsonObject_IntOf(jsonResponse,"Customer.BalanceWithJobs");
CustomerPrimaryPhoneFreeFormNumber = CkJsonObject_stringOf(jsonResponse,"Customer.PrimaryPhone.FreeFormNumber");
CustomerTaxable = CkJsonObject_BoolOf(jsonResponse,"Customer.Taxable");
CustomerMetaDataCreateTime = CkJsonObject_stringOf(jsonResponse,"Customer.MetaData.CreateTime");
CustomerMetaDataLastUpdatedTime = CkJsonObject_stringOf(jsonResponse,"Customer.MetaData.LastUpdatedTime");
CustomerBillAddrCity = CkJsonObject_stringOf(jsonResponse,"Customer.BillAddr.City");
CustomerBillAddrCountry = CkJsonObject_stringOf(jsonResponse,"Customer.BillAddr.Country");
CustomerBillAddrLine1 = CkJsonObject_stringOf(jsonResponse,"Customer.BillAddr.Line1");
CustomerBillAddrPostalCode = CkJsonObject_stringOf(jsonResponse,"Customer.BillAddr.PostalCode");
CustomerBillAddrCountrySubDivisionCode = CkJsonObject_stringOf(jsonResponse,"Customer.BillAddr.CountrySubDivisionCode");
CustomerBillAddrId = CkJsonObject_stringOf(jsonResponse,"Customer.BillAddr.Id");
CustomerMiddleName = CkJsonObject_stringOf(jsonResponse,"Customer.MiddleName");
CustomerNotes = CkJsonObject_stringOf(jsonResponse,"Customer.Notes");
CustomerActive = CkJsonObject_BoolOf(jsonResponse,"Customer.Active");
CustomerBalance = CkJsonObject_IntOf(jsonResponse,"Customer.Balance");
CustomerSyncToken = CkJsonObject_stringOf(jsonResponse,"Customer.SyncToken");
CustomerSuffix = CkJsonObject_stringOf(jsonResponse,"Customer.Suffix");
CustomerCompanyName = CkJsonObject_stringOf(jsonResponse,"Customer.CompanyName");
CustomerFamilyName = CkJsonObject_stringOf(jsonResponse,"Customer.FamilyName");
CustomerPrintOnCheckName = CkJsonObject_stringOf(jsonResponse,"Customer.PrintOnCheckName");
CustomerSparse = CkJsonObject_BoolOf(jsonResponse,"Customer.sparse");
CustomerId = CkJsonObject_stringOf(jsonResponse,"Customer.Id");
time = CkJsonObject_stringOf(jsonResponse,"time");
CkJsonObject_Dispose(jsonToken);
CkRest_Dispose(rest);
CkStringBuilder_Dispose(sbAuth);
CkJsonObject_Dispose(jsonReq);
CkStringBuilder_Dispose(sbRequestBody);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jsonResponse);
}