mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-04-01 02:41:39 -05:00
Wired trial days to stripe checkout session
refs https://github.com/TryGhost/Team/issues/1724 - wires trial days stored on a tier to stripe checkout session creation - removes deprecated `trial_from_plan` if trial days is set
This commit is contained in:
parent
2d12d9aa89
commit
54860d2b64
4 changed files with 49 additions and 6 deletions
|
@ -214,6 +214,11 @@ module.exports = class RouterController {
|
|||
const priceId = price.get('stripe_price_id');
|
||||
|
||||
const product = await this._productRepository.get({stripe_price_id: priceId});
|
||||
let trialDays;
|
||||
|
||||
if (this.labsService.isSet('freeTrial')) {
|
||||
trialDays = product.get('trial_days');
|
||||
}
|
||||
|
||||
if (product.get('active') !== true) {
|
||||
throw new NoPermissionError({
|
||||
|
@ -257,6 +262,7 @@ module.exports = class RouterController {
|
|||
coupon: couponId,
|
||||
successUrl,
|
||||
cancelUrl,
|
||||
trialDays,
|
||||
customerEmail: req.body.customerEmail,
|
||||
metadata: metadata
|
||||
});
|
||||
|
@ -311,6 +317,7 @@ module.exports = class RouterController {
|
|||
coupon: couponId,
|
||||
successUrl,
|
||||
cancelUrl,
|
||||
trialDays,
|
||||
metadata: metadata
|
||||
});
|
||||
const publicKey = this._stripeAPIService.getPublicKey();
|
||||
|
|
|
@ -288,6 +288,7 @@ class ProductRepository {
|
|||
* @param {string} data.id
|
||||
* @param {string} data.name
|
||||
* @param {string} data.description
|
||||
* @param {number} data.trial_days
|
||||
* @param {'public'|'none'} data.visibility
|
||||
* @param {string} data.welcome_page_url
|
||||
* @param {BenefitInput[]} data.benefits
|
||||
|
@ -345,9 +346,14 @@ class ProductRepository {
|
|||
productData.active = data.active;
|
||||
}
|
||||
|
||||
if (Reflect.has(data, 'trial_days')) {
|
||||
productData.trial_days = data.trial_days;
|
||||
}
|
||||
|
||||
if (existingProduct.get('type') === 'free') {
|
||||
delete productData.name;
|
||||
delete productData.active;
|
||||
delete productData.trial_days;
|
||||
}
|
||||
|
||||
if (existingProduct.get('active') === true && productData.active === false) {
|
||||
|
|
|
@ -360,6 +360,7 @@ module.exports = class StripeAPI {
|
|||
* @param {string} options.successUrl
|
||||
* @param {string} options.cancelUrl
|
||||
* @param {string} options.customerEmail
|
||||
* @param {number} options.trialDays
|
||||
* @param {string} [options.coupon]
|
||||
*
|
||||
* @returns {Promise<import('stripe').Stripe.Checkout.Session>}
|
||||
|
@ -372,6 +373,22 @@ module.exports = class StripeAPI {
|
|||
if (options.coupon) {
|
||||
discounts = [{coupon: options.coupon}];
|
||||
}
|
||||
|
||||
const subscriptionData = {
|
||||
trial_from_plan: true,
|
||||
items: [{
|
||||
plan: priceId
|
||||
}]
|
||||
};
|
||||
|
||||
/**
|
||||
* `trial_from_plan` is deprecated.
|
||||
* Replaces it in favor of custom trial period days stored in Ghost
|
||||
*/
|
||||
if (!isNaN(options.trialDays)) {
|
||||
delete subscriptionData.trial_from_plan;
|
||||
subscriptionData.trial_period_days = options.trialDays;
|
||||
}
|
||||
const session = await this._stripe.checkout.sessions.create({
|
||||
payment_method_types: ['card'],
|
||||
success_url: options.successUrl || this._config.checkoutSessionSuccessUrl,
|
||||
|
@ -390,12 +407,7 @@ module.exports = class StripeAPI {
|
|||
// It should be replaced with the line_items entry above when possible,
|
||||
// however, this would lose the "trial from plan" feature which has also
|
||||
// been deprecated by Stripe
|
||||
subscription_data: {
|
||||
trial_from_plan: true,
|
||||
items: [{
|
||||
plan: priceId
|
||||
}]
|
||||
}
|
||||
subscription_data: subscriptionData
|
||||
});
|
||||
|
||||
return session;
|
||||
|
|
|
@ -42,4 +42,22 @@ describe('StripeAPI', function () {
|
|||
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.success_url);
|
||||
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.cancel_url);
|
||||
});
|
||||
|
||||
it('createCheckoutSetupSession uses trialDays', async function (){
|
||||
await api.createCheckoutSession('priceId', null, {
|
||||
trialDays: 12
|
||||
});
|
||||
|
||||
should.not.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_from_plan);
|
||||
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_period_days);
|
||||
should.equal(mockStripe.checkout.sessions.create.firstCall.firstArg.subscription_data.trial_period_days, 12);
|
||||
});
|
||||
|
||||
it('createCheckoutSetupSession uses trial_from_plan without trialDays', async function (){
|
||||
await api.createCheckoutSession('priceId', 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);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue