Lua
Lua
Quickbooks Create an Invoice
See more QuickBooks Examples
Demonstrates how to create an invoice via 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.
-- --------------------------------------------------------------------------
-- Create the following JSON:
-- {
-- "Line": [
-- {
-- "DetailType": "SalesItemLineDetail",
-- "Amount": 100.0,
-- "SalesItemLineDetail": {
-- "ItemRef": {
-- "name": "Services",
-- "value": "1"
-- }
-- }
-- }
-- ],
-- "CustomerRef": {
-- "value": "1"
-- }
-- }
--
-- Use the this online tool to generate the code from sample JSON:
-- Generate Code to Create JSON
local jsonReq = chilkat.JsonObject.new()
jsonReq:update_string("Line[0].DetailType", "SalesItemLineDetail")
jsonReq:update_number("Line[0].Amount", "100.0")
jsonReq:update_string("Line[0].SalesItemLineDetail.ItemRef.name", "Services")
jsonReq:update_string("Line[0].SalesItemLineDetail.ItemRef.value", "1")
jsonReq:update_string("CustomerRef.value", "1")
local sbRequestBody = chilkat.StringBuilder.new()
jsonReq:emit_sb(sbRequestBody)
rest:add_header("Content-Type", "application/json")
rest:add_header("Accept", "application/json")
rest.allow_header_folding = false
local sbResponseBody = chilkat.StringBuilder.new()
success = rest:full_request_sb("POST", "/v3/company/<realmID>/invoice", sbRequestBody, 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": {
-- "DocNumber": "1069",
-- "SyncToken": "0",
-- "domain": "QBO",
-- "Balance": 100.0,
-- "BillAddr": {
-- "City": "Bayshore",
-- "Line1": "4581 Finch St.",
-- "PostalCode": "94326",
-- "Lat": "INVALID",
-- "Long": "INVALID",
-- "CountrySubDivisionCode": "CA",
-- "Id": "2"
-- },
-- "TxnDate": "2015-07-24",
-- "TotalAmt": 100.0,
-- "CustomerRef": {
-- "name": "Amy's Bird Sanctuary",
-- "value": "1"
-- },
-- "ShipAddr": {
-- "City": "Bayshore",
-- "Line1": "4581 Finch St.",
-- "PostalCode": "94326",
-- "Lat": "INVALID",
-- "Long": "INVALID",
-- "CountrySubDivisionCode": "CA",
-- "Id": "109"
-- },
-- "LinkedTxn": [
-- ],
-- "DueDate": "2015-08-23",
-- "PrintStatus": "NeedToPrint",
-- "Deposit": 0,
-- "sparse": false,
-- "EmailStatus": "NotSet",
-- "Line": [
-- {
-- "LineNum": 1,
-- "Amount": 100.0,
-- "SalesItemLineDetail": {
-- "TaxCodeRef": {
-- "value": "NON"
-- },
-- "ItemRef": {
-- "name": "Services",
-- "value": "1"
-- }
-- },
-- "Id": "1",
-- "DetailType": "SalesItemLineDetail"
-- },
-- {
-- "DetailType": "SubTotalLineDetail",
-- "Amount": 100.0,
-- "SubTotalLineDetail": {}
-- }
-- ],
-- "ApplyTaxAfterDiscount": false,
-- "CustomField": [
-- {
-- "DefinitionId": "1",
-- "Type": "StringType",
-- "Name": "Crew #"
-- }
-- ],
-- "Id": "238",
-- "TxnTaxDetail": {
-- "TotalTax": 0
-- },
-- "MetaData": {
-- "CreateTime": "2015-07-24T10:33:39-07:00",
-- "LastUpdatedTime": "2015-07-24T10:33:39-07:00"
-- }
-- },
-- "time": "2015-07-24T10:33:39.11-07:00"
-- }
--
local LineNum = 0
local Amount = ""
local SalesItemLineDetailTaxCodeRefValue = ""
local SalesItemLineDetailItemRefName = ""
local SalesItemLineDetailItemRefValue = ""
local Id = ""
local DetailType = ""
local DefinitionId = ""
local invType = ""
local Name = ""
local InvoiceDocNumber = jsonResponse:string_of("Invoice.DocNumber")
local InvoiceSyncToken = jsonResponse:string_of("Invoice.SyncToken")
local InvoiceDomain = jsonResponse:string_of("Invoice.domain")
local InvoiceBalance = jsonResponse:string_of("Invoice.Balance")
local InvoiceBillAddrCity = jsonResponse:string_of("Invoice.BillAddr.City")
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 InvoiceTxnDate = jsonResponse:string_of("Invoice.TxnDate")
local InvoiceTotalAmt = jsonResponse:string_of("Invoice.TotalAmt")
local InvoiceCustomerRefName = jsonResponse:string_of("Invoice.CustomerRef.name")
local InvoiceCustomerRefValue = jsonResponse:string_of("Invoice.CustomerRef.value")
local InvoiceShipAddrCity = jsonResponse:string_of("Invoice.ShipAddr.City")
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 InvoiceDueDate = jsonResponse:string_of("Invoice.DueDate")
local InvoicePrintStatus = jsonResponse:string_of("Invoice.PrintStatus")
local InvoiceDeposit = jsonResponse:int_of("Invoice.Deposit")
local InvoiceSparse = jsonResponse:bool_of("Invoice.sparse")
local InvoiceEmailStatus = jsonResponse:string_of("Invoice.EmailStatus")
local InvoiceApplyTaxAfterDiscount = jsonResponse:bool_of("Invoice.ApplyTaxAfterDiscount")
local InvoiceId = jsonResponse:string_of("Invoice.Id")
local InvoiceTxnTaxDetailTotalTax = jsonResponse:int_of("Invoice.TxnTaxDetail.TotalTax")
local InvoiceMetaDataCreateTime = jsonResponse:string_of("Invoice.MetaData.CreateTime")
local InvoiceMetaDataLastUpdatedTime = jsonResponse:string_of("Invoice.MetaData.LastUpdatedTime")
local time = jsonResponse:string_of("time")
local i = 0
local count_i = jsonResponse:size_of_array("Invoice.LinkedTxn")
while i < count_i do
jsonResponse.i = i
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("Invoice.Line")
while i < count_i do
jsonResponse.i = i
LineNum = jsonResponse:int_of("Invoice.Line[i].LineNum")
Amount = jsonResponse:string_of("Invoice.Line[i].Amount")
SalesItemLineDetailTaxCodeRefValue = jsonResponse:string_of("Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value")
SalesItemLineDetailItemRefName = jsonResponse:string_of("Invoice.Line[i].SalesItemLineDetail.ItemRef.name")
SalesItemLineDetailItemRefValue = jsonResponse:string_of("Invoice.Line[i].SalesItemLineDetail.ItemRef.value")
Id = jsonResponse:string_of("Invoice.Line[i].Id")
DetailType = jsonResponse:string_of("Invoice.Line[i].DetailType")
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("Invoice.CustomField")
while i < count_i do
jsonResponse.i = i
DefinitionId = jsonResponse:string_of("Invoice.CustomField[i].DefinitionId")
invType = jsonResponse:string_of("Invoice.CustomField[i].Type")
Name = jsonResponse:string_of("Invoice.CustomField[i].Name")
i = i + 1
end