2021-07-14 17:31:29 +03:00
|
|
|
const common = require('../../lib/common');
|
2021-01-18 16:55:40 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
module.exports = class RouterController {
|
2021-10-21 14:35:29 +03:00
|
|
|
/**
|
|
|
|
* RouterController
|
|
|
|
*
|
|
|
|
* @param {object} deps
|
|
|
|
* @param {any} deps.offersAPI
|
|
|
|
* @param {any} deps.paymentsService
|
|
|
|
* @param {any} deps.productRepository
|
|
|
|
* @param {any} deps.memberRepository
|
|
|
|
* @param {any} deps.StripePrice
|
|
|
|
* @param {boolean} deps.allowSelfSignup
|
|
|
|
* @param {any} deps.magicLinkService
|
|
|
|
* @param {import('@tryghost/members-stripe-service')} deps.stripeAPIService
|
|
|
|
* @param {any} deps.tokenService
|
|
|
|
* @param {{isSet(name: string): boolean}} deps.labsService
|
|
|
|
* @param {any} deps.config
|
|
|
|
* @param {any} deps.logging
|
|
|
|
*/
|
2021-01-18 16:55:40 +03:00
|
|
|
constructor({
|
2021-10-13 12:11:12 +03:00
|
|
|
offersAPI,
|
2021-10-21 14:35:29 +03:00
|
|
|
paymentsService,
|
2021-10-06 16:01:04 +03:00
|
|
|
productRepository,
|
2021-01-18 16:55:40 +03:00
|
|
|
memberRepository,
|
2021-05-04 18:57:58 +03:00
|
|
|
StripePrice,
|
2021-01-18 16:55:40 +03:00
|
|
|
allowSelfSignup,
|
|
|
|
magicLinkService,
|
|
|
|
stripeAPIService,
|
|
|
|
tokenService,
|
2021-01-27 18:19:09 +03:00
|
|
|
sendEmailWithMagicLink,
|
2021-09-28 14:36:30 +03:00
|
|
|
labsService,
|
2021-07-14 13:37:43 +03:00
|
|
|
config,
|
|
|
|
logging
|
2021-01-18 16:55:40 +03:00
|
|
|
}) {
|
2021-10-13 12:11:12 +03:00
|
|
|
this._offersAPI = offersAPI;
|
2021-10-21 14:35:29 +03:00
|
|
|
this._paymentsService = paymentsService;
|
2021-10-06 16:01:04 +03:00
|
|
|
this._productRepository = productRepository;
|
2021-01-18 16:55:40 +03:00
|
|
|
this._memberRepository = memberRepository;
|
2021-05-04 18:57:58 +03:00
|
|
|
this._StripePrice = StripePrice;
|
2021-01-18 16:55:40 +03:00
|
|
|
this._allowSelfSignup = allowSelfSignup;
|
|
|
|
this._magicLinkService = magicLinkService;
|
|
|
|
this._stripeAPIService = stripeAPIService;
|
|
|
|
this._tokenService = tokenService;
|
|
|
|
this._sendEmailWithMagicLink = sendEmailWithMagicLink;
|
2021-09-28 14:36:30 +03:00
|
|
|
this.labsService = labsService;
|
2021-01-27 18:19:09 +03:00
|
|
|
this._config = config;
|
2021-07-14 13:37:43 +03:00
|
|
|
this._logging = logging;
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async ensureStripe(_req, res, next) {
|
2021-01-26 14:26:28 +03:00
|
|
|
if (!this._stripeAPIService.configured) {
|
2021-01-18 16:55:40 +03:00
|
|
|
res.writeHead(400);
|
|
|
|
return res.end('Stripe not configured');
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await this._stripeAPIService.ready();
|
|
|
|
next();
|
|
|
|
} catch (err) {
|
|
|
|
res.writeHead(500);
|
|
|
|
return res.end('There was an error configuring stripe');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async createCheckoutSetupSession(req, res) {
|
|
|
|
const identity = req.body.identity;
|
|
|
|
|
2021-01-19 13:42:39 +03:00
|
|
|
if (!identity) {
|
|
|
|
res.writeHead(400);
|
|
|
|
return res.end();
|
|
|
|
}
|
|
|
|
|
2021-01-18 16:55:40 +03:00
|
|
|
let email;
|
|
|
|
try {
|
|
|
|
if (!identity) {
|
|
|
|
email = null;
|
|
|
|
} else {
|
|
|
|
const claims = await this._tokenService.decodeToken(identity);
|
|
|
|
email = claims && claims.sub;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
res.writeHead(401);
|
|
|
|
return res.end('Unauthorized');
|
|
|
|
}
|
|
|
|
|
|
|
|
const member = email ? await this._memberRepository.get({email}) : null;
|
|
|
|
|
|
|
|
if (!member) {
|
|
|
|
res.writeHead(403);
|
|
|
|
return res.end('Bad Request.');
|
|
|
|
}
|
2021-02-23 14:19:21 +03:00
|
|
|
|
|
|
|
let customer;
|
|
|
|
if (!req.body.subscription_id) {
|
|
|
|
customer = await this._stripeAPIService.getCustomerForMemberCheckoutSession(member);
|
|
|
|
} else {
|
|
|
|
const subscriptions = await member.related('stripeSubscriptions').fetch();
|
|
|
|
const subscription = subscriptions.models.find((sub) => {
|
|
|
|
return sub.get('subscription_id') === req.body.subscription_id;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!subscription) {
|
|
|
|
res.writeHead(404);
|
|
|
|
res.end(`Could not find subscription ${req.body.subscription_id}`);
|
|
|
|
}
|
|
|
|
customer = await this._stripeAPIService.getCustomer(subscription.get('customer_id'));
|
|
|
|
}
|
2021-01-18 16:55:40 +03:00
|
|
|
|
|
|
|
const session = await this._stripeAPIService.createCheckoutSetupSession(customer, {
|
2021-01-27 18:19:09 +03:00
|
|
|
successUrl: req.body.successUrl || this._config.billingSuccessUrl,
|
2021-02-23 14:19:21 +03:00
|
|
|
cancelUrl: req.body.cancelUrl || this._config.billingCancelUrl,
|
|
|
|
subscription_id: req.body.subscription_id
|
2021-01-18 16:55:40 +03:00
|
|
|
});
|
|
|
|
const publicKey = this._stripeAPIService.getPublicKey();
|
|
|
|
const sessionInfo = {
|
|
|
|
sessionId: session.id,
|
|
|
|
publicKey
|
|
|
|
};
|
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
});
|
|
|
|
|
|
|
|
res.end(JSON.stringify(sessionInfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
async createCheckoutSession(req, res) {
|
2021-10-06 16:01:04 +03:00
|
|
|
let ghostPriceId = req.body.priceId;
|
2021-01-18 16:55:40 +03:00
|
|
|
const identity = req.body.identity;
|
2021-09-28 14:36:30 +03:00
|
|
|
const offerId = req.body.offerId;
|
2021-10-18 16:27:17 +03:00
|
|
|
const metadata = req.body.metadata;
|
2021-01-18 16:55:40 +03:00
|
|
|
|
2021-10-06 16:01:04 +03:00
|
|
|
if (!ghostPriceId && !offerId) {
|
2021-01-18 16:55:40 +03:00
|
|
|
res.writeHead(400);
|
|
|
|
return res.end('Bad Request.');
|
|
|
|
}
|
|
|
|
|
2021-10-06 16:01:04 +03:00
|
|
|
if (offerId && ghostPriceId) {
|
|
|
|
res.writeHead(400);
|
|
|
|
return res.end('Bad Request.');
|
|
|
|
}
|
|
|
|
|
2021-10-21 14:35:29 +03:00
|
|
|
let couponId = null;
|
2021-11-03 17:13:11 +03:00
|
|
|
if (offerId) {
|
2021-10-06 16:01:04 +03:00
|
|
|
try {
|
2021-10-13 12:11:12 +03:00
|
|
|
const offer = await this._offersAPI.getOffer({id: offerId});
|
2021-10-06 16:01:04 +03:00
|
|
|
const tier = (await this._productRepository.get(offer.tier)).toJSON();
|
|
|
|
|
2021-10-13 12:19:35 +03:00
|
|
|
if (offer.status === 'archived') {
|
|
|
|
res.writeHead(403);
|
|
|
|
return res.end('Offer is archived.');
|
|
|
|
}
|
|
|
|
|
2021-10-13 12:11:12 +03:00
|
|
|
if (offer.cadence === 'month') {
|
2021-10-06 16:01:04 +03:00
|
|
|
ghostPriceId = tier.monthly_price_id;
|
|
|
|
} else {
|
|
|
|
ghostPriceId = tier.yearly_price_id;
|
|
|
|
}
|
|
|
|
|
2021-10-21 14:35:29 +03:00
|
|
|
const coupon = await this._paymentsService.getCouponForOffer(offerId);
|
|
|
|
couponId = coupon.id;
|
2021-10-18 16:27:17 +03:00
|
|
|
|
|
|
|
metadata.offer = offer.id;
|
2021-10-06 16:01:04 +03:00
|
|
|
} catch (err) {
|
|
|
|
res.writeHead(500);
|
|
|
|
return res.end('Could not use Offer.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 18:57:58 +03:00
|
|
|
const price = await this._StripePrice.findOne({
|
|
|
|
id: ghostPriceId
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!price) {
|
|
|
|
res.writeHead(404);
|
|
|
|
return res.end('Not Found.');
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
|
2021-05-04 18:57:58 +03:00
|
|
|
const priceId = price.get('stripe_price_id');
|
2021-01-18 16:55:40 +03:00
|
|
|
|
|
|
|
let email;
|
|
|
|
try {
|
|
|
|
if (!identity) {
|
|
|
|
email = null;
|
|
|
|
} else {
|
|
|
|
const claims = await this._tokenService.decodeToken(identity);
|
|
|
|
email = claims && claims.sub;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
res.writeHead(401);
|
|
|
|
return res.end('Unauthorized');
|
|
|
|
}
|
|
|
|
|
2021-07-02 18:42:43 +03:00
|
|
|
const member = email ? await this._memberRepository.get({email}, {withRelated: ['stripeCustomers', 'products']}) : null;
|
2021-01-18 16:55:40 +03:00
|
|
|
|
2021-11-03 11:57:28 +03:00
|
|
|
let successUrl = req.body.successUrl || this._config.checkoutSuccessUrl;
|
|
|
|
let cancelUrl = req.body.cancelUrl || this._config.checkoutCancelUrl;
|
|
|
|
|
|
|
|
if (this.labsService.isSet('membersAutoLogin')) {
|
2021-11-05 11:18:43 +03:00
|
|
|
if (!member && req.body.customerEmail && !req.body.successUrl) {
|
2021-11-03 11:57:28 +03:00
|
|
|
const memberExistsForCustomer = await this._memberRepository.get({email: req.body.customerEmail});
|
|
|
|
if (!memberExistsForCustomer) {
|
|
|
|
successUrl = await this._magicLinkService.getMagicLink({
|
|
|
|
tokenData: {
|
|
|
|
email: req.body.customerEmail
|
|
|
|
},
|
|
|
|
type: 'signup'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 16:55:40 +03:00
|
|
|
if (!member) {
|
|
|
|
const customer = null;
|
2021-05-04 18:57:58 +03:00
|
|
|
const session = await this._stripeAPIService.createCheckoutSession(priceId, customer, {
|
2021-10-21 19:06:36 +03:00
|
|
|
coupon: couponId,
|
2021-11-03 11:57:28 +03:00
|
|
|
successUrl,
|
|
|
|
cancelUrl,
|
2021-01-18 16:55:40 +03:00
|
|
|
customerEmail: req.body.customerEmail,
|
2021-10-18 16:27:17 +03:00
|
|
|
metadata: metadata
|
2021-01-18 16:55:40 +03:00
|
|
|
});
|
|
|
|
const publicKey = this._stripeAPIService.getPublicKey();
|
|
|
|
|
|
|
|
const sessionInfo = {
|
|
|
|
publicKey,
|
|
|
|
sessionId: session.id
|
|
|
|
};
|
|
|
|
|
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.end(JSON.stringify(sessionInfo));
|
|
|
|
}
|
|
|
|
|
2021-07-02 18:42:43 +03:00
|
|
|
if (member.related('products').length !== 0) {
|
|
|
|
res.writeHead(403);
|
|
|
|
return res.end('No permission');
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let stripeCustomer;
|
|
|
|
|
|
|
|
for (const customer of member.related('stripeCustomers').models) {
|
|
|
|
try {
|
|
|
|
const fetchedCustomer = await this._stripeAPIService.getCustomer(customer.get('customer_id'));
|
|
|
|
if (!fetchedCustomer.deleted) {
|
|
|
|
stripeCustomer = fetchedCustomer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2021-07-14 13:37:43 +03:00
|
|
|
this._logging.info('Ignoring error for fetching customer for checkout');
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stripeCustomer) {
|
2021-01-26 14:26:57 +03:00
|
|
|
stripeCustomer = await this._stripeAPIService.createCustomer({email: member.get('email')});
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-05-04 18:57:58 +03:00
|
|
|
const session = await this._stripeAPIService.createCheckoutSession(priceId, stripeCustomer, {
|
2021-10-21 19:06:36 +03:00
|
|
|
coupon: couponId,
|
2021-11-03 11:57:28 +03:00
|
|
|
successUrl,
|
|
|
|
cancelUrl,
|
2021-10-18 16:27:17 +03:00
|
|
|
metadata: metadata
|
2021-01-18 16:55:40 +03:00
|
|
|
});
|
|
|
|
const publicKey = this._stripeAPIService.getPublicKey();
|
|
|
|
|
|
|
|
const sessionInfo = {
|
|
|
|
publicKey,
|
|
|
|
sessionId: session.id
|
|
|
|
};
|
|
|
|
|
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.end(JSON.stringify(sessionInfo));
|
|
|
|
} catch (e) {
|
|
|
|
const error = e.message || 'Unable to initiate checkout session';
|
|
|
|
res.writeHead(400);
|
|
|
|
return res.end(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendMagicLink(req, res) {
|
2021-09-22 14:32:02 +03:00
|
|
|
const {email, emailType, requestSrc} = req.body;
|
2021-01-18 16:55:40 +03:00
|
|
|
if (!email) {
|
|
|
|
res.writeHead(400);
|
|
|
|
return res.end('Bad Request.');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!this._allowSelfSignup) {
|
2021-09-22 14:32:02 +03:00
|
|
|
const member = await this._memberRepository.get({email});
|
2021-01-18 16:55:40 +03:00
|
|
|
if (member) {
|
2021-09-22 14:32:02 +03:00
|
|
|
const tokenData = {};
|
|
|
|
await this._sendEmailWithMagicLink({email, tokenData, requestedType: emailType, requestSrc});
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-22 14:32:02 +03:00
|
|
|
const tokenData = _.pick(req.body, ['labels', 'name']);
|
|
|
|
await this._sendEmailWithMagicLink({email, tokenData, requestedType: emailType, requestSrc});
|
2021-01-18 16:55:40 +03:00
|
|
|
}
|
|
|
|
res.writeHead(201);
|
|
|
|
return res.end('Created.');
|
|
|
|
} catch (err) {
|
|
|
|
const statusCode = (err && err.statusCode) || 500;
|
|
|
|
common.logging.error(err);
|
|
|
|
res.writeHead(statusCode);
|
|
|
|
return res.end('Internal Server Error.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|