0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00

Fixed trial days validation for stripe checkout

refs e26c977c66

- updates check for valid non null trial days before creating checkout session
This commit is contained in:
Rishabh 2022-08-09 09:07:16 +05:30 committed by Rishabh Garg
parent ce80d250bf
commit da0254846b
2 changed files with 23 additions and 3 deletions

View file

@ -385,7 +385,7 @@ module.exports = class StripeAPI {
* `trial_from_plan` is deprecated. * `trial_from_plan` is deprecated.
* Replaces it in favor of custom trial period days stored in Ghost * Replaces it in favor of custom trial period days stored in Ghost
*/ */
if (!isNaN(options.trialDays)) { if (typeof options.trialDays === 'number' && options.trialDays > 0) {
delete subscriptionData.trial_from_plan; delete subscriptionData.trial_from_plan;
subscriptionData.trial_period_days = options.trialDays; subscriptionData.trial_period_days = options.trialDays;
} }

View file

@ -43,7 +43,7 @@ describe('StripeAPI', function () {
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.cancel_url); should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.cancel_url);
}); });
it('createCheckoutSetupSession uses trialDays', async function (){ it('createCheckoutSession sets valid trialDays', async function (){
await api.createCheckoutSession('priceId', null, { await api.createCheckoutSession('priceId', null, {
trialDays: 12 trialDays: 12
}); });
@ -53,11 +53,31 @@ describe('StripeAPI', function () {
should.equal(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_period_days, 12); should.equal(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_period_days, 12);
}); });
it('createCheckoutSetupSession uses trial_from_plan without trialDays', async function (){ it('createCheckoutSession uses trial_from_plan without trialDays', async function (){
await api.createCheckoutSession('priceId', null, {}); await api.createCheckoutSession('priceId', null, {});
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_from_plan); should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_from_plan);
should.equal(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_from_plan, true); should.equal(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_from_plan, true);
should.not.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_period_days); should.not.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_period_days);
}); });
it('createCheckoutSession ignores 0 trialDays', async function (){
await api.createCheckoutSession('priceId', null, {
trialDays: 0
});
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_from_plan);
should.equal(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_from_plan, true);
should.not.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_period_days);
});
it('createCheckoutSession ignores null trialDays', async function (){
await api.createCheckoutSession('priceId', null, {
trialDays: null
});
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_from_plan);
should.equal(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_from_plan, true);
should.not.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_period_days);
});
}); });