Sample code for 30+ languages & platforms
Ruby 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 Ruby Downloads

Ruby
require 'chilkat'

success = false

#  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.
http = Chilkat::CkHttp.new()
http.put_AuthToken("SANDBOX_ACCESS_TOKEN")
resp = Chilkat::CkHttpResponse.new()

http.SetUrlVar("id","PAYMENT_RESOURCE_ID")

success = http.HttpNoBody("GET","https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$id}",resp)
if (success != true)
    print "No HTTP response: " + http.lastErrorText() + "\n";
    exit
end

if ((resp.get_StatusCode() < 200) or (resp.get_StatusCode() >= 300))
    errorInfo = Chilkat::CkJsonObject.new()
    errorInfo.Load(resp.bodyStr())
    print "HTTP " + resp.get_StatusCode().to_s() + " " + errorInfo.stringOf("name") + " " + errorInfo.stringOf("details[0].issue") + "\n";
    print "PayPal debug ID: " + errorInfo.stringOf("debug_id") + "\n";
    exit
end

result = Chilkat::CkJsonObject.new()
parsedResult = result.Load(resp.bodyStr())
if (parsedResult != true)
    print "Expected a JSON response; HTTP " + resp.get_StatusCode().to_s() + "\n";
    exit
end

if ((result.SizeOfArray("line_items") != 1) or (result.StringOfEquals("line_items[0].name","Annual inspection",true) != true) or (result.StringOfEquals("line_items[0].unit_amount.currency_code","USD",true) != true))
    print "Select the USD inspection link from your catalog." + "\n";
    exit
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.HasMember("id") == true)
    result.Delete("id")
end

if (result.HasMember("create_time") == true)
    result.Delete("create_time")
end

if (result.HasMember("update_time") == true)
    result.Delete("update_time")
end

if (result.HasMember("status") == true)
    result.Delete("status")
end

if (result.HasMember("links") == true)
    result.Delete("links")
end

if (result.HasMember("payment_link") == true)
    result.Delete("payment_link")
end

result.UpdateString("line_items[0].unit_amount.value","95.00")
success = http.HttpJson("PUT","https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$id}",result,"application/json",resp)
if (success != true)
    print "No HTTP response: " + http.lastErrorText() + "\n";
    exit
end

if ((resp.get_StatusCode() < 200) or (resp.get_StatusCode() >= 300))
    updateError = Chilkat::CkJsonObject.new()
    updateError.Load(resp.bodyStr())
    print "HTTP " + resp.get_StatusCode().to_s() + " " + updateError.stringOf("name") + " " + updateError.stringOf("details[0].issue") + "\n";
    print "PayPal debug ID: " + updateError.stringOf("debug_id") + "\n";
    exit
end

print "The inspection price is now USD 95.00." + "\n";