JavaScript Requires Chilkat v11.0.0+
JavaScript
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.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat
v11.4.0 or greater.
var 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.
var http = new CkHttp();
http.AuthToken = "SANDBOX_ACCESS_TOKEN";
var resp = new CkHttpResponse();
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) {
console.log("No HTTP response: " + http.LastErrorText);
return;
}
if ((resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
var errorInfo = new CkJsonObject();
errorInfo.Load(resp.BodyStr);
console.log("HTTP " + resp.StatusCode + " " + errorInfo.StringOf("name") + " " + errorInfo.StringOf("details[0].issue"));
console.log("PayPal debug ID: " + errorInfo.StringOf("debug_id"));
return;
}
var result = new CkJsonObject();
var parsedResult = result.Load(resp.BodyStr);
if (parsedResult !== true) {
console.log("Expected a JSON response; HTTP " + resp.StatusCode);
return;
}
if ((result.SizeOfArray("line_items") !== 1) || (result.StringOfEquals("line_items[0].name","Annual inspection",true) !== true) || (result.StringOfEquals("line_items[0].unit_amount.currency_code","USD",true) !== true)) {
console.log("Select the USD inspection link from your catalog.");
return;
}
// 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");
}
if (result.HasMember("create_time") == true) {
result.Delete("create_time");
}
if (result.HasMember("update_time") == true) {
result.Delete("update_time");
}
if (result.HasMember("status") == true) {
result.Delete("status");
}
if (result.HasMember("links") == true) {
result.Delete("links");
}
if (result.HasMember("payment_link") == true) {
result.Delete("payment_link");
}
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) {
console.log("No HTTP response: " + http.LastErrorText);
return;
}
if ((resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
var updateError = new CkJsonObject();
updateError.Load(resp.BodyStr);
console.log("HTTP " + resp.StatusCode + " " + updateError.StringOf("name") + " " + updateError.StringOf("details[0].issue"));
console.log("PayPal debug ID: " + updateError.StringOf("debug_id"));
return;
}
console.log("The inspection price is now USD 95.00.");