在结账时检索stripe中的客户ID



我有一个Stripe checkout,我想检索客户ID,如果它是成功的。我遵循条纹教程,但它不适合我(结帐工作得很好,但我无法检索客户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,
}
});
}
}
});

如何检索Customer ID?

您应该通过提取查询参数来处理success_url,并通过调用Stripe的API来检索session_id,以获得customer: https://stripe.com/docs/api/checkout/sessions/retrieve。因此,对于success_url路由,您应该有一个get(...)路由。

或者,监听checkout.session.completedwebhook事件。

相关内容

  • 没有找到相关文章

最新更新