diff --git a/core/server/api/canary/membersStripeConnect.js b/core/server/api/canary/membersStripeConnect.js index 67925e56f7..f1baf3474e 100644 --- a/core/server/api/canary/membersStripeConnect.js +++ b/core/server/api/canary/membersStripeConnect.js @@ -1,7 +1,4 @@ const membersService = require('../../services/members'); -const config = require('../../../shared/config'); -const urlUtils = require('../../../shared/url-utils'); -const {BadRequestError} = require('@tryghost/errors'); module.exports = { docName: 'members_stripe_connect', @@ -18,13 +15,6 @@ module.exports = { } }, query(frame) { - const siteUrl = urlUtils.getSiteUrl(); - const productionMode = config.get('env') === 'production'; - const siteUrlUsingSSL = /^https/.test(siteUrl); - const cannotConnectToStripe = productionMode && !siteUrlUsingSSL; - if (cannotConnectToStripe) { - throw new BadRequestError('Cannot connect to stripe unless site is using https://'); - } // This is something you have to do if you want to use the "framework" with access to the raw req/res frame.response = async function (req, res) { function setSessionProp(prop, val) { diff --git a/core/server/services/members/stripe-connect.js b/core/server/services/members/stripe-connect.js index 4dd27f6cc1..2a1ac2b095 100644 --- a/core/server/services/members/stripe-connect.js +++ b/core/server/services/members/stripe-connect.js @@ -4,6 +4,9 @@ const {Buffer} = require('buffer'); const {randomBytes} = require('crypto'); const {URL} = require('url'); +const config = require('../../../shared/config'); +const urlUtils = require('../../../shared/url-utils'); + const messages = { incorrectState: 'State did not match.' }; @@ -24,6 +27,7 @@ const redirectURI = 'https://stripe.ghost.org'; * @returns {Promise} */ async function getStripeConnectOAuthUrl(setSessionProp, mode = 'live') { + checkCanConnect(); const randomState = randomBytes(16).toString('hex'); const state = Buffer.from(JSON.stringify({ mode, @@ -71,6 +75,16 @@ async function getStripeConnectTokenData(encodedData, getSessionProp) { }; } +function checkCanConnect() { + const siteUrl = urlUtils.getSiteUrl(); + const productionMode = config.get('env') === 'production'; + const siteUrlUsingSSL = /^https/.test(siteUrl); + const cannotConnectToStripe = productionMode && !siteUrlUsingSSL; + if (cannotConnectToStripe) { + throw new errors.BadRequestError('Cannot connect to stripe unless site is using https://'); + } +} + module.exports = { getStripeConnectOAuthUrl, getStripeConnectTokenData,