2020-05-28 13:40:51 +03:00
|
|
|
const membersService = require('../../services/members');
|
2021-05-13 13:06:54 +03:00
|
|
|
const config = require('../../../shared/config');
|
|
|
|
const urlUtils = require('../../../shared/url-utils');
|
|
|
|
const {BadRequestError} = require('@tryghost/errors');
|
2020-05-28 13:40:51 +03:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
docName: 'members_stripe_connect',
|
|
|
|
auth: {
|
|
|
|
permissions: true,
|
2020-06-10 14:23:04 +03:00
|
|
|
options: [
|
|
|
|
'mode'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
mode: {
|
|
|
|
values: ['live', 'test']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-05-28 13:40:51 +03:00
|
|
|
query(frame) {
|
2021-05-13 13:06:54 +03:00
|
|
|
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://');
|
|
|
|
}
|
2020-05-28 13:40:51 +03:00
|
|
|
// 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) {
|
|
|
|
req.session[prop] = val;
|
|
|
|
}
|
2020-06-10 14:23:04 +03:00
|
|
|
const mode = frame.options.mode || 'live';
|
|
|
|
const stripeConnectAuthURL = await membersService.stripeConnect.getStripeConnectOAuthUrl(setSessionProp, mode);
|
2020-05-28 13:40:51 +03:00
|
|
|
return res.redirect(stripeConnectAuthURL);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|