Skip to content

A Spring application to manage Customers, Plans and Subscriptions without using Stripe’s Dashboard

Notifications You must be signed in to change notification settings

utsavrai/RESTful-Service--StripeAPI-integration-with-Spring-Application

Repository files navigation

RESTful Service - A Spring application to manage Customers, Plans and Subscriptions without using Stripe's Dashboard

This spring application uses stripe api which is a technology company. Its software allows individuals and businesses to receive payments over the Internet. Stripe provides APIs that web developers can use to integrate payment processing into their websites and mobile applications. I have used stripe api with spring application which gives the user capabilities of managing customer, plans and subscription. Each of these entities have their respective controllers and services.

Following is an example of subscription plan that I used Customer Controller

@RestController

public class CustomerController {

 @Autowired

  private CustomerService customerService;

  @RequestMapping(value="/customers", method=RequestMethod.GET)

  public List<Customer> getAllCustomers() throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException{

   return customerService.getAllCustomers();

  }

  @RequestMapping(value="/customers/add",method = RequestMethod.POST)

  public Customer addCustomer(@RequestBody SCustomer cus) throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException {

   return customerService.addCustomer(cus);

  }

  @RequestMapping(value="/customers/delete/{cid}",method = RequestMethod.GET)

  public String deleteCustomer(@PathVariable String cid) throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException {

   return customerService.deleteCustomer(cid);

  }

  @RequestMapping(value="/customers/retrieve/{cid}",method = RequestMethod.GET)

  public String retrieveCustomer(@PathVariable String cid) throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException {

   return customerService.retrieveCustomer(cid);

  }

  @RequestMapping(value="/customers/update/{cid}",method = RequestMethod.PUT)

  public String updateCustomer(@PathVariable String cid,@RequestBody SCustomer cus) throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException {

   return customerService.updateCustomer(cid,cus);

  }

}

Example: Adding customer to stripe account using postman

First add Headers Make a post request at localhost:8080/customers/add Example: Get all customers

every request made at the endpoint of /customer is handled by customer controller which maps each request to their respective controller function which extract path variables if present and call respective function in customer services. This controller allows to major crud operation supported by stripe api.

Plan Controller

@RestController

public class PlanController {

 @Autowired

 PlanService planService;

 @RequestMapping(value="/plans", method=RequestMethod.GET)

 public List<Plan> getAllPlans() throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException{

  return planService.getAllPlans();

 }

 @RequestMapping(value="/plans/add",method = RequestMethod.POST)

 public String addPlan(@RequestBody SPlan sp) throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException {

  return planService.addPlan(sp);

 }

 @RequestMapping(value="plans/delete/{pid}",method = RequestMethod.GET)

 public String deletePlan(@PathVariable String pid) throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException {

  return planService.deletePlan(pid);

 }

}

Example: Get all Plans stripe plans are at the heart of subscriptions, establishing the billing cycle, currency, and base cost. Every plan is attached to a product, which represents the application or service offered to customers. Products can have more than one plan, reflecting variations in price and duration—–such as monthly and annual pricing at different rates. This controller allows to create plans, retrieve all plans and delete plan.

Subscription Controller

@RestController

public class SubscriptionController {

 @Autowired

 private SubscriptionService subscriptionService;

 @RequestMapping(value="/subscriptions", method=RequestMethod.GET)

 public List<Subscription> getAllCustomers() throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException{

  return subscriptionService.getAllSubscriptions();

  //return subscriptionService.getAllSubscriptions();

 }

 @RequestMapping(value="/subscriptions/add", method=RequestMethod.POST)

 public String addSubscription(@RequestBody SSubscription sub) throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException{

  return subscriptionService.addSubscription(sub);

  //return subscriptionService.getAllSubscriptions();

 }

 @RequestMapping(value = "/subscriptions/cancel/{cid}",method=RequestMethod.GET)

 public String cancelSub(@PathVariable String cid) throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException {

  return subscriptionService.cancelSub(cid);

 }

}

Example: Get all subscription Subscriptions allow you to charge a customer on a recurring basis. A subscription ties a customer to a particular plan you've created.

In this example I have used four subscriptions - zero$, zero$_30day_freeTrial, 99$, 99$_30day_freeTrial

The purpose of zero$ plan is to avoid any unethical way of subscribing to any other free trials, it does by checking if a user is in under zero$ plan then the user might have either canceled or his/her subscription must have ended. This ensures that the only way out from this plan is to subscribe to 99$ plan which means that the user have to pay, no options left!

when the user signs up, he/she is subscribed to the zero$_30day_freeTrial, that ensures that the user is under 30-day free trial.

Now in between the user can choose to switch to 99$ plan even though he/she is under free trial. Or the user can cancel its free trial which will switch him to zero$ plan. Once the trial period gets over the user is switched to zero$ plan.

Now suppose the user wants to get back in and wants to extend its trial, or the product is offering one more month of free trial but on one condition that this to avail the offer you have to enter your credit card details which will be secured by stripe, (nothing is stored by the product). With this offer the user is upgraded to 99$_30day_freeTrial plan in which the user won't be charged anything but as soon as the trial ends the user will be switched to 99$ plan and will be charged monthly. Now if the user cancels before the trial then he/she will be switched to zero$ plan which again makes sure that user cannot go back to any free trial plans which is handled at the back end.

Following is the state machine for the subscription

About

A Spring application to manage Customers, Plans and Subscriptions without using Stripe’s Dashboard

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages