如何检查用户是否已为特定产品付款?
我计划在我的页面中的 getServerSideProps() 函数中检查购买是否成功,该函数根据 success_url 确定从结帐重定向:
const session = await stripe.checkout.sessions.create({
line_items: list,
mode: 'payment',
success_url: ...,
cancel_url: ...
});
结账重定向到我的页面并传递 ?paystate 参数,该参数是 OK 还是 FAILED(具体取决于它是否来自 success_url 和 cancel_url)。 有人可以轻松地将这个查询字符串附加到页面末尾,或者伪造 http 引用来假装已支付产品费用,所以我需要一个服务器端 也检查一下。我怎样才能做到这一点?我试过:
const customers = await stripe.customers.list({ email: username });
if (customers.data.length > 0) {
const customerId = customers.data[0].id;
const charges = await stripe.charges.list({ customer: customerId });
//console.log('charges = ', charges);
// Process the list of chargeeps
charges.data.forEach((charge) => {
console.log(`Charge ID: ${charge.id}, Amount: ${charge.amount}, Description: ${charge.description}`);
});
}
但它不显示购买情况,而是显示单个订阅。我已经在测试模式下使用我在参数中传递的用户名进行了大量购买。
如何存档?
最佳答案
根据文档,验证付款的最佳方法是监听 Stripe webhook Apis。
实现 Webhook URL允许您实时接收您需要的所有 Stripe 事件。
就您而言,checkout.session.completed似乎是一个很好的解决方案,一旦通过 stripe 验证付款,就会触发此事件。在 Stripe 仪表板中,您可以选择并定义要监听的事件。
该事件将向您发送付款后管理逻辑所需的所有元素(customer_id、session_id 等)
关于javascript - 使用 Stripe API,如何检查用户是否为特定产品付费?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77640528/