From a138982f0e185f4e93cd4f2fb19c7cfefcebd500 Mon Sep 17 00:00:00 2001 From: Remi Jannel Date: Sat, 2 Feb 2019 11:08:17 -0800 Subject: [PATCH] Add support for the Subscription Schedule resource --- .../SubscriptionScheduleRevisions.js | 20 +++ lib/resources/SubscriptionSchedules.js | 43 +++++++ lib/stripe.js | 2 + test/resources/Checkout/Sessions.spec.js | 29 +---- test/resources/PaymentIntents.spec.js | 2 +- test/resources/SubscriptionSchedule.spec.js | 117 ++++++++++++++++++ .../SubscriptionScheduleRevision.spec.js | 44 +++++++ 7 files changed, 232 insertions(+), 25 deletions(-) create mode 100644 lib/resources/SubscriptionScheduleRevisions.js create mode 100644 lib/resources/SubscriptionSchedules.js create mode 100644 test/resources/SubscriptionSchedule.spec.js create mode 100644 test/resources/SubscriptionScheduleRevision.spec.js diff --git a/lib/resources/SubscriptionScheduleRevisions.js b/lib/resources/SubscriptionScheduleRevisions.js new file mode 100644 index 0000000000..c230e37379 --- /dev/null +++ b/lib/resources/SubscriptionScheduleRevisions.js @@ -0,0 +1,20 @@ +'use strict'; + +var StripeResource = require('../StripeResource'); + +/** + * SubscriptionScheduleRevisions is a unique resource in that, upon instantiation, + * requires a subscription schedule id, and therefore each of its methods only + * require the scheduleId argument. + * + * This streamlines the API specifically for the case of accessing a revision + * on a returned subscription schedule object. + * + * E.g. scheduleObject.revisions.retrieve(revisionId) + * (As opposed to the also-supported stripe.subscriptionSchedules.retrieveRevision(scheduleId, + * revisionId)) + */ +module.exports = StripeResource.extend({ + path: 'subscription_schedules/{scheduleId}/revisions', + includeBasic: ['list', 'retrieve',], +}); diff --git a/lib/resources/SubscriptionSchedules.js b/lib/resources/SubscriptionSchedules.js new file mode 100644 index 0000000000..0f838bdbb1 --- /dev/null +++ b/lib/resources/SubscriptionSchedules.js @@ -0,0 +1,43 @@ +'use strict'; + +var StripeResource = require('../StripeResource'); +var stripeMethod = StripeResource.method; + +module.exports = StripeResource.extend({ + + path: 'subscription_schedules', + + includeBasic: [ + 'create', 'list', 'retrieve', 'update', + 'setMetadata', 'getMetadata', + ], + + release: stripeMethod({ + method: 'POST', + path: '/{id}/release', + urlParams: ['id'], + }), + + cancel: stripeMethod({ + method: 'POST', + path: '/{id}/cancel', + urlParams: ['id'], + }), + + /** + * SubscriptionSchedules: SubscriptionScheduleRevisions methods + */ + + listRevisions: stripeMethod({ + method: 'GET', + path: '/{scheduleId}/revisions', + urlParams: ['scheduleId'], + methodType: 'list', + }), + + retrieveRevision: stripeMethod({ + method: 'GET', + path: '/{scheduleId}/revisions/{revisionId}', + urlParams: ['scheduleId', 'revisionId'], + }), +}); diff --git a/lib/stripe.js b/lib/stripe.js index 8943a6ae53..f86782b203 100644 --- a/lib/stripe.js +++ b/lib/stripe.js @@ -66,6 +66,7 @@ var resources = { Skus: require('./resources/SKUs'), Sources: require('./resources/Sources'), SubscriptionItems: require('./resources/SubscriptionItems'), + SubscriptionSchedules: require('./resources/SubscriptionSchedules'), Subscriptions: require('./resources/Subscriptions'), ThreeDSecure: require('./resources/ThreeDSecure'), Tokens: require('./resources/Tokens'), @@ -81,6 +82,7 @@ var resources = { CustomerCards: require('./resources/CustomerCards'), CustomerSubscriptions: require('./resources/CustomerSubscriptions'), Persons: require('./resources/Persons'), + SubscriptionScheduleRevisions: require('./resources/SubscriptionScheduleRevisions'), TransferReversals: require('./resources/TransferReversals'), // Namespaced resources diff --git a/test/resources/Checkout/Sessions.spec.js b/test/resources/Checkout/Sessions.spec.js index 0ff5bd3b80..94a248f1ba 100644 --- a/test/resources/Checkout/Sessions.spec.js +++ b/test/resources/Checkout/Sessions.spec.js @@ -8,8 +8,7 @@ describe('Checkout', function () { describe('Sessions Resource', function () { describe('create', function() { it('Sends the correct request', function() { - stripe.checkout.sessions.create({ - allowed_source_types: ['card'], + var params = { cancel_url: 'https://stripe.com/cancel', client_reference_id: '1234', line_items: [ @@ -27,34 +26,16 @@ describe('Checkout', function () { payment_intent_data: { receipt_email: 'test@stripe.com', }, + payment_method_types: ['card'], success_url: 'https://stripe.com/success', - }); + }; + stripe.checkout.sessions.create(params); expect(stripe.LAST_REQUEST).to.deep.equal({ method: 'POST', url: '/v1/checkout/sessions', headers: {}, - data: { - allowed_source_types: ['card'], - cancel_url: 'https://stripe.com/cancel', - client_reference_id: '1234', - line_items: [ - { - amount: 123, - currency: 'usd', - description: 'item 1', - images: [ - 'https://stripe.com/img1', - ], - name: 'name', - quantity: 2, - }, - ], - payment_intent_data: { - receipt_email: 'test@stripe.com', - }, - success_url: 'https://stripe.com/success', - }, + data: params, }); }); }); diff --git a/test/resources/PaymentIntents.spec.js b/test/resources/PaymentIntents.spec.js index 7712b7bfce..81f8f2c639 100644 --- a/test/resources/PaymentIntents.spec.js +++ b/test/resources/PaymentIntents.spec.js @@ -9,9 +9,9 @@ describe('Payment Intents Resource', function() { describe('create', function() { it('Sends the correct request', function() { var params = { - allowed_source_types: ['card'], amount: 200, currency: 'usd', + payment_method_types: ['card'], }; stripe.paymentIntents.create(params); expect(stripe.LAST_REQUEST).to.deep.equal({ diff --git a/test/resources/SubscriptionSchedule.spec.js b/test/resources/SubscriptionSchedule.spec.js new file mode 100644 index 0000000000..b38246c129 --- /dev/null +++ b/test/resources/SubscriptionSchedule.spec.js @@ -0,0 +1,117 @@ +'use strict'; + +var stripe = require('../../testUtils').getSpyableStripe(); +var expect = require('chai').expect; + +var SCHEDULE_TEST_ID = 'sub_sched_123'; +var REVISION_TEST_ID = 'sub_sched_rev_123'; + +describe('Subscription Schedule Resource', function() { + describe('cancel', function() { + it('Sends the correct request', function() { + var data = { + invoice_now: true, + } + stripe.subscriptionSchedules.cancel(SCHEDULE_TEST_ID, data); + expect(stripe.LAST_REQUEST).to.deep.equal({ + method: 'POST', + url: '/v1/subscription_schedules/' + SCHEDULE_TEST_ID + '/cancel', + data: data, + headers: {}, + }); + }); + }); + + describe('create', function() { + it('Sends the correct request', function() { + var data = { + customer: 'cus_123', + }; + stripe.subscriptionSchedules.create(data); + expect(stripe.LAST_REQUEST).to.deep.equal({ + method: 'POST', + url: '/v1/subscription_schedules', + data: data, + headers: {}, + }); + }); + }); + + describe('list', function() { + it('Sends the correct request', function() { + stripe.subscriptionSchedules.list(); + expect(stripe.LAST_REQUEST).to.deep.equal({ + method: 'GET', + url: '/v1/subscription_schedules', + data: {}, + headers: {}, + }); + }); + }); + + describe('release', function() { + it('Sends the correct request', function() { + var data = { + preserve_cancel_date: true, + } + stripe.subscriptionSchedules.release(SCHEDULE_TEST_ID, data); + expect(stripe.LAST_REQUEST).to.deep.equal({ + method: 'POST', + url: '/v1/subscription_schedules/' + SCHEDULE_TEST_ID + '/release', + data: data, + headers: {}, + }); + }); + }); + + describe('retrieve', function() { + it('Sends the correct request', function() { + stripe.subscriptionSchedules.retrieve(SCHEDULE_TEST_ID); + expect(stripe.LAST_REQUEST).to.deep.equal({ + method: 'GET', + url: '/v1/subscription_schedules/sub_sched_123', + data: {}, + headers: {}, + }); + }); + }); + + describe('update', function() { + it('Sends the correct request', function() { + var data = {metadata: {key: 'value'}}; + stripe.subscriptionSchedules.update(SCHEDULE_TEST_ID, data); + expect(stripe.LAST_REQUEST).to.deep.equal({ + method: 'POST', + url: '/v1/subscription_schedules/' + SCHEDULE_TEST_ID, + data: data, + headers: {}, + }); + }); + }); + + describe('Revision methods', function() { + describe('retrieveRevision', function() { + it('Sends the correct request', function() { + stripe.subscriptionSchedules.retrieveRevision(SCHEDULE_TEST_ID, REVISION_TEST_ID); + expect(stripe.LAST_REQUEST).to.deep.equal({ + method: 'GET', + url: '/v1/subscription_schedules/' + SCHEDULE_TEST_ID + '/revisions/' + REVISION_TEST_ID, + headers: {}, + data: {}, + }); + }); + }); + + describe('listRevisions', function() { + it('Sends the correct request', function() { + stripe.subscriptionSchedules.listRevisions(SCHEDULE_TEST_ID); + expect(stripe.LAST_REQUEST).to.deep.equal({ + method: 'GET', + url: '/v1/subscription_schedules/' + SCHEDULE_TEST_ID + '/revisions', + headers: {}, + data: {}, + }); + }); + }); + }); +}); diff --git a/test/resources/SubscriptionScheduleRevision.spec.js b/test/resources/SubscriptionScheduleRevision.spec.js new file mode 100644 index 0000000000..bbc76a5e1b --- /dev/null +++ b/test/resources/SubscriptionScheduleRevision.spec.js @@ -0,0 +1,44 @@ +'use strict'; + +var resources = require('../../lib/stripe').resources; +var stripe = require('../../testUtils').getSpyableStripe(); +var expect = require('chai').expect; + +var SCHEDULE_TEST_ID = 'sub_sched_123'; +var REVISION_TEST_ID = 'sub_sched_rev_123'; + +// Create new SubscriptionScheduleRevision instance with pre-filled scheduleId: +var revision = new resources.SubscriptionScheduleRevisions( + stripe, + {scheduleId: SCHEDULE_TEST_ID} +); + +// Use spy from existing resource: +revision._request = stripe.customers._request; + +describe('SubscriptionScheduleRevision Resource', function() { + describe('list', function() { + it('Sends the correct request', function() { + revision.list(); + expect(stripe.LAST_REQUEST).to.deep.equal({ + method: 'GET', + url: '/v1/subscription_schedules/' + SCHEDULE_TEST_ID + '/revisions', + data: {}, + headers: {}, + }); + }); + }); + + describe('retrieve', function() { + it('Sends the correct request', function() { + revision.retrieve(REVISION_TEST_ID); + expect(stripe.LAST_REQUEST).to.deep.equal({ + method: 'GET', + url: '/v1/subscription_schedules/' + SCHEDULE_TEST_ID + '/revisions/' + REVISION_TEST_ID, + data: {}, + headers: {}, + }); + }); + }); +}); +