Broadleaf Microservices
  • v1.0.0-latest-prod

APS Refund Transaction

Prerequisites

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

Executing a Refund Transaction

Use the following code snippet to perform a Refund transaction:

public class Refund {

 @Autowired
 PayPalCheckoutTransactionService transactionService;

 public void performRefund() {

		 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.CAPTUREID, "CaptureId")
		        .additionalField(MessageConstants.SALEID, "SaleId") // CaptureId or SaleId is required
                .transactionReferenceId(transactionReferenceId);

	 	PaymentResponse paymentResponse = transactionService.refund(paymentRequest);

		paymentResponse.isSuccessful();
		paymentResponse.getGatewayResponseCode(); //PayPa Response Code
        paymentResponse.getResponseMap().get(MessageConstants.REFUNDID); // Refund id
        paymentResponse.getResponseMap().get(MessageConstants.CAPTUREID); // Capture id. Will be populated if request contains Capture id
        paymentResponse.getResponseMap().get(MessageConstants.SALEID); // Sale id. Will be populated if request contains Sale id
		paymentResponse.getFailureType(); // Failure type in case the transaction was not successful.
	   	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()