Ghost/ghost/stripe/lib/StripeService.js
Naz af4d7b4938 Fixed failing migration requests when client runs in a test env
no issue

- When Ghost is running in a test environment, it is configured with an invalid Stripe key that looks like `sk_test***`. In this case the migrations try runnig creating request to Stripe, which fail. The failures pollute the output, which makes other valid errors lost.
- An example of such error log is following:
```
Invalid API Key provided: sk_test_******ripe

----------------------------------------

Error: Invalid API Key provided: sk_test_******ripe
    at res.toJSON.then.StripeAPIError.message (/home/naz/Workspace/Ghost/Ghost/node_modules/stripe/lib/StripeResource.js:214:23)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
```
- There doesn't seem to be a good reason to do migrations in the test environment. Skipping them as a special case to fix the output pollution problem seems like a right solution
2022-04-05 21:36:22 +12:00

90 lines
2.8 KiB
JavaScript

const WebhookManager = require('./WebhookManager');
const StripeAPI = require('./StripeAPI');
const StripeMigrations = require('./Migrations');
const WebhookController = require('./WebhookController');
module.exports = class StripeService {
constructor({
membersService,
StripeWebhook,
models
}) {
const api = new StripeAPI();
const webhookManager = new WebhookManager({
StripeWebhook,
api
});
const migrations = new StripeMigrations({
models,
api
});
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){
return membersService.api.sendEmailWithMagicLink({
email,
requestedType: 'signup-paid',
options: {
forceEmailType: true
},
tokenData: {}
});
}
});
this.models = models;
this.api = api;
this.webhookManager = webhookManager;
this.migrations = migrations;
this.webhookController = webhookController;
}
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();
this.api.configure(null);
}
async configure(config) {
this.api.configure({
secretKey: config.secretKey,
publicKey: config.publicKey,
enablePromoCodes: config.enablePromoCodes,
checkoutSessionSuccessUrl: config.checkoutSessionSuccessUrl,
checkoutSessionCancelUrl: config.checkoutSessionCancelUrl,
checkoutSetupSessionSuccessUrl: config.checkoutSetupSessionSuccessUrl,
checkoutSetupSessionCancelUrl: config.checkoutSetupSessionCancelUrl,
testEnv: config.testEnv
});
await this.webhookManager.configure({
webhookSecret: config.webhookSecret,
webhookHandlerUrl: config.webhookHandlerUrl
});
await this.webhookManager.start();
}
};