Giter Club home page Giter Club logo

subscription's Introduction

subscription

NodeJS library to simplify paid subscriptions with Stripe and LevelDB.

Install

$ npm install subscription

Usage

Define a new subscription:

subscription = require('subscription')('stripe-api-key', 'leveldb-path')

subscription.service.define('atlas magazine', { 'price': 1000, period: '1 month', currency: 'usd' }, function (error, atlas) {

  subscription.priceOf(atlas, '1 month')
  // => 1000 (Ten dollars)

    subscription.priceOf(atlas, '1 year')
  // => 12000 (One Twenty Dollars)

})

Purchase a subscription:

options = {
  customer: '[email protected]',
  length: '1 year',
  token: 'tok_2oWvm6yRBFSMSh' // obtain it with Stripe.js
}

subscription.purchase('atlas magazine', options, function (error, purchase) {
  purchase.amount
  // => 12000

  purchase.expires_ts
  // => 1390912838816
})

Validate a subscription:

subscription.has('[email protected]', 'atlas magazine', function (error, has) {
  has
  // => true
})

Get remaining subscription of a customer:

subscription.remaining('[email protected]', 'atlas magazine', function (error, remaining) {
  remaining
  // => 8035200000 (3 months)
})

List subscriptions of a user:

subscription.subscriptionsOf('[email protected]', function (error, subs) {
  subs
  // => ['atlas magazine']
})

Extend a subscription:

subscription.purchase.extension('atlas magazine', { customer: '[email protected]', length: '2 years', token: token }, function (error, purchase) {
  purchase.amount
  // => 24000
});

Upgrade a subscription:

subscription.purchase.upgrade({ customer: '[email protected]', from: 'cheaper', to: 'more expensive service', token: token }, function (error, purchase) {
  if (error) return callback(error);
});

Debugging

Verbose:

$ DEBUG=subscription:* npm test

Less Verbose:

$ DEBUG=subscription:fatal,subscription:purchase

Running Tests

$ API_KEY=sk_test_FIvJu2hkZszNIFzGgVNAqo2x npm test

More Docs

  • test.js
  • english-time: The library used for parsing time inputs.

subscription's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.