PureBasic
PureBasic
Quickbooks Create a New Customer
See more QuickBooks Examples
Demonstrates how to create a new customer via the Quickbooks REST API.Chilkat PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 0
; 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 = 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
; Connect to the REST server.
bTls.i = 1
port.i = 443
bAutoReconnect.i = 1
success = 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 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.i = CkJsonObject::ckCreate()
If jsonReq.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(jsonReq,"FullyQualifiedName","King Groceries")
CkJsonObject::ckUpdateString(jsonReq,"PrimaryEmailAddr.Address","jdrew@myemail.com")
CkJsonObject::ckUpdateString(jsonReq,"DisplayName","King's Groceries")
CkJsonObject::ckUpdateString(jsonReq,"Suffix","Jr")
CkJsonObject::ckUpdateString(jsonReq,"Title","Mr")
CkJsonObject::ckUpdateString(jsonReq,"MiddleName","B")
CkJsonObject::ckUpdateString(jsonReq,"Notes","Here are other details.")
CkJsonObject::ckUpdateString(jsonReq,"FamilyName","King")
CkJsonObject::ckUpdateString(jsonReq,"PrimaryPhone.FreeFormNumber","(555) 555-5555")
CkJsonObject::ckUpdateString(jsonReq,"CompanyName","King Groceries")
CkJsonObject::ckUpdateString(jsonReq,"BillAddr.CountrySubDivisionCode","CA")
CkJsonObject::ckUpdateString(jsonReq,"BillAddr.City","Mountain View")
CkJsonObject::ckUpdateString(jsonReq,"BillAddr.PostalCode","94042")
CkJsonObject::ckUpdateString(jsonReq,"BillAddr.Line1","123 Main Street")
CkJsonObject::ckUpdateString(jsonReq,"BillAddr.Country","USA")
CkJsonObject::ckUpdateString(jsonReq,"GivenName","James")
sbRequestBody.i = CkStringBuilder::ckCreate()
If sbRequestBody.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckEmitSb(jsonReq,sbRequestBody)
CkRest::ckAddHeader(rest,"Content-Type","application/json")
CkRest::ckAddHeader(rest,"Accept","application/json")
CkRest::setCkAllowHeaderFolding(rest, 0)
sbResponseBody.i = CkStringBuilder::ckCreate()
If sbResponseBody.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkRest::ckFullRequestSb(rest,"POST","/v3/company/<realmID>/customer",sbRequestBody,sbResponseBody)
If success <> 1
Debug CkRest::ckLastErrorText(rest)
CkJsonObject::ckDispose(jsonToken)
CkRest::ckDispose(rest)
CkStringBuilder::ckDispose(sbAuth)
CkJsonObject::ckDispose(jsonReq)
CkStringBuilder::ckDispose(sbRequestBody)
CkStringBuilder::ckDispose(sbResponseBody)
ProcedureReturn
EndIf
respStatusCode.i = CkRest::ckResponseStatusCode(rest)
; Success is indicated by a 200 response status code.
Debug "response status code = " + Str(respStatusCode)
jsonResponse.i = CkJsonObject::ckCreate()
If jsonResponse.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckLoadSb(jsonResponse,sbResponseBody)
CkJsonObject::setCkEmitCompact(jsonResponse, 0)
Debug CkJsonObject::ckEmit(jsonResponse)
If CkRest::ckResponseStatusCode(rest) <> 200
Debug "Failed."
CkJsonObject::ckDispose(jsonToken)
CkRest::ckDispose(rest)
CkStringBuilder::ckDispose(sbAuth)
CkJsonObject::ckDispose(jsonReq)
CkStringBuilder::ckDispose(sbRequestBody)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jsonResponse)
ProcedureReturn
EndIf
; 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"
; }
;
CustomerDomain.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.domain")
CustomerPrimaryEmailAddrAddress.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.PrimaryEmailAddr.Address")
CustomerDisplayName.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.DisplayName")
CustomerCurrencyRefName.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.CurrencyRef.name")
CustomerCurrencyRefValue.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.CurrencyRef.value")
CustomerDefaultTaxCodeRefValue.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.DefaultTaxCodeRef.value")
CustomerPreferredDeliveryMethod.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.PreferredDeliveryMethod")
CustomerGivenName.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.GivenName")
CustomerFullyQualifiedName.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.FullyQualifiedName")
CustomerBillWithParent.i = CkJsonObject::ckBoolOf(jsonResponse,"Customer.BillWithParent")
CustomerTitle.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.Title")
CustomerJob.i = CkJsonObject::ckBoolOf(jsonResponse,"Customer.Job")
CustomerBalanceWithJobs.i = CkJsonObject::ckIntOf(jsonResponse,"Customer.BalanceWithJobs")
CustomerPrimaryPhoneFreeFormNumber.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.PrimaryPhone.FreeFormNumber")
CustomerTaxable.i = CkJsonObject::ckBoolOf(jsonResponse,"Customer.Taxable")
CustomerMetaDataCreateTime.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.MetaData.CreateTime")
CustomerMetaDataLastUpdatedTime.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.MetaData.LastUpdatedTime")
CustomerBillAddrCity.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.BillAddr.City")
CustomerBillAddrCountry.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.BillAddr.Country")
CustomerBillAddrLine1.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.BillAddr.Line1")
CustomerBillAddrPostalCode.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.BillAddr.PostalCode")
CustomerBillAddrCountrySubDivisionCode.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.BillAddr.CountrySubDivisionCode")
CustomerBillAddrId.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.BillAddr.Id")
CustomerMiddleName.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.MiddleName")
CustomerNotes.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.Notes")
CustomerActive.i = CkJsonObject::ckBoolOf(jsonResponse,"Customer.Active")
CustomerBalance.i = CkJsonObject::ckIntOf(jsonResponse,"Customer.Balance")
CustomerSyncToken.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.SyncToken")
CustomerSuffix.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.Suffix")
CustomerCompanyName.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.CompanyName")
CustomerFamilyName.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.FamilyName")
CustomerPrintOnCheckName.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.PrintOnCheckName")
CustomerSparse.i = CkJsonObject::ckBoolOf(jsonResponse,"Customer.sparse")
CustomerId.s = CkJsonObject::ckStringOf(jsonResponse,"Customer.Id")
time.s = CkJsonObject::ckStringOf(jsonResponse,"time")
CkJsonObject::ckDispose(jsonToken)
CkRest::ckDispose(rest)
CkStringBuilder::ckDispose(sbAuth)
CkJsonObject::ckDispose(jsonReq)
CkStringBuilder::ckDispose(sbRequestBody)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jsonResponse)
ProcedureReturn
EndProcedure