Broadleaf Microservices
  • v1.0.0-latest-prod

PayPal Authorize Transaction

Prerequisites

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

Executing an Authorize Transaction

Authorize 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.

Authorize Transaction using Token

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

@RequiredArgsConstructor
public class Authorize {

 private final PayPalCheckoutTransactionService payPalCheckoutTransactionService;

 public void performAuthorization() {
		 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.authorize(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 Authorize transaction request & response parameters, refer to the PayPal API Reference.