Perl
Perl
Quickbooks Create a New Customer
See more QuickBooks Examples
Demonstrates how to create a new customer via the Quickbooks REST API.Chilkat Perl Downloads
use strict;
use warnings;
use Chilkat2;
# 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.
my $jsonToken = Chilkat2::JsonObject->new;
my $success = $jsonToken->LoadFile('qa_data/tokens/qb-access-token.json');
my $rest = Chilkat2::Rest->new;
# Connect to the REST server.
my $bTls = 1;
my $port = 443;
my $bAutoReconnect = 1;
$success = $rest->Connect('sandbox-quickbooks.api.intuit.com', $port, $bTls, $bAutoReconnect);
my $sbAuth = Chilkat2::StringBuilder->new;
$sbAuth->Append('Bearer ');
$sbAuth->Append($jsonToken->StringOf('access_token'));
$rest->Authorization($sbAuth->GetAsString());
# --------------------------------------------------------------------------
# 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
my $jsonReq = Chilkat2::JsonObject->new;
$jsonReq->UpdateString('FullyQualifiedName', 'King Groceries');
$jsonReq->UpdateString('PrimaryEmailAddr.Address', 'jdrew@myemail.com');
$jsonReq->UpdateString('DisplayName', 'King\'s Groceries');
$jsonReq->UpdateString('Suffix', 'Jr');
$jsonReq->UpdateString('Title', 'Mr');
$jsonReq->UpdateString('MiddleName', 'B');
$jsonReq->UpdateString('Notes', 'Here are other details.');
$jsonReq->UpdateString('FamilyName', 'King');
$jsonReq->UpdateString('PrimaryPhone.FreeFormNumber', '(555) 555-5555');
$jsonReq->UpdateString('CompanyName', 'King Groceries');
$jsonReq->UpdateString('BillAddr.CountrySubDivisionCode', 'CA');
$jsonReq->UpdateString('BillAddr.City', 'Mountain View');
$jsonReq->UpdateString('BillAddr.PostalCode', '94042');
$jsonReq->UpdateString('BillAddr.Line1', '123 Main Street');
$jsonReq->UpdateString('BillAddr.Country', 'USA');
$jsonReq->UpdateString('GivenName', 'James');
my $sbRequestBody = Chilkat2::StringBuilder->new;
$jsonReq->EmitSb($sbRequestBody);
$rest->AddHeader('Content-Type', 'application/json');
$rest->AddHeader('Accept', 'application/json');
$rest->AllowHeaderFolding(0);
my $sbResponseBody = Chilkat2::StringBuilder->new;
$success = $rest->FullRequestSb('POST', '/v3/company/<realmID>/customer', $sbRequestBody, $sbResponseBody);
if (!$success) {
print $rest->LastErrorText, "\n";
exit;
}
my $respStatusCode = $rest->ResponseStatusCode;
# Success is indicated by a 200 response status code.
print 'response status code = ', $respStatusCode, "\n";
my $jsonResponse = Chilkat2::JsonObject->new;
$jsonResponse->LoadSb($sbResponseBody);
$jsonResponse->EmitCompact(0);
print $jsonResponse->Emit(), "\n";
if ($rest->ResponseStatusCode != 200) {
print "Failed.\n";
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
# {
# "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"
# }
#
my $CustomerDomain = $jsonResponse->StringOf('Customer.domain');
my $CustomerPrimaryEmailAddrAddress = $jsonResponse->StringOf('Customer.PrimaryEmailAddr.Address');
my $CustomerDisplayName = $jsonResponse->StringOf('Customer.DisplayName');
my $CustomerCurrencyRefName = $jsonResponse->StringOf('Customer.CurrencyRef.name');
my $CustomerCurrencyRefValue = $jsonResponse->StringOf('Customer.CurrencyRef.value');
my $CustomerDefaultTaxCodeRefValue = $jsonResponse->StringOf('Customer.DefaultTaxCodeRef.value');
my $CustomerPreferredDeliveryMethod = $jsonResponse->StringOf('Customer.PreferredDeliveryMethod');
my $CustomerGivenName = $jsonResponse->StringOf('Customer.GivenName');
my $CustomerFullyQualifiedName = $jsonResponse->StringOf('Customer.FullyQualifiedName');
my $CustomerBillWithParent = $jsonResponse->BoolOf('Customer.BillWithParent');
my $CustomerTitle = $jsonResponse->StringOf('Customer.Title');
my $CustomerJob = $jsonResponse->BoolOf('Customer.Job');
my $CustomerBalanceWithJobs = $jsonResponse->IntOf('Customer.BalanceWithJobs');
my $CustomerPrimaryPhoneFreeFormNumber = $jsonResponse->StringOf('Customer.PrimaryPhone.FreeFormNumber');
my $CustomerTaxable = $jsonResponse->BoolOf('Customer.Taxable');
my $CustomerMetaDataCreateTime = $jsonResponse->StringOf('Customer.MetaData.CreateTime');
my $CustomerMetaDataLastUpdatedTime = $jsonResponse->StringOf('Customer.MetaData.LastUpdatedTime');
my $CustomerBillAddrCity = $jsonResponse->StringOf('Customer.BillAddr.City');
my $CustomerBillAddrCountry = $jsonResponse->StringOf('Customer.BillAddr.Country');
my $CustomerBillAddrLine1 = $jsonResponse->StringOf('Customer.BillAddr.Line1');
my $CustomerBillAddrPostalCode = $jsonResponse->StringOf('Customer.BillAddr.PostalCode');
my $CustomerBillAddrCountrySubDivisionCode = $jsonResponse->StringOf('Customer.BillAddr.CountrySubDivisionCode');
my $CustomerBillAddrId = $jsonResponse->StringOf('Customer.BillAddr.Id');
my $CustomerMiddleName = $jsonResponse->StringOf('Customer.MiddleName');
my $CustomerNotes = $jsonResponse->StringOf('Customer.Notes');
my $CustomerActive = $jsonResponse->BoolOf('Customer.Active');
my $CustomerBalance = $jsonResponse->IntOf('Customer.Balance');
my $CustomerSyncToken = $jsonResponse->StringOf('Customer.SyncToken');
my $CustomerSuffix = $jsonResponse->StringOf('Customer.Suffix');
my $CustomerCompanyName = $jsonResponse->StringOf('Customer.CompanyName');
my $CustomerFamilyName = $jsonResponse->StringOf('Customer.FamilyName');
my $CustomerPrintOnCheckName = $jsonResponse->StringOf('Customer.PrintOnCheckName');
my $CustomerSparse = $jsonResponse->BoolOf('Customer.sparse');
my $CustomerId = $jsonResponse->StringOf('Customer.Id');
my $time = $jsonResponse->StringOf('time');