【发布时间】:2020-12-12 03:00:26
【问题描述】:
当 checkout.session.completed 发生时,我正在使用 stripe webhook 将购买的产品添加到我的 sql 数据库中,这是我的代码:
router.post('/webhook', bodyParser.raw({ type: 'application/json' }), (request, response) => {
const sig = request.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
} catch (err) {
return response.status(400).send(`Webhook Error: ${err.message}`);
}
// Handle the checkout.session.completed event
if (event.type === 'checkout.session.completed') {
const session = event.data.object;
//here i want to increment the product's buy counter by 1 in the database but stripe only gives name and price, which are not unique, so i need the product id
}
// Return a response to acknowledge receipt of the event
response.json({ received: true });
});
我正在使用 Sequelize,所以当 checkout.session.completed 发生时我需要执行类似的操作:
products.findOne({
where: product_id: the purchased product id
}).then(product => {
product.update({ buy_counter: product.buy_counter++})
}
我可以通过任何方式在 Stripe 会话中找到产品的产品 ID
【问题讨论】:
标签: javascript node.js express sequelize.js stripe-payments