Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i update a user's subscription date using stripe webhooks?

I'm building a subscription plan in node.js, I read the documentation about how to subscribe a user to a plan, and it was successful.

Stripe's doc states that I have to store an active_until field in the database. It says when something changes use webhook, i know that webhook is like an event.

The real questions are

1) how do I do a repeat the bill every month using active_until? 2) How do I use webhook, I really don't understand.

Here's the code so far. 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('/');

  });
});

How do i Implement the active_until timestamps and the webhook event?

like image 448
Jack Moscovi Avatar asked Oct 24 '15 05:10

Jack Moscovi


2 Answers

active_until is just the name of a database column that you can create on your users table to store the timestamp representing when the user's account expires. The name of the column doesn't matter. You can use any name you want.

In order to verify if a user's subscription is current, Stripe is suggesting that you use logic like this:

If today's date <= user.active_until
  allow them access

Else
  show them an account expired message

The webhook is a request that Stripe's servers make to your server to tell you that something has happened. In this case, the event you are most interested in is invoice.payment_succeeded.

Your webhook will include logic like this:

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

You will also want to respond to other events in case the payment fails, etc.

like image 54
Nick Urban Avatar answered Nov 15 '22 03:11

Nick Urban


You don't need to repeat the bill every month. Stripe will do it for you. Once you subscribed your user to a plan, stripe will charge him till the paid period is over.

Every time stripe charges a customer it will generate a webhook, that is a request on your server to some specified URL. Stripe can generate different webhooks for different reasons.

For example when customer gets charged by subscription, Stripe will send you info about payment.

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

I don't have access to Stripe settings right now, but a remember setting urls for webhooks manually. Select your account name > Account Settings > Webhooks

active_until is just a reminder that customer is still active and have paid services in your system. It needs to be updated when getting webhooks. Stripe docs are really good, so go through them one more time. https://stripe.com/docs/guides/subscriptions

like image 41
Anton F Avatar answered Nov 15 '22 04:11

Anton F