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

PayPal: Look up a subscription and its recent payments

See more PayPal Examples

Start with the PayPal subscription ID saved with the local customer. Display its status and next billing time, then list transactions for the previous 30 days with each gross amount and currency.

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", "SUBSCRIPTION_ID")

local success = http:http_no_body("GET", "https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$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

print("Status: " .. result:string_of("status"))
print("Next billing: " .. result:string_of("billing_info.next_billing_time"))
local now = chilkat.DateTime.new()
now:set_from_current_system_time()
local query = chilkat.JsonObject.new()
query:update_string("end_time", now:get_as_timestamp(false))
now:add_days(-30)
query:update_string("start_time", now:get_as_timestamp(false))
success = http:http_params("GET", "https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/transactions", query, 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 historyError = chilkat.JsonObject.new()
    historyError:load(resp.body_str)
    print("HTTP " .. resp.status_code .. " " .. historyError:string_of("name") .. " " .. historyError:string_of("details[0].issue"))
    print("PayPal debug ID: " .. historyError:string_of("debug_id"))
    return
end

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

local i = 0
while i < history:size_of_array("transactions") do
    history.i = i
    print(history:string_of("transactions[i].time") .. "  " .. history:string_of("transactions[i].id") .. "  " .. history:string_of("transactions[i].status") .. "  " .. history:string_of("transactions[i].amount_with_breakdown.gross_amount.value") .. " " .. history:string_of("transactions[i].amount_with_breakdown.gross_amount.currency_code"))
    i = i + 1
end