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); + }); + }); }); });