Skip to content

Commit

Permalink
🐛 Fixed direct paid signups on Stripe beta (#20215)
Browse files Browse the repository at this point in the history
ref ONC-35

- customer_update should only be defined where cutomer_id exists and
labs are enabled.
- added additional unit testing
  • Loading branch information
ronaldlangeveld authored May 16, 2024
1 parent fe3d6a1 commit 010e839
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
10 changes: 8 additions & 2 deletions ghost/stripe/lib/StripeAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,6 @@ module.exports = class StripeAPI {
automatic_tax: {
enabled: this._config.enableAutomaticTax
},
customer_update: this._config.enableAutomaticTax ? {address: 'auto'} : {},
metadata,
discounts,
/*
Expand All @@ -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);

Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down
72 changes: 72 additions & 0 deletions ghost/stripe/test/unit/lib/StripeAPI.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});

0 comments on commit 010e839

Please sign in to comment.