Lua
Lua
Quickbooks Delete an Invoice
See more QuickBooks Examples
Demonstrates how to delete 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.
-- --------------------------------------------------------------------------
-- Create the following JSON:
-- {
-- "SyncToken": "3",
-- "Id": "33"
-- }
--
-- 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("SyncToken", "3")
jsonReq:update_string("Id", "33")
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?operation=delete", 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": {
-- "status": "Deleted",
-- "domain": "QBO",
-- "Id": "33"
-- },
-- "time": "2013-03-15T00:18:15.322-07:00"
-- }
--
local InvoiceStatus = jsonResponse:string_of("Invoice.status")
local InvoiceDomain = jsonResponse:string_of("Invoice.domain")
local InvoiceId = jsonResponse:string_of("Invoice.Id")
local time = jsonResponse:string_of("time")