我有一个 Stripe 结账,如果成功,我想检索客户 ID。我遵循了 Stripe 教程,但它对我不起作用(结帐效果很好,但我无法检索客户 ID 以将其添加到我的用户数据库中)
我的代码:
router.post("/create-checkout-session", ensureAuthenticated, async (req, res) => {
const { priceId } = req.body;
// See https://stripe.com/docs/api/checkout/sessions/create
// for additional parameters to pass.
try {
const session = await stripe.checkout.sessions.create({
mode: "subscription",
payment_method_types: ["card"],
line_items: [
{
price: priceId,
// For metered billing, do not pass quantity
quantity: 1,
},
],
// {CHECKOUT_SESSION_ID} is a string literal; do not change it!
// the actual Session ID is returned in the query parameter when your customer
// is redirected to the success page.
success_url: 'http://localhost:3000/fr/premiereconnexion?session_id={CHECKOUT_SESSION_ID}',
cancel_url: 'https://localhost:3000/fr/erreursouscription',
});
res.send({
sessionId: session.id,
customerID: session.customer
});
}
catch (e) {
res.status(400);
return res.send({
error: {
message: e.message,
}
});
}
}
});
答案 0 :(得分:4)
您如何检索客户 ID?
您应该通过提取查询参数并通过对 Stripe 的 API 调用检索 success_url
来获取 session_id
:https://stripe.com/docs/api/checkout/sessions/retrieve 来处理 customer
。因此,您应该为 get(...)
路线设置了 success_url
路线。
或者,收听 checkout.session.completed
网络钩子事件。