How to a Create Stripe Subscription Session using Spring Boot

Posted By : Sumit Kashyap | 29-Jun-2022

How to Create Stripe Subscription using Spring boot

 

What is Stripe?

Stripe is a popular payment processor or gateway that allows your customers to safely and efficiently transfer funds from their credit cards or bank accounts in a variety of currencies.


How to Create Stripe Subscription product and price in Stripe Dashboard?
 

1. Create Product:  What your business offers whether that’s goods or services.

2. Create Price: How much and how often to charge for products, including how much the product costs, what currency to use, and the interval if the price is for subscriptions.

3. Create Customer: Stripe Customer objects allow you to perform recurring charges for the same customer, and to track multiple charges.

4. Create Subscription: The product details associated with the plan that your customer subscribes to, which allow you to charge the customer regularly.


Subscription implementation using spring boot 
 

1. Add Dependency in the Application 

The following dependency add to pom.xml file.

<dependency>
    <groupId>com.stripe</groupId>
    <artifactId>stripe-java</artifactId>
    <version>20.113.0</version>
</dependency>

 

2. Create a Pojo class.

public class StripeCustomerDetail{

    long userId;

    String customerId;

    String subscriptionId;

    LocalDateTime subscriptionEndDate;

    LocalDateTime subscriptionStartDate;

    String subscriptionPlan;

 

3. Create a Customer Account :

Create a Customer Account on the Stripe Dashboard by using the following class.

Map<String, Object> customerParams = new HashMap<>();
User user = userRepository.findUserById(userId);
customerParams.put("name", user.getDisplayName());
customerParams.put("email", user.getEmail());
Customer customer = Customer.create(customerParams);

4. Get All Subscription Plan .

Map<String, Object> params = new HashMap<>();
PriceCollection prices = Price.list(params);
Optional<Price> optionalPrice = prices.getData().stream().filter(price ->
        (interval).equalsIgnoreCase(price.getRecurring().getInterval())
                && Objects.equals(intervalCount, price.getRecurring().getIntervalCount()).findFirst();

 

5. Create Subscription Session

StripeCustomerDetail stripeCustomerDetail = stripeCustomerDetailsRepository.findByUserId(userId);
String successUrl = envConfiguration.getFrontendServerDetail() + "/ArtistDashboard/artistHome";
String cancelUrl = envConfiguration.getFrontendServerDetail() + "/ArtistDashboard/artistHome?success=false";
SessionCreateParams sessionCreateParams = new SessionCreateParams.Builder()
        .addPaymentMethodType(SessionCreateParams.PaymentMethodType.CARD)
        .setMode(SessionCreateParams.Mode.SUBSCRIPTION).setSuccessUrl(successUrl).setCancelUrl(cancelUrl)
        .addLineItem(new SessionCreateParams.LineItem.Builder()
                .setQuantity(1L).setPrice(priceId).build())
        .setCustomer(stripeCustomerDetail.getCustomerId())
        .build();
stripeCustomerDetail.setSubscriptionPlan(subscriptionPlan);
Session session = Session.create(sessionCreateParams);
return session.getUrl();

 

By following the flow we create a Subscription session  procedure 

About Author

Author Image
Sumit Kashyap

He is trained in java and works on Spring Boot API .He is backend developer. He is enthusiastic and can handle multiple tasks at a same time.

Request for Proposal

Name is required

Comment is required

Sending message..