在结账时检索条带中的客户 ID
Retrieve customer ID in stripe during checkout
我有一个 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,
}
});
}
}
});
您如何检索客户 ID?
您应该通过提取查询参数并通过 API 调用 Stripe 检索 session_id
来处理 success_url
以获得 customer
:https://stripe.com/docs/api/checkout/sessions/retrieve .所以你应该有一条 get(...)
路线用于 success_url
路线。
或者,监听 checkout.session.completed
webhook 事件。
我有一个 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,
}
});
}
}
});
您如何检索客户 ID?
您应该通过提取查询参数并通过 API 调用 Stripe 检索 session_id
来处理 success_url
以获得 customer
:https://stripe.com/docs/api/checkout/sessions/retrieve .所以你应该有一条 get(...)
路线用于 success_url
路线。
或者,监听 checkout.session.completed
webhook 事件。