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

PayPal: Get a payable invoice link for your own email

See more PayPal Examples

Use a draft dated today or earlier. Make it payable with customer email suppressed, then retrieve the recipient view URL for the merchant's email. Future-dated invoices are scheduled for delivery and should use a different workflow.

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

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 parsedResult = result:load(resp.body_str)
if not parsedResult then
    print("Expected a JSON response; HTTP " .. resp.status_code)
    return
end

if not result:string_of_equals("status", "DRAFT", true) then
    print("Select an unsent draft.")
    return
end

-- Compare the ISO calendar dates at UTC midnight before asking PayPal to send.
local now = chilkat.DateTime.new()
now:set_from_current_system_time()
local stamp = chilkat.StringBuilder.new()
stamp:append(result:string_of("detail.invoice_date"))
stamp:append("T00:00:00Z")
local issue = chilkat.DateTime.new()
local validDate = issue:set_from_timestamp(stamp:get_as_string())
if (not validDate) or (now:diff_seconds(issue) < 0) then
    print("Select a draft with an issue date of today or earlier.")
    return
end

local body = chilkat.JsonObject.new()
body:update_bool("send_to_recipient", false)
body:update_bool("send_to_invoicer", false)
success = http:http_json("POST", "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/{$id}/send", 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 sendError = chilkat.JsonObject.new()
    sendError:load(resp.body_str)
    print("HTTP " .. resp.status_code .. " " .. sendError:string_of("name") .. " " .. sendError:string_of("details[0].issue"))
    print("PayPal debug ID: " .. sendError:string_of("debug_id"))
    return
end

-- Read back the invoice: acceptance of the send alone does not guarantee it is payable.
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 invoiceError = chilkat.JsonObject.new()
    invoiceError:load(resp.body_str)
    print("HTTP " .. resp.status_code .. " " .. invoiceError:string_of("name") .. " " .. invoiceError:string_of("details[0].issue"))
    print("PayPal debug ID: " .. invoiceError:string_of("debug_id"))
    return
end

local invoice = chilkat.JsonObject.new()
local parsedInvoice = invoice:load(resp.body_str)
if not parsedInvoice then
    print("Expected a JSON response; HTTP " .. resp.status_code)
    return
end

if ((invoice:string_of_equals("status", "SENT", true)) or ((invoice:string_of_equals("status", "UNPAID", true)) or (invoice:string_of_equals("status", "SHARED", true)))) and (invoice:has_member("detail.metadata.recipient_view_url")) then
    print("Customer payment page: " .. invoice:string_of("detail.metadata.recipient_view_url"))
    return
end

print("Invoice is " .. invoice:string_of("status") .. ". Retrieve it again when payable.")