Redirect to checkout in ReactJS

寵の児 提交于 2020-02-05 02:46:05

问题


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


回答1:


I found out how ti make it work.

Basically as per the documentation, there is the need to import the Stripe script in public/index.html

stripe.redirectToCheckout(...)

can be simply put into the onClick of a button. The thing that is really not clear in the docs, and that can mislead newbies like me, lies in setting the public key:

const stripe = Stripe('key');

doesn't work, because the script is not found at compile time.
This can be solved by using:

const stripe = window.Stripe('key');

This worked for me.



来源:https://stackoverflow.com/questions/56551841/redirect-to-checkout-in-reactjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!