Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flexible/Metered Billing API support #444

Merged
merged 1 commit into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/resources/UsageRecords.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
1 change: 1 addition & 0 deletions lib/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
68 changes: 61 additions & 7 deletions test/resources/Plans.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,89 @@ describe('Plans Resource', function() {
method: 'GET',
url: '/v1/plans/planId1',
headers: {},
data: {},
data: {}
});
});
});

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'}
}
});
});
});

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'}
});
});
});
Expand All @@ -51,7 +105,7 @@ describe('Plans Resource', function() {
method: 'DELETE',
url: '/v1/plans/planId4',
headers: {},
data: {},
data: {}
});
});
});
Expand All @@ -63,7 +117,7 @@ describe('Plans Resource', function() {
method: 'GET',
url: '/v1/plans',
headers: {},
data: {},
data: {}
});
});
});
Expand Down
27 changes: 27 additions & 0 deletions test/resources/UsageRecords.spec.js
Original file line number Diff line number Diff line change
@@ -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'
}
});
});
});
});