2022-01-14 12:05:19 +03:00
|
|
|
const WebhookManager = require('./WebhookManager');
|
|
|
|
const StripeAPI = require('./StripeAPI');
|
2023-05-02 23:43:47 +03:00
|
|
|
const StripeMigrations = require('./StripeMigrations');
|
2022-01-17 15:17:52 +03:00
|
|
|
const WebhookController = require('./WebhookController');
|
2023-06-06 19:46:03 +03:00
|
|
|
const DomainEvents = require('@tryghost/domain-events');
|
2023-06-08 17:59:30 +03:00
|
|
|
const {StripeLiveEnabledEvent, StripeLiveDisabledEvent} = require('./events');
|
2022-01-14 12:05:19 +03:00
|
|
|
|
|
|
|
module.exports = class StripeService {
|
|
|
|
constructor({
|
2024-04-04 17:17:14 +03:00
|
|
|
labs,
|
2022-01-17 15:17:52 +03:00
|
|
|
membersService,
|
2023-07-31 19:00:52 +03:00
|
|
|
donationService,
|
2023-08-07 16:36:59 +03:00
|
|
|
staffService,
|
2022-01-14 12:05:19 +03:00
|
|
|
StripeWebhook,
|
|
|
|
models
|
|
|
|
}) {
|
2024-04-04 17:17:14 +03:00
|
|
|
const api = new StripeAPI({labs});
|
2022-01-14 12:05:19 +03:00
|
|
|
const webhookManager = new WebhookManager({
|
|
|
|
StripeWebhook,
|
|
|
|
api
|
|
|
|
});
|
|
|
|
const migrations = new StripeMigrations({
|
|
|
|
models,
|
|
|
|
api
|
|
|
|
});
|
2022-01-17 15:17:52 +03:00
|
|
|
const webhookController = new WebhookController({
|
|
|
|
webhookManager,
|
|
|
|
api,
|
|
|
|
get memberRepository(){
|
|
|
|
return membersService.api.members;
|
|
|
|
},
|
|
|
|
get productRepository() {
|
|
|
|
return membersService.api.productRepository;
|
|
|
|
},
|
|
|
|
get eventRepository() {
|
|
|
|
return membersService.api.events;
|
|
|
|
},
|
2023-07-31 19:00:52 +03:00
|
|
|
get donationRepository() {
|
|
|
|
return donationService.repository;
|
|
|
|
},
|
2023-08-07 16:36:59 +03:00
|
|
|
get staffServiceEmails() {
|
|
|
|
return staffService.api.emails;
|
|
|
|
},
|
2022-01-17 15:17:52 +03:00
|
|
|
sendSignupEmail(email){
|
2022-01-26 23:21:21 +03:00
|
|
|
return membersService.api.sendEmailWithMagicLink({
|
2022-01-17 15:17:52 +03:00
|
|
|
email,
|
2022-02-01 12:54:16 +03:00
|
|
|
requestedType: 'signup-paid',
|
2022-01-17 15:17:52 +03:00
|
|
|
options: {
|
|
|
|
forceEmailType: true
|
|
|
|
},
|
|
|
|
tokenData: {}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2022-01-14 12:05:19 +03:00
|
|
|
|
|
|
|
this.models = models;
|
|
|
|
this.api = api;
|
|
|
|
this.webhookManager = webhookManager;
|
|
|
|
this.migrations = migrations;
|
2022-01-17 15:17:52 +03:00
|
|
|
this.webhookController = webhookController;
|
2022-01-14 12:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async connect() {
|
2023-06-06 19:46:03 +03:00
|
|
|
DomainEvents.dispatch(StripeLiveEnabledEvent.create({message: 'Stripe Live Mode Enabled'}));
|
2022-01-14 12:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async disconnect() {
|
|
|
|
await this.models.Product.forge().query().update({
|
|
|
|
monthly_price_id: null,
|
|
|
|
yearly_price_id: null
|
|
|
|
});
|
|
|
|
await this.models.StripePrice.forge().query().del();
|
|
|
|
await this.models.StripeProduct.forge().query().del();
|
|
|
|
await this.models.MemberStripeCustomer.forge().query().del();
|
|
|
|
await this.models.Offer.forge().query().update({
|
|
|
|
stripe_coupon_id: null
|
|
|
|
});
|
|
|
|
await this.webhookManager.stop();
|
2022-02-18 11:47:23 +03:00
|
|
|
|
|
|
|
this.api.configure(null);
|
2023-06-06 19:46:03 +03:00
|
|
|
|
|
|
|
DomainEvents.dispatch(StripeLiveDisabledEvent.create({message: 'Stripe Live Mode Disabled'}));
|
2022-01-14 12:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async configure(config) {
|
|
|
|
this.api.configure({
|
|
|
|
secretKey: config.secretKey,
|
|
|
|
publicKey: config.publicKey,
|
2022-02-09 16:00:39 +03:00
|
|
|
enablePromoCodes: config.enablePromoCodes,
|
2023-02-28 10:09:52 +03:00
|
|
|
get enableAutomaticTax() {
|
|
|
|
return config.enableAutomaticTax;
|
|
|
|
},
|
2022-02-09 16:00:39 +03:00
|
|
|
checkoutSessionSuccessUrl: config.checkoutSessionSuccessUrl,
|
|
|
|
checkoutSessionCancelUrl: config.checkoutSessionCancelUrl,
|
|
|
|
checkoutSetupSessionSuccessUrl: config.checkoutSetupSessionSuccessUrl,
|
2022-04-04 08:31:53 +03:00
|
|
|
checkoutSetupSessionCancelUrl: config.checkoutSetupSessionCancelUrl,
|
|
|
|
testEnv: config.testEnv
|
2022-01-14 12:05:19 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
await this.webhookManager.configure({
|
|
|
|
webhookSecret: config.webhookSecret,
|
|
|
|
webhookHandlerUrl: config.webhookHandlerUrl
|
|
|
|
});
|
|
|
|
await this.webhookManager.start();
|
|
|
|
}
|
|
|
|
};
|