Stripe Payment Gateway Integration in Scala

Working on report
Reading Time: 3 minutes

Introduction

In modern day there are a lot of companies implementing digital payment systems on their website to make easy and secure payment processes for customers.
There are a lot of payment methods available. Here is the list of payment methods:

  1. Paypal
  2. Stripe
  3. Razorpay
  4. PayU
  5. Instamojo
  6. CCAvenue
  7. Paytm

As an organization you need to be careful while choosing your payment gateway. There are some factors before choosing the correct option. Here is the list of factors:

  1. Customer experience
  2. Security
  3. Simple integration
  4. Multiple currency support
  5. Reputation & service support
  6. Cost of transaction
  7. Recurring Billing

In this blog we will be talking about how stripe checkout works when a customer makes an order.

Stripe Checkout Lifecycle

  1. Client sends order information
  2. Create a checkout session (server side)
  3. Stripe sends back checkout session url
  4. Redirect customer to url from checkout session
  5. Customer completes their payment
  6. Webhook
  7. Customer returns to your application

Implementation with source code

First of all we need to add stripe dependency in application

// https://mvnrepository.com/artifact/com.stripe/stripe-java

libraryDependencies += "com.stripe" % "stripe-java" % "22.0.0"
  1. Client sends order information:

As part of this client collects order information selected by customer. After collecting these info the client sends back to the back-end server to create checkout session.

  1. Create a checkout session:

A Checkout Session represents your customer’s session as they pay for one-time purchases or subscriptions through Checkout or Payment Links. It is recommended to create a new checkout each time when your customer attempts to pay. To create a checkout session you need to make a call to stripe api with the following details:

Scala code:

  import play.api.libs.ws._
  
  import scala.concurrent.ExecutionContext.Implicits.global

  def createCheckout() = Action { implicit request: Request[AnyContent] =>

    val url = "https://api.stripe.com/v1/checkout/sessions"

    val data = Map(

      "success_url" -> "https://example.com/success",

      "cancel_url" -> "https://example.com/cancel",

      "line_items[0][price]" -> "price_XXXXXX",

      "line_items[0][quantity]" -> "2",

      "mode" -> "payment"
    )

    val sk = "sk_test_XXXXXXX"

    ws.url(url).withAuth(sk,"",WSAuthScheme.BASIC)

    .post(data).map {

      res=>

        println(s"Stripe checkout response: ${res.json.toString()}")

    }

    Ok("Checkout session Generated successfully")
  }

Here we have passed only the required field for stripe checkout session. You can attach more fields while creating a session. You can also attach metadata. Metadata is useful for storing additional, structured information on an object. As an example, you could store your user’s full name and corresponding unique identifier from your system on a Stripe Customer object. Metadata is not used by Stripe.

  1. Stripe sends back checkout session url

After the successful checkout response. The stripe sends back a checkout session url. You can redirect your customer to the payment page.

  1. Redirect customer to url from checkout session

This step is covered in step3.

  1. Customer completes their payment

Customers enter their payment details on the payment page and complete the transaction.
UI payment demo is shown in below image:

  1. Webhook

After the transaction, a webhook fulfils the order using the checkout.session.completed event.
A webhook enables Stripe to push real-time notifications to your app. Stripe uses HTTPS to send these notifications to your app as a JSON payload. You can then use these notifications to execute actions in your backend systems.
We will discuss in more details about stripe webhook workflow in the upcoming session.

  1. Customer returns to your application:

After the payment is successfully made you can show them a success page or the order page which you have passed the url while creating the checkout session.

Conclusion

We have seen stripe online payment gateway is reliable, convenient and easy to integrate. It is the second most popular payment processing software after PayPal. Stripe payment gateway is choosen by the top companies. Some of them are Amazon, Apple, Walmart, Wayfair, Shopify, Figma, instacart, pelton etc.

Leave a Reply