From 010e8394aaeac7b429bf5af6ab29717cbb26a3f1 Mon Sep 17 00:00:00 2001 From: Ronald Langeveld Date: Thu, 16 May 2024 08:47:23 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20direct=20paid=20signups?= =?UTF-8?q?=20on=20Stripe=20beta=20(#20215)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ref ONC-35 - customer_update should only be defined where cutomer_id exists and labs are enabled. - added additional unit testing --- ghost/stripe/lib/StripeAPI.js | 10 ++- ghost/stripe/test/unit/lib/StripeAPI.test.js | 72 ++++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/ghost/stripe/lib/StripeAPI.js b/ghost/stripe/lib/StripeAPI.js index 576f885359..504b8b0c1f 100644 --- a/ghost/stripe/lib/StripeAPI.js +++ b/ghost/stripe/lib/StripeAPI.js @@ -475,7 +475,6 @@ module.exports = class StripeAPI { automatic_tax: { enabled: this._config.enableAutomaticTax }, - customer_update: this._config.enableAutomaticTax ? {address: 'auto'} : {}, metadata, discounts, /* @@ -498,6 +497,10 @@ module.exports = class StripeAPI { stripeSessionOptions.customer_email = customerEmail; } + if (customerId && this._config.enableAutomaticTax) { + stripeSessionOptions.customer_update = {address: 'auto'}; + } + // @ts-ignore const session = await this._stripe.checkout.sessions.create(stripeSessionOptions); @@ -527,7 +530,6 @@ module.exports = class StripeAPI { automatic_tax: { enabled: this._config.enableAutomaticTax }, - customer_update: this._config.enableAutomaticTax ? {address: 'auto'} : {}, metadata, customer: customer ? customer.id : undefined, customer_email: !customer && customerEmail ? customerEmail : undefined, @@ -548,6 +550,10 @@ module.exports = class StripeAPI { }] }; + if (customer && this._config.enableAutomaticTax) { + stripeSessionOptions.customer_update = {address: 'auto'}; + } + // @ts-ignore const session = await this._stripe.checkout.sessions.create(stripeSessionOptions); return session; diff --git a/ghost/stripe/test/unit/lib/StripeAPI.test.js b/ghost/stripe/test/unit/lib/StripeAPI.test.js index 61d7d942d6..9d87642764 100644 --- a/ghost/stripe/test/unit/lib/StripeAPI.test.js +++ b/ghost/stripe/test/unit/lib/StripeAPI.test.js @@ -341,5 +341,77 @@ describe('StripeAPI', function () { should.deepEqual(result, mockSubscription); }); + + describe('createCheckoutSetupSession automatic tax flag', function () { + beforeEach(function () { + mockStripe = { + checkout: { + sessions: { + create: sinon.stub().resolves() + } + }, + customers: { + create: sinon.stub().resolves() + } + }; + sinon.stub(mockLabs, 'isSet'); + mockLabs.isSet.withArgs('stripeAutomaticTax').returns(true); + const mockStripeConstructor = sinon.stub().returns(mockStripe); + StripeAPI.__set__('Stripe', mockStripeConstructor); + api.configure({ + checkoutSessionSuccessUrl: '/success', + checkoutSessionCancelUrl: '/cancel', + checkoutSetupSessionSuccessUrl: '/setup-success', + checkoutSetupSessionCancelUrl: '/setup-cancel', + secretKey: '', + enableAutomaticTax: true + }); + }); + + afterEach(function () { + sinon.restore(); + }); + + it('createCheckoutSession adds customer_update if automatic tax flag is enabled and customer is not undefined', async function () { + const mockCustomer = { + id: mockCustomerId, + customer_email: mockCustomerEmail, + name: 'Example Customer' + }; + + await api.createCheckoutSession('priceId', mockCustomer, { + trialDays: null + }); + should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.customer_update); + }); + + it('createCheckoutSession does not add customer_update if automatic tax flag is enabled and customer is undefined', async function () { + await api.createCheckoutSession('priceId', undefined, { + trialDays: null + }); + should.not.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.customer_update); + }); + + it('createCheckoutSession does not add customer_update if automatic tax flag is disabled', async function () { + const mockCustomer = { + id: mockCustomerId, + customer_email: mockCustomerEmail, + name: 'Example Customer' + }; + // set enableAutomaticTax: false + api.configure({ + checkoutSessionSuccessUrl: '/success', + checkoutSessionCancelUrl: '/cancel', + checkoutSetupSessionSuccessUrl: '/setup-success', + checkoutSetupSessionCancelUrl: '/setup-cancel', + secretKey: '', + enableAutomaticTax: false + }); + await api.createCheckoutSession('priceId', mockCustomer, { + trialDays: null + }); + should.not.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.customer_update); + }); + }); }); });