Broadleaf Microservices
  • v1.0.0-latest-prod

PayPal Capture Transaction

Prerequisites

Before performing a Capture transaction with PayPal, you’ll first need to set up your environment as described in the Environment Setup Guide.

Executing a Capture Transaction

Capture transactions can only be performed using the Authorization ID retrieved from Authorize transaction.

Use the following code snippet to perform a Capture transaction:

@RequiredArgsConstructor
public class Capture {

 private final PayPalCheckoutTransactionService payPalCheckoutTransactionService;

 public void performCapture() {
		 CurrencyUnit currency = Monetary.getCurrency(currencyCode);
		 MonetaryAmount transactionTotal = Monetary.getDefaultAmountFactory().setCurrency(currency).setNumber(amount).create();

		 PaymentRequest paymentRequest = new PaymentRequest()
		        .transactionReferenceId(transactionReferenceId)// This is the unique Transaction Reference ID
		        .transactionTotal(transactionTotal)
		        .additionalField(MessageConstants.AUTHORIZATION_ID, "paypal_authorization_id") // This is the Authorization ID retrieved from Authorize transaction
                .tenantId("TENANT"); // Declare the tenant id to help identify where transaction results should be recorded

	 	PaymentResponse paymentResponse = payPalCheckoutTransactionService.capture(paymentRequest);

		paymentResponse.isSuccessful();
		paymentResponse.getGatewayResponseCode(); //PayPal Response Code
		paymentResponse.getFailureType(); // Failure type in case the transaction was not successful.
	   	paymentResponse.getRawResponse(); // PayPal raw response stored as a JSON object.
        paymentResponse.getResponseMap().get(MessageConstants.CAPTURE); // PayPal Capture ID.
	}
}

To set additional fields in the PaymentRequest, use method

PaymentRequest.additionalField(key, value)

To get values from responseMap, use

PaymentResponse.getResponseMap().get(key, value)

To get the transaction reference ID from response:

PaymentResponse.getTransactionReferenceId()

To get the Broadleaf Payment ID from the response:

PaymentResponse.getPaymentId()

For more details on the Capture transaction request & response parameters, refer to the PayPal API Reference.