2021-10-04 14:18:22 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const logging = require('@tryghost/logging');
|
|
|
|
const StripeAPIService = require('@tryghost/members-stripe-service');
|
|
|
|
|
|
|
|
const config = require('../../../shared/config');
|
|
|
|
const settings = require('../../../shared/settings-cache');
|
|
|
|
const events = require('../../lib/common/events');
|
|
|
|
|
|
|
|
const {getConfig} = require('./config');
|
|
|
|
|
|
|
|
const api = new StripeAPIService({
|
|
|
|
logger: logging,
|
|
|
|
config: {}
|
|
|
|
});
|
|
|
|
|
|
|
|
const stripeKeySettings = [
|
|
|
|
'stripe_publishable_key',
|
|
|
|
'stripe_secret_key',
|
|
|
|
'stripe_connect_publishable_key',
|
|
|
|
'stripe_connect_secret_key'
|
|
|
|
];
|
|
|
|
|
|
|
|
function configureApi() {
|
|
|
|
const cfg = getConfig(settings, config);
|
|
|
|
if (cfg) {
|
|
|
|
api.configure(cfg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-10 15:03:03 +03:00
|
|
|
const debouncedConfigureApi = _.debounce(() => {
|
|
|
|
configureApi();
|
|
|
|
events.emit('services.stripe.reconfigured');
|
|
|
|
}, 600);
|
2021-10-04 14:18:22 +03:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
async init() {
|
|
|
|
configureApi();
|
|
|
|
events.on('settings.edited', function (model) {
|
|
|
|
if (!stripeKeySettings.includes(model.get('key'))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
debouncedConfigureApi();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
api
|
|
|
|
};
|