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

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        // 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 http = new Http();
        http.setAuthToken("SANDBOX_ACCESS_TOKEN");
        HttpResponse resp = new HttpResponse();

        http.setUrlVar("id", "PAYMENT_RESOURCE_ID");

        try {
            http.httpNoBody("GET", "https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$id}", resp);
        } catch (ChilkatException e) {
            System.out.println("No HTTP response: " + e.getLastErrorText());
            return;
        }

        if ((resp.getStatusCode() < 200) || (resp.getStatusCode() >= 300)) {
            JsonObject errorInfo = new JsonObject();
            errorInfo.load(resp.getBodyStr());
            System.out.println("HTTP " + resp.getStatusCode() + " " + errorInfo.stringOf("name") + " " + errorInfo.stringOf("details[0].issue"));
            System.out.println("PayPal debug ID: " + errorInfo.stringOf("debug_id"));
            return;
        }

        JsonObject result = new JsonObject();
        try {
            result.load(resp.getBodyStr());
        } catch (ChilkatException e) {
            System.out.println("Expected a JSON response; HTTP " + resp.getStatusCode());
            return;
        }

        if ((result.sizeOfArray("line_items") != 1) || ((!result.stringOfEquals("line_items[0].name", "Annual inspection", true)) || (!result.stringOfEquals("line_items[0].unit_amount.currency_code", "USD", 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")) {
            result.delete("id");
        }

        if (result.hasMember("create_time")) {
            result.delete("create_time");
        }

        if (result.hasMember("update_time")) {
            result.delete("update_time");
        }

        if (result.hasMember("status")) {
            result.delete("status");
        }

        if (result.hasMember("links")) {
            result.delete("links");
        }

        if (result.hasMember("payment_link")) {
            result.delete("payment_link");
        }

        result.updateString("line_items[0].unit_amount.value", "95.00");
        try {
            http.httpJson("PUT", "https://api-m.sandbox.paypal.com/v1/checkout/payment-resources/{$id}", result, "application/json", resp);
        } catch (ChilkatException e) {
            System.out.println("No HTTP response: " + e.getLastErrorText());
            return;
        }

        if ((resp.getStatusCode() < 200) || (resp.getStatusCode() >= 300)) {
            JsonObject updateError = new JsonObject();
            updateError.load(resp.getBodyStr());
            System.out.println("HTTP " + resp.getStatusCode() + " " + 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.");
    }
}