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

PayPal: Create a reusable payment link for a service

See more PayPal Examples

Offer an annual inspection for USD 90 through PayPal-hosted checkout. Store the returned resource ID with the catalog item and share the payment_link URL with customers.

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

-- Replace the thank-you URL with your business website. The same link can be shared
-- with several customers.
-- Create and persist this key once per business operation. On a timeout,
-- retry the same request with its saved key, not a newly generated one.
local crypt = chilkat.Crypt2.new()
local requestId = crypt:generate_uuid()
http:set_request_header("PayPal-Request-Id", requestId)
local body = chilkat.JsonObject.new()
body:update_string("integration_mode", "LINK")
body:update_string("type", "BUY_NOW")
body:update_string("reusable", "MULTIPLE")
body:update_string("return_url", "https://merchant.example/thank-you")
body:update_string("line_items[0].name", "Annual inspection")
body:update_string("line_items[0].unit_amount.currency_code", "USD")
body:update_string("line_items[0].unit_amount.value", "90.00")

local success = http:http_json("POST", "https://api-m.sandbox.paypal.com/v1/checkout/payment-resources", 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

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

print("Catalog payment resource ID: " .. result:string_of("id"))
-- Link order is not fixed. Find the customer-facing link to copy into an email.
local linkIndex = 0
while linkIndex < result:size_of_array("links") do
    result.i = linkIndex
    if result:string_of_equals("links[i].rel", "payment_link", true) then
        print("Share this payment link: " .. result:string_of("links[i].href"))
    end

    linkIndex = linkIndex + 1
end