Published in


Accepting a payment using Stripe (Kotlin/Javalin)

Stripe is a cloud-based service that enables charging customers for your products or services. With Stripe API, you don’t manage sensitive information like credit cards. Let’s learn how to use it.

1. Setup

  1. Register a Stripe account if you don’t own one.
  2. Create a new Kotlin project.

2. Web handlers

  1. Add Javalin (web framework) and Stripe client to your build.gradle.kts dependencies:

2. We need to set up the web request handlers. Create a src/kotlin/Main.kt with the following content:

Beware you need to replace two values:

  • STRIPE_API_KEY: Ensure your Stripe Dashboard is in “Test mode” (top-right corner). Head on to “Developers” (top right) → “API Keys” (left-side menu) → “Standard keys” → “Publishable key” (this is a demo key). Beware that you should set an environment variable with this rather than leave it in the code and thus in source control.
  • PRICE_ID: In the Stripe Dashboard find and click “Products” and then, create a new product. Confirm the product creation and within the newly created product, scroll until the “Pricing” section. There, you can copy the “API ID” of the desired price.

📝 You can fetch the product details (e.g. title, image URL) with Product.retrieve(PROD_ID).

3. Web pages

We need the HTML pages that the user will see. In this sample, we could use just resort to hardcoded HTML. However, let’s add a server-side templating library so we can display the price details obtained from the Stripe API. Add jte to build.gradle.kts:


We need a checkout page to represent the checkout of a shopping cart; it contains all the products the customer added. In our case, it contains a single product. In a real case, it could contain the products stored in a session. Add src/main/jte/checkout.jte with the following content:

We need a success page that represents the callback from Stripe when the payment is successful. Add src/main/jte/success.jte with the following content:

The cancel page deals with the user canceling the payment. Add src/main/jte/cancel.jte with the following content:

4. A test payment

Let’s do a fake payment in localhost:

  1. Run Main.kt and go to http://localhost:7000/checkout.
  2. Click the “Checkout” button to move on to the Stripe payment page.
  3. Now you can use the fake card 4242 4242 4242 4242 (the remaining details can be set at will). This fake card number simulates a fake successful payment so you can test the whole flow.

Learn more




Everything connected with Tech & Code. Follow to join our 900K+ monthly readers

Recommended from Medium

How to Build a Real-Time Leaderboard with Python and Redis

Update Java to version 11 on AWS Elastic Beanstalk in production

Building With Real-Time Payments

Some thoughts about the Three laws of performance

A collection of tools I use to organise my work

Getting started with Twint

Spring Security OAuth2

Summary: 8 Mistakes to avoid in HTML and CSS by Aaron Jack

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Luís Soares

Luís Soares

I write about Lean, TDD, CI/CD, trunk-based dev., user-centric dev, DDD, coding good practices, testing

More from Medium

How to Implement a Foreground Service with Notifications in Android

Android MultiTriggerBomb — Prevent Code Execution Till All Triggers Are Down (or Timer Is Expired)

KMM UI Architecture - Part 2

Android: Navigating with App Shortcuts