简体   繁体   中英

Unhandled Rejection (TypeError): stripe.redirectToCheckout is not a function

I am trying to implement Stripe checkout for a website. Unfortunately, the Stripe docs are very incomplete. I have:

import React from 'react';
import { loadStripe } from '@stripe/stripe-js';

const stripe = loadStripe('pk_test_key');

const handleClick = async (event) => {
  stripe.redirectToCheckout({
    lineItems: [
      {price: 'price_something', quantity: 1},
    ],
    mode: 'subscription',
    successUrl: 'https://example.com/success?session_id={CHECKOUT_SESSION_ID}',
    cancelUrl: 'https://example.com/cancel',
  }).then(function(result){
    alert(result);
  });
};

export default function Stripe(){
  return (
    <button role="link" onClick={handleClick}>
      Checkout
    </button>
  );
}

I click the button and I get an error: Unhandled Rejection (TypeError): stripe.redirectToCheckout is not a function

My key is correct one but I cannot find any information in their docs or anywhere else to solve this. Thanks!

From my understanding that loadStripe returns a Promise so try to wait for it resolved:

const handleClick = async (event) => {
  const stripe = await loadStripe('pk_test_key');

  stripe.redirectToCheckout({
    // ...
  })
};

it help check that you are using proper await keyword as I had missed that and wondering for 2 hours where's the mistake

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM