Sample code for 30+ languages & platforms
Java

Quickbooks Send an Invoice

See more QuickBooks Examples

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

Chilkat Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean 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.
    CkJsonObject jsonToken = new CkJsonObject();
    success = jsonToken.LoadFile("qa_data/tokens/qb-access-token.json");

    CkRest rest = new CkRest();

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

    CkStringBuilder sbAuth = new CkStringBuilder();
    sbAuth.Append("Bearer ");
    sbAuth.Append(jsonToken.stringOf("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("Content-Type","application/octet-stream");
    rest.put_AllowHeaderFolding(false);

    CkStringBuilder sbResponseBody = new CkStringBuilder();
    success = rest.FullRequestNoBodySb("POST","/v3/company/<realmID>/invoice/<invoiceId>/send?sendTo=<emailAddr>",sbResponseBody);
    if (success != true) {
        System.out.println(rest.lastErrorText());
        return;
        }

    int respStatusCode = rest.get_ResponseStatusCode();

    //  Success is indicated by a 200 response status code.
    System.out.println("response status code = " + respStatusCode);

    CkJsonObject jsonResponse = new CkJsonObject();
    jsonResponse.LoadSb(sbResponseBody);
    jsonResponse.put_EmitCompact(false);
    System.out.println(jsonResponse.emit());

    if (rest.get_ResponseStatusCode() != 200) {
        System.out.println("Failed.");
        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"
    //  }
    //  

    String Description;
    String DetailType;
    String SalesItemLineDetailTaxCodeRefValue;
    int SalesItemLineDetailQty;
    int SalesItemLineDetailUnitPrice;
    String SalesItemLineDetailServiceDate;
    String SalesItemLineDetailItemRefName;
    String SalesItemLineDetailItemRefValue;
    int LineNum;
    String Amount;
    String Id;
    String DiscountLineDetailDiscountAccountRefName;
    String DiscountLineDetailDiscountAccountRefValue;
    boolean DiscountLineDetailPercentBased;
    int DiscountLineDetailDiscountPercent;
    String TaxLineDetailNetAmountTaxable;
    int TaxLineDetailTaxPercent;
    String TaxLineDetailTaxRateRefValue;
    boolean TaxLineDetailPercentBased;
    String StringValue;
    String Type;
    String Name;

    String InvoiceTxnDate = jsonResponse.stringOf("Invoice.TxnDate");
    String InvoiceDomain = jsonResponse.stringOf("Invoice.domain");
    String InvoiceCurrencyRefName = jsonResponse.stringOf("Invoice.CurrencyRef.name");
    String InvoiceCurrencyRefValue = jsonResponse.stringOf("Invoice.CurrencyRef.value");
    String InvoiceShipDate = jsonResponse.stringOf("Invoice.ShipDate");
    String InvoiceTrackingNum = jsonResponse.stringOf("Invoice.TrackingNum");
    String InvoiceClassRefName = jsonResponse.stringOf("Invoice.ClassRef.name");
    String InvoiceClassRefValue = jsonResponse.stringOf("Invoice.ClassRef.value");
    String InvoicePrintStatus = jsonResponse.stringOf("Invoice.PrintStatus");
    String InvoiceSalesTermRefValue = jsonResponse.stringOf("Invoice.SalesTermRef.value");
    String InvoiceDeliveryInfoDeliveryType = jsonResponse.stringOf("Invoice.DeliveryInfo.DeliveryType");
    String InvoiceDeliveryInfoDeliveryTime = jsonResponse.stringOf("Invoice.DeliveryInfo.DeliveryTime");
    String InvoiceTotalAmt = jsonResponse.stringOf("Invoice.TotalAmt");
    String InvoiceDueDate = jsonResponse.stringOf("Invoice.DueDate");
    String InvoiceMetaDataCreateTime = jsonResponse.stringOf("Invoice.MetaData.CreateTime");
    String InvoiceMetaDataLastUpdatedTime = jsonResponse.stringOf("Invoice.MetaData.LastUpdatedTime");
    String InvoiceDocNumber = jsonResponse.stringOf("Invoice.DocNumber");
    String InvoicePrivateNote = jsonResponse.stringOf("Invoice.PrivateNote");
    boolean InvoiceSparse = jsonResponse.BoolOf("Invoice.sparse");
    String InvoiceDepositToAccountRefName = jsonResponse.stringOf("Invoice.DepositToAccountRef.name");
    String InvoiceDepositToAccountRefValue = jsonResponse.stringOf("Invoice.DepositToAccountRef.value");
    String InvoiceCustomerMemoValue = jsonResponse.stringOf("Invoice.CustomerMemo.value");
    String InvoiceEmailStatus = jsonResponse.stringOf("Invoice.EmailStatus");
    String InvoiceDeposit = jsonResponse.stringOf("Invoice.Deposit");
    String InvoiceBalance = jsonResponse.stringOf("Invoice.Balance");
    String InvoiceCustomerRefName = jsonResponse.stringOf("Invoice.CustomerRef.name");
    String InvoiceCustomerRefValue = jsonResponse.stringOf("Invoice.CustomerRef.value");
    String InvoiceTxnTaxDetailTxnTaxCodeRefValue = jsonResponse.stringOf("Invoice.TxnTaxDetail.TxnTaxCodeRef.value");
    String InvoiceTxnTaxDetailTotalTax = jsonResponse.stringOf("Invoice.TxnTaxDetail.TotalTax");
    String InvoiceSyncToken = jsonResponse.stringOf("Invoice.SyncToken");
    String InvoiceBillEmailAddress = jsonResponse.stringOf("Invoice.BillEmail.Address");
    String InvoiceShipAddrCity = jsonResponse.stringOf("Invoice.ShipAddr.City");
    String InvoiceShipAddrCountry = jsonResponse.stringOf("Invoice.ShipAddr.Country");
    String InvoiceShipAddrLine5 = jsonResponse.stringOf("Invoice.ShipAddr.Line5");
    String InvoiceShipAddrLine4 = jsonResponse.stringOf("Invoice.ShipAddr.Line4");
    String InvoiceShipAddrLine3 = jsonResponse.stringOf("Invoice.ShipAddr.Line3");
    String InvoiceShipAddrLine2 = jsonResponse.stringOf("Invoice.ShipAddr.Line2");
    String InvoiceShipAddrLine1 = jsonResponse.stringOf("Invoice.ShipAddr.Line1");
    String InvoiceShipAddrPostalCode = jsonResponse.stringOf("Invoice.ShipAddr.PostalCode");
    String InvoiceShipAddrLat = jsonResponse.stringOf("Invoice.ShipAddr.Lat");
    String InvoiceShipAddrLong = jsonResponse.stringOf("Invoice.ShipAddr.Long");
    String InvoiceShipAddrCountrySubDivisionCode = jsonResponse.stringOf("Invoice.ShipAddr.CountrySubDivisionCode");
    String InvoiceShipAddrId = jsonResponse.stringOf("Invoice.ShipAddr.Id");
    String InvoiceDepartmentRefName = jsonResponse.stringOf("Invoice.DepartmentRef.name");
    String InvoiceDepartmentRefValue = jsonResponse.stringOf("Invoice.DepartmentRef.value");
    String InvoiceShipMethodRefName = jsonResponse.stringOf("Invoice.ShipMethodRef.name");
    String InvoiceShipMethodRefValue = jsonResponse.stringOf("Invoice.ShipMethodRef.value");
    String InvoiceBillAddrCity = jsonResponse.stringOf("Invoice.BillAddr.City");
    String InvoiceBillAddrCountry = jsonResponse.stringOf("Invoice.BillAddr.Country");
    String InvoiceBillAddrLine5 = jsonResponse.stringOf("Invoice.BillAddr.Line5");
    String InvoiceBillAddrLine4 = jsonResponse.stringOf("Invoice.BillAddr.Line4");
    String InvoiceBillAddrLine3 = jsonResponse.stringOf("Invoice.BillAddr.Line3");
    String InvoiceBillAddrLine2 = jsonResponse.stringOf("Invoice.BillAddr.Line2");
    String InvoiceBillAddrLine1 = jsonResponse.stringOf("Invoice.BillAddr.Line1");
    String InvoiceBillAddrPostalCode = jsonResponse.stringOf("Invoice.BillAddr.PostalCode");
    String InvoiceBillAddrLat = jsonResponse.stringOf("Invoice.BillAddr.Lat");
    String InvoiceBillAddrLong = jsonResponse.stringOf("Invoice.BillAddr.Long");
    String InvoiceBillAddrCountrySubDivisionCode = jsonResponse.stringOf("Invoice.BillAddr.CountrySubDivisionCode");
    String InvoiceBillAddrId = jsonResponse.stringOf("Invoice.BillAddr.Id");
    boolean InvoiceApplyTaxAfterDiscount = jsonResponse.BoolOf("Invoice.ApplyTaxAfterDiscount");
    String InvoiceId = jsonResponse.stringOf("Invoice.Id");
    String time = jsonResponse.stringOf("time");
    int i = 0;
    int count_i = jsonResponse.SizeOfArray("Invoice.Line");
    while (i < count_i) {
        jsonResponse.put_I(i);
        Description = jsonResponse.stringOf("Invoice.Line[i].Description");
        DetailType = jsonResponse.stringOf("Invoice.Line[i].DetailType");
        SalesItemLineDetailTaxCodeRefValue = jsonResponse.stringOf("Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value");
        SalesItemLineDetailQty = jsonResponse.IntOf("Invoice.Line[i].SalesItemLineDetail.Qty");
        SalesItemLineDetailUnitPrice = jsonResponse.IntOf("Invoice.Line[i].SalesItemLineDetail.UnitPrice");
        SalesItemLineDetailServiceDate = jsonResponse.stringOf("Invoice.Line[i].SalesItemLineDetail.ServiceDate");
        SalesItemLineDetailItemRefName = jsonResponse.stringOf("Invoice.Line[i].SalesItemLineDetail.ItemRef.name");
        SalesItemLineDetailItemRefValue = jsonResponse.stringOf("Invoice.Line[i].SalesItemLineDetail.ItemRef.value");
        LineNum = jsonResponse.IntOf("Invoice.Line[i].LineNum");
        Amount = jsonResponse.stringOf("Invoice.Line[i].Amount");
        Id = jsonResponse.stringOf("Invoice.Line[i].Id");
        DiscountLineDetailDiscountAccountRefName = jsonResponse.stringOf("Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.name");
        DiscountLineDetailDiscountAccountRefValue = jsonResponse.stringOf("Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.value");
        DiscountLineDetailPercentBased = jsonResponse.BoolOf("Invoice.Line[i].DiscountLineDetail.PercentBased");
        DiscountLineDetailDiscountPercent = jsonResponse.IntOf("Invoice.Line[i].DiscountLineDetail.DiscountPercent");
        i = i+1;
        }

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

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