Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to checkout in ReactJS

I am trying to implement the Stripe function "redirect to checkout" in ReactJS. I have been looking around and there is no package that seems to help to do it.

const stripe = 
Stripe('key');

stripe.redirectToCheckout({
  items: [
    // Replace with the ID of your SKU
    {sku: 'sku_123', quantity: 1}
  ],
  successUrl: 'https://your-website.com/success',
  cancelUrl: 'https://your-website.com/canceled',
}).then(({error}) => {
  // If `redirectToCheckout` fails due to a browser or 
network
  // error, display the localized error message to your 
customer
  // using `error.message`.
});

This is where I got this source code: https://stripe.com/docs/stripe-js/reference#stripe-redirect-to-checkout

StripeJS only seems to support the standard checkout that does not receive the product SKU as the parameter

like image 363
Andrea Grippi Avatar asked Jun 11 '19 21:06

Andrea Grippi


People also ask

How do I redirect in react JS?

The Redirect component was usually used in previous versions of the react-router-dom package to quickly do redirects by simply importing the component from react-router-dom and then making use of the component by providing the to prop, passing the page you desire to redirect to.


1 Answers

After I read the new stripe-js docs https://stripe.com/docs/stripe-js/react I found this might be useful for you

Instead using stripe, install @stripe/stripe-js then the job can be done by

import { loadStripe } from "@stripe/stripe-js";

...

const stripePromise = loadStripe(
    "pk_.........."
  );

const stripe = await stripePromise;

stripe.redirectToCheckout({
      ...
    })
like image 125
Anson Cen Avatar answered Sep 24 '22 23:09

Anson Cen