CkJava Requires Chilkat v11.0.0+
CkJava
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.
Chilkat CkJava Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean 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.
CkHttp http = new CkHttp();
http.put_AuthToken("SANDBOX_ACCESS_TOKEN");
CkHttpResponse 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) {
System.out.println("No HTTP response: " + http.lastErrorText());
return;
}
if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
CkJsonObject errorInfo = new CkJsonObject();
errorInfo.Load(resp.bodyStr());
System.out.println("HTTP " + resp.get_StatusCode() + " " + errorInfo.stringOf("name") + " " + errorInfo.stringOf("details[0].issue"));
System.out.println("PayPal debug ID: " + errorInfo.stringOf("debug_id"));
return;
}
CkJsonObject result = new CkJsonObject();
boolean parsedResult = result.Load(resp.bodyStr());
if (parsedResult != true) {
System.out.println("Expected a JSON response; HTTP " + resp.get_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)) {
System.out.println("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) {
System.out.println("No HTTP response: " + http.lastErrorText());
return;
}
if ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
CkJsonObject updateError = new CkJsonObject();
updateError.Load(resp.bodyStr());
System.out.println("HTTP " + resp.get_StatusCode() + " " + updateError.stringOf("name") + " " + updateError.stringOf("details[0].issue"));
System.out.println("PayPal debug ID: " + updateError.stringOf("debug_id"));
return;
}
System.out.println("The inspection price is now USD 95.00.");
}
}