mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-23 10:53:34 +03:00
🐛 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:
parent
fe3d6a1ffe
commit
010e8394aa
@ -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;
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user