Hooked up Stripe service connect method on Stripe public key update

no issue

- Withing the `SettingsBREADService` we want to call the `connect` method of the Stripe Service, which then dispatches the `DomainEvent`
- In order to ensure that the Stripe service is connected in 'live' mode, we compare the public key and make sure no live key was set before so to avoid multiple triggers of this call.
This commit is contained in:
Aileen Booker 2023-06-07 13:13:56 -04:00 committed by Aileen Booker
parent f3e0cec0d2
commit 98fd6a7dbf

View File

@ -5,6 +5,7 @@ const {obfuscatedSetting, isSecretSetting, hideValueIfSecret} = require('./setti
const logging = require('@tryghost/logging');
const MagicLink = require('@tryghost/magic-link');
const verifyEmailTemplate = require('./emails/verify-email');
const stripeService = require('../../services/stripe');
const EMAIL_KEYS = ['members_support_address'];
const messages = {
@ -213,7 +214,7 @@ class SettingsBREADService {
// remove any email properties that are not allowed to be set without verification
const {filteredSettings: refilteredSettings, emailsToVerify} = await this.prepSettingsForEmailVerification(filteredSettings, getSetting);
const modelArray = await this.SettingsModel.edit(refilteredSettings, options).then((result) => {
const modelArray = await this.SettingsModel.edit(refilteredSettings, options).then(async (result) => {
// TODO: temporary fix for starting/stopping lexicalMultiplayer service when labs flag is changed
// this should be removed along with the flag, or set up in a more generic way
const labsSetting = result.find(setting => setting.get('key') === 'labs');
@ -229,6 +230,18 @@ class SettingsBREADService {
}
}
// Detect if Stripe is now connected in live mode
const stripePublicKeySetting = result.find(setting => setting.get('key') === 'stripe_connect_publishable_key');
if (stripePublicKeySetting) {
const previous = stripePublicKeySetting.previousAttributes().value;
const current = stripePublicKeySetting.get('value');
if (current?.match(/pk_test/) && (!previous?.match(/pk_test/) || !previous)) {
// This method currently only triggers a DomainEvent
await stripeService.connect();
}
}
return this._formatBrowse(_.keyBy(_.invokeMap(result, 'toJSON'), 'key'), options.context);
});