如何使用条纹webhooks更新用户的订阅date?

我在node.js中构build了一个订阅计划,我阅读了关于如何订阅用户到计划的文档,并且成功了。

Stripe的文档声明我必须在数据库中存储一个active_until字段。 它说什么时候改变使用webhook,我知道webhook就像一个事件。

真正的问题是

1)如何使用active_until每月重复帐单? 2)我如何使用webhook,我真的不明白。

这是迄今为止的代码。 var User = new mongoose.Schema({email:String,stripe:{customerId:String,plan:String}

 }); //payment route router.post('/billing/:plan_name', function(req, res, next) { var plan = req.params.plan_name; var stripeToken = req.body.stripeToken; console.log(stripeToken); if (!stripeToken) { req.flash('errors', { msg: 'Please provide a valid card.' }); return res.redirect('/awesome'); } User.findById({ _id: req.user._id}, function(err, user) { if (err) return next(err); stripe.customers.create({ source: stripeToken, // obtained with Stripe.js plan: plan, email: user.email }).then(function(customer) { user.stripe.plan = customer.plan; user.stripe.customerId = customer.id; console.log(customer); user.save(function(err) { console.log("Success"); if (err) return next(err); return next(null); }); }).catch(function(err) { // Deal with an error }); return res.redirect('/'); }); }); 

我如何实现active_until时间戳和webhook事件?

你不需要每月重复账单。 条纹将为你做。 一旦你订阅你的用户计划,条纹将收取费用直到付款期限结束。

每次分条费用的客户将产生一个webhook,这是一个请求在您的服务器上的某个指定的url。 Stripe可以根据不同的原因生成不同的webhooks。

例如,当客户通过订阅收费时,Stripe会向您发送有关付款的信息。

 router.post('/billing/catch_paid_invoice', function(req, res) { // Here you parse JSON data from Stripe }): 

我现在无法访问条纹设置,但是记得手动设置网页钩子。 select您的帐户名称>帐户设置> Webhooks

active_until只是提示客户仍然活跃,并已在您的系统中支付服务。 它需要得到更新时,WebHooks。 条纹文档非常好,所以再次浏览一遍。 https://stripe.com/docs/guides/subscriptions

active_until只是您可以在用户表上创build的数据库列的名称,用于存储表示用户帐户何时到期的时间戳。 列的名称并不重要。 你可以使用任何你想要的名字。

为了validation用户的订阅是否是最新的,Stripebuild议您使用这样的逻辑:

 If today's date <= user.active_until allow them access Else show them an account expired message 

webhook是Stripe服务器向服务器发出的一个请求,告诉你发生了什么事情。 在这种情况下,您最感兴趣的事件是invoice.payment_succeeded

你的webhook将包含这样的逻辑:

 if event type is "invoice.payment_succeeded" then update user.active_until to be equal to today's date + 1 month 

如果付款失败等,您也会想要回复其他事件