Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

PayPal: Cancel a sent, unpaid invoice

See more PayPal Examples

Cancel an unpaid invoice that has already been sent and notify the customer why. For a draft use delete instead. Cancellation does not return funds.

PayPal documentation

Chilkat Lua Downloads

Lua
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()

-- This cancels a sent, unpaid invoice. It does not send a refund.

http:set_url_var("id", "INVOICE_ID")
local body = chilkat.JsonObject.new()
body:update_string("subject", "Inspection invoice canceled")
body:update_string("note", "The inspection was canceled before work began.")
body:update_bool("send_to_recipient", true)

local success = http:http_json("POST", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/cancel", body, "application/json", 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

print("Cancellation accepted; HTTP " .. resp.status_code)