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

PayPal: Pay with a previously saved card

See more PayPal Examples

Buyer interaction. Your application uses a browser for buyer approval or authentication when PayPal requests it. Configure the merchant return and cancel URLs for this flow.

When a returning buyer selects their saved card, look up its vault ID under their authenticated local account. Send the order price and currency from your own cart. Creating the order can capture immediately; inspect each capture before fulfillment. Follow payer-action if further authentication is required.

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

-- Resolve the saved token under this signed-in buyer. A vault ID from a different
-- customer must never be accepted from the browser.

-- 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)
http:set_request_header("Prefer", "return=representation")
local body = chilkat.JsonObject.new()
body:update_string("intent", "CAPTURE")
body:update_string("purchase_units[0].amount.currency_code", "USD")
body:update_string("purchase_units[0].amount.value", "30.00")
body:update_string("payment_source.card.vault_id", "SAVED_CARD_TOKEN")
body:update_string("payment_source.card.stored_credential.payment_initiator", "CUSTOMER")
body:update_string("payment_source.card.stored_credential.payment_type", "ONE_TIME")
body:update_string("payment_source.card.stored_credential.usage", "SUBSEQUENT")
body:update_string("payment_source.card.experience_context.return_url", "https://merchant.example/paypal/return")
body:update_string("payment_source.card.experience_context.cancel_url", "https://merchant.example/paypal/cancel")

local success = http:http_json("POST", "https://api-m.sandbox.paypal.com/v2/checkout/orders", 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 parsed = result:load(resp.body_str)
if not parsed then
    print("Expected a JSON response; HTTP " .. resp.status_code)
    return
end

print("ID: " .. result:string_of("id"))
print("Status: " .. result:string_of("status"))
-- Link order is not fixed. Send the buyer to the matching URL in a browser.
local linkIndex = 0
while linkIndex < result:size_of_array("links") do
    result.i = linkIndex
    if result:string_of_equals("links[i].rel", "payer-action", true) then
        print("Approval URL: " .. result:string_of("links[i].href"))
    end

    linkIndex = linkIndex + 1
end

-- A purchase unit may have no captures yet, or more than one.
local i = 0
while i < result:size_of_array("purchase_units") do
    result.i = i
    local j = 0
    while j < result:size_of_array("purchase_units[i].payments.captures") do
        result.j = j
        print("Unit: " .. result:string_of("purchase_units[i].reference_id") .. "  capture: " .. result:string_of("purchase_units[i].payments.captures[j].id"))
        print("  " .. result:string_of("purchase_units[i].payments.captures[j].status") .. "  " .. result:string_of("purchase_units[i].payments.captures[j].amount.value") .. " " .. result:string_of("purchase_units[i].payments.captures[j].amount.currency_code"))
        j = j + 1
    end

    i = i + 1
end