lf_*_*ine 4 express stripe-payments
我有一个 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,
}
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
您如何检索客户 ID?
您应该success_url通过提取查询参数并session_id通过对 Stripe 的 API 调用进行检索来进行处理,以获得customer:https://stripe.com/docs/api/checkout/sessions/retrieve。所以你应该有一个路线get(...)的路线success_url。
或者,监听checkout.session.completedwebhook 事件。
| 归档时间: |
|
| 查看次数: |
3802 次 |
| 最近记录: |