Den Dribbles

Creating your first Stripe Charge with Java + Spring in 5 minutes

June 27, 2020

In this short series, we are going to look at how to create a charge to Stripe in a number of their officially supported languages!

In this article, we are going to look at how to do so with Java and Spring.

The expectations are that you have Java installed and have your Stripe API keys setup and ready to go and that you have a general idea of how Gradle works.

The following comes in part from my documentation website.


mkdir hello-spring-stripe && cd hello-spring-stripe
mkdir -p src/main/java/stripe
touch build.gradle src/main/java/stripe/ src/main/java/stripe/ src/main/java/stripe/ .env
# init gradle
gradle wrapper

Setup build.gradle

Add the following for Stripe + Spring:

buildscript {
    repositories {
    dependencies {

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'hello-spring-jar'
    version =  '0.1.0'

repositories {

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {

Set up dotenv file

We are using a local dotenv file for this “Hello, World!” level example.

# required
# not required

Setting up the main application

In src/main/java/stripe/, add the following:

package stripecharge;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class Application {

  public static void main(String[] args) {, args);

Setting up the model

In src/main/java/stripe/

package stripecharge;

import java.util.HashMap;
import java.util.Map;

public class StripeCharge {

  private final long amount;
  private final String receiptEmail;
  private final String source;
  private final String currency;

  public StripeCharge(long amount, String receiptEmail) {
    this.amount = amount;
    this.source = "tok_visa";
    this.currency = "usd";
    this.receiptEmail = receiptEmail;

  public Map<String, Object> getCharge() {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("amount", this.amount);
    params.put("currency", this.currency);
    // source should obtained with Stripe.js
    params.put("source", this.source);
      "My First Test Charge (created for API docs)"
    return params;

Setting up the controller

In src/main/java/stripe/

package stripecharge;

// NOTE: RequestMapping + RequestParam not required for this demo
// but kept in for reference purposes if wanted to play around later
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RestController;
import io.github.cdimascio.dotenv.Dotenv;
import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.Charge;

public class StripeChargeController {

  public ResponseEntity<String> createCharge(@RequestBody StripeCharge stripeCharge) {
    try {
      // for demonstrations sake, using .env file
      Dotenv dotenv = Dotenv.load();

      // creating the charge
      Stripe.apiKey = dotenv.get("SK_TEST_KEY");
      Charge charge = Charge.create(stripeCharge.getCharge());
      return new ResponseEntity<String>("Success", HttpStatus.CREATED);
    } catch (StripeException e) {
      return new ResponseEntity<String>("Failure", HttpStatus.INTERNAL_SERVER_ERROR);


Build, Run and Ping

Run ./gradlew build to build to project or ./gradlew bootRun to build and run.

Once the project is running, we can ping http POST http://localhost:8080/api/charge amount:=1700 (using HTTPie) and we get a response like the following:

HTTP/1.1 201
Content-Length: 7
Content-Type: application/json;charset=UTF-8
Date: Sun, 12 Jan 2020 03:33:10 GMT


I chose to use HTTPie because I feel it is a fun tool that more should know about! Alternative, you could do the above using curl as well (or anything that can make a POST request for a matter of fact).

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"amount":1700,"receiptEmail":""}' \

If you now go and check your Stripe dashboard, you will be able to see a charge.

Stripe Dashboard

Resources and Further Reading

  1. Gradle Intro Docs
  2. Spring Guide to Rest Services
  3. Stripe API
  4. Stripe with Spring
  5. Stripe Github Java Usage
  6. Spring ResponseEntity
  7. Tutorials Point: Service Components
  8. Basic Spring Boot Gitignore file

Image credit: Gio Bartlett

Related Articles

A personal blog on all things of interest. Written by Dennis O'Keeffe, Follow me on Twitter