Sample code for 30+ languages & platforms
Unicode C++

Quickbooks Create an Invoice

See more QuickBooks Examples

Demonstrates how to create an invoice via the Quickbooks REST API.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkJsonObjectW.h>
#include <CkRestW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool 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.
    CkJsonObjectW jsonToken;
    success = jsonToken.LoadFile(L"qa_data/tokens/qb-access-token.json");

    CkRestW rest;

    // Connect to the REST server.
    bool bTls = true;
    int port = 443;
    bool bAutoReconnect = true;
    success = rest.Connect(L"sandbox-quickbooks.api.intuit.com",port,bTls,bAutoReconnect);

    CkStringBuilderW sbAuth;
    sbAuth.Append(L"Bearer ");
    sbAuth.Append(jsonToken.stringOf(L"access_token"));
    rest.put_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:

    // {
    //   "Line": [
    //     {
    //       "DetailType": "SalesItemLineDetail",
    //       "Amount": 100.0,
    //       "SalesItemLineDetail": {
    //         "ItemRef": {
    //           "name": "Services",
    //           "value": "1"
    //         }
    //       }
    //     }
    //   ],
    //   "CustomerRef": {
    //     "value": "1"
    //   }
    // }
    // 
    // Use the this online tool to generate the code from sample JSON: 
    // Generate Code to Create JSON

    CkJsonObjectW jsonReq;
    jsonReq.UpdateString(L"Line[0].DetailType",L"SalesItemLineDetail");
    jsonReq.UpdateNumber(L"Line[0].Amount",L"100.0");
    jsonReq.UpdateString(L"Line[0].SalesItemLineDetail.ItemRef.name",L"Services");
    jsonReq.UpdateString(L"Line[0].SalesItemLineDetail.ItemRef.value",L"1");
    jsonReq.UpdateString(L"CustomerRef.value",L"1");

    CkStringBuilderW sbRequestBody;
    jsonReq.EmitSb(sbRequestBody);

    rest.AddHeader(L"Content-Type",L"application/json");
    rest.AddHeader(L"Accept",L"application/json");
    rest.put_AllowHeaderFolding(false);

    CkStringBuilderW sbResponseBody;
    success = rest.FullRequestSb(L"POST",L"/v3/company/<realmID>/invoice",sbRequestBody,sbResponseBody);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    int respStatusCode = rest.get_ResponseStatusCode();

    // Success is indicated by a 200 response status code.
    wprintf(L"response status code = %d\n",respStatusCode);

    CkJsonObjectW jsonResponse;
    jsonResponse.LoadSb(sbResponseBody);
    jsonResponse.put_EmitCompact(false);
    wprintf(L"%s\n",jsonResponse.emit());

    if (rest.get_ResponseStatusCode() != 200) {
        wprintf(L"Failed.\n");
        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

    // {
    //   "Invoice": {
    //     "DocNumber": "1069",
    //     "SyncToken": "0",
    //     "domain": "QBO",
    //     "Balance": 100.0,
    //     "BillAddr": {
    //       "City": "Bayshore",
    //       "Line1": "4581 Finch St.",
    //       "PostalCode": "94326",
    //       "Lat": "INVALID",
    //       "Long": "INVALID",
    //       "CountrySubDivisionCode": "CA",
    //       "Id": "2"
    //     },
    //     "TxnDate": "2015-07-24",
    //     "TotalAmt": 100.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": 100.0,
    //         "SalesItemLineDetail": {
    //           "TaxCodeRef": {
    //             "value": "NON"
    //           },
    //           "ItemRef": {
    //             "name": "Services",
    //             "value": "1"
    //           }
    //         },
    //         "Id": "1",
    //         "DetailType": "SalesItemLineDetail"
    //       },
    //       {
    //         "DetailType": "SubTotalLineDetail",
    //         "Amount": 100.0,
    //         "SubTotalLineDetail": {}
    //       }
    //     ],
    //     "ApplyTaxAfterDiscount": false,
    //     "CustomField": [
    //       {
    //         "DefinitionId": "1",
    //         "Type": "StringType",
    //         "Name": "Crew #"
    //       }
    //     ],
    //     "Id": "238",
    //     "TxnTaxDetail": {
    //       "TotalTax": 0
    //     },
    //     "MetaData": {
    //       "CreateTime": "2015-07-24T10:33:39-07:00",
    //       "LastUpdatedTime": "2015-07-24T10:33:39-07:00"
    //     }
    //   },
    //   "time": "2015-07-24T10:33:39.11-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.

    int LineNum;
    const wchar_t *Amount = 0;
    const wchar_t *SalesItemLineDetailTaxCodeRefValue = 0;
    const wchar_t *SalesItemLineDetailItemRefName = 0;
    const wchar_t *SalesItemLineDetailItemRefValue = 0;
    const wchar_t *Id = 0;
    const wchar_t *DetailType = 0;
    const wchar_t *DefinitionId = 0;
    const wchar_t *invType = 0;
    const wchar_t *Name = 0;

    const wchar_t *InvoiceDocNumber = jsonResponse.stringOf(L"Invoice.DocNumber");
    const wchar_t *InvoiceSyncToken = jsonResponse.stringOf(L"Invoice.SyncToken");
    const wchar_t *InvoiceDomain = jsonResponse.stringOf(L"Invoice.domain");
    const wchar_t *InvoiceBalance = jsonResponse.stringOf(L"Invoice.Balance");
    const wchar_t *InvoiceBillAddrCity = jsonResponse.stringOf(L"Invoice.BillAddr.City");
    const wchar_t *InvoiceBillAddrLine1 = jsonResponse.stringOf(L"Invoice.BillAddr.Line1");
    const wchar_t *InvoiceBillAddrPostalCode = jsonResponse.stringOf(L"Invoice.BillAddr.PostalCode");
    const wchar_t *InvoiceBillAddrLat = jsonResponse.stringOf(L"Invoice.BillAddr.Lat");
    const wchar_t *InvoiceBillAddrLong = jsonResponse.stringOf(L"Invoice.BillAddr.Long");
    const wchar_t *InvoiceBillAddrCountrySubDivisionCode = jsonResponse.stringOf(L"Invoice.BillAddr.CountrySubDivisionCode");
    const wchar_t *InvoiceBillAddrId = jsonResponse.stringOf(L"Invoice.BillAddr.Id");
    const wchar_t *InvoiceTxnDate = jsonResponse.stringOf(L"Invoice.TxnDate");
    const wchar_t *InvoiceTotalAmt = jsonResponse.stringOf(L"Invoice.TotalAmt");
    const wchar_t *InvoiceCustomerRefName = jsonResponse.stringOf(L"Invoice.CustomerRef.name");
    const wchar_t *InvoiceCustomerRefValue = jsonResponse.stringOf(L"Invoice.CustomerRef.value");
    const wchar_t *InvoiceShipAddrCity = jsonResponse.stringOf(L"Invoice.ShipAddr.City");
    const wchar_t *InvoiceShipAddrLine1 = jsonResponse.stringOf(L"Invoice.ShipAddr.Line1");
    const wchar_t *InvoiceShipAddrPostalCode = jsonResponse.stringOf(L"Invoice.ShipAddr.PostalCode");
    const wchar_t *InvoiceShipAddrLat = jsonResponse.stringOf(L"Invoice.ShipAddr.Lat");
    const wchar_t *InvoiceShipAddrLong = jsonResponse.stringOf(L"Invoice.ShipAddr.Long");
    const wchar_t *InvoiceShipAddrCountrySubDivisionCode = jsonResponse.stringOf(L"Invoice.ShipAddr.CountrySubDivisionCode");
    const wchar_t *InvoiceShipAddrId = jsonResponse.stringOf(L"Invoice.ShipAddr.Id");
    const wchar_t *InvoiceDueDate = jsonResponse.stringOf(L"Invoice.DueDate");
    const wchar_t *InvoicePrintStatus = jsonResponse.stringOf(L"Invoice.PrintStatus");
    int InvoiceDeposit = jsonResponse.IntOf(L"Invoice.Deposit");
    bool InvoiceSparse = jsonResponse.BoolOf(L"Invoice.sparse");
    const wchar_t *InvoiceEmailStatus = jsonResponse.stringOf(L"Invoice.EmailStatus");
    bool InvoiceApplyTaxAfterDiscount = jsonResponse.BoolOf(L"Invoice.ApplyTaxAfterDiscount");
    const wchar_t *InvoiceId = jsonResponse.stringOf(L"Invoice.Id");
    int InvoiceTxnTaxDetailTotalTax = jsonResponse.IntOf(L"Invoice.TxnTaxDetail.TotalTax");
    const wchar_t *InvoiceMetaDataCreateTime = jsonResponse.stringOf(L"Invoice.MetaData.CreateTime");
    const wchar_t *InvoiceMetaDataLastUpdatedTime = jsonResponse.stringOf(L"Invoice.MetaData.LastUpdatedTime");
    const wchar_t *time = jsonResponse.stringOf(L"time");
    int i = 0;
    int count_i = jsonResponse.SizeOfArray(L"Invoice.LinkedTxn");
    while (i < count_i) {
        jsonResponse.put_I(i);
        i = i + 1;
    }

    i = 0;
    count_i = jsonResponse.SizeOfArray(L"Invoice.Line");
    while (i < count_i) {
        jsonResponse.put_I(i);
        LineNum = jsonResponse.IntOf(L"Invoice.Line[i].LineNum");
        Amount = jsonResponse.stringOf(L"Invoice.Line[i].Amount");
        SalesItemLineDetailTaxCodeRefValue = jsonResponse.stringOf(L"Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value");
        SalesItemLineDetailItemRefName = jsonResponse.stringOf(L"Invoice.Line[i].SalesItemLineDetail.ItemRef.name");
        SalesItemLineDetailItemRefValue = jsonResponse.stringOf(L"Invoice.Line[i].SalesItemLineDetail.ItemRef.value");
        Id = jsonResponse.stringOf(L"Invoice.Line[i].Id");
        DetailType = jsonResponse.stringOf(L"Invoice.Line[i].DetailType");
        i = i + 1;
    }

    i = 0;
    count_i = jsonResponse.SizeOfArray(L"Invoice.CustomField");
    while (i < count_i) {
        jsonResponse.put_I(i);
        DefinitionId = jsonResponse.stringOf(L"Invoice.CustomField[i].DefinitionId");
        invType = jsonResponse.stringOf(L"Invoice.CustomField[i].Type");
        Name = jsonResponse.stringOf(L"Invoice.CustomField[i].Name");
        i = i + 1;
    }
    }