Creating your first Stripe Charge with Java + Spring in 5 minutes
Published: Jun 27, 2020
Last updated: Jun 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.
We are using a local dotenv file for this "Hello, World!" level example.
# required
SK_TEST_KEY=sk_...
# not required
PK_TEST_KEY=pk_...
Setting up the main application
In src/main/java/stripe/Application.java, add the following:
package stripecharge;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Setting up the model
In src/main/java/stripe/StripeCharge.java:
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);
params.put(
"description",
"My First Test Charge (created for API docs)"
);
params.put("receipt_email",this.receiptEmail);
return params;
}
}
Setting up the controller
In src/main/java/stripe/StripeChargeController.java:
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;
import com.stripe.net.RequestOptions;
@RestController
@RequestMapping("/api")
public class StripeChargeController {
@PostMapping("/charge")
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());
System.out.println(charge);
return new ResponseEntity<String>("Success", HttpStatus.CREATED);
} catch (StripeException e) {
e.printStackTrace();
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 receiptEmail=hello_spring@example.com (using HTTPie) and we get a response like the following:
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).