Broadleaf Microservices
  • v1.0.0-latest-prod

APS Purchase Transaction

Prerequisites

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

Executing a Purchase Transaction

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

For more details on the Purchase transaction request & response parameters, refer to the Amazon Payment Services documentation.

Executing a Purchase Transaction with Installments

Add the following attributes in the PaymentRequest object while initiating a PURCHASE Transaction:

paymentRequest.additionalField(MessageConstants.APS_INSTALLMENTS, "HOSTED");
paymentRequest.additionalField(MessageConstants.APS_PLAN_CODE, "<plan-code-received from getInstallmentsPlan service>");
paymentRequest.additionalField(MessageConstants.APS_ISSUER_CODE, "<issuer-code-received from getInstallmentsPlan service>");

For more details on the Purchase transaction request & response parameters with Installments, refer to the Amazon Payment Services with installments documentation.