首页 > 解决方案 > Stripe redirectToCheckout 方法在 .then 部分显示任何响应之前重定向到成功的 url

问题描述

我正在尝试在 stripe.redirectTocheckout 函数的 .then 部分中运行一段代码。但它会在 .then({}) 中显示任何响应/结果之前将我重定向到successfulUrl 部分,因此.then 部分中的代码不会运行。请帮助我。我被困在这里很久了。谢谢你。

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");
})

标签: vuejs2stripe-payments

解决方案


如果失败,该then选项就在那里。如果它没有失败,则没有理由显示错误。

恕我直言,这有点令人困惑,因为then()通常与成功和catch()错误相关联。但请注意以下文档中的评论:

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

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


推荐阅读