Broadleaf Microservices
  • v1.0.0-latest-prod

Stripe Authorize Transaction

Prerequisites

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

Executing an Authorize Transaction

@RequiredArgsConstructor
public class Authorize {

 private final StripeTransactionService stripeTransactionService;

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

		 PaymentRequest paymentRequest = new PaymentRequest()
		        .transactionReferenceId(transactionReferenceId)
		        .transactionTotal(transactionTotal)
		        .additionalField("PAYMENT_METHOD_ID", paymentMethodId) // The id of the payment method obtained from the Stripe Element integration
		        .additionalField("STRIPE_CUSTOMER_ID", stripeCustomerId); // Id of the Stripe customer. This field is only expected if the transaction is being performed with a saved payment method or if you wish to tie the payment method to an existing customer. Otherwise, this field should not be provided.

	 	PaymentResponse paymentResponse = stripeTransactionService.authorize(paymentRequest);

		paymentResponse.isSuccessful();
		paymentResponse.getGatewayResponseCode(); // The Stripe response code
		paymentResponse.getFailureType(); // Failure type in case the transaction was not successful.
	   	paymentResponse.getRawResponse(); // Stripe raw response stored as a JSON object.
        paymentResponse.getResponseMap().get("TRANSACTION_ID"); // The Stripe transaction ID, which will be used for subsequent transaction requests.
        paymentResponse.getResponseMap().get("PAYMENT_INTENT_ID"); // The Stripe Payment Intent ID.
        paymentResponse.getResponseMap().get("ERROR_CODE"); // The Stripe error response code.
        paymentResponse.getResponseMap().get("ERROR_MESSAGE"); // The Stripe error message.
	}
}

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 Broadleaf Payment ID from the response:

PaymentResponse.getPaymentId()