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

Quickbooks Send an Invoice

See more QuickBooks Examples

Demonstrates how to send 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.
    //  --------------------------------------------------------------------------

    //  Technically, the POST has an empty request body, but the Quickbooks documentation indicates that 
    //  the Content-Type header should be set to "application/octet-stream", which really makes no sense
    //  because there is not content. (How can no content have a type???)
    rest.AddHeader(L"Content-Type",L"application/octet-stream");
    rest.put_AllowHeaderFolding(false);

    CkStringBuilderW sbResponseBody;
    success = rest.FullRequestNoBodySb(L"POST",L"/v3/company/<realmID>/invoice/<invoiceId>/send?sendTo=<emailAddr>",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": "2013-03-14",
    //      "domain": "QBO",
    //      "CurrencyRef": {
    //        "name": "United States Dollar",
    //        "value": "USD"
    //      },
    //      "ShipDate": "2013-03-01",
    //      "TrackingNum": "123456789",
    //      "ClassRef": {
    //        "name": "Class 1",
    //        "value": "200900000000000003901"
    //      },
    //      "PrintStatus": "NeedToPrint",
    //      "SalesTermRef": {
    //        "value": "4"
    //      },
    //      "DeliveryInfo": {
    //        "DeliveryType": "Email",
    //        "DeliveryTime": "2014-12-17T11:50:52-08:00"
    //      },
    //      "TotalAmt": 52.0,
    //      "Line": [
    //        {
    //          "Description": "Sample invoice create request",
    //          "DetailType": "SalesItemLineDetail",
    //          "SalesItemLineDetail": {
    //            "TaxCodeRef": {
    //              "value": "TAX"
    //            },
    //            "Qty": 1,
    //            "UnitPrice": 50,
    //            "ServiceDate": "2013-03-04",
    //            "ItemRef": {
    //              "name": "Hours",
    //              "value": "2"
    //            }
    //          },
    //          "LineNum": 1,
    //          "Amount": 50.0,
    //          "Id": "1"
    //        },
    //        {
    //          "DetailType": "SubTotalLineDetail",
    //          "Amount": 50.0,
    //          "SubTotalLineDetail": {}
    //        },
    //        {
    //          "DetailType": "DiscountLineDetail",
    //          "Amount": 5.0,
    //          "DiscountLineDetail": {
    //            "DiscountAccountRef": {
    //              "name": "Discounts given",
    //              "value": "30"
    //            },
    //            "PercentBased": true,
    //            "DiscountPercent": 10
    //          }
    //        },
    //        {
    //          "DetailType": "SalesItemLineDetail",
    //          "Amount": 2.0,
    //          "SalesItemLineDetail": {
    //            "ItemRef": {
    //              "value": "SHIPPING_ITEM_ID"
    //            }
    //          }
    //        }
    //      ],
    //      "DueDate": "2013-05-13",
    //      "MetaData": {
    //        "CreateTime": "2013-03-14T01:42:16-07:00",
    //        "LastUpdatedTime": "2014-12-17T11:50:58-08:00"
    //      },
    //      "DocNumber": "Sample_Inv#2",
    //      "PrivateNote": "Summary for sample invoice",
    //      "sparse": false,
    //      "DepositToAccountRef": {
    //        "name": "Undeposited Funds",
    //        "value": "4"
    //      },
    //      "CustomerMemo": {
    //        "value": "This is the customer message"
    //      },
    //      "EmailStatus": "EmailSent",
    //      "Deposit": 12.0,
    //      "Balance": 40.0,
    //      "CustomerRef": {
    //        "name": "Mr V3 Service Customer Jr2",
    //        "value": "15"
    //      },
    //      "TxnTaxDetail": {
    //        "TxnTaxCodeRef": {
    //          "value": "5"
    //        },
    //        "TotalTax": 5.0,
    //        "TaxLine": [
    //          {
    //            "DetailType": "TaxLineDetail",
    //            "Amount": 5.0,
    //            "TaxLineDetail": {
    //              "NetAmountTaxable": 50.0,
    //              "TaxPercent": 10,
    //              "TaxRateRef": {
    //                "value": "2"
    //              },
    //              "PercentBased": true
    //            }
    //          }
    //        ]
    //      },
    //      "SyncToken": "0",
    //      "BillEmail": {
    //        "Address": "test@intuit.com"
    //      },
    //      "ShipAddr": {
    //        "City": "San Jose",
    //        "Country": "USA",
    //        "Line5": "Cube 999",
    //        "Line4": "Dept 12",
    //        "Line3": "123 street",
    //        "Line2": "Building 1",
    //        "Line1": "Intuit",
    //        "PostalCode": "95123",
    //        "Lat": "37.2374847",
    //        "Long": "-121.8277925",
    //        "CountrySubDivisionCode": "CA",
    //        "Id": "36"
    //      },
    //      "DepartmentRef": {
    //        "name": "Mountain View",
    //        "value": "1"
    //      },
    //      "ShipMethodRef": {
    //        "name": "UPS",
    //        "value": "UPS"
    //      },
    //      "BillAddr": {
    //        "City": "Mountain View",
    //        "Country": "USA",
    //        "Line5": "Cube 999",
    //        "Line4": "Dept 12",
    //        "Line3": "123 street",
    //        "Line2": "Building 1",
    //        "Line1": "Google",
    //        "PostalCode": "95123",
    //        "Lat": "37.2374847",
    //        "Long": "-121.8277925",
    //        "CountrySubDivisionCode": "CA",
    //        "Id": "35"
    //      },
    //      "ApplyTaxAfterDiscount": false,
    //      "CustomField": [
    //        {
    //          "StringValue": "Custom1",
    //          "Type": "StringType",
    //          "Name": "Custom 1"
    //        },
    //        {
    //          "StringValue": "Custom2",
    //          "Type": "StringType",
    //          "Name": "Custom 2"
    //        },
    //        {
    //          "StringValue": "Custom3",
    //          "Type": "StringType",
    //          "Name": "Custom 3"
    //        }
    //      ],
    //      "Id": "96"
    //    },
    //    "time": "2013-03-14T13:32:04.895-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.

    const wchar_t *Description = 0;
    const wchar_t *DetailType = 0;
    const wchar_t *SalesItemLineDetailTaxCodeRefValue = 0;
    int SalesItemLineDetailQty;
    int SalesItemLineDetailUnitPrice;
    const wchar_t *SalesItemLineDetailServiceDate = 0;
    const wchar_t *SalesItemLineDetailItemRefName = 0;
    const wchar_t *SalesItemLineDetailItemRefValue = 0;
    int LineNum;
    const wchar_t *Amount = 0;
    const wchar_t *Id = 0;
    const wchar_t *DiscountLineDetailDiscountAccountRefName = 0;
    const wchar_t *DiscountLineDetailDiscountAccountRefValue = 0;
    bool DiscountLineDetailPercentBased;
    int DiscountLineDetailDiscountPercent;
    const wchar_t *TaxLineDetailNetAmountTaxable = 0;
    int TaxLineDetailTaxPercent;
    const wchar_t *TaxLineDetailTaxRateRefValue = 0;
    bool TaxLineDetailPercentBased;
    const wchar_t *StringValue = 0;
    const wchar_t *Type = 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 *InvoiceCurrencyRefName = jsonResponse.stringOf(L"Invoice.CurrencyRef.name");
    const wchar_t *InvoiceCurrencyRefValue = jsonResponse.stringOf(L"Invoice.CurrencyRef.value");
    const wchar_t *InvoiceShipDate = jsonResponse.stringOf(L"Invoice.ShipDate");
    const wchar_t *InvoiceTrackingNum = jsonResponse.stringOf(L"Invoice.TrackingNum");
    const wchar_t *InvoiceClassRefName = jsonResponse.stringOf(L"Invoice.ClassRef.name");
    const wchar_t *InvoiceClassRefValue = jsonResponse.stringOf(L"Invoice.ClassRef.value");
    const wchar_t *InvoicePrintStatus = jsonResponse.stringOf(L"Invoice.PrintStatus");
    const wchar_t *InvoiceSalesTermRefValue = jsonResponse.stringOf(L"Invoice.SalesTermRef.value");
    const wchar_t *InvoiceDeliveryInfoDeliveryType = jsonResponse.stringOf(L"Invoice.DeliveryInfo.DeliveryType");
    const wchar_t *InvoiceDeliveryInfoDeliveryTime = jsonResponse.stringOf(L"Invoice.DeliveryInfo.DeliveryTime");
    const wchar_t *InvoiceTotalAmt = jsonResponse.stringOf(L"Invoice.TotalAmt");
    const wchar_t *InvoiceDueDate = jsonResponse.stringOf(L"Invoice.DueDate");
    const wchar_t *InvoiceMetaDataCreateTime = jsonResponse.stringOf(L"Invoice.MetaData.CreateTime");
    const wchar_t *InvoiceMetaDataLastUpdatedTime = jsonResponse.stringOf(L"Invoice.MetaData.LastUpdatedTime");
    const wchar_t *InvoiceDocNumber = jsonResponse.stringOf(L"Invoice.DocNumber");
    const wchar_t *InvoicePrivateNote = jsonResponse.stringOf(L"Invoice.PrivateNote");
    bool InvoiceSparse = jsonResponse.BoolOf(L"Invoice.sparse");
    const wchar_t *InvoiceDepositToAccountRefName = jsonResponse.stringOf(L"Invoice.DepositToAccountRef.name");
    const wchar_t *InvoiceDepositToAccountRefValue = jsonResponse.stringOf(L"Invoice.DepositToAccountRef.value");
    const wchar_t *InvoiceCustomerMemoValue = jsonResponse.stringOf(L"Invoice.CustomerMemo.value");
    const wchar_t *InvoiceEmailStatus = jsonResponse.stringOf(L"Invoice.EmailStatus");
    const wchar_t *InvoiceDeposit = jsonResponse.stringOf(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");
    const wchar_t *InvoiceTxnTaxDetailTxnTaxCodeRefValue = jsonResponse.stringOf(L"Invoice.TxnTaxDetail.TxnTaxCodeRef.value");
    const wchar_t *InvoiceTxnTaxDetailTotalTax = jsonResponse.stringOf(L"Invoice.TxnTaxDetail.TotalTax");
    const wchar_t *InvoiceSyncToken = jsonResponse.stringOf(L"Invoice.SyncToken");
    const wchar_t *InvoiceBillEmailAddress = jsonResponse.stringOf(L"Invoice.BillEmail.Address");
    const wchar_t *InvoiceShipAddrCity = jsonResponse.stringOf(L"Invoice.ShipAddr.City");
    const wchar_t *InvoiceShipAddrCountry = jsonResponse.stringOf(L"Invoice.ShipAddr.Country");
    const wchar_t *InvoiceShipAddrLine5 = jsonResponse.stringOf(L"Invoice.ShipAddr.Line5");
    const wchar_t *InvoiceShipAddrLine4 = jsonResponse.stringOf(L"Invoice.ShipAddr.Line4");
    const wchar_t *InvoiceShipAddrLine3 = jsonResponse.stringOf(L"Invoice.ShipAddr.Line3");
    const wchar_t *InvoiceShipAddrLine2 = jsonResponse.stringOf(L"Invoice.ShipAddr.Line2");
    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 *InvoiceDepartmentRefName = jsonResponse.stringOf(L"Invoice.DepartmentRef.name");
    const wchar_t *InvoiceDepartmentRefValue = jsonResponse.stringOf(L"Invoice.DepartmentRef.value");
    const wchar_t *InvoiceShipMethodRefName = jsonResponse.stringOf(L"Invoice.ShipMethodRef.name");
    const wchar_t *InvoiceShipMethodRefValue = jsonResponse.stringOf(L"Invoice.ShipMethodRef.value");
    const wchar_t *InvoiceBillAddrCity = jsonResponse.stringOf(L"Invoice.BillAddr.City");
    const wchar_t *InvoiceBillAddrCountry = jsonResponse.stringOf(L"Invoice.BillAddr.Country");
    const wchar_t *InvoiceBillAddrLine5 = jsonResponse.stringOf(L"Invoice.BillAddr.Line5");
    const wchar_t *InvoiceBillAddrLine4 = jsonResponse.stringOf(L"Invoice.BillAddr.Line4");
    const wchar_t *InvoiceBillAddrLine3 = jsonResponse.stringOf(L"Invoice.BillAddr.Line3");
    const wchar_t *InvoiceBillAddrLine2 = jsonResponse.stringOf(L"Invoice.BillAddr.Line2");
    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");
    bool InvoiceApplyTaxAfterDiscount = jsonResponse.BoolOf(L"Invoice.ApplyTaxAfterDiscount");
    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);
        Description = jsonResponse.stringOf(L"Invoice.Line[i].Description");
        DetailType = jsonResponse.stringOf(L"Invoice.Line[i].DetailType");
        SalesItemLineDetailTaxCodeRefValue = jsonResponse.stringOf(L"Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value");
        SalesItemLineDetailQty = jsonResponse.IntOf(L"Invoice.Line[i].SalesItemLineDetail.Qty");
        SalesItemLineDetailUnitPrice = jsonResponse.IntOf(L"Invoice.Line[i].SalesItemLineDetail.UnitPrice");
        SalesItemLineDetailServiceDate = jsonResponse.stringOf(L"Invoice.Line[i].SalesItemLineDetail.ServiceDate");
        SalesItemLineDetailItemRefName = jsonResponse.stringOf(L"Invoice.Line[i].SalesItemLineDetail.ItemRef.name");
        SalesItemLineDetailItemRefValue = jsonResponse.stringOf(L"Invoice.Line[i].SalesItemLineDetail.ItemRef.value");
        LineNum = jsonResponse.IntOf(L"Invoice.Line[i].LineNum");
        Amount = jsonResponse.stringOf(L"Invoice.Line[i].Amount");
        Id = jsonResponse.stringOf(L"Invoice.Line[i].Id");
        DiscountLineDetailDiscountAccountRefName = jsonResponse.stringOf(L"Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.name");
        DiscountLineDetailDiscountAccountRefValue = jsonResponse.stringOf(L"Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.value");
        DiscountLineDetailPercentBased = jsonResponse.BoolOf(L"Invoice.Line[i].DiscountLineDetail.PercentBased");
        DiscountLineDetailDiscountPercent = jsonResponse.IntOf(L"Invoice.Line[i].DiscountLineDetail.DiscountPercent");
        i = i + 1;
    }

    i = 0;
    count_i = jsonResponse.SizeOfArray(L"Invoice.TxnTaxDetail.TaxLine");
    while (i < count_i) {
        jsonResponse.put_I(i);
        DetailType = jsonResponse.stringOf(L"Invoice.TxnTaxDetail.TaxLine[i].DetailType");
        Amount = jsonResponse.stringOf(L"Invoice.TxnTaxDetail.TaxLine[i].Amount");
        TaxLineDetailNetAmountTaxable = jsonResponse.stringOf(L"Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.NetAmountTaxable");
        TaxLineDetailTaxPercent = jsonResponse.IntOf(L"Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxPercent");
        TaxLineDetailTaxRateRefValue = jsonResponse.stringOf(L"Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxRateRef.value");
        TaxLineDetailPercentBased = jsonResponse.BoolOf(L"Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.PercentBased");
        i = i + 1;
    }

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