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.

Luís Soares
Published in
3 min readJun 16, 2022


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 to 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



Luís Soares
Writer for

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