Skip to content

Commit

Permalink
Merge pull request #121 from p3ol/feature/subscribe-2023
Browse files Browse the repository at this point in the history
✨feat: add price to switch offer
  • Loading branch information
dackmin authored Aug 24, 2023
2 parents 5c4d239 + 06b17f5 commit 2d21ccf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/customers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,18 @@ export default class Customer {
* @param {String} customerId Customer ID
* @param {String} subscriptionId Subscription ID
* @param {String} offer Offer ID or slug
* @param {Object} opts (optional) Request options
* @param {Object} opts (optional) options { price, ...requestOptions }
* @returns {Promise} { subscription }
*/
async switchSubscriptionOffer (customerId, subscriptionId, offer, opts = {}) {
async switchSubscriptionOffer (customerId, subscriptionId, offer, opts = {}) {
return this.client.request({
...opts,
method: 'POST',
resource: `/subscribe/customers/${assert(customerId)}` +
`/subscriptions/${assert(subscriptionId)}/change`,
body: {
offer,
price: opts.price
},
});
}
Expand Down
33 changes: 33 additions & 0 deletions lib/customers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,39 @@ describe('customers.js', () => {
},
});
});
it('should allow to change a customer\'s subscription offer', async () => {
nock(globalThis.__API_URL__)
.post(
'/subscribe/customers/customer-id/subscriptions/subscription-id' +
'/change',
{
offer: 'premium-offer',
price: 'price_id',
clientId: globalThis.__CLIENT_ID__,
clientSecret: globalThis.__CLIENT_SECRET__,
}
)
.reply(200, {
subscription: {
id: 'subscription-id',
price: 'price_id'
},
});

expect(
await customers.switchSubscriptionOffer(
'customer-id',
'subscription-id',
'premium-offer',
{ price: 'price_id' }
)
).toMatchObject({
subscription: {
id: 'subscription-id',
price: 'price_id'
},
});
});
});

describe('cancelSubscription()', () => {
Expand Down
3 changes: 3 additions & 0 deletions lib/offers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class Offers {
* @param {Array<String>} exclude (optional) Offers slugs to exclude
* @param {String} status (optional) Filter by offers status
* (active, inactive, archived)
* @param {Boolean} sandbox (optional) Live or test mode (default: false)
* @param {Object} opts (optional) Request options
* @returns {Promise} Array of offers object
*/
Expand All @@ -26,6 +27,7 @@ export default class Offers {
include = [],
exclude = [],
status = 'active',
sandbox = false,
opts = {}
) {
return this.client.request({
Expand All @@ -39,6 +41,7 @@ export default class Offers {
include,
exclude,
status,
sandbox
},
});
}
Expand Down
1 change: 1 addition & 0 deletions lib/offers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('offers.js', () => {
page: 1,
count: 10,
status: 'active',
sandbox: false,
})
.reply(200, {
offers: [{
Expand Down

0 comments on commit 2d21ccf

Please sign in to comment.