Stripe redirectToCheckout method redirects to successful url before showing any response in the .then section

Viewed 2866

I am trying to run a piece of code in the .then section of stripe.redirectTocheckout function. But it redirects me to the successfulUrl part before showing any response/result in the .then({}) and hence the code in the .then section does not run. Kindly help me. I am stuck here from a long time. Thank you.

stripe.redirectToCheckout({
  items: [{ sku: 'abcssdd', quantity: 1 }],
  successUrl:'https://your-website.com/congratulation',
  cancelUrl: 'https://your-website.com/canceled',
})
.then(function (result) {
  if (result.error) {
    var displayError = document.getElementById('error-message');
    displayError.textContent = result.error.message;
  }
  console.log("Inside Then");
})
1 Answers

The then option is there in case it fails. If it's not failing, there is no reason to show error.

It's a little confusing, IMHO, because then() is usually associated with success and catch() with the error., but note the comments form their docs below:

const stripe = Stripe('pk_test_TYauvdEDq54NiTpjx');

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`.
});

src: https://stripe.com/docs/stripe-js/reference#stripe-redirect-to-checkout

Related