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

PayPal: Update the price on a reusable payment link

See more PayPal Examples

Retrieve the simple USD inspection link from the create example. Check its item and currency, retain its configuration, and replace the price with USD 95. PUT sends the complete editable definition.

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

local success = http:http_no_body("GET", "https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$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 (result:size_of_array("line_items") ~= 1) or ((not result:string_of_equals("line_items[0].name", "Annual inspection", true)) or (not result:string_of_equals("line_items[0].unit_amount.currency_code", "USD", true))) then
    print("Select the USD inspection link from your catalog.")
    return
end

-- Preserve line items, taxes, shipping and checkout settings from the retrieved link.
-- Remove the resource metadata PayPal added to the GET response.
if result:has_member("id") then
    result:delete("id")
end

if result:has_member("create_time") then
    result:delete("create_time")
end

if result:has_member("update_time") then
    result:delete("update_time")
end

if result:has_member("status") then
    result:delete("status")
end

if result:has_member("links") then
    result:delete("links")
end

if result:has_member("payment_link") then
    result:delete("payment_link")
end

result:update_string("line_items[0].unit_amount.value", "95.00")
success = http:http_json("PUT", "https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$id}", result, "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 updateError = chilkat.JsonObject.new()
    updateError:load(resp.body_str)
    print("HTTP " .. resp.status_code .. " " .. updateError:string_of("name") .. " " .. updateError:string_of("details[0].issue"))
    print("PayPal debug ID: " .. updateError:string_of("debug_id"))
    return
end

print("The inspection price is now USD 95.00.")