Broadleaf Microservices
  • v1.0.0-latest-prod

PayPal Authorize Transaction

Prerequisites

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

Executing an Authorize Transaction

Use the following code snippet to perform an Authorize transaction:

public class Authorize {

 @Autowired
 PayPalCheckoutTransactionService transactionService;

 public void performAuthorization() {

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

		 PaymentRequest paymentRequest = new PaymentRequest()
                .paymentId(paymentId)
                .transactionTotal(transactionTotal)
                .orderSubtotal(transactionTotal)
		        .paymentOwnerType("BLC_CART")
		        .paymentOwnerId(cartId)
		        .additionalField(MessageConstants.PAYMENTID, "payPalPaymentId")
		        .additionalField(MessageConstants.PAYERID, "payer_id")
                .transactionReferenceId(transactionReferenceId);

	 	PaymentResponse paymentResponse = transactionService.authorize(paymentRequest);

		paymentResponse.isSuccessful();
		paymentResponse.getGatewayResponseCode(); //PayPa Response Code
		paymentResponse.getFailureType(); // Failure type in case the transaction was not successful.
	  	paymentResponse.getResponseMap().get(MessageConstants.AUTHORIZATIONID); // PayPal Authorization id
	   	paymentResponse.getRawResponse(); // PayPal Raw response stored as a JSON object
	}
}

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()

Executing an Authorize and Capture Transaction

To execute the Authorize and Capture Transaction use the same request but instead of transactionService.authorize(paymentRequest) use transactionService.authorizeAndCapture(paymentRequest)