Use the following code snippet to perform a Purchase transaction:
public class Purchase {
@Autowired
APSTransactionService apsTransactionService;
public void performPurchase() {
CurrencyUnit currency = Monetary.getCurrency(currencyCode);
MonetaryAmount transactionTotal = Monetary.getDefaultAmountFactory().setCurrency(currency).setNumber(amount).create();
PaymentRequest paymentRequest = new PaymentRequest()
.transactionReferenceId(merchantReference)// this is merchant reference ID
.customer()
.customerId("1")
.email("test@test.com")
.done()
.transactionTotal(transactionTotal)
.additionalField(MessageConstants.APS_TOKEN_NAME, tokenName)
.additionalField(MessageConstants.APS_LANGUAGE, "en") // If not set here, the default language will be gathered from broadleaf.amazonpaymentservices.default-language
.additionalField(MessageConstants.APS_CHECK_3DS, "NO"); // Set this parameter to "YES" to enable 3DS verification
PaymentResponse paymentResponse = apsTransactionService.authorizeAndPurchase(paymentRequest);
paymentResponse.isSuccessful();
paymentResponse.getGatewayResponseCode(); //APS Response Code
paymentResponse.getFailureType(); // Failure type in case the transaction was not successful.
paymentResponse.getResponseMap().get(MessageConstants.APS_RESPONSE_MESSAGE); // APS API Response Message
paymentResponse.getRawResponse(); // APS 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()
To get the merchant reference ID from response:
PaymentResponse.getPaymentId()
To get fort ID from response:
PaymentResponse.responseMap(MessageConstants.APS_FORT_ID, String.valueOf(response.get(MessageConstants.APS_FORT_ID)))