[英]Redirect to a different page after Stripe payment - Node.js
我正在按照https://stripe.com/docs/recipes/custom-checkout上的指南,在我的网站上放一个自定义结帐按钮。 到目前为止,测试付款将通过,我可以创建一个客户并收取新费用。
我停留的部分实际上是在交易后将用户重定向到/success
页面,例如,其中包含收费的详细信息。 我试过使用res.render('success')
甚至res.redirect('/success')
但它没有执行。
<button id="upgrade_membership"</button>
<script>
var checkoutHandler = StripeCheckout.configure({
key: "<%= keyPublishable %>",
locale: "auto"
});
var button = document.getElementById("upgrade_membership");
button.addEventListener("click", function(ev) {
checkoutHandler.open({
name: "Name",
description: "Description",
image: 'images/path.jpg',
token: handleToken
});
});
function handleToken(token) {
fetch("/charge", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify(token)
})
.then(response => {
if (!response.ok)
throw response;
return response.json();
})
.then(output => {
console.log("Purchase succeeded:", output);
})
.catch(err => {
console.log("Purchase failed:", err);
})
}
</script>
服务器
app.post("/charge", async (req, res) => {
let amount = 1000;
// Check user has stripe id as stripe do not check for uniqueness of email address
try {
var user_stripe_id = await queries.get_stripe_id_by_email(req.body.email);
} catch (err) {
console.log("Error with Query " + err);
return;
}
if(user_stripe_id !== null) {
// Need to process payment as a current customer on stripe
return;
} else {
// Create Stripe Customer
stripe.customers.create({
email: req.body.email,
card: req.body.id
})
.then(customer =>
// Charge Customer payment
stripe.charges.create({
amount,
description: "description here",
currency: "GBP",
customer: customer.id
}))
.then(charge => {
// gets all the way here and logs out the charge object
// want to redirect here
res.send(charge)
// if i replace the above with res.render('home') nothing happens
})
.catch(err => {
console.log("Error:", err);
res.status(500).send({error: "Purchase Failed"});
});
} // if/else
});
因此,在浏览器控制台中成功进行交易后,我Purchase succeeded:
打印。
我要实现的是将服务器重定向到另一页
UPDATE
因此,我尝试在付款成功后在此处呈现其他页面,但是我什么都无法工作
res.render('home') // does not work
return res.render('home') // does not work
return res.redirect('/') // does not work
res.redirect('/') // does not work
更令人困惑
console.log("I WANT TO RENDER A NEW PAGE")
res.render('home')
console.log("WHY IS THIS BEING LOGGED")
两个控制台日志都已记录
是因为我then
处于连锁状态吗? 真的不明白这里发生了什么
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.