0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Moved the last of the Stripe config out of Members

refs https://github.com/TryGhost/Team/issues/1322

We no longer restart the Members service based on the Stripe service
being updated, which meant that if it was initially configured with
missing URL's and later Stripe connected, it would not get the new
config until a server restart. This moves the last of Stripe config into
the Stripe service, so that all things concerning Stripe can be handled
in one place and updated together.
This commit is contained in:
Fabien "egg" O'Carroll 2022-02-09 15:00:39 +02:00 committed by Fabien 'egg' O'Carroll
parent e5356117b4
commit 6ce441f760
6 changed files with 65 additions and 23 deletions

View file

@ -28,7 +28,6 @@ module.exports = function MembersAPI({
getSigninURL,
tokenProvider
},
paymentConfig,
mail: {
transporter,
getText,
@ -65,8 +64,6 @@ module.exports = function MembersAPI({
issuer
});
const stripeConfig = paymentConfig && paymentConfig.stripe || {};
const memberAnalyticsService = MemberAnalyticsService.create(MemberAnalyticEvent);
memberAnalyticsService.eventHandler.setupSubscribers();
@ -157,13 +154,7 @@ module.exports = function MembersAPI({
stripeAPIService,
tokenService,
sendEmailWithMagicLink,
labsService,
config: {
checkoutSuccessUrl: stripeConfig.checkoutSuccessUrl,
checkoutCancelUrl: stripeConfig.checkoutCancelUrl,
billingSuccessUrl: stripeConfig.billingSuccessUrl,
billingCancelUrl: stripeConfig.billingCancelUrl
}
labsService
});
const wellKnownController = new WellKnownController({

View file

@ -16,7 +16,6 @@ module.exports = class RouterController {
* @param {import('@tryghost/members-stripe-service')} deps.stripeAPIService
* @param {any} deps.tokenService
* @param {{isSet(name: string): boolean}} deps.labsService
* @param {any} deps.config
*/
constructor({
offersAPI,
@ -29,8 +28,7 @@ module.exports = class RouterController {
stripeAPIService,
tokenService,
sendEmailWithMagicLink,
labsService,
config
labsService
}) {
this._offersAPI = offersAPI;
this._paymentsService = paymentsService;
@ -43,7 +41,6 @@ module.exports = class RouterController {
this._tokenService = tokenService;
this._sendEmailWithMagicLink = sendEmailWithMagicLink;
this.labsService = labsService;
this._config = config;
}
async ensureStripe(_req, res, next) {
@ -105,8 +102,8 @@ module.exports = class RouterController {
}
const session = await this._stripeAPIService.createCheckoutSetupSession(customer, {
successUrl: req.body.successUrl || this._config.billingSuccessUrl,
cancelUrl: req.body.cancelUrl || this._config.billingCancelUrl,
successUrl: req.body.successUrl,
cancelUrl: req.body.cancelUrl,
subscription_id: req.body.subscription_id
});
const publicKey = this._stripeAPIService.getPublicKey();
@ -197,8 +194,8 @@ module.exports = class RouterController {
const member = email ? await this._memberRepository.get({email}, {withRelated: ['stripeCustomers', 'products']}) : null;
let successUrl = req.body.successUrl || this._config.checkoutSuccessUrl;
let cancelUrl = req.body.cancelUrl || this._config.checkoutCancelUrl;
let successUrl = req.body.successUrl;
let cancelUrl = req.body.cancelUrl;
if (!member && req.body.customerEmail && !req.body.successUrl) {
const memberExistsForCustomer = await this._memberRepository.get({email: req.body.customerEmail});

View file

@ -20,6 +20,10 @@ const STRIPE_API_VERSION = '2020-08-27';
* @prop {string} secretKey
* @prop {string} publicKey
* @prop {boolean} enablePromoCodes
* @prop {string} checkoutSessionSuccessUrl
* @prop {string} checkoutSessionCancelUrl
* @prop {string} checkoutSetupSessionSuccessUrl
* @prop {string} checkoutSetupSessionCancelUrl
*/
module.exports = class StripeAPI {
@ -353,8 +357,8 @@ module.exports = class StripeAPI {
}
const session = await this._stripe.checkout.sessions.create({
payment_method_types: ['card'],
success_url: options.successUrl,
cancel_url: options.cancelUrl,
success_url: options.successUrl || this._config.checkoutSessionSuccessUrl,
cancel_url: options.cancelUrl || this._config.checkoutSessionCancelUrl,
customer_email: customerEmail,
// @ts-ignore - we need to update to latest stripe library to correctly use newer features
allow_promotion_codes: discounts ? undefined : this._config.enablePromoCodes,
@ -391,8 +395,8 @@ module.exports = class StripeAPI {
const session = await this._stripe.checkout.sessions.create({
mode: 'setup',
payment_method_types: ['card'],
success_url: options.successUrl,
cancel_url: options.cancelUrl,
success_url: options.successUrl || this._config.checkoutSetupSessionSuccessUrl,
cancel_url: options.cancelUrl || this._config.checkoutSetupSessionCancelUrl,
customer_email: customer.email,
setup_intent_data: {
metadata: {

View file

@ -70,7 +70,11 @@ module.exports = class StripeService {
this.api.configure({
secretKey: config.secretKey,
publicKey: config.publicKey,
enablePromoCodes: config.enablePromoCodes
enablePromoCodes: config.enablePromoCodes,
checkoutSessionSuccessUrl: config.checkoutSessionSuccessUrl,
checkoutSessionCancelUrl: config.checkoutSessionCancelUrl,
checkoutSetupSessionSuccessUrl: config.checkoutSetupSessionSuccessUrl,
checkoutSetupSessionCancelUrl: config.checkoutSetupSessionCancelUrl
});
await this.webhookManager.configure({

View file

@ -22,6 +22,7 @@
"@babel/eslint-parser": "7.16.5",
"c8": "7.11.0",
"mocha": "9.1.3",
"rewire": "^6.0.0",
"should": "13.2.3",
"sinon": "11.1.2"
},

View file

@ -0,0 +1,45 @@
const sinon = require('sinon');
const should = require('should');
const rewire = require('rewire');
const StripeAPI = rewire('../../../lib/StripeAPI');
const api = new StripeAPI();
describe('StripeAPI', function () {
let mockStripe;
beforeEach(function () {
mockStripe = {
checkout: {
sessions: {
create: sinon.stub().resolves()
}
}
};
const mockStripeConstructor = sinon.stub().returns(mockStripe);
StripeAPI.__set__('Stripe', mockStripeConstructor);
api.configure({
checkoutSessionSuccessUrl: '/success',
checkoutSessionCancelUrl: '/cancel',
checkoutSetupSessionSuccessUrl: '/setup-success',
checkoutSetupSessionCancelUrl: '/setup-cancel',
secretKey: ''
});
});
afterEach(function () {
sinon.restore();
});
it('createCheckoutSession sends success_url and cancel_url', async function (){
await api.createCheckoutSession('priceId', null, {});
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.success_url);
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.cancel_url);
});
it('createCheckoutSetupSession sends success_url and cancel_url', async function (){
await api.createCheckoutSetupSession('priceId', {});
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.success_url);
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.cancel_url);
});
});