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

Jac*_*ovi 7 webhooks mongoose node.js stripe-payments

我正在node.js中构建订阅计划,我阅读了有关如何订阅用户计划的文档,并且成功了.

Stripe的doc声明我必须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('/');

  });
});
Run Code Online (Sandbox Code Playgroud)

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

小智 2

您无需每个月重复账单。Stripe 会为您做到这一点。一旦您为用户订阅了套餐,stripe 就会向他收费,直到付费期结束。

每次 Stripe 向客户收费时,它都会生成一个 Webhook,即在您的服务器上向某个指定 URL 发出的请求。Stripe 可以出于不同的原因生成不同的 webhook。

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

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

我现在无法访问 Stripe 设置,但请记住手动设置 Webhook 的 url。 选择您的帐户名称 > 帐户设置 > Webhooks

active_until 只是提醒客户仍然活跃并且在您的系统中拥有付费服务。获取 webhooks 时需要更新。Stripe 文档非常好,所以再看一遍。 https://stripe.com/docs/guides/subscriptions