mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-17 23:44:39 -05:00
Use @tryghost/logging instead of injected argument
This commit is contained in:
parent
9f7a1fa50d
commit
ba2c0818e0
12 changed files with 80 additions and 143 deletions
|
@ -1,7 +1,8 @@
|
||||||
const {Router} = require('express');
|
const {Router} = require('express');
|
||||||
const body = require('body-parser');
|
const body = require('body-parser');
|
||||||
const MagicLink = require('@tryghost/magic-link');
|
const MagicLink = require('@tryghost/magic-link');
|
||||||
const common = require('./common');
|
const errors = require('@tryghost/errors');
|
||||||
|
const logging = require('@tryghost/logging');
|
||||||
|
|
||||||
const MemberAnalyticsService = require('@tryghost/member-analytics-service');
|
const MemberAnalyticsService = require('@tryghost/member-analytics-service');
|
||||||
const MembersAnalyticsIngress = require('@tryghost/members-analytics-ingress');
|
const MembersAnalyticsIngress = require('@tryghost/members-analytics-ingress');
|
||||||
|
@ -59,13 +60,8 @@ module.exports = function MembersAPI({
|
||||||
},
|
},
|
||||||
stripeAPIService,
|
stripeAPIService,
|
||||||
offersAPI,
|
offersAPI,
|
||||||
logger,
|
|
||||||
labsService
|
labsService
|
||||||
}) {
|
}) {
|
||||||
if (logger) {
|
|
||||||
common.logging.setLogger(logger);
|
|
||||||
}
|
|
||||||
|
|
||||||
const tokenService = new TokenService({
|
const tokenService = new TokenService({
|
||||||
privateKey,
|
privateKey,
|
||||||
publicKey,
|
publicKey,
|
||||||
|
@ -83,8 +79,7 @@ module.exports = function MembersAPI({
|
||||||
StripeProduct,
|
StripeProduct,
|
||||||
StripePrice,
|
StripePrice,
|
||||||
Product,
|
Product,
|
||||||
Settings,
|
Settings
|
||||||
logger
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const productRepository = new ProductRepository({
|
const productRepository = new ProductRepository({
|
||||||
|
@ -96,7 +91,6 @@ module.exports = function MembersAPI({
|
||||||
|
|
||||||
const memberRepository = new MemberRepository({
|
const memberRepository = new MemberRepository({
|
||||||
stripeAPIService,
|
stripeAPIService,
|
||||||
logger,
|
|
||||||
tokenService,
|
tokenService,
|
||||||
productRepository,
|
productRepository,
|
||||||
Member,
|
Member,
|
||||||
|
@ -111,7 +105,6 @@ module.exports = function MembersAPI({
|
||||||
});
|
});
|
||||||
|
|
||||||
const eventRepository = new EventRepository({
|
const eventRepository = new EventRepository({
|
||||||
logger,
|
|
||||||
MemberSubscribeEvent,
|
MemberSubscribeEvent,
|
||||||
MemberPaidSubscriptionEvent,
|
MemberPaidSubscriptionEvent,
|
||||||
MemberPaymentEvent,
|
MemberPaymentEvent,
|
||||||
|
@ -204,13 +197,11 @@ module.exports = function MembersAPI({
|
||||||
checkoutCancelUrl: stripeConfig.checkoutCancelUrl,
|
checkoutCancelUrl: stripeConfig.checkoutCancelUrl,
|
||||||
billingSuccessUrl: stripeConfig.billingSuccessUrl,
|
billingSuccessUrl: stripeConfig.billingSuccessUrl,
|
||||||
billingCancelUrl: stripeConfig.billingCancelUrl
|
billingCancelUrl: stripeConfig.billingCancelUrl
|
||||||
},
|
}
|
||||||
logging: common.logging
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const wellKnownController = new WellKnownController({
|
const wellKnownController = new WellKnownController({
|
||||||
tokenService,
|
tokenService
|
||||||
logging: common.logging
|
|
||||||
});
|
});
|
||||||
|
|
||||||
async function disconnectStripe() {
|
async function disconnectStripe() {
|
||||||
|
@ -348,7 +339,7 @@ module.exports = function MembersAPI({
|
||||||
|
|
||||||
async function setMemberGeolocationFromIp(email, ip) {
|
async function setMemberGeolocationFromIp(email, ip) {
|
||||||
if (!email || !ip) {
|
if (!email || !ip) {
|
||||||
throw new common.errors.IncorrectUsageError({
|
throw new errors.IncorrectUsageError({
|
||||||
message: 'setMemberGeolocationFromIp() expects email and ip arguments to be present'
|
message: 'setMemberGeolocationFromIp() expects email and ip arguments to be present'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -358,7 +349,7 @@ module.exports = function MembersAPI({
|
||||||
const member = (await users.get({email})).toJSON();
|
const member = (await users.get({email})).toJSON();
|
||||||
|
|
||||||
if (!member) {
|
if (!member) {
|
||||||
throw new common.errors.NotFoundError({
|
throw new errors.NotFoundError({
|
||||||
message: `Member with email address ${email} does not exist`
|
message: `Member with email address ${email} does not exist`
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -407,7 +398,7 @@ module.exports = function MembersAPI({
|
||||||
|
|
||||||
middleware.handleStripeWebhook.use(body.raw({type: 'application/json'}), async function (req, res) {
|
middleware.handleStripeWebhook.use(body.raw({type: 'application/json'}), async function (req, res) {
|
||||||
if (!stripeAPIService.configured) {
|
if (!stripeAPIService.configured) {
|
||||||
common.logging.error(`Stripe not configured, not handling webhook`);
|
logging.error(`Stripe not configured, not handling webhook`);
|
||||||
res.writeHead(400);
|
res.writeHead(400);
|
||||||
return res.end();
|
return res.end();
|
||||||
}
|
}
|
||||||
|
@ -420,17 +411,17 @@ module.exports = function MembersAPI({
|
||||||
try {
|
try {
|
||||||
event = stripeWebhookService.parseWebhook(req.body, req.headers['stripe-signature']);
|
event = stripeWebhookService.parseWebhook(req.body, req.headers['stripe-signature']);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
common.logging.error(err);
|
logging.error(err);
|
||||||
res.writeHead(401);
|
res.writeHead(401);
|
||||||
return res.end();
|
return res.end();
|
||||||
}
|
}
|
||||||
common.logging.info(`Handling webhook ${event.type}`);
|
logging.info(`Handling webhook ${event.type}`);
|
||||||
try {
|
try {
|
||||||
await stripeWebhookService.handleWebhook(event);
|
await stripeWebhookService.handleWebhook(event);
|
||||||
res.writeHead(200);
|
res.writeHead(200);
|
||||||
res.end();
|
res.end();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
common.logging.error(`Error handling webhook ${event.type}`, err);
|
logging.error(`Error handling webhook ${event.type}`, err);
|
||||||
res.writeHead(err.statusCode || 500);
|
res.writeHead(err.statusCode || 500);
|
||||||
res.end();
|
res.end();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +0,0 @@
|
||||||
let currentLogger = {
|
|
||||||
error: global.console.error,
|
|
||||||
info: global.console.info,
|
|
||||||
warn: global.console.warn
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
get logging() {
|
|
||||||
const loggerInterface = Object.create(currentLogger);
|
|
||||||
return Object.assign(loggerInterface, {
|
|
||||||
setLogger(newLogger) {
|
|
||||||
currentLogger = newLogger;
|
|
||||||
// Overwrite any existing reference to loggerInterface
|
|
||||||
Object.assign(loggerInterface, Object.create(newLogger));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
get errors() {
|
|
||||||
return require('@tryghost/ignition-errors');
|
|
||||||
}
|
|
||||||
};
|
|
|
@ -1,4 +1,4 @@
|
||||||
const errors = require('@tryghost/ignition-errors');
|
const errors = require('@tryghost/errors');
|
||||||
|
|
||||||
module.exports = class MemberController {
|
module.exports = class MemberController {
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
const common = require('../../lib/common');
|
const logging = require('@tryghost/logging');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
module.exports = class RouterController {
|
module.exports = class RouterController {
|
||||||
|
@ -17,7 +17,6 @@ module.exports = class RouterController {
|
||||||
* @param {any} deps.tokenService
|
* @param {any} deps.tokenService
|
||||||
* @param {{isSet(name: string): boolean}} deps.labsService
|
* @param {{isSet(name: string): boolean}} deps.labsService
|
||||||
* @param {any} deps.config
|
* @param {any} deps.config
|
||||||
* @param {any} deps.logging
|
|
||||||
*/
|
*/
|
||||||
constructor({
|
constructor({
|
||||||
offersAPI,
|
offersAPI,
|
||||||
|
@ -31,8 +30,7 @@ module.exports = class RouterController {
|
||||||
tokenService,
|
tokenService,
|
||||||
sendEmailWithMagicLink,
|
sendEmailWithMagicLink,
|
||||||
labsService,
|
labsService,
|
||||||
config,
|
config
|
||||||
logging
|
|
||||||
}) {
|
}) {
|
||||||
this._offersAPI = offersAPI;
|
this._offersAPI = offersAPI;
|
||||||
this._paymentsService = paymentsService;
|
this._paymentsService = paymentsService;
|
||||||
|
@ -46,7 +44,6 @@ module.exports = class RouterController {
|
||||||
this._sendEmailWithMagicLink = sendEmailWithMagicLink;
|
this._sendEmailWithMagicLink = sendEmailWithMagicLink;
|
||||||
this.labsService = labsService;
|
this.labsService = labsService;
|
||||||
this._config = config;
|
this._config = config;
|
||||||
this._logging = logging;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async ensureStripe(_req, res, next) {
|
async ensureStripe(_req, res, next) {
|
||||||
|
@ -248,7 +245,7 @@ module.exports = class RouterController {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this._logging.info('Ignoring error for fetching customer for checkout');
|
logging.info('Ignoring error for fetching customer for checkout');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -304,7 +301,7 @@ module.exports = class RouterController {
|
||||||
return res.end('Created.');
|
return res.end('Created.');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const statusCode = (err && err.statusCode) || 500;
|
const statusCode = (err && err.statusCode) || 500;
|
||||||
common.logging.error(err);
|
logging.error(err);
|
||||||
res.writeHead(statusCode);
|
res.writeHead(statusCode);
|
||||||
return res.end('Internal Server Error.');
|
return res.end('Internal Server Error.');
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
const errors = require('@tryghost/errors');
|
const errors = require('@tryghost/errors');
|
||||||
|
const logging = require('@tryghost/logging');
|
||||||
const tpl = require('@tryghost/tpl');
|
const tpl = require('@tryghost/tpl');
|
||||||
|
|
||||||
const messages = {
|
const messages = {
|
||||||
|
@ -14,22 +15,13 @@ const messages = {
|
||||||
* @prop {() => Promise<JWKS>} getPublicKeys
|
* @prop {() => Promise<JWKS>} getPublicKeys
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {object} ILogging
|
|
||||||
* @prop {(msg) => void} info
|
|
||||||
* @prop {(msg) => void} warn
|
|
||||||
* @prop {(msg) => void} error
|
|
||||||
*/
|
|
||||||
|
|
||||||
module.exports = class WellKnownController {
|
module.exports = class WellKnownController {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {object} deps
|
* @param {object} deps
|
||||||
* @param {ITokenService} deps.tokenService
|
* @param {ITokenService} deps.tokenService
|
||||||
* @param {ILogging} deps.logging
|
|
||||||
*/
|
*/
|
||||||
constructor(deps) {
|
constructor(deps) {
|
||||||
this._logging = deps.logging;
|
|
||||||
this._tokenService = deps.tokenService;
|
this._tokenService = deps.tokenService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +39,7 @@ module.exports = class WellKnownController {
|
||||||
message: tpl(messages.keyStoreError),
|
message: tpl(messages.keyStoreError),
|
||||||
err
|
err
|
||||||
});
|
});
|
||||||
this._logging.error(error);
|
logging.error(error);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,12 @@
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
const logging = require('@tryghost/logging');
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {object} ILogger
|
|
||||||
* @prop {(x: any) => void} error
|
|
||||||
* @prop {(x: any) => void} info
|
|
||||||
* @prop {(x: any) => void} warn
|
|
||||||
*/
|
|
||||||
module.exports = class StripeMigrations {
|
module.exports = class StripeMigrations {
|
||||||
/**
|
/**
|
||||||
* StripeMigrations
|
* StripeMigrations
|
||||||
*
|
*
|
||||||
* @param {object} params
|
* @param {object} params
|
||||||
*
|
*
|
||||||
* @param {ILogger} params.logger
|
|
||||||
* @param {any} params.StripeCustomerSubscription
|
* @param {any} params.StripeCustomerSubscription
|
||||||
* @param {any} params.StripeProduct
|
* @param {any} params.StripeProduct
|
||||||
* @param {any} params.StripePrice
|
* @param {any} params.StripePrice
|
||||||
|
@ -26,10 +20,8 @@ module.exports = class StripeMigrations {
|
||||||
StripePrice,
|
StripePrice,
|
||||||
Product,
|
Product,
|
||||||
Settings,
|
Settings,
|
||||||
stripeAPIService,
|
stripeAPIService
|
||||||
logger
|
|
||||||
}) {
|
}) {
|
||||||
this._logging = logger;
|
|
||||||
this._StripeCustomerSubscription = StripeCustomerSubscription;
|
this._StripeCustomerSubscription = StripeCustomerSubscription;
|
||||||
this._StripeProduct = StripeProduct;
|
this._StripeProduct = StripeProduct;
|
||||||
this._StripePrice = StripePrice;
|
this._StripePrice = StripePrice;
|
||||||
|
@ -52,7 +44,7 @@ module.exports = class StripeMigrations {
|
||||||
|
|
||||||
if (subscriptions.length > 0 && products.length === 0 && prices.length === 0 && defaultProduct) {
|
if (subscriptions.length > 0 && products.length === 0 && prices.length === 0 && defaultProduct) {
|
||||||
try {
|
try {
|
||||||
this._logging.info(`Populating products and prices for existing stripe customers`);
|
logging.info(`Populating products and prices for existing stripe customers`);
|
||||||
const uniquePlans = _.uniq(subscriptions.map(d => _.get(d, 'plan.id')));
|
const uniquePlans = _.uniq(subscriptions.map(d => _.get(d, 'plan.id')));
|
||||||
|
|
||||||
let stripePrices = [];
|
let stripePrices = [];
|
||||||
|
@ -64,13 +56,13 @@ module.exports = class StripeMigrations {
|
||||||
stripePrices.push(stripePrice);
|
stripePrices.push(stripePrice);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err && err.statusCode === 404) {
|
if (err && err.statusCode === 404) {
|
||||||
this._logging.warn(`Plan ${plan} not found on Stripe - ignoring`);
|
logging.warn(`Plan ${plan} not found on Stripe - ignoring`);
|
||||||
} else {
|
} else {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this._logging.info(`Adding ${stripePrices.length} prices from Stripe`);
|
logging.info(`Adding ${stripePrices.length} prices from Stripe`);
|
||||||
for (const stripePrice of stripePrices) {
|
for (const stripePrice of stripePrices) {
|
||||||
// We expanded the product when fetching this price.
|
// We expanded the product when fetching this price.
|
||||||
/** @type {import('stripe').Stripe.Product} */
|
/** @type {import('stripe').Stripe.Product} */
|
||||||
|
@ -93,8 +85,8 @@ module.exports = class StripeMigrations {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this._logging.error(`Failed to populate products/prices from stripe`);
|
logging.error(`Failed to populate products/prices from stripe`);
|
||||||
this._logging.error(e);
|
logging.error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -129,7 +121,7 @@ module.exports = class StripeMigrations {
|
||||||
|
|
||||||
async populateStripePricesFromStripePlansSetting(plans) {
|
async populateStripePricesFromStripePlansSetting(plans) {
|
||||||
if (!plans) {
|
if (!plans) {
|
||||||
this._logging.info('Skipping stripe_plans -> stripe_prices migration');
|
logging.info('Skipping stripe_plans -> stripe_prices migration');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let defaultStripeProduct;
|
let defaultStripeProduct;
|
||||||
|
@ -137,14 +129,14 @@ module.exports = class StripeMigrations {
|
||||||
defaultStripeProduct = stripeProductsPage.data[0];
|
defaultStripeProduct = stripeProductsPage.data[0];
|
||||||
|
|
||||||
if (!defaultStripeProduct) {
|
if (!defaultStripeProduct) {
|
||||||
this._logging.info('Could not find Stripe Product - creating one');
|
logging.info('Could not find Stripe Product - creating one');
|
||||||
const productsPage = await this._Product.findPage({limit: 1});
|
const productsPage = await this._Product.findPage({limit: 1});
|
||||||
const defaultProduct = productsPage.data[0];
|
const defaultProduct = productsPage.data[0];
|
||||||
const stripeProduct = await this._stripeAPIService.createProduct({
|
const stripeProduct = await this._stripeAPIService.createProduct({
|
||||||
name: defaultProduct.get('name')
|
name: defaultProduct.get('name')
|
||||||
});
|
});
|
||||||
if (!defaultProduct) {
|
if (!defaultProduct) {
|
||||||
this._logging.error('Could not find Product - skipping stripe_plans -> stripe_prices migration');
|
logging.error('Could not find Product - skipping stripe_plans -> stripe_prices migration');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
defaultStripeProduct = await this._StripeProduct.add({
|
defaultStripeProduct = await this._StripeProduct.add({
|
||||||
|
@ -157,10 +149,10 @@ module.exports = class StripeMigrations {
|
||||||
const price = await this.findPriceByPlan(plan);
|
const price = await this.findPriceByPlan(plan);
|
||||||
|
|
||||||
if (!price) {
|
if (!price) {
|
||||||
this._logging.info(`Could not find Stripe Price ${JSON.stringify(plan)}`);
|
logging.info(`Could not find Stripe Price ${JSON.stringify(plan)}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this._logging.info(`Creating Stripe Price ${JSON.stringify(plan)}`);
|
logging.info(`Creating Stripe Price ${JSON.stringify(plan)}`);
|
||||||
const price = await this._stripeAPIService.createPrice({
|
const price = await this._stripeAPIService.createPrice({
|
||||||
currency: plan.currency,
|
currency: plan.currency,
|
||||||
amount: plan.amount,
|
amount: plan.amount,
|
||||||
|
@ -182,21 +174,21 @@ module.exports = class StripeMigrations {
|
||||||
interval: price.recurring.interval
|
interval: price.recurring.interval
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this._logging.error({err, message: 'Adding price failed'});
|
logging.error({err, message: 'Adding price failed'});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async updatePortalPlansSetting(plans) {
|
async updatePortalPlansSetting(plans) {
|
||||||
this._logging.info('Migrating portal_plans setting from names to ids');
|
logging.info('Migrating portal_plans setting from names to ids');
|
||||||
const portalPlansSetting = await this._Settings.findOne({key: 'portal_plans'});
|
const portalPlansSetting = await this._Settings.findOne({key: 'portal_plans'});
|
||||||
|
|
||||||
let portalPlans;
|
let portalPlans;
|
||||||
try {
|
try {
|
||||||
portalPlans = JSON.parse(portalPlansSetting.get('value'));
|
portalPlans = JSON.parse(portalPlansSetting.get('value'));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this._logging.error({
|
logging.error({
|
||||||
message: 'Could not parse portal_plans setting, skipping migration',
|
message: 'Could not parse portal_plans setting, skipping migration',
|
||||||
err
|
err
|
||||||
});
|
});
|
||||||
|
@ -208,7 +200,7 @@ module.exports = class StripeMigrations {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!containsOldValues) {
|
if (!containsOldValues) {
|
||||||
this._logging.info('Could not find names in portal_plans setting, skipping migration');
|
logging.info('Could not find names in portal_plans setting, skipping migration');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,7 +230,7 @@ module.exports = class StripeMigrations {
|
||||||
return newPortalPlans.concat(newPlan);
|
return newPortalPlans.concat(newPlan);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
this._logging.info(`Updating portal_plans setting to ${JSON.stringify(newPortalPlans)}`);
|
logging.info(`Updating portal_plans setting to ${JSON.stringify(newPortalPlans)}`);
|
||||||
await this._Settings.edit({
|
await this._Settings.edit({
|
||||||
key: 'portal_plans',
|
key: 'portal_plans',
|
||||||
value: JSON.stringify(newPortalPlans)
|
value: JSON.stringify(newPortalPlans)
|
||||||
|
@ -248,11 +240,11 @@ module.exports = class StripeMigrations {
|
||||||
}
|
}
|
||||||
|
|
||||||
async populateMembersMonthlyPriceIdSettings() {
|
async populateMembersMonthlyPriceIdSettings() {
|
||||||
this._logging.info('Populating members_monthly_price_id from stripe_plans');
|
logging.info('Populating members_monthly_price_id from stripe_plans');
|
||||||
const monthlyPriceId = await this._Settings.findOne({key: 'members_monthly_price_id'});
|
const monthlyPriceId = await this._Settings.findOne({key: 'members_monthly_price_id'});
|
||||||
|
|
||||||
if (monthlyPriceId.get('value')) {
|
if (monthlyPriceId.get('value')) {
|
||||||
this._logging.info('Skipping population of members_monthly_price_id, already populated');
|
logging.info('Skipping population of members_monthly_price_id, already populated');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -261,7 +253,7 @@ module.exports = class StripeMigrations {
|
||||||
try {
|
try {
|
||||||
plans = JSON.parse(stripePlans.get('value'));
|
plans = JSON.parse(stripePlans.get('value'));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this._logging.warn('Skipping population of members_monthly_price_id, could not parse stripe_plans');
|
logging.warn('Skipping population of members_monthly_price_id, could not parse stripe_plans');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -270,7 +262,7 @@ module.exports = class StripeMigrations {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!monthlyPlan) {
|
if (!monthlyPlan) {
|
||||||
this._logging.warn('Skipping population of members_monthly_price_id, could not find Monthly plan');
|
logging.warn('Skipping population of members_monthly_price_id, could not find Monthly plan');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -284,13 +276,13 @@ module.exports = class StripeMigrations {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!monthlyPrice) {
|
if (!monthlyPrice) {
|
||||||
this._logging.info('Could not find active Monthly price from stripe_plans - searching by interval');
|
logging.info('Could not find active Monthly price from stripe_plans - searching by interval');
|
||||||
monthlyPrice = await this._StripePrice.where('amount', '>', 0)
|
monthlyPrice = await this._StripePrice.where('amount', '>', 0)
|
||||||
.where({interval: 'month', active: true}).fetch();
|
.where({interval: 'month', active: true}).fetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!monthlyPrice) {
|
if (!monthlyPrice) {
|
||||||
this._logging.info('Could not any active Monthly price - creating a new one');
|
logging.info('Could not any active Monthly price - creating a new one');
|
||||||
let defaultStripeProduct;
|
let defaultStripeProduct;
|
||||||
const stripeProductsPage = await this._StripeProduct.findPage({limit: 1});
|
const stripeProductsPage = await this._StripeProduct.findPage({limit: 1});
|
||||||
defaultStripeProduct = stripeProductsPage.data[0];
|
defaultStripeProduct = stripeProductsPage.data[0];
|
||||||
|
@ -320,11 +312,11 @@ module.exports = class StripeMigrations {
|
||||||
}
|
}
|
||||||
|
|
||||||
async populateMembersYearlyPriceIdSettings() {
|
async populateMembersYearlyPriceIdSettings() {
|
||||||
this._logging.info('Populating members_yearly_price_id from stripe_plans');
|
logging.info('Populating members_yearly_price_id from stripe_plans');
|
||||||
const yearlyPriceId = await this._Settings.findOne({key: 'members_yearly_price_id'});
|
const yearlyPriceId = await this._Settings.findOne({key: 'members_yearly_price_id'});
|
||||||
|
|
||||||
if (yearlyPriceId.get('value')) {
|
if (yearlyPriceId.get('value')) {
|
||||||
this._logging.info('Skipping population of members_yearly_price_id, already populated');
|
logging.info('Skipping population of members_yearly_price_id, already populated');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -333,7 +325,7 @@ module.exports = class StripeMigrations {
|
||||||
try {
|
try {
|
||||||
plans = JSON.parse(stripePlans.get('value'));
|
plans = JSON.parse(stripePlans.get('value'));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this._logging.warn('Skipping population of members_yearly_price_id, could not parse stripe_plans');
|
logging.warn('Skipping population of members_yearly_price_id, could not parse stripe_plans');
|
||||||
}
|
}
|
||||||
|
|
||||||
const yearlyPlan = plans.find((plan) => {
|
const yearlyPlan = plans.find((plan) => {
|
||||||
|
@ -341,7 +333,7 @@ module.exports = class StripeMigrations {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!yearlyPlan) {
|
if (!yearlyPlan) {
|
||||||
this._logging.warn('Skipping population of members_yearly_price_id, could not find yearly plan');
|
logging.warn('Skipping population of members_yearly_price_id, could not find yearly plan');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -355,13 +347,13 @@ module.exports = class StripeMigrations {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!yearlyPrice) {
|
if (!yearlyPrice) {
|
||||||
this._logging.info('Could not find active yearly price from stripe_plans - searching by interval');
|
logging.info('Could not find active yearly price from stripe_plans - searching by interval');
|
||||||
yearlyPrice = await this._StripePrice.where('amount', '>', 0)
|
yearlyPrice = await this._StripePrice.where('amount', '>', 0)
|
||||||
.where({interval: 'year', active: true}).fetch();
|
.where({interval: 'year', active: true}).fetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!yearlyPrice) {
|
if (!yearlyPrice) {
|
||||||
this._logging.info('Could not any active yearly price - creating a new one');
|
logging.info('Could not any active yearly price - creating a new one');
|
||||||
let defaultStripeProduct;
|
let defaultStripeProduct;
|
||||||
const stripeProductsPage = await this._StripeProduct.findPage({limit: 1});
|
const stripeProductsPage = await this._StripeProduct.findPage({limit: 1});
|
||||||
defaultStripeProduct = stripeProductsPage.data[0];
|
defaultStripeProduct = stripeProductsPage.data[0];
|
||||||
|
@ -391,12 +383,12 @@ module.exports = class StripeMigrations {
|
||||||
}
|
}
|
||||||
|
|
||||||
async populateDefaultProductMonthlyPriceId() {
|
async populateDefaultProductMonthlyPriceId() {
|
||||||
this._logging.info('Migrating members_monthly_price_id setting to monthly_price_id column');
|
logging.info('Migrating members_monthly_price_id setting to monthly_price_id column');
|
||||||
const productsPage = await this._Product.findPage({limit: 1});
|
const productsPage = await this._Product.findPage({limit: 1});
|
||||||
const defaultProduct = productsPage.data[0];
|
const defaultProduct = productsPage.data[0];
|
||||||
|
|
||||||
if (defaultProduct.get('monthly_price_id')) {
|
if (defaultProduct.get('monthly_price_id')) {
|
||||||
this._logging.warn('Skipping migration, monthly_price_id already set');
|
logging.warn('Skipping migration, monthly_price_id already set');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -407,12 +399,12 @@ module.exports = class StripeMigrations {
|
||||||
}
|
}
|
||||||
|
|
||||||
async populateDefaultProductYearlyPriceId() {
|
async populateDefaultProductYearlyPriceId() {
|
||||||
this._logging.info('Migrating members_yearly_price_id setting to yearly_price_id column');
|
logging.info('Migrating members_yearly_price_id setting to yearly_price_id column');
|
||||||
const productsPage = await this._Product.findPage({limit: 1});
|
const productsPage = await this._Product.findPage({limit: 1});
|
||||||
const defaultProduct = productsPage.data[0];
|
const defaultProduct = productsPage.data[0];
|
||||||
|
|
||||||
if (defaultProduct.get('yearly_price_id')) {
|
if (defaultProduct.get('yearly_price_id')) {
|
||||||
this._logging.warn('Skipping migration, yearly_price_id already set');
|
logging.warn('Skipping migration, yearly_price_id already set');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -423,14 +415,14 @@ module.exports = class StripeMigrations {
|
||||||
}
|
}
|
||||||
|
|
||||||
async revertPortalPlansSetting() {
|
async revertPortalPlansSetting() {
|
||||||
this._logging.info('Migrating portal_plans setting from ids to names');
|
logging.info('Migrating portal_plans setting from ids to names');
|
||||||
const portalPlansSetting = await this._Settings.findOne({key: 'portal_plans'});
|
const portalPlansSetting = await this._Settings.findOne({key: 'portal_plans'});
|
||||||
|
|
||||||
let portalPlans;
|
let portalPlans;
|
||||||
try {
|
try {
|
||||||
portalPlans = JSON.parse(portalPlansSetting.get('value'));
|
portalPlans = JSON.parse(portalPlansSetting.get('value'));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this._logging.error({
|
logging.error({
|
||||||
message: 'Could not parse portal_plans setting, skipping migration',
|
message: 'Could not parse portal_plans setting, skipping migration',
|
||||||
err
|
err
|
||||||
});
|
});
|
||||||
|
@ -442,7 +434,7 @@ module.exports = class StripeMigrations {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (containsNamedValues) {
|
if (containsNamedValues) {
|
||||||
this._logging.info('The portal_plans setting already contains names, skipping migration');
|
logging.info('The portal_plans setting already contains names, skipping migration');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const portalPlanIds = portalPlans.filter((plan) => {
|
const portalPlanIds = portalPlans.filter((plan) => {
|
||||||
|
@ -450,7 +442,7 @@ module.exports = class StripeMigrations {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (portalPlanIds.length === 0) {
|
if (portalPlanIds.length === 0) {
|
||||||
this._logging.info('No price ids found in portal_plans setting, skipping migration');
|
logging.info('No price ids found in portal_plans setting, skipping migration');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const defaultPortalPlans = portalPlans.filter((plan) => {
|
const defaultPortalPlans = portalPlans.filter((plan) => {
|
||||||
|
@ -469,7 +461,7 @@ module.exports = class StripeMigrations {
|
||||||
|
|
||||||
return updatedPortalPlans;
|
return updatedPortalPlans;
|
||||||
}, defaultPortalPlans);
|
}, defaultPortalPlans);
|
||||||
this._logging.info(`Updating portal_plans setting to ${JSON.stringify(newPortalPlans)}`);
|
logging.info(`Updating portal_plans setting to ${JSON.stringify(newPortalPlans)}`);
|
||||||
await this._Settings.edit({
|
await this._Settings.edit({
|
||||||
key: 'portal_plans',
|
key: 'portal_plans',
|
||||||
value: JSON.stringify(newPortalPlans)
|
value: JSON.stringify(newPortalPlans)
|
||||||
|
@ -486,13 +478,13 @@ module.exports = class StripeMigrations {
|
||||||
return !sub.toJSON().price;
|
return !sub.toJSON().price;
|
||||||
});
|
});
|
||||||
if (invalidSubscriptions.length > 0) {
|
if (invalidSubscriptions.length > 0) {
|
||||||
this._logging.warn(`Deleting ${invalidSubscriptions.length} invalid subscription(s)`);
|
logging.warn(`Deleting ${invalidSubscriptions.length} invalid subscription(s)`);
|
||||||
for (let sub of invalidSubscriptions) {
|
for (let sub of invalidSubscriptions) {
|
||||||
this._logging.warn(`Deleting subscription - ${sub.id} - no price found`);
|
logging.warn(`Deleting subscription - ${sub.id} - no price found`);
|
||||||
await sub.destroy();
|
await sub.destroy();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this._logging.info(`No invalid subscriptions, skipping migration`);
|
logging.info(`No invalid subscriptions, skipping migration`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,15 +4,13 @@ module.exports = class EventRepository {
|
||||||
MemberPaymentEvent,
|
MemberPaymentEvent,
|
||||||
MemberStatusEvent,
|
MemberStatusEvent,
|
||||||
MemberLoginEvent,
|
MemberLoginEvent,
|
||||||
MemberPaidSubscriptionEvent,
|
MemberPaidSubscriptionEvent
|
||||||
logger
|
|
||||||
}) {
|
}) {
|
||||||
this._MemberSubscribeEvent = MemberSubscribeEvent;
|
this._MemberSubscribeEvent = MemberSubscribeEvent;
|
||||||
this._MemberPaidSubscriptionEvent = MemberPaidSubscriptionEvent;
|
this._MemberPaidSubscriptionEvent = MemberPaidSubscriptionEvent;
|
||||||
this._MemberPaymentEvent = MemberPaymentEvent;
|
this._MemberPaymentEvent = MemberPaymentEvent;
|
||||||
this._MemberStatusEvent = MemberStatusEvent;
|
this._MemberStatusEvent = MemberStatusEvent;
|
||||||
this._MemberLoginEvent = MemberLoginEvent;
|
this._MemberLoginEvent = MemberLoginEvent;
|
||||||
this._logging = logger;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async registerPayment(data) {
|
async registerPayment(data) {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const errors = require('@tryghost/errors');
|
const errors = require('@tryghost/errors');
|
||||||
|
const logging = require('@tryghost/logging');
|
||||||
const tpl = require('@tryghost/tpl');
|
const tpl = require('@tryghost/tpl');
|
||||||
const DomainEvents = require('@tryghost/domain-events');
|
const DomainEvents = require('@tryghost/domain-events');
|
||||||
const {SubscriptionCreatedEvent} = require('@tryghost/member-events');
|
const {SubscriptionCreatedEvent} = require('@tryghost/member-events');
|
||||||
|
@ -33,7 +34,6 @@ module.exports = class MemberRepository {
|
||||||
* @param {any} deps.productRepository
|
* @param {any} deps.productRepository
|
||||||
* @param {import('../../services/stripe-api')} deps.stripeAPIService
|
* @param {import('../../services/stripe-api')} deps.stripeAPIService
|
||||||
* @param {ITokenService} deps.tokenService
|
* @param {ITokenService} deps.tokenService
|
||||||
* @param {any} deps.logger
|
|
||||||
*/
|
*/
|
||||||
constructor({
|
constructor({
|
||||||
Member,
|
Member,
|
||||||
|
@ -47,8 +47,7 @@ module.exports = class MemberRepository {
|
||||||
OfferRedemption,
|
OfferRedemption,
|
||||||
stripeAPIService,
|
stripeAPIService,
|
||||||
productRepository,
|
productRepository,
|
||||||
tokenService,
|
tokenService
|
||||||
logger
|
|
||||||
}) {
|
}) {
|
||||||
this._Member = Member;
|
this._Member = Member;
|
||||||
this._MemberSubscribeEvent = MemberSubscribeEvent;
|
this._MemberSubscribeEvent = MemberSubscribeEvent;
|
||||||
|
@ -61,7 +60,6 @@ module.exports = class MemberRepository {
|
||||||
this._stripeAPIService = stripeAPIService;
|
this._stripeAPIService = stripeAPIService;
|
||||||
this._productRepository = productRepository;
|
this._productRepository = productRepository;
|
||||||
this.tokenService = tokenService;
|
this.tokenService = tokenService;
|
||||||
this._logging = logger;
|
|
||||||
|
|
||||||
DomainEvents.subscribe(SubscriptionCreatedEvent, async function (event) {
|
DomainEvents.subscribe(SubscriptionCreatedEvent, async function (event) {
|
||||||
if (!event.data.offerId) {
|
if (!event.data.offerId) {
|
||||||
|
@ -586,11 +584,11 @@ module.exports = class MemberRepository {
|
||||||
}, options);
|
}, options);
|
||||||
} else {
|
} else {
|
||||||
// Log error if no Ghost products found
|
// Log error if no Ghost products found
|
||||||
this._logging.error(`There was an error linking subscription - ${subscription.id}, no Products exist.`);
|
logging.error(`There was an error linking subscription - ${subscription.id}, no Products exist.`);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this._logging.error(`Failed to handle prices and product for - ${subscription.id}.`);
|
logging.error(`Failed to handle prices and product for - ${subscription.id}.`);
|
||||||
this._logging.error(e);
|
logging.error(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
const subscriptionData = {
|
const subscriptionData = {
|
||||||
|
@ -679,8 +677,8 @@ module.exports = class MemberRepository {
|
||||||
activeSubscriptionForChangedProduct = true;
|
activeSubscriptionForChangedProduct = true;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this._logging.error(`Failed to attach products to member - ${data.id}`);
|
logging.error(`Failed to attach products to member - ${data.id}`);
|
||||||
this._logging.error(e);
|
logging.error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -704,8 +702,8 @@ module.exports = class MemberRepository {
|
||||||
activeSubscriptionForGhostProduct = true;
|
activeSubscriptionForGhostProduct = true;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this._logging.error(`Failed to attach products to member - ${data.id}`);
|
logging.error(`Failed to attach products to member - ${data.id}`);
|
||||||
this._logging.error(e);
|
logging.error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -730,8 +728,8 @@ module.exports = class MemberRepository {
|
||||||
// Edit member with updated products assoicated
|
// Edit member with updated products assoicated
|
||||||
updatedMember = await this._Member.edit({status: status, products: memberProducts}, {...options, id: data.id});
|
updatedMember = await this._Member.edit({status: status, products: memberProducts}, {...options, id: data.id});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this._logging.error(`Failed to update member - ${data.id} - with related products`);
|
logging.error(`Failed to update member - ${data.id} - with related products`);
|
||||||
this._logging.error(e);
|
logging.error(e);
|
||||||
updatedMember = await this._Member.edit({status: status}, {...options, id: data.id});
|
updatedMember = await this._Member.edit({status: status}, {...options, id: data.id});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -909,7 +907,7 @@ module.exports = class MemberRepository {
|
||||||
const fetchedCustomer = await this._stripeAPIService.getCustomer(customer.get('customer_id'));
|
const fetchedCustomer = await this._stripeAPIService.getCustomer(customer.get('customer_id'));
|
||||||
stripeCustomer = fetchedCustomer;
|
stripeCustomer = fetchedCustomer;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this._logging.info('Ignoring error for fetching customer for checkout');
|
logging.info('Ignoring error for fetching customer for checkout');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1072,8 +1070,8 @@ module.exports = class MemberRepository {
|
||||||
subscription: updatedSubscription
|
subscription: updatedSubscription
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this._logging.error(`There was an error cancelling subscription ${subscription.get('subscription_id')}`);
|
logging.error(`There was an error cancelling subscription ${subscription.get('subscription_id')}`);
|
||||||
this._logging.error(err);
|
logging.error(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,8 +28,8 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tryghost/debug": "^0.1.2",
|
"@tryghost/debug": "^0.1.2",
|
||||||
"@tryghost/domain-events": "^0.1.3",
|
"@tryghost/domain-events": "^0.1.3",
|
||||||
"@tryghost/errors": "^0.2.9",
|
"@tryghost/errors": "^1.1.0",
|
||||||
"@tryghost/ignition-errors": "^0.1.2",
|
"@tryghost/logging": "^1.0.2",
|
||||||
"@tryghost/magic-link": "^1.0.14",
|
"@tryghost/magic-link": "^1.0.14",
|
||||||
"@tryghost/member-analytics-service": "^0.1.4",
|
"@tryghost/member-analytics-service": "^0.1.4",
|
||||||
"@tryghost/member-events": "^0.3.1",
|
"@tryghost/member-events": "^0.3.1",
|
||||||
|
|
|
@ -5,7 +5,7 @@ const debug = require('@tryghost/debug')('members-ssr');
|
||||||
const {
|
const {
|
||||||
BadRequestError,
|
BadRequestError,
|
||||||
IncorrectUsageError
|
IncorrectUsageError
|
||||||
} = require('@tryghost/ignition-errors');
|
} = require('@tryghost/errors');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {import('http').IncomingMessage} Request
|
* @typedef {import('http').IncomingMessage} Request
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tryghost/debug": "^0.1.2",
|
"@tryghost/debug": "^0.1.2",
|
||||||
"@tryghost/ignition-errors": "^0.1.2",
|
"@tryghost/errors": "^1.1.0",
|
||||||
"bluebird": "^3.5.3",
|
"bluebird": "^3.5.3",
|
||||||
"concat-stream": "^2.0.0",
|
"concat-stream": "^2.0.0",
|
||||||
"cookies": "^0.8.0",
|
"cookies": "^0.8.0",
|
||||||
|
|
|
@ -15,13 +15,6 @@ const STRIPE_API_VERSION = '2020-08-27';
|
||||||
* @typedef {import('stripe').Stripe.WebhookEndpoint} IWebhookEndpoint
|
* @typedef {import('stripe').Stripe.WebhookEndpoint} IWebhookEndpoint
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {object} ILogger
|
|
||||||
* @prop {(x: any) => void} error
|
|
||||||
* @prop {(x: any) => void} info
|
|
||||||
* @prop {(x: any) => void} warn
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {object} IStripeServiceConfig
|
* @typedef {object} IStripeServiceConfig
|
||||||
* @prop {string} secretKey
|
* @prop {string} secretKey
|
||||||
|
@ -39,13 +32,11 @@ module.exports = class StripeService {
|
||||||
* StripeService
|
* StripeService
|
||||||
*
|
*
|
||||||
* @param {object} params
|
* @param {object} params
|
||||||
* @param {ILogger} params.logger
|
|
||||||
* @param {IStripeServiceConfig} params.config
|
* @param {IStripeServiceConfig} params.config
|
||||||
*/
|
*/
|
||||||
constructor({config, logger}) {
|
constructor({config}) {
|
||||||
/** @type {Stripe} */
|
/** @type {Stripe} */
|
||||||
this._stripe = null;
|
this._stripe = null;
|
||||||
this.logging = logger;
|
|
||||||
this._configured = false;
|
this._configured = false;
|
||||||
if (config.secretKey) {
|
if (config.secretKey) {
|
||||||
this.configure(config);
|
this.configure(config);
|
||||||
|
|
Loading…
Add table
Reference in a new issue