Habilelabs-Logo
Blog

Struts2 – Stripe Payment Gateway Integration & Stripe Subscription

January 3rd, 2020 . 10 minutes read
Blog featured image

Stripe is one of the most popular cloud-based payment gateway service providers that makes individuals as well as corporate businesses to receive payments over the world wide online. It has some client-side libraries in JavaScript and native mobiles. To handle the payment request on server side, it has a collection of rich server-side libraries in various technologies such as Java, Node.js, PHP, Ruby, etc. These libraries help us to reduce the complexity of payment receiving methodology by having the abstracted layers.

So, due to its simplicity, we don’t even have to deal with the card detail directly, instead, we can deal with a token symbolizing an authorization to recharge the card. As Stripe has a lot of useful services, Struts2 offers support for integrating the Stripe API and implementing the payment functionality into your Struts2 applications. Here, we will learn about some useful steps to integrate Stripe payment gateway with struts2 application.

Integrating Stripe with a Struts2 Application

To start integrating this payment gateway with the API, we require to create an account on Stripe.

#Create a Stripe Account

Creating an account is very simple, and the only thing you need to do is verify your email address and phone number. Although this will only provide you with a test account while your application is approved by Stripe, which can take anywhere from a few minutes to a few days.

Right at the dashboard, you’ll be able to find the live/test keys for the API:

create stripe account
Create Stripe Account

Before getting started to coding, you need to understand a very important that Stripe service is based on tokens. Whenever you want to charge your credit card, all you need to do is to create token passing credit card info as parameters and then use the created token as a source. A token can be used only once, so if you’ll try to use it twice, you will get an error that token is invalid or expired.

A token can be generated on the back-end from Stripe using an appropriate method, but it’s not a good idea to send the credit card details directly to your server, as someone can easily intercept the data (Man-in-the-middle attack).

Sending credit card info directly to Stripe using Stripe.js library is the best and secure way. The flow for such a case is the following:

  • Send data from the web page to Stripe.
  • Stripe returns token.
  • Send a token to the backend server.
  • Charge credit card using token.

Such an approach has two benefits:

1. You don’t pass any sensitive data to your backend server.

2. Even if the token will be intercepted by a man-in-the-middle he cannot retrieve any credit card info from this token

Here we are using the test API keys for integration. Since the stripe-java dependency isn’t in the initial pom.xml, we’ll add it directly to this file:

There are much more payment transaction API for integration, but most of these are having two frequently integration methods of charging a customer:

1. One-time Purchase (I am going explain this way here in this blog)

2. Subscription (Click here to learn about the Stripe subscription for recurring payments.

One-Time Purchase

To implement the one-time payment, you need to follow the below steps to integrate charge API in the struts2 application.

#1. Create Checkout Form

Stripe Checkout is a customizable, localizable and mobile-ready widget used to render a form to introduce credit card details. It includes and configures “checkout.js”, which is responsible for:

– “Pay with Card” button rendering

– Payment overlay dialog rendering (triggered after clicking “Pay with Card”)

– Credit card validation

– “Remember me” feature (associates the card with a mobile number)

– Sending the credit card to Stripe and replacing it with a token in the enclosing form

checkout form

The “checkout.js” script automatically triggers a request to Stripe right before submission, which then appends the Stripe token and the user email as the hidden fields “stripeToken” and “stripeEmail”.

The amount (“data-amount”) is used only for display purposes (along with “data-currency”). Its unit is Cents of the used currency, so we divide it by 100 to display it.

The Stripe public key is passed to Stripe after the user asks to pay. Do not use the secret key here, as this is sent to the browser.

#2. Configure struts.xml File

Map the above form action in struts.xml configuration file:

#3. Create Struts2 Action

Create the PaymentAction.java to handle the stripe request in back-end:

Subscription for Recurring Payment

Before we move to integrate the subscription API, we need to have a quick glance at the subscription requirement. A subscription, formally for recurring payments is referred to as Automatic Payment process, which means that a customer has permitted the merchant or retailer to deduct payments for services or goods every month from his/her bank account to automatically charge his/her credit card in the amount due each month.

recurring subscription payments

The merchant needs to get one-time front permission from the customer. The merchant deducts the amount for a scheduled day in every month automatically, so the customer needs not worry about the late fee. The automatic payments continue until the consumer retracts his permission.

In the previous paragraph about One-Time Purchase, we learned about how to create a stripe account and get the publishable key and secret key.

To implement the subscription for a recurring payment, you need to follow the below steps to integrate charge API in the struts2 application.

Use stripe’s official guide to set up a subscription and charge a customer. In this link, you will be able to create some products and plans on your stripe account. Here, we will cover the following points:

1. Using Stripe form element for card details.

2. Create customer by user inputs.

3. Apply Subscription for a selected plan.

#1. Create Checkout Form for Subscription

Create an HTML form with below UI:

checkout form for subscription

#2. Code to Create Subscription Form

Use the following HTML code for the above UI design-

#3. Credit Card Validation and Form Submission

Before the closing body tag in the above code snippet, add the following JavaScript code which handles the client credit card validation and the form submission with the appropriate plan, the amount generated stripe token.

Conclusion:

In this blog, we’ve shown how to make use of the Stripe Java API to charge a credit card and using the struts2 framework. To test the entire charge and subscription flow, we don’t need to use a real credit card even in test mode. We can use Stripe testing cards instead.

The charge operation is one of many possibilities offered by the Stripe Java API. The official API reference will guide us through the whole set of operations.

Author: Habilelabs
Share: