2021-04-12 18:08:01 +03:00
|
|
|
const _ = require('lodash');
|
2021-12-02 17:46:58 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
2021-04-12 18:08:01 +03:00
|
|
|
|
|
|
|
module.exports = class StripeMigrations {
|
|
|
|
/**
|
|
|
|
* StripeMigrations
|
|
|
|
*
|
|
|
|
* @param {object} params
|
|
|
|
*
|
2022-01-13 17:47:28 +03:00
|
|
|
* @param {any} params.models
|
2021-05-04 19:03:06 +03:00
|
|
|
* @param {import('../services/stripe-api')} params.stripeAPIService
|
2021-04-12 18:08:01 +03:00
|
|
|
*/
|
|
|
|
constructor({
|
2022-01-13 17:47:28 +03:00
|
|
|
models,
|
|
|
|
api
|
2021-04-12 18:08:01 +03:00
|
|
|
}) {
|
2022-01-13 17:47:28 +03:00
|
|
|
this.models = models;
|
|
|
|
this.api = api;
|
|
|
|
}
|
|
|
|
|
|
|
|
async execute() {
|
2022-01-18 12:18:08 +03:00
|
|
|
if (!this.api._configured) {
|
|
|
|
logging.info('Stripe not configured - skipping migrations');
|
|
|
|
return;
|
2022-04-04 08:31:53 +03:00
|
|
|
} else if (this.api.testEnv) {
|
|
|
|
logging.info('Stripe is in test mode - skipping migrations');
|
|
|
|
return;
|
2022-01-18 12:18:08 +03:00
|
|
|
}
|
2022-04-04 08:31:53 +03:00
|
|
|
|
2022-07-25 15:53:53 +03:00
|
|
|
try {
|
|
|
|
await this.populateProductsAndPrices();
|
|
|
|
await this.populateStripePricesFromStripePlansSetting();
|
|
|
|
await this.populateMembersMonthlyPriceIdSettings();
|
|
|
|
await this.populateMembersYearlyPriceIdSettings();
|
|
|
|
await this.populateDefaultProductMonthlyPriceId();
|
|
|
|
await this.populateDefaultProductYearlyPriceId();
|
|
|
|
await this.revertPortalPlansSetting();
|
|
|
|
await this.removeInvalidSubscriptions();
|
|
|
|
await this.setDefaultProductName();
|
|
|
|
await this.updateStripeProductNamesFromDefaultProduct();
|
|
|
|
} catch (err) {
|
|
|
|
logging.error(err);
|
|
|
|
}
|
2021-04-12 18:08:01 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 13:24:01 +03:00
|
|
|
async populateProductsAndPrices(options) {
|
|
|
|
if (!options) {
|
2022-01-13 17:47:28 +03:00
|
|
|
return this.models.Product.transaction((transacting) => {
|
2022-01-10 13:24:01 +03:00
|
|
|
return this.populateProductsAndPrices({transacting});
|
|
|
|
});
|
|
|
|
}
|
2022-01-13 17:47:28 +03:00
|
|
|
const subscriptionModels = await this.models.StripeCustomerSubscription.findAll(options);
|
|
|
|
const priceModels = await this.models.StripePrice.findAll(options);
|
|
|
|
const productModels = await this.models.StripeProduct.findAll(options);
|
2021-04-12 18:08:01 +03:00
|
|
|
const subscriptions = subscriptionModels.toJSON();
|
|
|
|
const prices = priceModels.toJSON();
|
|
|
|
const products = productModels.toJSON();
|
2022-01-13 17:47:28 +03:00
|
|
|
const {data} = await this.models.Product.findPage({
|
2022-01-10 13:24:01 +03:00
|
|
|
...options,
|
2022-01-17 20:32:02 +03:00
|
|
|
limit: 1,
|
|
|
|
filter: 'type:paid'
|
2021-04-12 18:08:01 +03:00
|
|
|
});
|
|
|
|
const defaultProduct = data[0] && data[0].toJSON();
|
|
|
|
|
|
|
|
if (subscriptions.length > 0 && products.length === 0 && prices.length === 0 && defaultProduct) {
|
|
|
|
try {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info(`Populating products and prices for existing stripe customers`);
|
2021-04-12 18:08:01 +03:00
|
|
|
const uniquePlans = _.uniq(subscriptions.map(d => _.get(d, 'plan.id')));
|
|
|
|
|
2021-05-20 17:35:58 +03:00
|
|
|
let stripePrices = [];
|
2021-04-12 18:08:01 +03:00
|
|
|
for (const plan of uniquePlans) {
|
2021-04-22 09:19:08 +03:00
|
|
|
try {
|
2022-01-13 17:47:28 +03:00
|
|
|
const stripePrice = await this.api.getPrice(plan, {
|
2021-04-22 09:19:08 +03:00
|
|
|
expand: ['product']
|
|
|
|
});
|
2021-05-20 17:35:58 +03:00
|
|
|
stripePrices.push(stripePrice);
|
2021-04-22 09:19:08 +03:00
|
|
|
} catch (err) {
|
|
|
|
if (err && err.statusCode === 404) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.warn(`Plan ${plan} not found on Stripe - ignoring`);
|
2021-04-22 09:19:08 +03:00
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2021-04-12 18:08:01 +03:00
|
|
|
}
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info(`Adding ${stripePrices.length} prices from Stripe`);
|
2021-05-20 17:35:58 +03:00
|
|
|
for (const stripePrice of stripePrices) {
|
|
|
|
// We expanded the product when fetching this price.
|
|
|
|
/** @type {import('stripe').Stripe.Product} */
|
|
|
|
const stripeProduct = (stripePrice.product);
|
2021-04-12 18:08:01 +03:00
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
await this.models.StripeProduct.upsert({
|
2021-04-12 18:08:01 +03:00
|
|
|
product_id: defaultProduct.id,
|
|
|
|
stripe_product_id: stripeProduct.id
|
2022-01-10 13:24:01 +03:00
|
|
|
}, options);
|
2021-04-12 18:08:01 +03:00
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
await this.models.StripePrice.add({
|
2021-05-20 17:35:58 +03:00
|
|
|
stripe_price_id: stripePrice.id,
|
2021-04-12 18:08:01 +03:00
|
|
|
stripe_product_id: stripeProduct.id,
|
2021-05-20 17:35:58 +03:00
|
|
|
active: stripePrice.active,
|
|
|
|
nickname: stripePrice.nickname,
|
|
|
|
currency: stripePrice.currency,
|
|
|
|
amount: stripePrice.unit_amount,
|
2021-04-12 18:08:01 +03:00
|
|
|
type: 'recurring',
|
2021-05-20 17:35:58 +03:00
|
|
|
interval: stripePrice.recurring.interval
|
2022-01-10 13:24:01 +03:00
|
|
|
}, options);
|
2021-04-12 18:08:01 +03:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.error(`Failed to populate products/prices from stripe`);
|
|
|
|
logging.error(e);
|
2021-04-12 18:08:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-04 19:03:06 +03:00
|
|
|
|
2022-01-10 13:24:01 +03:00
|
|
|
async findPriceByPlan(plan, options) {
|
2021-05-04 19:03:06 +03:00
|
|
|
const currency = plan.currency ? plan.currency.toLowerCase() : 'usd';
|
|
|
|
const amount = Number.isInteger(plan.amount) ? plan.amount : parseInt(plan.amount);
|
|
|
|
const interval = plan.interval;
|
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
const price = await this.models.StripePrice.findOne({
|
2021-05-04 19:03:06 +03:00
|
|
|
currency,
|
|
|
|
amount,
|
|
|
|
interval
|
2022-01-10 13:24:01 +03:00
|
|
|
}, options);
|
2021-05-04 19:03:06 +03:00
|
|
|
|
|
|
|
return price;
|
|
|
|
}
|
|
|
|
|
2022-01-10 13:24:01 +03:00
|
|
|
async getPlanFromPrice(priceId, options) {
|
2022-01-13 17:47:28 +03:00
|
|
|
const price = await this.models.StripePrice.findOne({
|
2021-06-03 13:14:43 +03:00
|
|
|
id: priceId
|
2022-01-10 13:24:01 +03:00
|
|
|
}, options);
|
2021-06-03 13:14:43 +03:00
|
|
|
|
|
|
|
if (price && price.get('interval') === 'month') {
|
|
|
|
return 'monthly';
|
|
|
|
}
|
|
|
|
if (price && price.get('interval') === 'year') {
|
|
|
|
return 'yearly';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
async populateStripePricesFromStripePlansSetting(options) {
|
2022-01-10 13:24:01 +03:00
|
|
|
if (!options) {
|
2022-01-13 17:47:28 +03:00
|
|
|
return this.models.Product.transaction((transacting) => {
|
|
|
|
return this.populateStripePricesFromStripePlansSetting({transacting});
|
2022-01-10 13:24:01 +03:00
|
|
|
});
|
|
|
|
}
|
2022-01-13 17:47:28 +03:00
|
|
|
const plansSetting = await this.models.Settings.findOne({key: 'stripe_plans'}, options);
|
|
|
|
let plans;
|
|
|
|
try {
|
|
|
|
plans = JSON.parse(plansSetting.get('value'));
|
|
|
|
} catch (err) {
|
|
|
|
return;
|
|
|
|
}
|
2021-05-04 19:03:06 +03:00
|
|
|
let defaultStripeProduct;
|
2022-01-13 17:47:28 +03:00
|
|
|
const stripeProductsPage = await this.models.StripeProduct.findPage({...options, limit: 1});
|
2021-05-04 19:03:06 +03:00
|
|
|
defaultStripeProduct = stripeProductsPage.data[0];
|
|
|
|
|
|
|
|
if (!defaultStripeProduct) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('Could not find Stripe Product - creating one');
|
2022-01-13 17:47:28 +03:00
|
|
|
const productsPage = await this.models.Product.findPage({...options, limit: 1, filter: 'type: paid'});
|
2021-05-04 19:03:06 +03:00
|
|
|
const defaultProduct = productsPage.data[0];
|
2022-01-13 17:47:28 +03:00
|
|
|
const stripeProduct = await this.api.createProduct({
|
2021-05-10 20:39:16 +03:00
|
|
|
name: defaultProduct.get('name')
|
|
|
|
});
|
2021-05-04 19:03:06 +03:00
|
|
|
if (!defaultProduct) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.error('Could not find Product - skipping stripe_plans -> stripe_prices migration');
|
2021-05-04 19:03:06 +03:00
|
|
|
return;
|
|
|
|
}
|
2022-01-13 17:47:28 +03:00
|
|
|
defaultStripeProduct = await this.models.StripeProduct.add({
|
2021-05-04 19:03:06 +03:00
|
|
|
product_id: defaultProduct.id,
|
|
|
|
stripe_product_id: stripeProduct.id
|
2022-01-10 13:24:01 +03:00
|
|
|
}, options);
|
2021-05-04 19:03:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const plan of plans) {
|
2022-10-03 17:50:28 +03:00
|
|
|
const existingPrice = await this.findPriceByPlan(plan, options);
|
2021-05-04 19:03:06 +03:00
|
|
|
|
2022-10-03 17:50:28 +03:00
|
|
|
if (!existingPrice) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info(`Could not find Stripe Price ${JSON.stringify(plan)}`);
|
2021-05-04 19:03:06 +03:00
|
|
|
|
|
|
|
try {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info(`Creating Stripe Price ${JSON.stringify(plan)}`);
|
2022-01-13 17:47:28 +03:00
|
|
|
const price = await this.api.createPrice({
|
2021-05-04 19:03:06 +03:00
|
|
|
currency: plan.currency,
|
|
|
|
amount: plan.amount,
|
|
|
|
nickname: plan.name,
|
|
|
|
interval: plan.interval,
|
|
|
|
active: true,
|
|
|
|
type: 'recurring',
|
|
|
|
product: defaultStripeProduct.get('stripe_product_id')
|
|
|
|
});
|
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
await this.models.StripePrice.add({
|
2021-05-04 19:03:06 +03:00
|
|
|
stripe_price_id: price.id,
|
|
|
|
stripe_product_id: defaultStripeProduct.get('stripe_product_id'),
|
|
|
|
active: price.active,
|
|
|
|
nickname: price.nickname,
|
|
|
|
currency: price.currency,
|
|
|
|
amount: price.unit_amount,
|
|
|
|
type: 'recurring',
|
|
|
|
interval: price.recurring.interval
|
2022-01-10 13:24:01 +03:00
|
|
|
}, options);
|
2021-05-04 19:03:06 +03:00
|
|
|
} catch (err) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.error({err, message: 'Adding price failed'});
|
2021-05-04 19:03:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 13:24:01 +03:00
|
|
|
async updatePortalPlansSetting(plans, options) {
|
|
|
|
if (!options) {
|
2022-01-13 17:47:28 +03:00
|
|
|
return this.models.Product.transaction((transacting) => {
|
2022-01-10 13:24:01 +03:00
|
|
|
return this.updatePortalPlansSetting(plans, {transacting});
|
|
|
|
});
|
|
|
|
}
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('Migrating portal_plans setting from names to ids');
|
2022-01-13 17:47:28 +03:00
|
|
|
const portalPlansSetting = await this.models.Settings.findOne({key: 'portal_plans'}, options);
|
2021-05-04 19:03:06 +03:00
|
|
|
|
|
|
|
let portalPlans;
|
|
|
|
try {
|
|
|
|
portalPlans = JSON.parse(portalPlansSetting.get('value'));
|
|
|
|
} catch (err) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.error({
|
2021-05-04 19:03:06 +03:00
|
|
|
message: 'Could not parse portal_plans setting, skipping migration',
|
|
|
|
err
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const containsOldValues = !!portalPlans.find((plan) => {
|
|
|
|
return ['monthly', 'yearly'].includes(plan);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!containsOldValues) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('Could not find names in portal_plans setting, skipping migration');
|
2021-05-04 19:03:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newPortalPlans = await portalPlans.reduce(async (newPortalPlansPromise, plan) => {
|
|
|
|
let newPlan = plan;
|
|
|
|
if (plan === 'monthly') {
|
2022-10-03 17:50:28 +03:00
|
|
|
const monthlyPlan = plans.find((planItem) => {
|
|
|
|
return planItem.name === 'Monthly';
|
2021-05-04 19:03:06 +03:00
|
|
|
});
|
|
|
|
if (!monthlyPlan) {
|
|
|
|
return newPortalPlansPromise;
|
|
|
|
}
|
2022-01-10 13:24:01 +03:00
|
|
|
const price = await this.findPriceByPlan(monthlyPlan, options);
|
2021-05-04 19:03:06 +03:00
|
|
|
newPlan = price.id;
|
|
|
|
}
|
|
|
|
if (plan === 'yearly') {
|
2022-10-03 17:50:28 +03:00
|
|
|
const yearlyPlan = plans.find((planItem) => {
|
|
|
|
return planItem.name === 'Yearly';
|
2021-05-04 19:03:06 +03:00
|
|
|
});
|
|
|
|
if (!yearlyPlan) {
|
|
|
|
return newPortalPlansPromise;
|
|
|
|
}
|
2022-01-10 13:24:01 +03:00
|
|
|
const price = await this.findPriceByPlan(yearlyPlan, options);
|
2021-05-04 19:03:06 +03:00
|
|
|
newPlan = price.id;
|
|
|
|
}
|
2022-10-03 17:50:28 +03:00
|
|
|
const newPortalPlansMemo = await newPortalPlansPromise;
|
|
|
|
return newPortalPlansMemo.concat(newPlan);
|
2021-05-04 19:03:06 +03:00
|
|
|
}, []);
|
|
|
|
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info(`Updating portal_plans setting to ${JSON.stringify(newPortalPlans)}`);
|
2022-01-13 17:47:28 +03:00
|
|
|
await this.models.Settings.edit({
|
2021-05-04 19:03:06 +03:00
|
|
|
key: 'portal_plans',
|
|
|
|
value: JSON.stringify(newPortalPlans)
|
|
|
|
}, {
|
2022-01-10 13:24:01 +03:00
|
|
|
...options,
|
2021-05-04 19:03:06 +03:00
|
|
|
id: portalPlansSetting.id
|
|
|
|
});
|
|
|
|
}
|
2021-05-19 16:56:42 +03:00
|
|
|
|
2022-01-10 13:24:01 +03:00
|
|
|
async populateMembersMonthlyPriceIdSettings(options) {
|
|
|
|
if (!options) {
|
2022-01-13 17:47:28 +03:00
|
|
|
return this.models.Product.transaction((transacting) => {
|
2022-01-10 13:24:01 +03:00
|
|
|
return this.populateMembersMonthlyPriceIdSettings({transacting});
|
|
|
|
});
|
|
|
|
}
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('Populating members_monthly_price_id from stripe_plans');
|
2022-01-13 17:47:28 +03:00
|
|
|
const monthlyPriceId = await this.models.Settings.findOne({key: 'members_monthly_price_id'}, options);
|
2021-05-19 16:56:42 +03:00
|
|
|
|
|
|
|
if (monthlyPriceId.get('value')) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('Skipping population of members_monthly_price_id, already populated');
|
2021-05-19 16:56:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
const stripePlans = await this.models.Settings.findOne({key: 'stripe_plans'}, options);
|
2021-05-19 16:56:42 +03:00
|
|
|
let plans;
|
|
|
|
try {
|
|
|
|
plans = JSON.parse(stripePlans.get('value'));
|
|
|
|
} catch (err) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.warn('Skipping population of members_monthly_price_id, could not parse stripe_plans');
|
2021-07-16 10:31:55 +03:00
|
|
|
return;
|
2021-05-19 16:56:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const monthlyPlan = plans.find((plan) => {
|
|
|
|
return plan.name === 'Monthly';
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!monthlyPlan) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.warn('Skipping population of members_monthly_price_id, could not find Monthly plan');
|
2021-05-19 16:56:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let monthlyPrice;
|
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
monthlyPrice = await this.models.StripePrice.findOne({
|
2021-05-19 16:56:42 +03:00
|
|
|
amount: monthlyPlan.amount,
|
|
|
|
currency: monthlyPlan.currency,
|
|
|
|
interval: monthlyPlan.interval,
|
|
|
|
active: true
|
2022-01-10 13:24:01 +03:00
|
|
|
}, options);
|
2021-05-19 16:56:42 +03:00
|
|
|
|
|
|
|
if (!monthlyPrice) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('Could not find active Monthly price from stripe_plans - searching by interval');
|
2022-01-13 17:47:28 +03:00
|
|
|
monthlyPrice = await this.models.StripePrice.where('amount', '>', 0)
|
2022-01-10 13:24:01 +03:00
|
|
|
.where({interval: 'month', active: true}).fetch(options);
|
2021-05-19 16:56:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!monthlyPrice) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('Could not any active Monthly price - creating a new one');
|
2021-05-19 16:56:42 +03:00
|
|
|
let defaultStripeProduct;
|
2022-01-13 17:47:28 +03:00
|
|
|
const stripeProductsPage = await this.models.StripeProduct.findPage({...options, limit: 1});
|
2021-05-19 16:56:42 +03:00
|
|
|
defaultStripeProduct = stripeProductsPage.data[0];
|
2022-01-13 17:47:28 +03:00
|
|
|
const price = await this.api.createPrice({
|
2021-05-19 16:56:42 +03:00
|
|
|
currency: 'usd',
|
|
|
|
amount: 5000,
|
|
|
|
nickname: 'Monthly',
|
|
|
|
interval: 'month',
|
|
|
|
active: true,
|
|
|
|
type: 'recurring',
|
|
|
|
product: defaultStripeProduct.get('stripe_product_id')
|
|
|
|
});
|
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
monthlyPrice = await this.models.StripePrice.add({
|
2021-05-19 16:56:42 +03:00
|
|
|
stripe_price_id: price.id,
|
|
|
|
stripe_product_id: defaultStripeProduct.get('stripe_product_id'),
|
|
|
|
active: price.active,
|
|
|
|
nickname: price.nickname,
|
|
|
|
currency: price.currency,
|
|
|
|
amount: price.unit_amount,
|
|
|
|
type: 'recurring',
|
|
|
|
interval: price.recurring.interval
|
2022-01-10 13:24:01 +03:00
|
|
|
}, options);
|
2021-05-19 16:56:42 +03:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
await this.models.Settings.edit({key: 'members_monthly_price_id', value: monthlyPrice.id}, {...options, id: monthlyPriceId.id});
|
2021-05-19 16:56:42 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 13:24:01 +03:00
|
|
|
async populateMembersYearlyPriceIdSettings(options) {
|
|
|
|
if (!options) {
|
2022-01-13 17:47:28 +03:00
|
|
|
return this.models.Product.transaction((transacting) => {
|
2022-01-10 13:24:01 +03:00
|
|
|
return this.populateMembersYearlyPriceIdSettings({transacting});
|
|
|
|
});
|
|
|
|
}
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('Populating members_yearly_price_id from stripe_plans');
|
2022-01-13 17:47:28 +03:00
|
|
|
const yearlyPriceId = await this.models.Settings.findOne({key: 'members_yearly_price_id'}, options);
|
2021-05-19 16:56:42 +03:00
|
|
|
|
|
|
|
if (yearlyPriceId.get('value')) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('Skipping population of members_yearly_price_id, already populated');
|
2021-05-19 16:56:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
const stripePlans = await this.models.Settings.findOne({key: 'stripe_plans'}, options);
|
2021-05-19 16:56:42 +03:00
|
|
|
let plans;
|
|
|
|
try {
|
|
|
|
plans = JSON.parse(stripePlans.get('value'));
|
|
|
|
} catch (err) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.warn('Skipping population of members_yearly_price_id, could not parse stripe_plans');
|
2021-05-19 16:56:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const yearlyPlan = plans.find((plan) => {
|
|
|
|
return plan.name === 'Yearly';
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!yearlyPlan) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.warn('Skipping population of members_yearly_price_id, could not find yearly plan');
|
2021-05-19 16:56:42 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let yearlyPrice;
|
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
yearlyPrice = await this.models.StripePrice.findOne({
|
2021-05-19 16:56:42 +03:00
|
|
|
amount: yearlyPlan.amount,
|
|
|
|
currency: yearlyPlan.currency,
|
|
|
|
interval: yearlyPlan.interval,
|
|
|
|
active: true
|
2022-01-10 13:24:01 +03:00
|
|
|
}, options);
|
2021-05-19 16:56:42 +03:00
|
|
|
|
|
|
|
if (!yearlyPrice) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('Could not find active yearly price from stripe_plans - searching by interval');
|
2022-01-13 17:47:28 +03:00
|
|
|
yearlyPrice = await this.models.StripePrice.where('amount', '>', 0)
|
2022-01-10 13:24:01 +03:00
|
|
|
.where({interval: 'year', active: true}).fetch(options);
|
2021-05-19 16:56:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!yearlyPrice) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('Could not any active yearly price - creating a new one');
|
2021-05-19 16:56:42 +03:00
|
|
|
let defaultStripeProduct;
|
2022-01-13 17:47:28 +03:00
|
|
|
const stripeProductsPage = await this.models.StripeProduct.findPage({...options, limit: 1});
|
2021-05-19 16:56:42 +03:00
|
|
|
defaultStripeProduct = stripeProductsPage.data[0];
|
2022-01-13 17:47:28 +03:00
|
|
|
const price = await this.api.createPrice({
|
2021-05-19 16:56:42 +03:00
|
|
|
currency: 'usd',
|
|
|
|
amount: 500,
|
|
|
|
nickname: 'Yearly',
|
|
|
|
interval: 'year',
|
|
|
|
active: true,
|
|
|
|
type: 'recurring',
|
|
|
|
product: defaultStripeProduct.get('stripe_product_id')
|
|
|
|
});
|
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
yearlyPrice = await this.models.StripePrice.add({
|
2021-05-19 16:56:42 +03:00
|
|
|
stripe_price_id: price.id,
|
|
|
|
stripe_product_id: defaultStripeProduct.get('stripe_product_id'),
|
|
|
|
active: price.active,
|
|
|
|
nickname: price.nickname,
|
|
|
|
currency: price.currency,
|
|
|
|
amount: price.unit_amount,
|
|
|
|
type: 'recurring',
|
|
|
|
interval: price.recurring.interval
|
2022-01-10 13:24:01 +03:00
|
|
|
}, options);
|
2021-05-19 16:56:42 +03:00
|
|
|
}
|
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
await this.models.Settings.edit({key: 'members_yearly_price_id', value: yearlyPrice.id}, {...options, id: yearlyPriceId.id});
|
2021-05-19 16:56:42 +03:00
|
|
|
}
|
2021-05-26 17:52:44 +03:00
|
|
|
|
2022-01-10 13:24:01 +03:00
|
|
|
async populateDefaultProductMonthlyPriceId(options) {
|
|
|
|
if (!options) {
|
2022-01-13 17:47:28 +03:00
|
|
|
return this.models.Product.transaction((transacting) => {
|
2022-01-10 13:24:01 +03:00
|
|
|
return this.populateDefaultProductMonthlyPriceId({transacting});
|
|
|
|
});
|
|
|
|
}
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('Migrating members_monthly_price_id setting to monthly_price_id column');
|
2022-01-13 17:47:28 +03:00
|
|
|
const productsPage = await this.models.Product.findPage({...options, limit: 1, filter: 'type:paid'});
|
2021-05-26 17:52:44 +03:00
|
|
|
const defaultProduct = productsPage.data[0];
|
|
|
|
|
|
|
|
if (defaultProduct.get('monthly_price_id')) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.warn('Skipping migration, monthly_price_id already set');
|
2021-05-26 17:52:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
const monthlyPriceIdSetting = await this.models.Settings.findOne({key: 'members_monthly_price_id'}, options);
|
2021-05-26 17:52:44 +03:00
|
|
|
const monthlyPriceId = monthlyPriceIdSetting.get('value');
|
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
await this.models.Product.edit({monthly_price_id: monthlyPriceId}, {...options, id: defaultProduct.id});
|
2021-05-26 17:52:44 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 13:24:01 +03:00
|
|
|
async populateDefaultProductYearlyPriceId(options) {
|
|
|
|
if (!options) {
|
2022-01-13 17:47:28 +03:00
|
|
|
return this.models.Product.transaction((transacting) => {
|
2022-01-10 13:24:01 +03:00
|
|
|
return this.populateDefaultProductYearlyPriceId({transacting});
|
|
|
|
});
|
|
|
|
}
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('Migrating members_yearly_price_id setting to yearly_price_id column');
|
2022-01-13 17:47:28 +03:00
|
|
|
const productsPage = await this.models.Product.findPage({...options, limit: 1, filter: 'type:paid'});
|
2021-05-26 17:52:44 +03:00
|
|
|
const defaultProduct = productsPage.data[0];
|
|
|
|
|
|
|
|
if (defaultProduct.get('yearly_price_id')) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.warn('Skipping migration, yearly_price_id already set');
|
2021-05-26 17:52:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
const yearlyPriceIdSetting = await this.models.Settings.findOne({key: 'members_yearly_price_id'}, options);
|
2021-05-26 17:52:44 +03:00
|
|
|
const yearlyPriceId = yearlyPriceIdSetting.get('value');
|
|
|
|
|
2022-01-13 17:47:28 +03:00
|
|
|
await this.models.Product.edit({yearly_price_id: yearlyPriceId}, {...options, id: defaultProduct.id});
|
2021-05-26 17:52:44 +03:00
|
|
|
}
|
2021-06-03 13:14:43 +03:00
|
|
|
|
2022-01-10 13:24:01 +03:00
|
|
|
async revertPortalPlansSetting(options) {
|
|
|
|
if (!options) {
|
2022-01-13 17:47:28 +03:00
|
|
|
return this.models.Product.transaction((transacting) => {
|
2022-01-10 13:24:01 +03:00
|
|
|
return this.revertPortalPlansSetting({transacting});
|
|
|
|
});
|
|
|
|
}
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('Migrating portal_plans setting from ids to names');
|
2022-01-13 17:47:28 +03:00
|
|
|
const portalPlansSetting = await this.models.Settings.findOne({key: 'portal_plans'}, options);
|
2021-06-03 13:14:43 +03:00
|
|
|
|
|
|
|
let portalPlans;
|
|
|
|
try {
|
|
|
|
portalPlans = JSON.parse(portalPlansSetting.get('value'));
|
|
|
|
} catch (err) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.error({
|
2021-06-03 13:14:43 +03:00
|
|
|
message: 'Could not parse portal_plans setting, skipping migration',
|
|
|
|
err
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const containsNamedValues = !!portalPlans.find((plan) => {
|
|
|
|
return ['monthly', 'yearly'].includes(plan);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (containsNamedValues) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('The portal_plans setting already contains names, skipping migration');
|
2021-06-03 13:14:43 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const portalPlanIds = portalPlans.filter((plan) => {
|
|
|
|
return plan !== 'free';
|
|
|
|
});
|
|
|
|
|
|
|
|
if (portalPlanIds.length === 0) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info('No price ids found in portal_plans setting, skipping migration');
|
2021-06-03 13:14:43 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const defaultPortalPlans = portalPlans.filter((plan) => {
|
|
|
|
return plan === 'free';
|
|
|
|
});
|
|
|
|
|
|
|
|
const newPortalPlans = await portalPlanIds.reduce(async (newPortalPlansPromise, priceId) => {
|
2022-01-10 13:24:01 +03:00
|
|
|
const plan = await this.getPlanFromPrice(priceId, options);
|
2021-06-03 13:14:43 +03:00
|
|
|
|
|
|
|
if (!plan) {
|
|
|
|
return newPortalPlansPromise;
|
|
|
|
}
|
|
|
|
|
2022-10-03 17:50:28 +03:00
|
|
|
const newPortalPlansMemo = await newPortalPlansPromise;
|
|
|
|
const updatedPortalPlans = newPortalPlansMemo.filter(d => d !== plan).concat(plan);
|
2021-06-03 13:14:43 +03:00
|
|
|
|
|
|
|
return updatedPortalPlans;
|
|
|
|
}, defaultPortalPlans);
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info(`Updating portal_plans setting to ${JSON.stringify(newPortalPlans)}`);
|
2022-01-13 17:47:28 +03:00
|
|
|
await this.models.Settings.edit({
|
2021-06-03 13:14:43 +03:00
|
|
|
key: 'portal_plans',
|
|
|
|
value: JSON.stringify(newPortalPlans)
|
|
|
|
}, {
|
2022-01-10 13:24:01 +03:00
|
|
|
...options,
|
2021-06-03 13:14:43 +03:00
|
|
|
id: portalPlansSetting.id
|
|
|
|
});
|
|
|
|
}
|
2021-07-14 14:28:04 +03:00
|
|
|
|
2022-01-10 13:24:01 +03:00
|
|
|
async removeInvalidSubscriptions(options) {
|
|
|
|
if (!options) {
|
2022-01-13 17:47:28 +03:00
|
|
|
return this.models.Product.transaction((transacting) => {
|
2022-01-10 13:24:01 +03:00
|
|
|
return this.removeInvalidSubscriptions({transacting});
|
|
|
|
});
|
|
|
|
}
|
2022-01-13 17:47:28 +03:00
|
|
|
const subscriptionModels = await this.models.StripeCustomerSubscription.findAll({
|
2022-01-10 13:24:01 +03:00
|
|
|
...options,
|
2021-07-14 14:28:04 +03:00
|
|
|
withRelated: ['stripePrice']
|
|
|
|
});
|
|
|
|
const invalidSubscriptions = subscriptionModels.filter((sub) => {
|
|
|
|
return !sub.toJSON().price;
|
|
|
|
});
|
|
|
|
if (invalidSubscriptions.length > 0) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.warn(`Deleting ${invalidSubscriptions.length} invalid subscription(s)`);
|
2021-07-14 14:28:04 +03:00
|
|
|
for (let sub of invalidSubscriptions) {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.warn(`Deleting subscription - ${sub.id} - no price found`);
|
2022-01-10 13:24:01 +03:00
|
|
|
await sub.destroy(options);
|
2021-07-14 14:28:04 +03:00
|
|
|
}
|
|
|
|
} else {
|
2021-12-02 17:46:58 +03:00
|
|
|
logging.info(`No invalid subscriptions, skipping migration`);
|
2021-07-14 14:28:04 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-01 14:15:04 +03:00
|
|
|
|
|
|
|
async setDefaultProductName(options) {
|
|
|
|
if (!options) {
|
|
|
|
return this.models.Product.transaction((transacting) => {
|
|
|
|
return this.setDefaultProductName({transacting});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const {data} = await this.models.Product.findPage({
|
|
|
|
...options,
|
|
|
|
limit: 1,
|
|
|
|
filter: 'type:paid'
|
|
|
|
});
|
|
|
|
|
|
|
|
const defaultProduct = data[0] && data[0].toJSON();
|
|
|
|
|
|
|
|
if (defaultProduct && defaultProduct.name === 'Default Product') {
|
|
|
|
const siteTitle = await this.models.Settings.findOne({key: 'title'}, options);
|
|
|
|
if (siteTitle) {
|
|
|
|
await this.models.Product.edit({
|
|
|
|
name: siteTitle.get('value')
|
|
|
|
}, {
|
|
|
|
...options,
|
|
|
|
id: defaultProduct.id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-09 17:41:59 +03:00
|
|
|
|
|
|
|
async updateStripeProductNamesFromDefaultProduct(options) {
|
|
|
|
if (!options) {
|
|
|
|
return this.models.Product.transaction((transacting) => {
|
|
|
|
return this.updateStripeProductNamesFromDefaultProduct({transacting});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const {data} = await this.models.StripeProduct.findPage({
|
|
|
|
...options,
|
|
|
|
limit: 'all'
|
|
|
|
});
|
|
|
|
|
|
|
|
const siteTitle = await this.models.Settings.findOne({key: 'title'}, options);
|
|
|
|
|
|
|
|
if (!siteTitle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const model of data) {
|
|
|
|
const product = await this.api.getProduct(model.get('stripe_product_id'));
|
|
|
|
|
|
|
|
if (product.name === 'Default Product') {
|
|
|
|
await this.api.updateProduct(product.id, {
|
|
|
|
name: siteTitle.get('value')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-12 18:08:01 +03:00
|
|
|
};
|