// Initialize Stripe.js using your publishable key
const stripe = Stripe('pk_test_***');
// Retrieve the "payment_intent_client_secret" query parameter appended to
// your return_url by Stripe.js
const clientSecret = new URLSearchParams(window.location.search).get(
'payment_intent_client_secret'
);
// Retrieve the PaymentIntent
stripe.retrievePaymentIntent(clientSecret).then(({paymentIntent}) => {
const message = document.querySelector('#message')
// Inspect the PaymentIntent `status` to indicate the status of the payment
// to your customer.
//
// Some payment methods will [immediately succeed or fail][0] upon
// confirmation, while others will first enter a `processing` state.
//
// [0]: https://stripe.com/docs/payments/payment-methods#payment-notification
switch (paymentIntent.status) {
case 'succeeded':
message.innerText = 'Success! Payment received.';
break;
case 'processing':
message.innerText = "Payment processing. We'll update you when payment is received.";
break;
case 'requires_payment_method':
message.innerText = 'Payment failed. Please try another payment method.';
// Redirect your user back to your payment page to attempt collecting
// payment again
break;
default:
message.innerText = 'Something went wrong.';
break;
}
1条答案
按热度按时间2vuwiymt1#
锁定87天,目前有disputes about this answer’s content正在解析,目前不接受新的交互。
这种Stripe集成方法仍然是Stripe文档的主要集成部分推荐的官方方法:
确保return_url对应于您网站上提供付款状态的页面。当Stripe将客户重定向到return_url时,我们提供以下URL查询参数:
**payment_intent:**PaymentIntent的唯一标识
**payment_intent_client_secret:**PaymentIntent对象的客户端secret
...
使用其中一个查询参数检索PaymentIntent。检查PaymentIntent的状态以决定向客户显示什么。您还可以在提供return_url时附加自己的查询参数,这些参数在重定向过程中始终存在。
然后使用以下代码演示此方法:
这证实了我的怀疑,
client_secret
,顾名思义,是为了在客户端看到(即由刚刚支付的用户),并且关于将其嵌入URL的一般语言不适用于返回URL。