Sample code for 30+ languages & platforms
Xbase++

Quickbooks Send an Invoice

See more QuickBooks Examples

Demonstrates how to send an invoice using the Quickbooks REST API.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oJsonToken
LOCAL oRest
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect
LOCAL oSbAuth
LOCAL oSbResponseBody
LOCAL nRespStatusCode
LOCAL oJsonResponse
LOCAL cDescription
LOCAL cDetailType
LOCAL cSalesItemLineDetailTaxCodeRefValue
LOCAL nSalesItemLineDetailQty
LOCAL nSalesItemLineDetailUnitPrice
LOCAL cSalesItemLineDetailServiceDate
LOCAL cSalesItemLineDetailItemRefName
LOCAL cSalesItemLineDetailItemRefValue
LOCAL nLineNum
LOCAL cAmount
LOCAL cId
LOCAL cDiscountLineDetailDiscountAccountRefName
LOCAL cDiscountLineDetailDiscountAccountRefValue
LOCAL nDiscountLineDetailPercentBased
LOCAL nDiscountLineDetailDiscountPercent
LOCAL cTaxLineDetailNetAmountTaxable
LOCAL nTaxLineDetailTaxPercent
LOCAL cTaxLineDetailTaxRateRefValue
LOCAL nTaxLineDetailPercentBased
LOCAL cStringValue
LOCAL cType
LOCAL cName
LOCAL cInvoiceTxnDate
LOCAL cInvoiceDomain
LOCAL cInvoiceCurrencyRefName
LOCAL cInvoiceCurrencyRefValue
LOCAL cInvoiceShipDate
LOCAL cInvoiceTrackingNum
LOCAL cInvoiceClassRefName
LOCAL cInvoiceClassRefValue
LOCAL cInvoicePrintStatus
LOCAL cInvoiceSalesTermRefValue
LOCAL cInvoiceDeliveryInfoDeliveryType
LOCAL cInvoiceDeliveryInfoDeliveryTime
LOCAL cInvoiceTotalAmt
LOCAL cInvoiceDueDate
LOCAL cInvoiceMetaDataCreateTime
LOCAL cInvoiceMetaDataLastUpdatedTime
LOCAL cInvoiceDocNumber
LOCAL cInvoicePrivateNote
LOCAL nInvoiceSparse
LOCAL cInvoiceDepositToAccountRefName
LOCAL cInvoiceDepositToAccountRefValue
LOCAL cInvoiceCustomerMemoValue
LOCAL cInvoiceEmailStatus
LOCAL cInvoiceDeposit
LOCAL cInvoiceBalance
LOCAL cInvoiceCustomerRefName
LOCAL cInvoiceCustomerRefValue
LOCAL cInvoiceTxnTaxDetailTxnTaxCodeRefValue
LOCAL cInvoiceTxnTaxDetailTotalTax
LOCAL cInvoiceSyncToken
LOCAL cInvoiceBillEmailAddress
LOCAL cInvoiceShipAddrCity
LOCAL cInvoiceShipAddrCountry
LOCAL cInvoiceShipAddrLine5
LOCAL cInvoiceShipAddrLine4
LOCAL cInvoiceShipAddrLine3
LOCAL cInvoiceShipAddrLine2
LOCAL cInvoiceShipAddrLine1
LOCAL cInvoiceShipAddrPostalCode
LOCAL cInvoiceShipAddrLat
LOCAL cInvoiceShipAddrLong
LOCAL cInvoiceShipAddrCountrySubDivisionCode
LOCAL cInvoiceShipAddrId
LOCAL cInvoiceDepartmentRefName
LOCAL cInvoiceDepartmentRefValue
LOCAL cInvoiceShipMethodRefName
LOCAL cInvoiceShipMethodRefValue
LOCAL cInvoiceBillAddrCity
LOCAL cInvoiceBillAddrCountry
LOCAL cInvoiceBillAddrLine5
LOCAL cInvoiceBillAddrLine4
LOCAL cInvoiceBillAddrLine3
LOCAL cInvoiceBillAddrLine2
LOCAL cInvoiceBillAddrLine1
LOCAL cInvoiceBillAddrPostalCode
LOCAL cInvoiceBillAddrLat
LOCAL cInvoiceBillAddrLong
LOCAL cInvoiceBillAddrCountrySubDivisionCode
LOCAL cInvoiceBillAddrId
LOCAL nInvoiceApplyTaxAfterDiscount
LOCAL cInvoiceId
LOCAL cTime
LOCAL i
LOCAL nCount_i

nSuccess := 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.
oJsonToken := CreateObject("Chilkat.JsonObject")
nSuccess := oJsonToken:LoadFile("qa_data/tokens/qb-access-token.json")

oRest := CreateObject("Chilkat.Rest")

//  Connect to the REST server.
nBTls := 1
nPort := 443
nBAutoReconnect := 1
nSuccess := oRest:Connect("sandbox-quickbooks.api.intuit.com", nPort, nBTls, nBAutoReconnect)

oSbAuth := CreateObject("Chilkat.StringBuilder")
oSbAuth:Append("Bearer ")
oSbAuth:Append(oJsonToken:StringOf("access_token"))
oRest:Authorization := oSbAuth: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???)
oRest:AddHeader("Content-Type", "application/octet-stream")
oRest:AllowHeaderFolding := 0

oSbResponseBody := CreateObject("Chilkat.StringBuilder")
nSuccess := oRest:FullRequestNoBodySb("POST", "/v3/company/<realmID>/invoice/<invoiceId>/send?sendTo=<emailAddr>", oSbResponseBody)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oJsonToken:destroy()
    oRest:destroy()
    oSbAuth:destroy()
    oSbResponseBody:destroy()
    RETURN
ENDIF

nRespStatusCode := oRest:ResponseStatusCode

//  Success is indicated by a 200 response status code.
? "response status code = " + Str(nRespStatusCode)

oJsonResponse := CreateObject("Chilkat.JsonObject")
oJsonResponse:LoadSb(oSbResponseBody)
oJsonResponse:EmitCompact := 0
? oJsonResponse:Emit()

IF (oRest:ResponseStatusCode != 200)
    ? "Failed."
    oJsonToken:destroy()
    oRest:destroy()
    oSbAuth:destroy()
    oSbResponseBody:destroy()
    oJsonResponse:destroy()
    RETURN
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

//  {
//    "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"
//  }
//  

cInvoiceTxnDate := oJsonResponse:StringOf("Invoice.TxnDate")
cInvoiceDomain := oJsonResponse:StringOf("Invoice.domain")
cInvoiceCurrencyRefName := oJsonResponse:StringOf("Invoice.CurrencyRef.name")
cInvoiceCurrencyRefValue := oJsonResponse:StringOf("Invoice.CurrencyRef.value")
cInvoiceShipDate := oJsonResponse:StringOf("Invoice.ShipDate")
cInvoiceTrackingNum := oJsonResponse:StringOf("Invoice.TrackingNum")
cInvoiceClassRefName := oJsonResponse:StringOf("Invoice.ClassRef.name")
cInvoiceClassRefValue := oJsonResponse:StringOf("Invoice.ClassRef.value")
cInvoicePrintStatus := oJsonResponse:StringOf("Invoice.PrintStatus")
cInvoiceSalesTermRefValue := oJsonResponse:StringOf("Invoice.SalesTermRef.value")
cInvoiceDeliveryInfoDeliveryType := oJsonResponse:StringOf("Invoice.DeliveryInfo.DeliveryType")
cInvoiceDeliveryInfoDeliveryTime := oJsonResponse:StringOf("Invoice.DeliveryInfo.DeliveryTime")
cInvoiceTotalAmt := oJsonResponse:StringOf("Invoice.TotalAmt")
cInvoiceDueDate := oJsonResponse:StringOf("Invoice.DueDate")
cInvoiceMetaDataCreateTime := oJsonResponse:StringOf("Invoice.MetaData.CreateTime")
cInvoiceMetaDataLastUpdatedTime := oJsonResponse:StringOf("Invoice.MetaData.LastUpdatedTime")
cInvoiceDocNumber := oJsonResponse:StringOf("Invoice.DocNumber")
cInvoicePrivateNote := oJsonResponse:StringOf("Invoice.PrivateNote")
nInvoiceSparse := oJsonResponse:BoolOf("Invoice.sparse")
cInvoiceDepositToAccountRefName := oJsonResponse:StringOf("Invoice.DepositToAccountRef.name")
cInvoiceDepositToAccountRefValue := oJsonResponse:StringOf("Invoice.DepositToAccountRef.value")
cInvoiceCustomerMemoValue := oJsonResponse:StringOf("Invoice.CustomerMemo.value")
cInvoiceEmailStatus := oJsonResponse:StringOf("Invoice.EmailStatus")
cInvoiceDeposit := oJsonResponse:StringOf("Invoice.Deposit")
cInvoiceBalance := oJsonResponse:StringOf("Invoice.Balance")
cInvoiceCustomerRefName := oJsonResponse:StringOf("Invoice.CustomerRef.name")
cInvoiceCustomerRefValue := oJsonResponse:StringOf("Invoice.CustomerRef.value")
cInvoiceTxnTaxDetailTxnTaxCodeRefValue := oJsonResponse:StringOf("Invoice.TxnTaxDetail.TxnTaxCodeRef.value")
cInvoiceTxnTaxDetailTotalTax := oJsonResponse:StringOf("Invoice.TxnTaxDetail.TotalTax")
cInvoiceSyncToken := oJsonResponse:StringOf("Invoice.SyncToken")
cInvoiceBillEmailAddress := oJsonResponse:StringOf("Invoice.BillEmail.Address")
cInvoiceShipAddrCity := oJsonResponse:StringOf("Invoice.ShipAddr.City")
cInvoiceShipAddrCountry := oJsonResponse:StringOf("Invoice.ShipAddr.Country")
cInvoiceShipAddrLine5 := oJsonResponse:StringOf("Invoice.ShipAddr.Line5")
cInvoiceShipAddrLine4 := oJsonResponse:StringOf("Invoice.ShipAddr.Line4")
cInvoiceShipAddrLine3 := oJsonResponse:StringOf("Invoice.ShipAddr.Line3")
cInvoiceShipAddrLine2 := oJsonResponse:StringOf("Invoice.ShipAddr.Line2")
cInvoiceShipAddrLine1 := oJsonResponse:StringOf("Invoice.ShipAddr.Line1")
cInvoiceShipAddrPostalCode := oJsonResponse:StringOf("Invoice.ShipAddr.PostalCode")
cInvoiceShipAddrLat := oJsonResponse:StringOf("Invoice.ShipAddr.Lat")
cInvoiceShipAddrLong := oJsonResponse:StringOf("Invoice.ShipAddr.Long")
cInvoiceShipAddrCountrySubDivisionCode := oJsonResponse:StringOf("Invoice.ShipAddr.CountrySubDivisionCode")
cInvoiceShipAddrId := oJsonResponse:StringOf("Invoice.ShipAddr.Id")
cInvoiceDepartmentRefName := oJsonResponse:StringOf("Invoice.DepartmentRef.name")
cInvoiceDepartmentRefValue := oJsonResponse:StringOf("Invoice.DepartmentRef.value")
cInvoiceShipMethodRefName := oJsonResponse:StringOf("Invoice.ShipMethodRef.name")
cInvoiceShipMethodRefValue := oJsonResponse:StringOf("Invoice.ShipMethodRef.value")
cInvoiceBillAddrCity := oJsonResponse:StringOf("Invoice.BillAddr.City")
cInvoiceBillAddrCountry := oJsonResponse:StringOf("Invoice.BillAddr.Country")
cInvoiceBillAddrLine5 := oJsonResponse:StringOf("Invoice.BillAddr.Line5")
cInvoiceBillAddrLine4 := oJsonResponse:StringOf("Invoice.BillAddr.Line4")
cInvoiceBillAddrLine3 := oJsonResponse:StringOf("Invoice.BillAddr.Line3")
cInvoiceBillAddrLine2 := oJsonResponse:StringOf("Invoice.BillAddr.Line2")
cInvoiceBillAddrLine1 := oJsonResponse:StringOf("Invoice.BillAddr.Line1")
cInvoiceBillAddrPostalCode := oJsonResponse:StringOf("Invoice.BillAddr.PostalCode")
cInvoiceBillAddrLat := oJsonResponse:StringOf("Invoice.BillAddr.Lat")
cInvoiceBillAddrLong := oJsonResponse:StringOf("Invoice.BillAddr.Long")
cInvoiceBillAddrCountrySubDivisionCode := oJsonResponse:StringOf("Invoice.BillAddr.CountrySubDivisionCode")
cInvoiceBillAddrId := oJsonResponse:StringOf("Invoice.BillAddr.Id")
nInvoiceApplyTaxAfterDiscount := oJsonResponse:BoolOf("Invoice.ApplyTaxAfterDiscount")
cInvoiceId := oJsonResponse:StringOf("Invoice.Id")
cTime := oJsonResponse:StringOf("time")
i := 0
nCount_i := oJsonResponse:SizeOfArray("Invoice.Line")
DO WHILE i < nCount_i
    oJsonResponse:I := i
    cDescription := oJsonResponse:StringOf("Invoice.Line[i].Description")
    cDetailType := oJsonResponse:StringOf("Invoice.Line[i].DetailType")
    cSalesItemLineDetailTaxCodeRefValue := oJsonResponse:StringOf("Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value")
    nSalesItemLineDetailQty := oJsonResponse:IntOf("Invoice.Line[i].SalesItemLineDetail.Qty")
    nSalesItemLineDetailUnitPrice := oJsonResponse:IntOf("Invoice.Line[i].SalesItemLineDetail.UnitPrice")
    cSalesItemLineDetailServiceDate := oJsonResponse:StringOf("Invoice.Line[i].SalesItemLineDetail.ServiceDate")
    cSalesItemLineDetailItemRefName := oJsonResponse:StringOf("Invoice.Line[i].SalesItemLineDetail.ItemRef.name")
    cSalesItemLineDetailItemRefValue := oJsonResponse:StringOf("Invoice.Line[i].SalesItemLineDetail.ItemRef.value")
    nLineNum := oJsonResponse:IntOf("Invoice.Line[i].LineNum")
    cAmount := oJsonResponse:StringOf("Invoice.Line[i].Amount")
    cId := oJsonResponse:StringOf("Invoice.Line[i].Id")
    cDiscountLineDetailDiscountAccountRefName := oJsonResponse:StringOf("Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.name")
    cDiscountLineDetailDiscountAccountRefValue := oJsonResponse:StringOf("Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.value")
    nDiscountLineDetailPercentBased := oJsonResponse:BoolOf("Invoice.Line[i].DiscountLineDetail.PercentBased")
    nDiscountLineDetailDiscountPercent := oJsonResponse:IntOf("Invoice.Line[i].DiscountLineDetail.DiscountPercent")
    i := i + 1
ENDDO
i := 0
nCount_i := oJsonResponse:SizeOfArray("Invoice.TxnTaxDetail.TaxLine")
DO WHILE i < nCount_i
    oJsonResponse:I := i
    cDetailType := oJsonResponse:StringOf("Invoice.TxnTaxDetail.TaxLine[i].DetailType")
    cAmount := oJsonResponse:StringOf("Invoice.TxnTaxDetail.TaxLine[i].Amount")
    cTaxLineDetailNetAmountTaxable := oJsonResponse:StringOf("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.NetAmountTaxable")
    nTaxLineDetailTaxPercent := oJsonResponse:IntOf("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxPercent")
    cTaxLineDetailTaxRateRefValue := oJsonResponse:StringOf("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxRateRef.value")
    nTaxLineDetailPercentBased := oJsonResponse:BoolOf("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.PercentBased")
    i := i + 1
ENDDO
i := 0
nCount_i := oJsonResponse:SizeOfArray("Invoice.CustomField")
DO WHILE i < nCount_i
    oJsonResponse:I := i
    cStringValue := oJsonResponse:StringOf("Invoice.CustomField[i].StringValue")
    cType := oJsonResponse:StringOf("Invoice.CustomField[i].Type")
    cName := oJsonResponse:StringOf("Invoice.CustomField[i].Name")
    i := i + 1
ENDDO

oJsonToken:destroy()
oRest:destroy()
oSbAuth:destroy()
oSbResponseBody:destroy()
oJsonResponse:destroy()