From 60296d4139091154769cea6f5d3a4dbae7dfe43a Mon Sep 17 00:00:00 2001 From: Alexander Thiemann Date: Wed, 4 Apr 2018 14:09:03 -0700 Subject: [PATCH] flexible and metered billing --- lib/resources/UsageRecords.js | 16 +++++++ lib/stripe.js | 1 + test/resources/Plans.spec.js | 68 ++++++++++++++++++++++++++--- test/resources/UsageRecords.spec.js | 27 ++++++++++++ 4 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 lib/resources/UsageRecords.js create mode 100644 test/resources/UsageRecords.spec.js diff --git a/lib/resources/UsageRecords.js b/lib/resources/UsageRecords.js new file mode 100644 index 0000000000..8230549467 --- /dev/null +++ b/lib/resources/UsageRecords.js @@ -0,0 +1,16 @@ +'use strict'; + +var stripeMethod = require('../StripeMethod'); + +module.exports = require('../StripeResource').extend({ + path: 'subscription_items', + create: function(args) { + var requestPath = args.subscription_item + '/usage_records'; + var reqArgs = Object.assign({}, args); + delete reqArgs.subscription_item; + return stripeMethod({ + method: 'POST', + path: requestPath + }).bind(this)(reqArgs); + } +}); diff --git a/lib/stripe.js b/lib/stripe.js index 3f2f9d252f..5d67234ea2 100644 --- a/lib/stripe.js +++ b/lib/stripe.js @@ -62,6 +62,7 @@ var resources = { SubscriptionItems: require('./resources/SubscriptionItems'), ThreeDSecure: require('./resources/ThreeDSecure'), Sources: require('./resources/Sources'), + UsageRecords: require('./resources/UsageRecords'), // The following rely on pre-filled IDs: CustomerCards: require('./resources/CustomerCards'), diff --git a/test/resources/Plans.spec.js b/test/resources/Plans.spec.js index fa184ae23d..45c04a0fe6 100644 --- a/test/resources/Plans.spec.js +++ b/test/resources/Plans.spec.js @@ -11,7 +11,7 @@ describe('Plans Resource', function() { method: 'GET', url: '/v1/plans/planId1', headers: {}, - data: {}, + data: {} }); }); }); @@ -19,13 +19,66 @@ describe('Plans Resource', function() { describe('create', function() { it('Sends the correct request', function() { stripe.plans.create({ - amount: 200, currency: 'usd', + amount: 200, + currency: 'usd' }); expect(stripe.LAST_REQUEST).to.deep.equal({ method: 'POST', url: '/v1/plans', headers: {}, - data: {amount: 200, currency: 'usd'}, + data: {amount: 200, currency: 'usd'} + }); + }); + + it('Sends the correct request for metered', function() { + stripe.plans.create({ + amount: 200, + currency: 'usd', + usage_type: 'metered' + }); + expect(stripe.LAST_REQUEST).to.deep.equal({ + method: 'POST', + url: '/v1/plans', + headers: {}, + data: {amount: 200, currency: 'usd', usage_type: 'metered'} + }); + }); + + it('Sends the correct request for tiered plans', function() { + stripe.plans.create({ + currency: 'usd', + billing_scheme: 'tiered', + tiers: [{up_to: 123, amount: 100}, {up_to: 'inf', amount: 200}], + tiers_mode: 'volume' + }); + expect(stripe.LAST_REQUEST).to.deep.equal({ + method: 'POST', + url: '/v1/plans', + headers: {}, + data: { + currency: 'usd', + billing_scheme: 'tiered', + tiers: [{up_to: 123, amount: 100}, {up_to: 'inf', amount: 200}], + tiers_mode: 'volume' + } + }); + }); + + it('Sends the correct request for transform usage plans', function() { + stripe.plans.create({ + amount: 200, + currency: 'usd', + transform_usage: {divide_by: 123, round: 'up'} + }); + expect(stripe.LAST_REQUEST).to.deep.equal({ + method: 'POST', + url: '/v1/plans', + headers: {}, + data: { + amount: 200, + currency: 'usd', + transform_usage: {divide_by: 123, round: 'up'} + } }); }); }); @@ -33,13 +86,14 @@ describe('Plans Resource', function() { describe('update', function() { it('Sends the correct request', function() { stripe.plans.update('planId3', { - amount: 1900, currency: 'usd', + amount: 1900, + currency: 'usd' }); expect(stripe.LAST_REQUEST).to.deep.equal({ method: 'POST', url: '/v1/plans/planId3', headers: {}, - data: {amount: 1900, currency: 'usd'}, + data: {amount: 1900, currency: 'usd'} }); }); }); @@ -51,7 +105,7 @@ describe('Plans Resource', function() { method: 'DELETE', url: '/v1/plans/planId4', headers: {}, - data: {}, + data: {} }); }); }); @@ -63,7 +117,7 @@ describe('Plans Resource', function() { method: 'GET', url: '/v1/plans', headers: {}, - data: {}, + data: {} }); }); }); diff --git a/test/resources/UsageRecords.spec.js b/test/resources/UsageRecords.spec.js new file mode 100644 index 0000000000..f14cac308a --- /dev/null +++ b/test/resources/UsageRecords.spec.js @@ -0,0 +1,27 @@ +'use strict'; + +var stripe = require('../testUtils').getSpyableStripe(); +var expect = require('chai').expect; + +describe('UsageRecords Resource', function() { + describe('create', function() { + it('Sends the correct request', function() { + stripe.usageRecords.create({ + subscription_item: 'si_123', + quantity: 123, + timestmap: 123321, + action: 'increment' + }); + expect(stripe.LAST_REQUEST).to.deep.equal({ + method: 'POST', + url: '/v1/subscription_items/si_123/usage_records', + headers: {}, + data: { + quantity: 123, + timestmap: 123321, + action: 'increment' + } + }); + }); + }); +});