Stripe Payment Integration using Java: One Time Payment

Vipul Kumar Gupta
3 min readJul 2, 2021

--

Stripe provides APIs that web developers can use to integrate payment processing into their websites and mobile applications. Stripe supports multiple payment options like cards, wallets, bank redirects, etc. which can be used in different use-cases like one-time payment, recurring payment, etc.

In this article, we will discuss one-time payments using cards.

To make use of the Stripe API for Java in the project, we add the corresponding dependency.

API Keys

Before integrating stripe payment APIs in our application, we need to register a Stripe account and obtain secret/public Stripe API keys.

Stripe terms

Payment Intent guides you through the process of collecting payment from your customer. We recommend that you create exactly one PaymentIntent for each order or customer session in your system.

Payment Method objects represent your customer’s payment instruments. They can be used with PaymentIntents to collect payments or saved to Customer objects to store instrument details for future payments.

Payment Flow

  1. Create payment Intent:- when the user is at checkout, Payment Intent needs to be created. To create a payment intent amount, currency, and payment method type is required. In a real-world scenario, we should add the customer/order id in the metadata so that we can track the payment intent for that customer/order.

2. Request Stripe to create Payment intent:- build the request for stripe to create Payment intent.

Input — amount, currency, payment method type (Card), metadata (some customer/order id )

3. Payment intent response:- we will get the payment intent response with the client secret.

output — payment intent id and client secret (use to create stripe payment method and update in payment intent by UI)

4. Return “client secret”:- return the client secret to UI.

5. Create payment method:- Get the client card details and request Stripe to create a Payment method with the client secret to create the payment.

6. Payment method response:- after creating the payment method, paymentMethodId will be returned as a response to UI and will be mapped to PaymentIntent.

To confirm if the payment get successful or not we can refer to the status of PaymentIntent, it can be any one of the following values- requires_payment_method, requires_confirmation, requires_action, processing, requires_capture, canceled, or succeeded. Read more about each PaymentIntent status.

we can configure Webhook in the stripe dashboard. So that we get the payment status update automatically and complete the request accordingly.

The official API reference should be referred to the whole set of operations.

--

--