Skip to content

Commit

Permalink
Merge pull request #593 from stripe/remi-fix-upcoming
Browse files Browse the repository at this point in the history
retrieveUpcoming on Invoice can now take one hash as parameter
  • Loading branch information
remi-stripe committed Apr 22, 2019
2 parents f5ed96c + 2c1071d commit f649690
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lib/resources/Invoices.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,29 @@ module.exports = StripeResource.extend({
retrieveUpcoming: stripeMethod({
method: 'GET',
path: function(urlData) {
var url = 'upcoming?customer=' + urlData.customerId;
var url = 'upcoming?';
var 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.invoiceOptions && typeof urlData.invoiceOptions === 'string') {
return url + '&subscription=' + urlData.invoiceOptions;
} else if (urlData.invoiceOptions && typeof urlData.invoiceOptions === 'object') {
return url + '&' + utils.stringifyRequestData(urlData.invoiceOptions);
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: ['customerId', 'optional!invoiceOptions'],
urlParams: ['optional!invoiceOptionsOrCustomerId', 'optional!invoiceOptionsOrSubscriptionId'],
}),

sendInvoice: stripeMethod({
Expand Down
20 changes: 20 additions & 0 deletions test/resources/Invoices.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ describe('Invoices Resource', function() {
});
});

describe('Without a customer id but options', function() {
it('Sends the correct request', function() {
stripe.invoices.retrieveUpcoming({
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', function() {
it('Sends the correct request', function() {
stripe.invoices.retrieveUpcoming('cus_123', 'sub_123',
Expand Down

0 comments on commit f649690

Please sign in to comment.