Skip to content

muneendra-yeddala/stripecart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stripe Cart to Order

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.

Dependencies

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>

General Flow

  1. User Clicks on Product List link on top menu
  2. Add Product to cart
  3. Click on my cart
  4. Add customer informtion
  5. Add credit card information
  6. Submit the Cart for Order
  7. 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
  8. Display Payment Intent Id along with Order Id on Order Confirmation Screen

Flow Diagram

Frontend

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

Backend

  • 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/

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published