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

PayPal: Suspend an active subscription

See more PayPal Examples

Pause future billing after checking that the subscription is active. Save the reason with the customer support record.

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

if not result:string_of_equals("status", "ACTIVE", true) then
    print("Subscription status is " .. result:string_of("status") .. ". No change made.")
    return
end

local body = chilkat.JsonObject.new()
body:update_string("reason", "Customer requested a billing pause.")
success = http:http_json("POST", "https://api-m.sandbox.paypal.com/v1/billing/subscriptions/{$id}/suspend", 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 changeError = chilkat.JsonObject.new()
    changeError:load(resp.body_str)
    print("HTTP " .. resp.status_code .. " " .. changeError:string_of("name") .. " " .. changeError:string_of("details[0].issue"))
    print("PayPal debug ID: " .. changeError:string_of("debug_id"))
    return
end

print("PayPal accepted the subscription change.")