8

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?

dmulter
  • 2,608
  • 3
  • 15
  • 24
Jack Moscovi
  • 2,195
  • 7
  • 30
  • 49

2 Answers2

3

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.

Nick Urban
  • 3,568
  • 2
  • 22
  • 36
2

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

Anton F
  • 328
  • 1
  • 8
  • 1
    Thank you for your detailed explanation, I already added all the events on stripe website, but I'm really confuse, do I need router.post for web hooks? – Jack Moscovi Oct 24 '15 at 09:58
  • and is it possible if you could add a code of how do i add active_until? does it need to timestamps or boolean? – Jack Moscovi Oct 24 '15 at 10:00
  • and one more thing How do i test the webhooks? – Jack Moscovi Oct 24 '15 at 10:09
  • yes, you need to catch post requests from stripe. Set up router.post for url you specified in webhook. Then dump everything it get to see what to do with it. if i remember correctly, stripe have button somewhere that allows you to send any webhook. – Anton F Oct 24 '15 at 10:23
  • Active until doesnt have anything to do with stripe. stripe will charge subscribed customer anyway. You set active until when creating new sybscription just ti know until what time some subscription is active. Its up to you to determine format – Anton F Oct 24 '15 at 10:27
  • Can you code later on? so i could accept your answer – Jack Moscovi Oct 24 '15 at 14:05
  • Can you put the code ? Its been 2 days and I need help – Jack Moscovi Oct 28 '15 at 11:18