Lua
Lua
Quickbooks Send an Invoice
See more QuickBooks Examples
Demonstrates how to send an invoice using the Quickbooks REST API.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- 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.
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/qb-access-token.json")
local rest = chilkat.Rest.new()
-- Connect to the REST server.
local bTls = true
local port = 443
local bAutoReconnect = true
success = rest:connect("sandbox-quickbooks.api.intuit.com", port, bTls, bAutoReconnect)
local sbAuth = chilkat.StringBuilder.new()
sbAuth:append("Bearer ")
sbAuth:append(jsonToken:string_of("access_token"))
rest.authorization = sbAuth:get_as_string()
-- --------------------------------------------------------------------------
-- 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:add_header("Content-Type", "application/octet-stream")
rest.allow_header_folding = false
local sbResponseBody = chilkat.StringBuilder.new()
success = rest:full_request_no_body_sb("POST", "/v3/company/<realmID>/invoice/<invoiceId>/send?sendTo=<emailAddr>", sbResponseBody)
if not success then
print(rest.last_error_text)
return
end
local respStatusCode = rest.response_status_code
-- Success is indicated by a 200 response status code.
print("response status code = " .. respStatusCode)
local jsonResponse = chilkat.JsonObject.new()
jsonResponse:load_sb(sbResponseBody)
jsonResponse.emit_compact = false
print(jsonResponse:emit())
if rest.response_status_code ~= 200 then
print("Failed.")
return
end
-- 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"
-- }
--
local Description = ""
local DetailType = ""
local SalesItemLineDetailTaxCodeRefValue = ""
local SalesItemLineDetailQty = 0
local SalesItemLineDetailUnitPrice = 0
local SalesItemLineDetailServiceDate = ""
local SalesItemLineDetailItemRefName = ""
local SalesItemLineDetailItemRefValue = ""
local LineNum = 0
local Amount = ""
local Id = ""
local DiscountLineDetailDiscountAccountRefName = ""
local DiscountLineDetailDiscountAccountRefValue = ""
local DiscountLineDetailPercentBased = false
local DiscountLineDetailDiscountPercent = 0
local TaxLineDetailNetAmountTaxable = ""
local TaxLineDetailTaxPercent = 0
local TaxLineDetailTaxRateRefValue = ""
local TaxLineDetailPercentBased = false
local StringValue = ""
local Type = ""
local Name = ""
local InvoiceTxnDate = jsonResponse:string_of("Invoice.TxnDate")
local InvoiceDomain = jsonResponse:string_of("Invoice.domain")
local InvoiceCurrencyRefName = jsonResponse:string_of("Invoice.CurrencyRef.name")
local InvoiceCurrencyRefValue = jsonResponse:string_of("Invoice.CurrencyRef.value")
local InvoiceShipDate = jsonResponse:string_of("Invoice.ShipDate")
local InvoiceTrackingNum = jsonResponse:string_of("Invoice.TrackingNum")
local InvoiceClassRefName = jsonResponse:string_of("Invoice.ClassRef.name")
local InvoiceClassRefValue = jsonResponse:string_of("Invoice.ClassRef.value")
local InvoicePrintStatus = jsonResponse:string_of("Invoice.PrintStatus")
local InvoiceSalesTermRefValue = jsonResponse:string_of("Invoice.SalesTermRef.value")
local InvoiceDeliveryInfoDeliveryType = jsonResponse:string_of("Invoice.DeliveryInfo.DeliveryType")
local InvoiceDeliveryInfoDeliveryTime = jsonResponse:string_of("Invoice.DeliveryInfo.DeliveryTime")
local InvoiceTotalAmt = jsonResponse:string_of("Invoice.TotalAmt")
local InvoiceDueDate = jsonResponse:string_of("Invoice.DueDate")
local InvoiceMetaDataCreateTime = jsonResponse:string_of("Invoice.MetaData.CreateTime")
local InvoiceMetaDataLastUpdatedTime = jsonResponse:string_of("Invoice.MetaData.LastUpdatedTime")
local InvoiceDocNumber = jsonResponse:string_of("Invoice.DocNumber")
local InvoicePrivateNote = jsonResponse:string_of("Invoice.PrivateNote")
local InvoiceSparse = jsonResponse:bool_of("Invoice.sparse")
local InvoiceDepositToAccountRefName = jsonResponse:string_of("Invoice.DepositToAccountRef.name")
local InvoiceDepositToAccountRefValue = jsonResponse:string_of("Invoice.DepositToAccountRef.value")
local InvoiceCustomerMemoValue = jsonResponse:string_of("Invoice.CustomerMemo.value")
local InvoiceEmailStatus = jsonResponse:string_of("Invoice.EmailStatus")
local InvoiceDeposit = jsonResponse:string_of("Invoice.Deposit")
local InvoiceBalance = jsonResponse:string_of("Invoice.Balance")
local InvoiceCustomerRefName = jsonResponse:string_of("Invoice.CustomerRef.name")
local InvoiceCustomerRefValue = jsonResponse:string_of("Invoice.CustomerRef.value")
local InvoiceTxnTaxDetailTxnTaxCodeRefValue = jsonResponse:string_of("Invoice.TxnTaxDetail.TxnTaxCodeRef.value")
local InvoiceTxnTaxDetailTotalTax = jsonResponse:string_of("Invoice.TxnTaxDetail.TotalTax")
local InvoiceSyncToken = jsonResponse:string_of("Invoice.SyncToken")
local InvoiceBillEmailAddress = jsonResponse:string_of("Invoice.BillEmail.Address")
local InvoiceShipAddrCity = jsonResponse:string_of("Invoice.ShipAddr.City")
local InvoiceShipAddrCountry = jsonResponse:string_of("Invoice.ShipAddr.Country")
local InvoiceShipAddrLine5 = jsonResponse:string_of("Invoice.ShipAddr.Line5")
local InvoiceShipAddrLine4 = jsonResponse:string_of("Invoice.ShipAddr.Line4")
local InvoiceShipAddrLine3 = jsonResponse:string_of("Invoice.ShipAddr.Line3")
local InvoiceShipAddrLine2 = jsonResponse:string_of("Invoice.ShipAddr.Line2")
local InvoiceShipAddrLine1 = jsonResponse:string_of("Invoice.ShipAddr.Line1")
local InvoiceShipAddrPostalCode = jsonResponse:string_of("Invoice.ShipAddr.PostalCode")
local InvoiceShipAddrLat = jsonResponse:string_of("Invoice.ShipAddr.Lat")
local InvoiceShipAddrLong = jsonResponse:string_of("Invoice.ShipAddr.Long")
local InvoiceShipAddrCountrySubDivisionCode = jsonResponse:string_of("Invoice.ShipAddr.CountrySubDivisionCode")
local InvoiceShipAddrId = jsonResponse:string_of("Invoice.ShipAddr.Id")
local InvoiceDepartmentRefName = jsonResponse:string_of("Invoice.DepartmentRef.name")
local InvoiceDepartmentRefValue = jsonResponse:string_of("Invoice.DepartmentRef.value")
local InvoiceShipMethodRefName = jsonResponse:string_of("Invoice.ShipMethodRef.name")
local InvoiceShipMethodRefValue = jsonResponse:string_of("Invoice.ShipMethodRef.value")
local InvoiceBillAddrCity = jsonResponse:string_of("Invoice.BillAddr.City")
local InvoiceBillAddrCountry = jsonResponse:string_of("Invoice.BillAddr.Country")
local InvoiceBillAddrLine5 = jsonResponse:string_of("Invoice.BillAddr.Line5")
local InvoiceBillAddrLine4 = jsonResponse:string_of("Invoice.BillAddr.Line4")
local InvoiceBillAddrLine3 = jsonResponse:string_of("Invoice.BillAddr.Line3")
local InvoiceBillAddrLine2 = jsonResponse:string_of("Invoice.BillAddr.Line2")
local InvoiceBillAddrLine1 = jsonResponse:string_of("Invoice.BillAddr.Line1")
local InvoiceBillAddrPostalCode = jsonResponse:string_of("Invoice.BillAddr.PostalCode")
local InvoiceBillAddrLat = jsonResponse:string_of("Invoice.BillAddr.Lat")
local InvoiceBillAddrLong = jsonResponse:string_of("Invoice.BillAddr.Long")
local InvoiceBillAddrCountrySubDivisionCode = jsonResponse:string_of("Invoice.BillAddr.CountrySubDivisionCode")
local InvoiceBillAddrId = jsonResponse:string_of("Invoice.BillAddr.Id")
local InvoiceApplyTaxAfterDiscount = jsonResponse:bool_of("Invoice.ApplyTaxAfterDiscount")
local InvoiceId = jsonResponse:string_of("Invoice.Id")
local time = jsonResponse:string_of("time")
local i = 0
local count_i = jsonResponse:size_of_array("Invoice.Line")
while i < count_i do
jsonResponse.i = i
Description = jsonResponse:string_of("Invoice.Line[i].Description")
DetailType = jsonResponse:string_of("Invoice.Line[i].DetailType")
SalesItemLineDetailTaxCodeRefValue = jsonResponse:string_of("Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value")
SalesItemLineDetailQty = jsonResponse:int_of("Invoice.Line[i].SalesItemLineDetail.Qty")
SalesItemLineDetailUnitPrice = jsonResponse:int_of("Invoice.Line[i].SalesItemLineDetail.UnitPrice")
SalesItemLineDetailServiceDate = jsonResponse:string_of("Invoice.Line[i].SalesItemLineDetail.ServiceDate")
SalesItemLineDetailItemRefName = jsonResponse:string_of("Invoice.Line[i].SalesItemLineDetail.ItemRef.name")
SalesItemLineDetailItemRefValue = jsonResponse:string_of("Invoice.Line[i].SalesItemLineDetail.ItemRef.value")
LineNum = jsonResponse:int_of("Invoice.Line[i].LineNum")
Amount = jsonResponse:string_of("Invoice.Line[i].Amount")
Id = jsonResponse:string_of("Invoice.Line[i].Id")
DiscountLineDetailDiscountAccountRefName = jsonResponse:string_of("Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.name")
DiscountLineDetailDiscountAccountRefValue = jsonResponse:string_of("Invoice.Line[i].DiscountLineDetail.DiscountAccountRef.value")
DiscountLineDetailPercentBased = jsonResponse:bool_of("Invoice.Line[i].DiscountLineDetail.PercentBased")
DiscountLineDetailDiscountPercent = jsonResponse:int_of("Invoice.Line[i].DiscountLineDetail.DiscountPercent")
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("Invoice.TxnTaxDetail.TaxLine")
while i < count_i do
jsonResponse.i = i
DetailType = jsonResponse:string_of("Invoice.TxnTaxDetail.TaxLine[i].DetailType")
Amount = jsonResponse:string_of("Invoice.TxnTaxDetail.TaxLine[i].Amount")
TaxLineDetailNetAmountTaxable = jsonResponse:string_of("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.NetAmountTaxable")
TaxLineDetailTaxPercent = jsonResponse:int_of("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxPercent")
TaxLineDetailTaxRateRefValue = jsonResponse:string_of("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.TaxRateRef.value")
TaxLineDetailPercentBased = jsonResponse:bool_of("Invoice.TxnTaxDetail.TaxLine[i].TaxLineDetail.PercentBased")
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("Invoice.CustomField")
while i < count_i do
jsonResponse.i = i
StringValue = jsonResponse:string_of("Invoice.CustomField[i].StringValue")
Type = jsonResponse:string_of("Invoice.CustomField[i].Type")
Name = jsonResponse:string_of("Invoice.CustomField[i].Name")
i = i + 1
end