2021-12-02 17:46:58 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-03-25 15:19:01 +03:00
|
|
|
|
|
|
|
module.exports = class MemberController {
|
2021-09-22 14:32:02 +03:00
|
|
|
/**
|
|
|
|
* @param {object} deps
|
|
|
|
* @param {any} deps.memberRepository
|
2022-01-21 15:08:31 +03:00
|
|
|
* @param {any} deps.productRepository
|
2021-09-22 14:32:02 +03:00
|
|
|
* @param {any} deps.StripePrice
|
|
|
|
* @param {any} deps.tokenService
|
|
|
|
* @param {any} deps.sendEmailWithMagicLink
|
|
|
|
*/
|
2021-03-25 15:19:01 +03:00
|
|
|
constructor({
|
|
|
|
memberRepository,
|
2022-01-21 15:08:31 +03:00
|
|
|
productRepository,
|
2021-05-04 18:57:58 +03:00
|
|
|
StripePrice,
|
2021-09-22 14:32:02 +03:00
|
|
|
tokenService,
|
2022-01-10 12:42:05 +03:00
|
|
|
sendEmailWithMagicLink
|
2021-03-25 15:19:01 +03:00
|
|
|
}) {
|
|
|
|
this._memberRepository = memberRepository;
|
2022-01-21 15:08:31 +03:00
|
|
|
this._productRepository = productRepository;
|
2021-05-04 18:57:58 +03:00
|
|
|
this._StripePrice = StripePrice;
|
2021-03-25 15:19:01 +03:00
|
|
|
this._tokenService = tokenService;
|
2021-09-22 14:32:02 +03:00
|
|
|
this._sendEmailWithMagicLink = sendEmailWithMagicLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateEmailAddress(req, res) {
|
|
|
|
const identity = req.body.identity;
|
|
|
|
const email = req.body.email;
|
|
|
|
const options = {
|
|
|
|
forceEmailType: true
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!identity) {
|
|
|
|
res.writeHead(403);
|
|
|
|
return res.end('No Permission.');
|
|
|
|
}
|
|
|
|
|
|
|
|
let tokenData = {};
|
|
|
|
try {
|
|
|
|
const member = await this._memberRepository.getByToken(identity);
|
|
|
|
tokenData.oldEmail = member.get('email');
|
|
|
|
} catch (err) {
|
|
|
|
res.writeHead(401);
|
2021-09-23 12:12:23 +03:00
|
|
|
return res.end('Unauthorized.');
|
2021-09-22 14:32:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this._sendEmailWithMagicLink({email, tokenData, requestedType: 'updateEmail', options});
|
|
|
|
res.writeHead(201);
|
|
|
|
return res.end('Created.');
|
|
|
|
} catch (err) {
|
|
|
|
res.writeHead(500);
|
|
|
|
return res.end('Internal Server Error.');
|
|
|
|
}
|
2021-03-25 15:19:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async updateSubscription(req, res) {
|
|
|
|
try {
|
|
|
|
const identity = req.body.identity;
|
|
|
|
const subscriptionId = req.params.id;
|
|
|
|
const cancelAtPeriodEnd = req.body.cancel_at_period_end;
|
2021-03-25 15:42:59 +03:00
|
|
|
const smartCancel = req.body.smart_cancel;
|
2021-03-25 15:19:01 +03:00
|
|
|
const cancellationReason = req.body.cancellation_reason;
|
2022-05-16 21:27:23 +03:00
|
|
|
let ghostPriceId = req.body.priceId;
|
|
|
|
const tierId = req.body.tierId;
|
|
|
|
const cadence = req.body.cadence;
|
|
|
|
|
|
|
|
if (cancelAtPeriodEnd === undefined && ghostPriceId === undefined && smartCancel === undefined && tierId === undefined && cadence === undefined) {
|
2021-03-25 15:19:01 +03:00
|
|
|
throw new errors.BadRequestError({
|
|
|
|
message: 'Updating subscription failed!',
|
2021-05-04 18:57:58 +03:00
|
|
|
help: 'Request should contain "cancel_at_period_end" or "priceId" or "smart_cancel" field.'
|
2021-03-25 15:19:01 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:42:59 +03:00
|
|
|
if ((cancelAtPeriodEnd === undefined || cancelAtPeriodEnd === false) && !smartCancel && cancellationReason !== undefined) {
|
2021-03-25 15:19:01 +03:00
|
|
|
throw new errors.BadRequestError({
|
|
|
|
message: 'Updating subscription failed!',
|
2021-03-25 15:42:59 +03:00
|
|
|
help: '"cancellation_reason" field requires the "cancel_at_period_end" or "smart_cancel" field to be true.'
|
2021-03-25 15:19:01 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cancellationReason && cancellationReason.length > 500) {
|
|
|
|
throw new errors.BadRequestError({
|
|
|
|
message: 'Updating subscription failed!',
|
|
|
|
help: '"cancellation_reason" field can be a maximum of 500 characters.'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let email;
|
|
|
|
try {
|
|
|
|
if (!identity) {
|
|
|
|
throw new errors.BadRequestError({
|
|
|
|
message: 'Updating subscription failed! Could not find member'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const claims = await this._tokenService.decodeToken(identity);
|
|
|
|
email = claims && claims.sub;
|
|
|
|
} catch (err) {
|
|
|
|
res.writeHead(401);
|
|
|
|
return res.end('Unauthorized');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!email) {
|
|
|
|
throw new errors.BadRequestError({
|
|
|
|
message: 'Invalid token'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-16 21:27:23 +03:00
|
|
|
if (tierId && cadence) {
|
|
|
|
const tier = await this._productRepository.get({id: tierId});
|
|
|
|
if (tier) {
|
|
|
|
if (cadence === 'month') {
|
|
|
|
ghostPriceId = tier.get('monthly_price_id');
|
|
|
|
} else {
|
|
|
|
ghostPriceId = tier.get('yearly_price_id');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 18:57:58 +03:00
|
|
|
if (ghostPriceId !== undefined) {
|
|
|
|
const price = await this._StripePrice.findOne({
|
|
|
|
id: ghostPriceId
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!price) {
|
|
|
|
res.writeHead(404);
|
|
|
|
return res.end('Not Found.');
|
2021-03-25 15:19:01 +03:00
|
|
|
}
|
2021-05-04 18:57:58 +03:00
|
|
|
|
|
|
|
const priceId = price.get('stripe_price_id');
|
2022-01-21 15:08:31 +03:00
|
|
|
const product = await this._productRepository.get({stripe_price_id: priceId});
|
|
|
|
|
|
|
|
if (product.get('active') !== true) {
|
|
|
|
res.writeHead(403);
|
|
|
|
return res.end('Tier is archived.');
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:19:01 +03:00
|
|
|
await this._memberRepository.updateSubscription({
|
|
|
|
email,
|
|
|
|
subscription: {
|
|
|
|
subscription_id: subscriptionId,
|
2021-05-04 18:57:58 +03:00
|
|
|
price: priceId
|
2021-03-25 15:19:01 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (cancelAtPeriodEnd !== undefined) {
|
|
|
|
await this._memberRepository.updateSubscription({
|
|
|
|
email,
|
|
|
|
subscription: {
|
|
|
|
subscription_id: subscriptionId,
|
|
|
|
cancel_at_period_end: cancelAtPeriodEnd,
|
|
|
|
cancellationReason
|
|
|
|
}
|
|
|
|
});
|
2021-03-25 15:42:59 +03:00
|
|
|
} else if (smartCancel) {
|
|
|
|
const currentSubscription = await this._memberRepository.getSubscription({
|
|
|
|
email,
|
|
|
|
subscription: {
|
|
|
|
subscription_id: subscriptionId
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (['past_due', 'unpaid'].includes(currentSubscription.status)) {
|
|
|
|
await this._memberRepository.cancelSubscription({
|
|
|
|
email,
|
|
|
|
subscription: {
|
|
|
|
subscription_id: subscriptionId,
|
|
|
|
cancellationReason
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await this._memberRepository.updateSubscription({
|
|
|
|
email,
|
|
|
|
subscription: {
|
|
|
|
subscription_id: subscriptionId,
|
|
|
|
cancel_at_period_end: true,
|
|
|
|
cancellationReason
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-03-25 15:19:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
res.writeHead(204);
|
|
|
|
res.end();
|
|
|
|
} catch (err) {
|
2022-08-29 15:02:58 +03:00
|
|
|
res.writeHead(err.statusCode || 500, {
|
|
|
|
'Content-Type': 'text/plain;charset=UTF-8'
|
|
|
|
});
|
2021-03-25 15:19:01 +03:00
|
|
|
res.end(err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|