Broadleaf Microservices
  • v1.0.0-latest-prod

PayPal AuthorizeAndCapture Transaction

Prerequisites

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

Executing an AuthorizeAndCapture Transaction

AuthorizeAndCapture transactions can be performed using a PayPal order id.

Note
Before executing transactions, the frontend PayPal integration must have prompted the customer to login & select their preferred payment method for the order.

AuthorizeAndCapture Transaction using Token

Use the following code snippet to perform an AuthorizeAndCapture transaction with an order id:

@RequiredArgsConstructor
public class AuthorizeAndCapture {

 private final PayPalCheckoutTransactionService payPalCheckoutTransactionService;

 public void performAuthorizeAndCapture() {
		 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.ORDER_ID, "paypal_order_id")
                .tenantId("TENANT"); // Declare the tenant id to help identify where transaction results should be recorded

	 	PaymentResponse paymentResponse = payPalCheckoutTransactionService.authorizeAndCapture(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.AUTHORIZATION_ID); // PayPal Authorization ID used for subsequent transactions.
	}
}

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 AuthorizeAndCapture transaction request & response parameters, refer to the PayPal API Reference.