From 0268bee13e1ac6339b3c4da224e12694866ea6f6 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 26 Jan 2021 11:26:28 +0000 Subject: [PATCH] Fixed checks for if Stripe is configured no-issue --- .../lib/controllers/router/index.js | 2 +- .../lib/repositories/member/index.js | 20 +++++++++---------- .../lib/services/stripe-api/index.js | 6 ++++++ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/ghost/members-api/lib/controllers/router/index.js b/ghost/members-api/lib/controllers/router/index.js index d6e09da2c6..54bc8c489e 100644 --- a/ghost/members-api/lib/controllers/router/index.js +++ b/ghost/members-api/lib/controllers/router/index.js @@ -33,7 +33,7 @@ module.exports = class RouterController { } async ensureStripe(_req, res, next) { - if (!this._stripeAPIService) { + if (!this._stripeAPIService.configured) { res.writeHead(400); return res.end('Stripe not configured'); } diff --git a/ghost/members-api/lib/repositories/member/index.js b/ghost/members-api/lib/repositories/member/index.js index 3bbcfb276f..17c7f001db 100644 --- a/ghost/members-api/lib/repositories/member/index.js +++ b/ghost/members-api/lib/repositories/member/index.js @@ -125,8 +125,8 @@ module.exports = class MemberRepository { } async linkStripeCustomer(data, options) { - if (!this._stripeAPIService) { - return; + if (!this._stripeAPIService.configured) { + throw new Error('Cannot link Stripe Customer with no Stripe Connection'); } const customer = await this._stripeAPIService.getCustomer(data.customer_id); @@ -151,8 +151,8 @@ module.exports = class MemberRepository { } async linkSubscription(data, options) { - if (!this._stripeAPIService) { - return; + if (!this._stripeAPIService.configured) { + throw new Error('Cannot link Stripe Subscription with no Stripe Connection'); } const member = await this._Member.findOne({ id: data.id @@ -205,8 +205,8 @@ module.exports = class MemberRepository { } async updateSubscription(data, options) { - if (!this._stripeAPIService) { - return; + if (!this._stripeAPIService.configured) { + throw new Error('Cannot update Stripe Subscription with no Stripe Connection'); } const member = await this._Member.findOne({ id: data.id @@ -241,8 +241,8 @@ module.exports = class MemberRepository { } async setComplimentarySubscription(data, options) { - if (!this._stripeAPIService) { - return; + if (!this._stripeAPIService.configured) { + throw new Error('Cannot update Stripe Subscription with no Stripe Connection'); } const member = await this._Member.findOne({ id: data.id @@ -323,8 +323,8 @@ module.exports = class MemberRepository { } async cancelComplimentarySubscription(data) { - if (!this._stripeAPIService) { - return; + if (!this._stripeAPIService.configured) { + throw new Error('Cannot cancel Complimentary Subscription with no Stripe Connection'); } const member = await this._Member.findOne({ diff --git a/ghost/members-api/lib/services/stripe-api/index.js b/ghost/members-api/lib/services/stripe-api/index.js index ae6dd5745c..3af2743baa 100644 --- a/ghost/members-api/lib/services/stripe-api/index.js +++ b/ghost/members-api/lib/services/stripe-api/index.js @@ -47,11 +47,16 @@ module.exports = class StripeAPIService { */ constructor({config, logger}) { this.logging = logger; + this._configured = false; if (config.secretKey) { this.configure(config); } } + get configured() { + return this._configured; + } + configure(config) { this._stripe = new Stripe(config.secretKey); this._config = config; @@ -61,6 +66,7 @@ module.exports = class StripeAPIService { } else { this._rateLimitBucket = new LeakyBucket(EXPECTED_API_EFFICIENCY * 100, 1); } + this._configured = true; } /**