0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00
ghost/core/server/services/stripe/config.js
Fabien 'egg' O'Carroll a565da06b2
🐛 Fixed Offer Redemptions being over counted (#13988)
refs https://github.com/TryGhost/Team/issues/1257

Offer Redemptions were being overcounted due to the way we were updating
Stripe configuration for the Members service. We would create a new
instance of the members-api, which would have event handlers for
creating Offer Redemptions - by creating a new instance each time Stripe
config changed, we would overcount them.

Here we've pulled out Stripe related logic into the Stripe service, and
updated it internally - rather than creating a new instance. This means
that we've been able to remove all of the logic for re-instantiating the
members-api.

- Bumped members-api & stripe-service
- Removed reinstantiation of members-api
- Used stripe service to execute migrations
- Updated Stripe Service to handle webhooks & migrations
- Used webhook controller from stripe service
- Used disconnect method from stripe service
- Removed unused stripe dependency
- Removed Stripe webhook config from members-api
2022-01-18 17:56:47 +02:00

72 lines
2.2 KiB
JavaScript

const logging = require('@tryghost/logging');
const tpl = require('@tryghost/tpl');
const messages = {
remoteWebhooksInDevelopment: 'Cannot use remote webhooks in development. See https://ghost.org/docs/webhooks/#stripe-webhooks for developing with Stripe.'
};
// @TODO Refactor to a class w/ constructor
module.exports = {
getConfig(settings, config, urlUtils) {
/**
* @param {'direct' | 'connect'} type - The "type" of keys to fetch from settings
* @returns {{publicKey: string, secretKey: string} | null}
*/
function getStripeKeys(type) {
const secretKey = settings.get(`stripe_${type === 'connect' ? 'connect_' : ''}secret_key`);
const publicKey = settings.get(`stripe_${type === 'connect' ? 'connect_' : ''}publishable_key`);
if (!secretKey || !publicKey) {
return null;
}
return {
secretKey,
publicKey
};
}
/**
* @returns {{publicKey: string, secretKey: string} | null}
*/
function getActiveStripeKeys() {
const stripeDirect = config.get('stripeDirect');
if (stripeDirect) {
return getStripeKeys('direct');
}
const connectKeys = getStripeKeys('connect');
if (!connectKeys) {
return getStripeKeys('direct');
}
return connectKeys;
}
const keys = getActiveStripeKeys();
if (!keys) {
return null;
}
const env = config.get('env');
let webhookSecret = process.env.WEBHOOK_SECRET;
if (env !== 'production') {
if (!webhookSecret) {
webhookSecret = 'DEFAULT_WEBHOOK_SECRET';
logging.warn(tpl(messages.remoteWebhooksInDevelopment));
}
}
const webhookHandlerUrl = new URL('members/webhooks/stripe/', urlUtils.getSiteUrl());
return {
secretKey: keys.secretKey,
publicKey: keys.publicKey,
enablePromoCodes: config.get('enableStripePromoCodes'),
webhookSecret: webhookSecret,
webhookHandlerUrl: webhookHandlerUrl.href
};
}
};