From f3e0cec0d2e52c67583d4d1c2d6afad1a3efdb51 Mon Sep 17 00:00:00 2001 From: Aileen Booker Date: Tue, 6 Jun 2023 12:46:03 -0400 Subject: [PATCH] Added `StripeLiveEnabledEvent` and `StripeLiveDisabledEvent` to Stripe service no issue - This is preparation work to be able to listen to events whenever Stripe gets enabled in live mode or disabled again. - This creates two new event types, which are being dispatched with `DomainEvents` from the Stripe service on the `connect()` and - vice versa - the `disconnect()` methods. --- ghost/stripe/lib/StripeLiveDisabledEvent.js | 23 +++++++++++++++++++++ ghost/stripe/lib/StripeLiveEnabledEvent.js | 23 +++++++++++++++++++++ ghost/stripe/lib/StripeService.js | 6 ++++++ 3 files changed, 52 insertions(+) create mode 100644 ghost/stripe/lib/StripeLiveDisabledEvent.js create mode 100644 ghost/stripe/lib/StripeLiveEnabledEvent.js diff --git a/ghost/stripe/lib/StripeLiveDisabledEvent.js b/ghost/stripe/lib/StripeLiveDisabledEvent.js new file mode 100644 index 0000000000..c25949b19a --- /dev/null +++ b/ghost/stripe/lib/StripeLiveDisabledEvent.js @@ -0,0 +1,23 @@ +/** + * @typedef {object} StripeLiveDisabledEventData + * @prop {string?} message + */ + +module.exports = class StripeLiveDisabledEvent { + /** + * @param {StripeLiveDisabledEventData} data + * @param {Date} timestamp + */ + constructor(data, timestamp) { + this.data = data; + this.timestamp = timestamp; + } + + /** + * @param {StripeLiveDisabledEventData} data + * @param {Date} [timestamp] + */ + static create(data, timestamp) { + return new StripeLiveDisabledEvent(data, timestamp || new Date); + } +}; diff --git a/ghost/stripe/lib/StripeLiveEnabledEvent.js b/ghost/stripe/lib/StripeLiveEnabledEvent.js new file mode 100644 index 0000000000..1df50bd45f --- /dev/null +++ b/ghost/stripe/lib/StripeLiveEnabledEvent.js @@ -0,0 +1,23 @@ +/** + * @typedef {object} StripeLiveEnabledEventData + * @prop {string} message + */ + +module.exports = class StripeLiveEnabledEvent { + /** + * @param {StripeLiveEnabledEventData} data + * @param {Date} timestamp + */ + constructor(data, timestamp) { + this.data = data; + this.timestamp = timestamp; + } + + /** + * @param {StripeLiveEnabledEventData} data + * @param {Date} [timestamp] + */ + static create(data, timestamp) { + return new StripeLiveEnabledEvent(data, timestamp || new Date); + } +}; diff --git a/ghost/stripe/lib/StripeService.js b/ghost/stripe/lib/StripeService.js index 0e81a54998..194b0b53f6 100644 --- a/ghost/stripe/lib/StripeService.js +++ b/ghost/stripe/lib/StripeService.js @@ -2,6 +2,9 @@ const WebhookManager = require('./WebhookManager'); const StripeAPI = require('./StripeAPI'); const StripeMigrations = require('./StripeMigrations'); const WebhookController = require('./WebhookController'); +const DomainEvents = require('@tryghost/domain-events'); +const StripeLiveEnabledEvent = require('./StripeLiveEnabledEvent'); +const StripeLiveDisabledEvent = require('./StripeLiveDisabledEvent'); module.exports = class StripeService { constructor({ @@ -50,6 +53,7 @@ module.exports = class StripeService { } async connect() { + DomainEvents.dispatch(StripeLiveEnabledEvent.create({message: 'Stripe Live Mode Enabled'})); } async disconnect() { @@ -66,6 +70,8 @@ module.exports = class StripeService { await this.webhookManager.stop(); this.api.configure(null); + + DomainEvents.dispatch(StripeLiveDisabledEvent.create({message: 'Stripe Live Mode Disabled'})); } async configure(config) {