Ad

Stripe / Node.js : How Retrieve Stripe Subscription Safely + Increment 1

I just started exploring Stripe for handling payments in node.js. I want to create a payment system that goes as following:

  1. I create a workspace and I start a Stripe subscription of 10 dollars / month.
  2. When someone joins my workspace it will cost me 10 dollar / month extra.

So, when I want to add a person to my subscription. How would I handle this? I found the below function, but I was wondering two things:

  1. How do I add one person to this subscription? It now says quantity: 2, but how do I simply increment 1 with every user?

  2. in the example below I use ID 'sub_6OZnwv0DZBrrPt' to retrieve this Stripe subscription. I was wondering from where I can get this ID? I could save this subscription ID in the workspace mongo database document after I created this subscription, but I'm not sure if it is safe to keep it like that in my database? Let me know if you have any suggestions.

this is the function to update a subscription

stripe.subscriptions.update(
    'sub_6OZnwv0DZBryPt',
    { quantity: 2 },
    (err, subscription) => {
      // asynchronously called
    }
  );
Ad

Answer

On 1.) you need to retrieve the current subscription based on the stored ID. This can be done as:

stripe.subscriptions.retrieve(
  "sub_6OZnwv0DZBryPt",
  function(err, subscription) {
    // asynchronously called
  }
);

The subscription object will have information about the current quantity (see the example response and doc on the subscription object). Which leads to your second question.

To retrieve the subscription you need to store the ID only. It's safe to do so, the ID is meaningless to others unless they have your test / live keys as well. Be sure you secure those keys, and feel free to store IDs like subscription_id, customer_id, etc.

Ad
source: stackoverflow.com
Ad