🐛 Fixed direct paid signups on Stripe beta (#20215)

ref ONC-35

- customer_update should only be defined where cutomer_id exists and
labs are enabled.
- added additional unit testing
This commit is contained in:
Ronald Langeveld 2024-05-16 08:47:23 +08:00 committed by GitHub
parent fe3d6a1ffe
commit 010e8394aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 80 additions and 2 deletions

View File

@ -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;

View File

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