如何使用条纹webhook更新用户的订阅日期?

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

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事件?


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

每次分条向客户收费时,它都会生成一个webhook,这是您的服务器上某个指定URL的请求。 Stripe可以根据不同的原因生成不同的webhooks。

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

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

我目前无法访问条纹设置,但是请记住设置手动为webhooks设置网址。 选择您的帐户名称>帐户设置> Webhooks

active_until只是一个提醒,顾客仍然活跃在你的系统有付费服务。 它需要在获取webhook时更新。 条纹文档非常好,所以再次浏览一遍。 https://stripe.com/docs/guides/subscriptions


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

为了验证用户的订阅是否最新,Stripe建议您使用这样的逻辑:

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

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

链接地址: http://www.djcxy.com/p/88515.html

上一篇: How do i update a user's subscription date using stripe webhooks?

下一篇: Fill area between two lines, with high/low and dates