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

Remove legacy parameter support in invoices.retrieveUpcoming() #621

Merged
merged 1 commit into from
May 9, 2019
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
66 changes: 12 additions & 54 deletions lib/resources/Invoices.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const StripeResource = require('../StripeResource');
const stripeMethod = StripeResource.method;
const utils = require('../utils');

module.exports = StripeResource.extend({
path: 'invoices',
Expand All @@ -14,6 +13,17 @@ module.exports = StripeResource.extend({
urlParams: ['id'],
}),

listLineItems: stripeMethod({
method: 'GET',
path: '{id}/lines',
urlParams: ['id'],
}),

listUpcomingLineItems: stripeMethod({
method: 'GET',
path: 'upcoming/lines',
}),

markUncollectible: stripeMethod({
method: 'POST',
path: '{id}/mark_uncollectible',
Expand All @@ -26,61 +36,9 @@ module.exports = StripeResource.extend({
urlParams: ['id'],
}),

retrieveLines: stripeMethod({
method: 'GET',
path: '{id}/lines',
urlParams: ['id'],
}),

retrieveUpcoming: stripeMethod({
method: 'GET',
path(urlData) {
let url = 'upcoming?';
let hasParam = false;

// If you pass just a hash with the relevant parameters, including customer id inside.
if (
urlData.invoiceOptionsOrCustomerId &&
typeof urlData.invoiceOptionsOrCustomerId === 'object'
) {
return (
url + utils.stringifyRequestData(urlData.invoiceOptionsOrCustomerId)
);
}

// Legacy implementation where the first parameter is a customer id as a string
if (
urlData.invoiceOptionsOrCustomerId &&
typeof urlData.invoiceOptionsOrCustomerId === 'string'
) {
url = `${url}customer=${urlData.invoiceOptionsOrCustomerId}`;
hasParam = true;
}

// Legacy support where second argument is the subscription id
if (
urlData.invoiceOptionsOrSubscriptionId &&
typeof urlData.invoiceOptionsOrSubscriptionId === 'string'
) {
return `${url + (hasParam ? '&' : '')}subscription=${
urlData.invoiceOptionsOrSubscriptionId
}`;
} else if (
urlData.invoiceOptionsOrSubscriptionId &&
typeof urlData.invoiceOptionsOrSubscriptionId === 'object'
) {
return (
url +
(hasParam ? '&' : '') +
utils.stringifyRequestData(urlData.invoiceOptionsOrSubscriptionId)
);
}
return url;
},
urlParams: [
'optional!invoiceOptionsOrCustomerId',
'optional!invoiceOptionsOrSubscriptionId',
],
path: 'upcoming',
}),

sendInvoice: stripeMethod({
Expand Down
18 changes: 7 additions & 11 deletions test/StripeResource.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ describe('StripeResource', () => {
});

describe('_request', () => {
it('encodes the query string in GET requests', (done) => {
it('encodes the body in GET requests', (done) => {
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
const options = {
host: stripe.getConstant('DEFAULT_HOST'),
path: '/v1/invoices/upcoming',
data: {
customer: 'cus_123',
subscription_items: [
{plan: 'foo', quantity: 2},
{id: 'si_123', deleted: true},
Expand All @@ -62,18 +63,13 @@ describe('StripeResource', () => {
};

const scope = nock(`https://${options.host}`)
.get(options.path)
.query(Object.assign({customer: 'cus_123'}, options.data))
.get(options.path, options.data)
.reply(200, '{}');

realStripe.invoices.retrieveUpcoming(
'cus_123',
options.data,
(err, response) => {
done();
scope.done();
}
);
realStripe.invoices.retrieveUpcoming(options.data, (err, response) => {
done();
scope.done();
});
});

it('encodes the body in POST requests', (done) => {
Expand Down
116 changes: 28 additions & 88 deletions test/resources/Invoices.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ describe('Invoices Resource', () => {
});
});

describe('retrieveLines', () => {
describe('listLineItems', () => {
it('Sends the correct request', () => {
stripe.invoices.retrieveLines('in_123');
stripe.invoices.listLineItems('in_123');
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/invoices/in_123/lines',
Expand All @@ -77,101 +77,41 @@ describe('Invoices Resource', () => {
});

describe('retrieveUpcoming', () => {
describe('With just a customer ID', () => {
it('Sends the correct request', () => {
stripe.invoices.retrieveUpcoming('cus_123');
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/invoices/upcoming?customer=cus_123',
headers: {},
data: {},
});
});
});

describe('With a subscription ID in addition to a customer ID', () => {
it('Sends the correct request', () => {
stripe.invoices.retrieveUpcoming('cus_123', 'sub_123');
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/invoices/upcoming?customer=cus_123&subscription=sub_123',
headers: {},
data: {},
});
});
});

describe('With an options object that includes `subscription_items`', () => {
it('Sends the correct request', () => {
stripe.invoices.retrieveUpcoming('cus_123', {
subscription_items: [{plan: 'potato'}, {plan: 'rutabaga'}],
});

expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url:
'/v1/invoices/upcoming?customer=cus_123&' +
'subscription_items[0][plan]=potato&subscription_items[1][plan]=rutabaga',
headers: {},
data: {},
});
it('Sends the correct request', () => {
stripe.invoices.retrieveUpcoming({
customer: 'cus_abc',
subscription_items: [{plan: 'potato'}, {plan: 'rutabaga'}],
});
});

describe('Without a customer id but options', () => {
it('Sends the correct request', () => {
stripe.invoices.retrieveUpcoming({
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/invoices/upcoming',
headers: {},
data: {
customer: 'cus_abc',
subscription_items: [{plan: 'potato'}, {plan: 'rutabaga'}],
});

expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url:
'/v1/invoices/upcoming?customer=cus_abc&' +
'subscription_items[0][plan]=potato&subscription_items[1][plan]=rutabaga',
headers: {},
data: {},
});
},
});
});
});

describe('With an options object that includes `subscription_items` in addition to a subscription ID', () => {
it('Sends the correct request', () => {
stripe.invoices.retrieveUpcoming('cus_123', 'sub_123', {
subscription_items: [
{plan: 'potato'},
{plan: 'rutabaga'},
{id: 'SOME_ID', deleted: true},
],
subscription_prorate: true,
});

expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/invoices/upcoming?customer=cus_123&subscription=sub_123',
headers: {},
data: {
subscription_items: [
{plan: 'potato'},
{plan: 'rutabaga'},
{id: 'SOME_ID', deleted: true},
],
subscription_prorate: true,
},
});
describe('listUpcomingLineItems', () => {
it('Sends the correct request', () => {
stripe.invoices.listUpcomingLineItems({
customer: 'cus_abc',
subscription_items: [{plan: 'potato'}, {plan: 'rutabaga'}],
limit: 5,
});
});

describe('With a options object in addition to a customer ID', () => {
it('Sends the correct request', () => {
stripe.invoices.retrieveUpcoming('cus_123', {plan: 'planId123'});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/invoices/upcoming?customer=cus_123&plan=planId123',
headers: {},
data: {},
});
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/invoices/upcoming/lines',
headers: {},
data: {
customer: 'cus_abc',
subscription_items: [{plan: 'potato'}, {plan: 'rutabaga'}],
limit: 5,
},
});
});
});
Expand Down