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

Quickbooks Update an Invoice

See more QuickBooks Examples

Demonstrates how to update an invoice using 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:

    // {
    //   "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"
    //   },
    //   "CustomerMemo": {
    //     "value": "Added customer memo."
    //   },
    //   "ShipAddr": {
    //     "City": "Bayshore",
    //     "Line1": "4581 Finch St.",
    //     "PostalCode": "94326",
    //     "Lat": "INVALID",
    //     "Long": "INVALID",
    //     "CountrySubDivisionCode": "CA",
    //     "Id": "109"
    //   },
    //   "LinkedTxn": [
    //   ],
    //   "DueDate": "2015-08-23",
    //   "PrintStatus": "NeedToPrint",
    //   "EmailStatus": "NotSet",
    //   "sparse": false,
    //   "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"
    //   }
    // }
    // 
    // Use the this online tool to generate the code from sample JSON: 
    // Generate Code to Create JSON

    CkJsonObjectW jsonReq;
    jsonReq.UpdateString(L"DocNumber",L"1070");
    jsonReq.UpdateString(L"SyncToken",L"0");
    jsonReq.UpdateString(L"domain",L"QBO");
    jsonReq.UpdateNumber(L"Balance",L"150.0");
    jsonReq.UpdateString(L"BillAddr.City",L"Bayshore");
    jsonReq.UpdateString(L"BillAddr.Line1",L"4581 Finch St.");
    jsonReq.UpdateString(L"BillAddr.PostalCode",L"94326");
    jsonReq.UpdateString(L"BillAddr.Lat",L"INVALID");
    jsonReq.UpdateString(L"BillAddr.Long",L"INVALID");
    jsonReq.UpdateString(L"BillAddr.CountrySubDivisionCode",L"CA");
    jsonReq.UpdateString(L"BillAddr.Id",L"2");
    jsonReq.UpdateString(L"TxnDate",L"2015-07-24");
    jsonReq.UpdateNumber(L"TotalAmt",L"150.0");
    jsonReq.UpdateString(L"CustomerRef.name",L"Amy's Bird Sanctuary");
    jsonReq.UpdateString(L"CustomerRef.value",L"1");
    jsonReq.UpdateString(L"CustomerMemo.value",L"Added customer memo.");
    jsonReq.UpdateString(L"ShipAddr.City",L"Bayshore");
    jsonReq.UpdateString(L"ShipAddr.Line1",L"4581 Finch St.");
    jsonReq.UpdateString(L"ShipAddr.PostalCode",L"94326");
    jsonReq.UpdateString(L"ShipAddr.Lat",L"INVALID");
    jsonReq.UpdateString(L"ShipAddr.Long",L"INVALID");
    jsonReq.UpdateString(L"ShipAddr.CountrySubDivisionCode",L"CA");
    jsonReq.UpdateString(L"ShipAddr.Id",L"109");
    jsonReq.UpdateNewArray(L"LinkedTxn");
    jsonReq.UpdateString(L"DueDate",L"2015-08-23");
    jsonReq.UpdateString(L"PrintStatus",L"NeedToPrint");
    jsonReq.UpdateString(L"EmailStatus",L"NotSet");
    jsonReq.UpdateBool(L"sparse",false);
    jsonReq.UpdateInt(L"Line[0].LineNum",1);
    jsonReq.UpdateNumber(L"Line[0].Amount",L"150.0");
    jsonReq.UpdateString(L"Line[0].SalesItemLineDetail.TaxCodeRef.value",L"NON");
    jsonReq.UpdateString(L"Line[0].SalesItemLineDetail.ItemRef.name",L"Services");
    jsonReq.UpdateString(L"Line[0].SalesItemLineDetail.ItemRef.value",L"1");
    jsonReq.UpdateString(L"Line[0].Id",L"1");
    jsonReq.UpdateString(L"Line[0].DetailType",L"SalesItemLineDetail");
    jsonReq.UpdateString(L"Line[1].DetailType",L"SubTotalLineDetail");
    jsonReq.UpdateNumber(L"Line[1].Amount",L"150.0");
    jsonReq.UpdateNewObject(L"Line[1].SubTotalLineDetail");
    jsonReq.UpdateBool(L"ApplyTaxAfterDiscount",false);
    jsonReq.UpdateString(L"CustomField[0].DefinitionId",L"1");
    jsonReq.UpdateString(L"CustomField[0].Type",L"StringType");
    jsonReq.UpdateString(L"CustomField[0].Name",L"Crew #");
    jsonReq.UpdateString(L"Id",L"239");
    jsonReq.UpdateInt(L"TxnTaxDetail.TotalTax",0);
    jsonReq.UpdateString(L"MetaData.CreateTime",L"2015-07-24T10:35:08-07:00");
    jsonReq.UpdateString(L"MetaData.LastUpdatedTime",L"2015-07-24T10:35:08-07:00");

    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": {
    //     "TxnDate": "2015-07-24",
    //     "domain": "QBO",
    //     "PrintStatus": "NeedToPrint",
    //     "TotalAmt": 150.0,
    //     "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": {}
    //       }
    //     ],
    //     "DueDate": "2015-08-23",
    //     "ApplyTaxAfterDiscount": false,
    //     "DocNumber": "1070",
    //     "sparse": false,
    //     "CustomerMemo": {
    //       "value": "Added customer memo."
    //     },
    //     "Deposit": 0,
    //     "Balance": 150.0,
    //     "CustomerRef": {
    //       "name": "Amy's Bird Sanctuary",
    //       "value": "1"
    //     },
    //     "TxnTaxDetail": {
    //       "TotalTax": 0
    //     },
    //     "SyncToken": "1",
    //     "LinkedTxn": [
    //     ],
    //     "ShipAddr": {
    //       "CountrySubDivisionCode": "CA",
    //       "City": "Bayshore",
    //       "PostalCode": "94326",
    //       "Id": "118",
    //       "Line1": "4581 Finch St."
    //     },
    //     "EmailStatus": "NotSet",
    //     "BillAddr": {
    //       "CountrySubDivisionCode": "CA",
    //       "City": "Bayshore",
    //       "PostalCode": "94326",
    //       "Id": "117",
    //       "Line1": "4581 Finch St."
    //     },
    //     "MetaData": {
    //       "CreateTime": "2015-07-24T10:35:08-07:00",
    //       "LastUpdatedTime": "2015-07-24T10:53:39-07:00"
    //     },
    //     "CustomField": [
    //       {
    //         "DefinitionId": "1",
    //         "Type": "StringType",
    //         "Name": "Crew #"
    //       }
    //     ],
    //     "Id": "239"
    //   },
    //   "time": "2015-07-24T10:53:39.287-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 *InvoiceTxnDate = jsonResponse.stringOf(L"Invoice.TxnDate");
    const wchar_t *InvoiceDomain = jsonResponse.stringOf(L"Invoice.domain");
    const wchar_t *InvoicePrintStatus = jsonResponse.stringOf(L"Invoice.PrintStatus");
    const wchar_t *InvoiceTotalAmt = jsonResponse.stringOf(L"Invoice.TotalAmt");
    const wchar_t *InvoiceDueDate = jsonResponse.stringOf(L"Invoice.DueDate");
    bool InvoiceApplyTaxAfterDiscount = jsonResponse.BoolOf(L"Invoice.ApplyTaxAfterDiscount");
    const wchar_t *InvoiceDocNumber = jsonResponse.stringOf(L"Invoice.DocNumber");
    bool InvoiceSparse = jsonResponse.BoolOf(L"Invoice.sparse");
    const wchar_t *InvoiceCustomerMemoValue = jsonResponse.stringOf(L"Invoice.CustomerMemo.value");
    int InvoiceDeposit = jsonResponse.IntOf(L"Invoice.Deposit");
    const wchar_t *InvoiceBalance = jsonResponse.stringOf(L"Invoice.Balance");
    const wchar_t *InvoiceCustomerRefName = jsonResponse.stringOf(L"Invoice.CustomerRef.name");
    const wchar_t *InvoiceCustomerRefValue = jsonResponse.stringOf(L"Invoice.CustomerRef.value");
    int InvoiceTxnTaxDetailTotalTax = jsonResponse.IntOf(L"Invoice.TxnTaxDetail.TotalTax");
    const wchar_t *InvoiceSyncToken = jsonResponse.stringOf(L"Invoice.SyncToken");
    const wchar_t *InvoiceShipAddrCountrySubDivisionCode = jsonResponse.stringOf(L"Invoice.ShipAddr.CountrySubDivisionCode");
    const wchar_t *InvoiceShipAddrCity = jsonResponse.stringOf(L"Invoice.ShipAddr.City");
    const wchar_t *InvoiceShipAddrPostalCode = jsonResponse.stringOf(L"Invoice.ShipAddr.PostalCode");
    const wchar_t *InvoiceShipAddrId = jsonResponse.stringOf(L"Invoice.ShipAddr.Id");
    const wchar_t *InvoiceShipAddrLine1 = jsonResponse.stringOf(L"Invoice.ShipAddr.Line1");
    const wchar_t *InvoiceEmailStatus = jsonResponse.stringOf(L"Invoice.EmailStatus");
    const wchar_t *InvoiceBillAddrCountrySubDivisionCode = jsonResponse.stringOf(L"Invoice.BillAddr.CountrySubDivisionCode");
    const wchar_t *InvoiceBillAddrCity = jsonResponse.stringOf(L"Invoice.BillAddr.City");
    const wchar_t *InvoiceBillAddrPostalCode = jsonResponse.stringOf(L"Invoice.BillAddr.PostalCode");
    const wchar_t *InvoiceBillAddrId = jsonResponse.stringOf(L"Invoice.BillAddr.Id");
    const wchar_t *InvoiceBillAddrLine1 = jsonResponse.stringOf(L"Invoice.BillAddr.Line1");
    const wchar_t *InvoiceMetaDataCreateTime = jsonResponse.stringOf(L"Invoice.MetaData.CreateTime");
    const wchar_t *InvoiceMetaDataLastUpdatedTime = jsonResponse.stringOf(L"Invoice.MetaData.LastUpdatedTime");
    const wchar_t *InvoiceId = jsonResponse.stringOf(L"Invoice.Id");
    const wchar_t *time = jsonResponse.stringOf(L"time");
    int i = 0;
    int 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.LinkedTxn");
    while (i < count_i) {
        jsonResponse.put_I(i);
        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;
    }
    }