From c1b72b3997f58a7287292ec807fe854a0693c54f Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 4 Apr 2024 10:17:14 -0400 Subject: [PATCH] Wired up additional payment types with labs flag ref ENG-812 ref https://linear.app/tryghost/issue/ENG-812 --- .../core/server/services/stripe/service.js | 2 ++ ghost/stripe/lib/StripeAPI.js | 9 ++++++-- ghost/stripe/lib/StripeService.js | 3 ++- ghost/stripe/test/unit/lib/StripeAPI.test.js | 21 ++++++++++++++++++- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/ghost/core/core/server/services/stripe/service.js b/ghost/core/core/server/services/stripe/service.js index 0bbeb24db7..3ae213aef0 100644 --- a/ghost/core/core/server/services/stripe/service.js +++ b/ghost/core/core/server/services/stripe/service.js @@ -11,6 +11,7 @@ const {getConfig} = require('./config'); const settingsHelpers = require('../settings-helpers'); const donationService = require('../donations'); const staffService = require('../staff'); +const labs = require('../../../shared/labs'); async function configureApi() { const cfg = getConfig({settingsHelpers, config, urlUtils}); @@ -30,6 +31,7 @@ const debouncedConfigureApi = _.debounce(() => { }, 600); module.exports = new StripeService({ + labs, membersService, models: _.pick(models, [ 'Product', diff --git a/ghost/stripe/lib/StripeAPI.js b/ghost/stripe/lib/StripeAPI.js index c7a271c578..009b28e051 100644 --- a/ghost/stripe/lib/StripeAPI.js +++ b/ghost/stripe/lib/StripeAPI.js @@ -41,14 +41,19 @@ module.exports = class StripeAPI { /** * StripeAPI */ - constructor() { + constructor(deps) { /** @type {Stripe} */ this._stripe = null; this._configured = false; + this.labs = deps.labs; } get PAYMENT_METHOD_TYPES() { - return ['card']; + if (this.labs.isSet('additionalPaymentMethods')) { + return undefined; + } else { + return ['card']; + } } get configured() { diff --git a/ghost/stripe/lib/StripeService.js b/ghost/stripe/lib/StripeService.js index 7728b9c9f5..1ec78f3a1d 100644 --- a/ghost/stripe/lib/StripeService.js +++ b/ghost/stripe/lib/StripeService.js @@ -7,13 +7,14 @@ const {StripeLiveEnabledEvent, StripeLiveDisabledEvent} = require('./events'); module.exports = class StripeService { constructor({ + labs, membersService, donationService, staffService, StripeWebhook, models }) { - const api = new StripeAPI(); + const api = new StripeAPI({labs}); const webhookManager = new WebhookManager({ StripeWebhook, api diff --git a/ghost/stripe/test/unit/lib/StripeAPI.test.js b/ghost/stripe/test/unit/lib/StripeAPI.test.js index 3382fd2814..c720c9a3d4 100644 --- a/ghost/stripe/test/unit/lib/StripeAPI.test.js +++ b/ghost/stripe/test/unit/lib/StripeAPI.test.js @@ -2,12 +2,17 @@ const sinon = require('sinon'); const should = require('should'); const rewire = require('rewire'); const StripeAPI = rewire('../../../lib/StripeAPI'); -const api = new StripeAPI(); describe('StripeAPI', function () { const mockCustomerEmail = 'foo@example.com'; const mockCustomerId = 'cust_mock_123456'; const mockCustomerName = 'Example Customer'; + let mockLabs = { + isSet() { + return false; + } + }; + const api = new StripeAPI({labs: mockLabs}); let mockStripe; @@ -20,6 +25,7 @@ describe('StripeAPI', function () { } } }; + sinon.stub(mockLabs, 'isSet'); const mockStripeConstructor = sinon.stub().returns(mockStripe); StripeAPI.__set__('Stripe', mockStripeConstructor); api.configure({ @@ -35,6 +41,19 @@ describe('StripeAPI', function () { sinon.restore(); }); + it('Sends card as payment method if labs flag not enabled', async function () { + await api.createCheckoutSession('priceId', null, {}); + + should.deepEqual(mockStripe.checkout.sessions.create.firstCall.firstArg.payment_method_types, ['card']); + }); + + it('Sends no payment methods if labs flag is enabled', async function () { + mockLabs.isSet.withArgs('additionalPaymentMethods').returns(true); + await api.createCheckoutSession('priceId', null, {}); + + should.deepEqual(mockStripe.checkout.sessions.create.firstCall.firstArg.payment_method_types, undefined); + }); + it('sends success_url and cancel_url', async function () { await api.createCheckoutSession('priceId', null, {});