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
}
}