Lua Requires Chilkat v11.0.0+
Lua
PayPal: Delete an unsent draft invoice
See more PayPal Examples
This example deliberately checks for DRAFT before deletion. Use the invoice ID from your own records. A successful DELETE has no response body; use cancellation for a sent, unpaid invoice.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- See Unlock Chilkat before making requests.
-- For the merchant's internal backoffice application. Load the sandbox token
-- from the merchant's credential store. Keep it out of publicly distributed apps.
-- See Get a PayPal access token.
local http = chilkat.Http.new()
http.auth_token = "SANDBOX_ACCESS_TOKEN"
local resp = chilkat.HttpResponse.new()
http:set_url_var("id", "INVOICE_ID")
local success = http:http_no_body("GET", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}", resp)
if not success then
print("No HTTP response: " .. http.last_error_text)
return
end
if (resp.status_code < 200) or (resp.status_code >= 300) then
local errorInfo = chilkat.JsonObject.new()
errorInfo:load(resp.body_str)
print("HTTP " .. resp.status_code .. " " .. errorInfo:string_of("name") .. " " .. errorInfo:string_of("details[0].issue"))
print("PayPal debug ID: " .. errorInfo:string_of("debug_id"))
return
end
local result = chilkat.JsonObject.new()
local parsed = result:load(resp.body_str)
if not parsed then
print("Expected a JSON response; HTTP " .. resp.status_code)
return
end
if not result:string_of_equals("status", "DRAFT", true) then
print("Leaving invoice unchanged: " .. result:string_of("status"))
return
end
success = http:http_no_body("DELETE", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}", resp)
if not success then
print("No HTTP response: " .. http.last_error_text)
return
end
if (resp.status_code < 200) or (resp.status_code >= 300) then
local deleteError = chilkat.JsonObject.new()
deleteError:load(resp.body_str)
print("HTTP " .. resp.status_code .. " " .. deleteError:string_of("name") .. " " .. deleteError:string_of("details[0].issue"))
print("PayPal debug ID: " .. deleteError:string_of("debug_id"))
return
end
print("Draft deleted; HTTP " .. resp.status_code)