gpt4 book ai didi

javascript - 如何强制重定向到 Stripe Checkout 中 success_url 之外的另一个页面?

转载 作者:行者123 更新时间:2023-12-02 00:06:43 24 4
gpt4 key购买 nike

问题:

我目前正在使用 webhook:当收到 checkout.session.completed 事件时,默认情况下用户会被重定向到 success_url,即使我尝试使用 res 将他重定向到另一个页面。重定向(“/页面”)(Node.js)。如果我的执行代码失败,真的没有办法将他重定向到另一个页面吗? (即:支付成功,但是fulfillment code失败,所以重定向到success_url是不合适的)


引用:

https://stripe.com/docs/payments/checkout/one-time

https://stripe.com/docs/payments/checkout/fulfillment#webhooks

“checkout.session.completed webhook 在您的客户被重定向之前发送到您的服务器。您的 webhook 确认(任何 2xx 状态代码)将触发客户重定向到 success_url”


我尝试了什么:

Chaining Express.js 4's res.status(401) to a redirect

所以如果我要发送一个 4xx 状态代码,这不应该阻止 success_url 重定向吗?这确实是我观察到的。

但我似乎无法重定向到另一个页面。我试过:

res.status(401).location('/submit/wait/').end();
res.redirect(401, '/submit/wait/');
res.set('Content-Type', 'text/html');
res.status(401).send('<!DOCTYPE html><html><head><meta http-equiv="refresh" content="0; url=/submit/wait"></head></html>`);Is there really no way to then just redirect to another page ?  

代码

router.post('/webhook', bodyParser.raw({type: 'application/json'}), (req, res) => {

const sig = req.headers['stripe-signature'];
let event;

try {
event = stripe.webhooks.constructEvent(req.rawBody, sig, endpointSecret);
} catch (err) {
return res.status(400).send(`Webhook Error: ${err.message}`);
}

if (event.type === 'checkout.session.completed') {

const session = event.data.object;

(async () => {

try {
//FULFIL ORDER CODE

} catch(e) {
console.log("ERROR FULFILLING ORDER: "+e);


res.status(401).location('/submit/wait/').end();

}
})();
}
else {
console.log("DEFAULT RESPONSE");

// Return a response to acknowledge receipt of the event
res.json({received: true});
}

});

最佳答案

The checkout.session.completed event发送到您的 webhook 端点只是为了让您知道结帐 session 已完成。除了响应成功或失败之外,您不能使用事件通知以任何方式更改结帐 session 。

客户端重定向延迟到您响应 checkout.session.completed 事件的原因是允许您在用户被重定向到 success_url 之前在您的服务器上执行完成任务(如更新数据库、进行其他 API 调用等)。这可确保客户登陆反射(reflect)其成功购买的页面。

success_url 本身在此过程中无法更改,但您可以在您的服务器上设置一个页面,该页面将根据结账 session ID 将用户重定向到您希望的任何页面,并且将 success_url 设置为该页面。你可以include the Checkout Session ID in your success_url using the {CHECKOUT_SESSION_ID} placeholder .

关于javascript - 如何强制重定向到 Stripe Checkout 中 success_url 之外的另一个页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60385090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com