Stripe is a cloud-based service that enables businesses and individuals to receive payments over the internet and offers both client-side libraries (JavaScript and native mobile) and server-side libraries (Java, Ruby, Node.js, etc.).
Stripe provides a layer of abstraction that reduces the complexity of receiving payments. As a result, we don't need to deal with credit card details directly – instead, we deal with a token symbolizing an authorization to paymentintent, intent confirm and capture.
Simple Spring Boot project that allows users browse prodcuts, add to cart, input customer info and a credit card and later will charge the card for a certain amount using the Stripe API for Java.
To make use of the Stripe API for Java in the project, we add the corresponding dependency to our pom.xml:
<dependency>
<groupId>com.stripe</groupId>
<artifactId>stripe-java</artifactId>
<version>20.128.0</version>
</dependency>
- User Clicks on Product List link on top menu
- Pre configured products are fetched from Stripe with Products API (https://stripe.com/docs/api/products)
- Add Product to cart
- Click on my cart
- Add customer informtion
- Add credit card information
- Card input fileds are rendered with the help of Stripe html elements
- Send card details to Stripe and generate the token. for more of token (https://stripe.com/docs/js/tokens_sources/create_token)
- Submit the Cart for Order
- Backend does the following steps
- Create Customer information on Stripe
- Create payment intent for cart amount with token (generated at step 5) and customer id
- Covert Cart to Order
- Capture the Payment Intent
- Display Payment Intent Id along with Order Id on Order Confirmation Screen
- Create Stripe Element for card payment.
<div id="card-element"></div>
- Mount the card element div with javascript
document.addEventListener("DOMContentLoaded", () => {
const stripe = Stripe('pk_test_xxxxx');
const element = stripe.elements();
const cardElement = element.create("card");
cardElement.mount("#card-element");
}
- Generate Token for the user entered card details
<script>
document.addEventListener("DOMContentLoaded", () => {
const stripe = Stripe('pk_test_xxxxxx');
const element = stripe.elements();
const cardElement = element.create("card");
cardElement.mount("#card-element");
$('#cardForm').off('click').on('click', function(e) {
// $("#cardForm").submit(function (e) {
e.preventDefault();
stripe.createToken(cardElement).then(function (result) {
console.log(result);
if (result.error) {
console.log(result.error.message);
$("#paymentError").html(result.error.message);
} else {
console.log(result.token.id);
// $("#cardForm").html(result.token.id);
$('#paymentToken').val(result.token.id);
// alert($('#paymentToken').val())
$("#cardForm").submit();
}
});
});
});
</script>
- Get Prodcut information from Stripe
public List<Product> getProducts() {
try {
Stripe.apiKey = stripeSecretKey;
ProductListParams params = ProductListParams.builder().setActive(Boolean.TRUE).setLimit(Long.valueOf(3))
.build();
ProductCollection products = Product.list(params);
return products.getData();
} catch (Exception e) {
LOG.error("",e);
}
return List.of();
}
- Register Customer with Stripe
private String createCustomer(CustomerInfo customer) throws StripeException {
Stripe.apiKey = stripeSecretKey;
CustomerCreateParams params = CustomerCreateParams.builder().setEmail(customer.getEmail())
.setSource(customer.getPaymentToken()).build();
Customer customerStripe = Customer.create(params);
return customerStripe.getId();
}
- Create PaymentInetent with Stripe
public String createIntent(CartInfo cartInfo) throws StripeException {
Stripe.apiKey = stripeSecretKey;
String customerId = this.createCustomer(cartInfo.getCustomerInfo());
PaymentIntentCreateParams paramsIntent = PaymentIntentCreateParams.builder()
.setAmount((long) cartInfo.getAmountTotal()).setCurrency("jpy").setCustomer(customerId)
.setConfirm(Boolean.TRUE).setCaptureMethod(CaptureMethod.MANUAL)
.setDescription("Order for Customer " + cartInfo.getCustomerInfo().getEmail()).build();
PaymentIntent charge = PaymentIntent.create(paramsIntent);
return charge.getId();
}
- Capture PaymentIntent with Stripe
public String captureIntent(String id) throws StripeException {
Stripe.apiKey = stripeSecretKey;
PaymentIntent intent = PaymentIntent.retrieve(id);
PaymentIntent updatedCharge = intent.capture();
return updatedCharge.getId();
}
Stripe also Provides the APIs something called Charge for businesses where payment intent is not a must. Like countries Japan
- Create Payment Charge with Strip
public String createCharge(CartInfo cartInfo) throws StripeException {
Stripe.apiKey = stripeSecretKey;
String customerId = this.createCustomer(cartInfo.getCustomerInfo());
ChargeCreateParams params = ChargeCreateParams.builder().setAmount((long) cartInfo.getAmountTotal())
.setCurrency("jpy").setCustomer(customerId)
// .setSource(cartInfo.getCustomerInfo().getPaymentToken())
.setCapture(Boolean.FALSE).setDescription("Order for Customer " + cartInfo.getCustomerInfo().getEmail())
.build();
Charge charge = Charge.create(params);
return charge.getId();
}
- To run the application
java -jar stripecart-0.0.1-SNAPSHOT.jar
http://localhost:8080/