2022-01-14 12:05:19 +03:00
|
|
|
const WebhookManager = require('./WebhookManager');
|
|
|
|
const StripeAPI = require('./StripeAPI');
|
|
|
|
const StripeMigrations = require('./Migrations');
|
2022-01-17 15:17:52 +03:00
|
|
|
const WebhookController = require('./WebhookController');
|
2022-01-14 12:05:19 +03:00
|
|
|
|
|
|
|
module.exports = class StripeService {
|
|
|
|
constructor({
|
2022-01-17 15:17:52 +03:00
|
|
|
membersService,
|
2022-01-14 12:05:19 +03:00
|
|
|
StripeWebhook,
|
|
|
|
models
|
|
|
|
}) {
|
|
|
|
const api = new StripeAPI();
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
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() {
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
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();
|
|
|
|
}
|
|
|
|
};
|