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

PayPal: Capture a buyer-approved order

See more PayPal Examples

After approval, match the order to the checkout in your merchant records. Read each capture ID and status. Fulfill only when the expected amount and currency are captured and the capture is COMPLETED.

PayPal documentation

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
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();

        // Use the order ID you stored before redirecting the buyer, associated with this cart
        // and session.

        // Use the approved order created with intent CAPTURE.
        http.setUrlVar("id", "ORDER_ID");
        // Create and persist this key once per business operation. On a timeout,
        // retry the same request with its saved key, not a newly generated one.
        Crypt2 crypt = new Crypt2();
        String requestId = crypt.generateUuid();
        http.setRequestHeader("PayPal-Request-Id", requestId);
        http.setRequestHeader("Prefer", "return=representation");
        JsonObject body = new JsonObject();

        try {
            http.httpJson("POST", "https://api-m.sandbox.paypal.com/v2/checkout/orders/{$id}/capture", body, "application/json", 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;
        }

        System.out.println("ID: " + result.stringOf("id"));
        System.out.println("Status: " + result.stringOf("status"));
        // A purchase unit may have no captures yet, or more than one.
        int i = 0;
        while (i < result.sizeOfArray("purchase_units")) {
            result.setI(i);
            int j = 0;
            while (j < result.sizeOfArray("purchase_units[i].payments.captures")) {
                result.setJ(j);
                System.out.println("Unit: " + result.stringOf("purchase_units[i].reference_id") + "  capture: " + result.stringOf("purchase_units[i].payments.captures[j].id"));
                System.out.println("  " + result.stringOf("purchase_units[i].payments.captures[j].status") + "  " + result.stringOf("purchase_units[i].payments.captures[j].amount.value") + " " + result.stringOf("purchase_units[i].payments.captures[j].amount.currency_code"));
                j = j + 1;
            }

            i = i + 1;
        }

        // When a buyer opted to save a card, the token may arrive separately from capture.
        if (result.stringOfEquals("payment_source.card.attributes.vault.status", "VAULTED", true)) {
            String cardToken = result.stringOf("payment_source.card.attributes.vault.id");
            String customerId = result.stringOf("payment_source.card.attributes.vault.customer.id");
            // Store cardToken and customerId with this buyer in the merchant credential store.
        }

        if (result.stringOfEquals("payment_source.card.attributes.vault.status", "APPROVED", true)) {
            System.out.println("Await the verified VAULT.PAYMENT-TOKEN.CREATED webhook for the token.");
        }
    }
}