2021-07-28 04:51:20 -04:00
|
|
|
const SessionManager = require('../Authentication/SessionManager')
|
2019-05-29 05:21:06 -04:00
|
|
|
const SubscriptionHandler = require('./SubscriptionHandler')
|
|
|
|
const PlansLocator = require('./PlansLocator')
|
|
|
|
const SubscriptionViewModelBuilder = require('./SubscriptionViewModelBuilder')
|
|
|
|
const LimitationsManager = require('./LimitationsManager')
|
|
|
|
const RecurlyWrapper = require('./RecurlyWrapper')
|
2021-07-07 05:38:56 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2019-05-29 05:21:06 -04:00
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const GeoIpLookup = require('../../infrastructure/GeoIpLookup')
|
|
|
|
const FeaturesUpdater = require('./FeaturesUpdater')
|
|
|
|
const planFeatures = require('./planFeatures')
|
|
|
|
const GroupPlansData = require('./GroupPlansData')
|
|
|
|
const V1SubscriptionManager = require('./V1SubscriptionManager')
|
2019-12-16 05:52:21 -05:00
|
|
|
const Errors = require('../Errors/Errors')
|
2020-07-16 02:47:46 -04:00
|
|
|
const HttpErrorHandler = require('../Errors/HttpErrorHandler')
|
2019-08-22 07:57:50 -04:00
|
|
|
const SubscriptionErrors = require('./Errors')
|
2021-05-19 04:29:21 -04:00
|
|
|
const SplitTestHandler = require('../SplitTests/SplitTestHandler')
|
|
|
|
const AnalyticsManager = require('../Analytics/AnalyticsManager')
|
2021-06-10 04:04:30 -04:00
|
|
|
const RecurlyEventHandler = require('./RecurlyEventHandler')
|
2021-05-26 08:26:59 -04:00
|
|
|
const { expressify } = require('../../util/promises')
|
2020-07-16 02:47:46 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2021-05-19 04:29:21 -04:00
|
|
|
|
|
|
|
const SUBSCRIPTION_PAGE_SPLIT_TEST = 'subscription-page'
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
async function plansPage(req, res) {
|
|
|
|
const plans = SubscriptionViewModelBuilder.buildPlansList()
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-06-23 09:14:16 -04:00
|
|
|
const {
|
|
|
|
currencyCode: recommendedCurrency,
|
|
|
|
} = await GeoIpLookup.promises.getCurrencyCode(
|
2021-05-26 08:26:59 -04:00
|
|
|
(req.query ? req.query.ip : undefined) || req.ip
|
|
|
|
)
|
|
|
|
|
|
|
|
res.render('subscriptions/plans', {
|
|
|
|
title: 'plans_and_pricing',
|
|
|
|
plans,
|
|
|
|
gaExperiments: Settings.gaExperiments.plansPage,
|
|
|
|
gaOptimize: true,
|
|
|
|
recomendedCurrency: recommendedCurrency,
|
|
|
|
planFeatures,
|
|
|
|
groupPlans: GroupPlansData,
|
|
|
|
})
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
// get to show the recurly.js page
|
|
|
|
async function paymentPage(req, res) {
|
2021-07-28 04:51:20 -04:00
|
|
|
const user = SessionManager.getSessionUser(req.session)
|
2021-05-26 08:26:59 -04:00
|
|
|
const plan = PlansLocator.findLocalPlanInSettings(req.query.planCode)
|
|
|
|
if (!plan) {
|
|
|
|
return HttpErrorHandler.unprocessableEntity(req, res, 'Plan not found')
|
|
|
|
}
|
|
|
|
const hasSubscription = await LimitationsManager.promises.userHasV1OrV2Subscription(
|
|
|
|
user
|
|
|
|
)
|
|
|
|
if (hasSubscription) {
|
|
|
|
res.redirect('/user/subscription?hasSubscription=true')
|
|
|
|
} else {
|
|
|
|
// LimitationsManager.userHasV2Subscription only checks Mongo. Double check with
|
|
|
|
// Recurly as well at this point (we don't do this most places for speed).
|
|
|
|
const valid = await SubscriptionHandler.promises.validateNoSubscriptionInRecurly(
|
|
|
|
user._id
|
|
|
|
)
|
|
|
|
if (!valid) {
|
|
|
|
res.redirect('/user/subscription?hasSubscription=true')
|
|
|
|
} else {
|
2021-08-24 04:54:22 -04:00
|
|
|
let currency = null
|
|
|
|
if (req.query.currency) {
|
|
|
|
const queryCurrency = req.query.currency.toUpperCase()
|
|
|
|
if (GeoIpLookup.isValidCurrencyParam(queryCurrency)) {
|
|
|
|
currency = queryCurrency
|
|
|
|
}
|
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
const {
|
2021-06-23 09:14:16 -04:00
|
|
|
currencyCode: recommendedCurrency,
|
2021-05-26 08:26:59 -04:00
|
|
|
countryCode,
|
|
|
|
} = await GeoIpLookup.promises.getCurrencyCode(
|
|
|
|
(req.query ? req.query.ip : undefined) || req.ip
|
|
|
|
)
|
|
|
|
if (recommendedCurrency && currency == null) {
|
|
|
|
currency = recommendedCurrency
|
|
|
|
}
|
|
|
|
res.render('subscriptions/new', {
|
|
|
|
title: 'subscribe',
|
|
|
|
currency,
|
|
|
|
countryCode,
|
|
|
|
plan,
|
|
|
|
showStudentPlan: req.query.ssp === 'true',
|
|
|
|
recurlyConfig: JSON.stringify({
|
|
|
|
currency,
|
|
|
|
subdomain: Settings.apis.recurly.subdomain,
|
|
|
|
}),
|
|
|
|
showCouponField: !!req.query.scf,
|
|
|
|
showVatField: !!req.query.svf,
|
|
|
|
gaOptimize: true,
|
|
|
|
})
|
2020-03-30 14:12:32 -04:00
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 08:37:15 -04:00
|
|
|
async function userSubscriptionPage(req, res) {
|
2021-07-28 04:51:20 -04:00
|
|
|
const user = SessionManager.getSessionUser(req.session)
|
2021-05-26 08:37:15 -04:00
|
|
|
const results = await SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel(
|
|
|
|
user
|
|
|
|
)
|
|
|
|
const {
|
|
|
|
personalSubscription,
|
|
|
|
memberGroupSubscriptions,
|
|
|
|
managedGroupSubscriptions,
|
2021-08-16 09:08:16 -04:00
|
|
|
currentInstitutionsWithLicence,
|
2021-05-26 08:37:15 -04:00
|
|
|
managedInstitutions,
|
|
|
|
managedPublishers,
|
|
|
|
v1SubscriptionStatus,
|
|
|
|
} = results
|
|
|
|
const hasSubscription = await LimitationsManager.promises.userHasV1OrV2Subscription(
|
|
|
|
user
|
|
|
|
)
|
|
|
|
const fromPlansPage = req.query.hasSubscription
|
|
|
|
const plans = SubscriptionViewModelBuilder.buildPlansList(
|
|
|
|
personalSubscription ? personalSubscription.plan : undefined
|
|
|
|
)
|
2021-05-19 04:29:21 -04:00
|
|
|
|
2021-05-26 08:37:15 -04:00
|
|
|
let subscriptionCopy = 'default'
|
|
|
|
if (
|
|
|
|
personalSubscription ||
|
|
|
|
hasSubscription ||
|
|
|
|
(memberGroupSubscriptions && memberGroupSubscriptions.length > 0) ||
|
2021-08-16 09:08:16 -04:00
|
|
|
currentInstitutionsWithLicence.length > 0
|
2021-05-26 08:37:15 -04:00
|
|
|
) {
|
|
|
|
AnalyticsManager.recordEvent(user._id, 'subscription-page-view')
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
const testSegmentation = await SplitTestHandler.promises.getTestSegmentation(
|
|
|
|
user._id,
|
|
|
|
SUBSCRIPTION_PAGE_SPLIT_TEST
|
|
|
|
)
|
|
|
|
if (testSegmentation.enabled) {
|
|
|
|
subscriptionCopy = testSegmentation.variant
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:37:15 -04:00
|
|
|
AnalyticsManager.recordEvent(user._id, 'subscription-page-view', {
|
|
|
|
splitTestId: SUBSCRIPTION_PAGE_SPLIT_TEST,
|
|
|
|
splitTestVariantId: testSegmentation.variant,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
AnalyticsManager.recordEvent(user._id, 'subscription-page-view')
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
|
|
|
{ err: error },
|
|
|
|
`Failed to get segmentation for user '${user._id}' and split test '${SUBSCRIPTION_PAGE_SPLIT_TEST}'`
|
2021-05-26 08:26:59 -04:00
|
|
|
)
|
2021-05-26 08:37:15 -04:00
|
|
|
AnalyticsManager.recordEvent(user._id, 'subscription-page-view')
|
2019-08-22 07:57:50 -04:00
|
|
|
}
|
2021-05-26 08:37:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
title: 'your_subscription',
|
|
|
|
plans,
|
|
|
|
user,
|
|
|
|
hasSubscription,
|
|
|
|
subscriptionCopy,
|
|
|
|
fromPlansPage,
|
|
|
|
personalSubscription,
|
|
|
|
memberGroupSubscriptions,
|
|
|
|
managedGroupSubscriptions,
|
|
|
|
managedInstitutions,
|
|
|
|
managedPublishers,
|
|
|
|
v1SubscriptionStatus,
|
2021-08-16 09:08:16 -04:00
|
|
|
currentInstitutionsWithLicence,
|
2021-05-26 08:37:15 -04:00
|
|
|
}
|
|
|
|
res.render('subscriptions/dashboard', data)
|
2021-05-26 08:26:59 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
function createSubscription(req, res, next) {
|
2021-07-28 04:51:20 -04:00
|
|
|
const user = SessionManager.getSessionUser(req.session)
|
2021-05-26 08:26:59 -04:00
|
|
|
const recurlyTokenIds = {
|
|
|
|
billing: req.body.recurly_token_id,
|
|
|
|
threeDSecureActionResult:
|
|
|
|
req.body.recurly_three_d_secure_action_result_token_id,
|
|
|
|
}
|
|
|
|
const { subscriptionDetails } = req.body
|
2021-04-14 09:17:21 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
LimitationsManager.userHasV1OrV2Subscription(
|
|
|
|
user,
|
|
|
|
function (err, hasSubscription) {
|
|
|
|
if (err) {
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
if (hasSubscription) {
|
|
|
|
logger.warn({ user_id: user._id }, 'user already has subscription')
|
|
|
|
return res.sendStatus(409) // conflict
|
|
|
|
}
|
|
|
|
return SubscriptionHandler.createSubscription(
|
|
|
|
user,
|
|
|
|
subscriptionDetails,
|
|
|
|
recurlyTokenIds,
|
|
|
|
function (err) {
|
|
|
|
if (!err) {
|
|
|
|
return res.sendStatus(201)
|
|
|
|
}
|
2019-08-22 07:57:50 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
if (
|
|
|
|
err instanceof SubscriptionErrors.RecurlyTransactionError ||
|
|
|
|
err instanceof Errors.InvalidError
|
|
|
|
) {
|
|
|
|
logger.error({ err }, 'recurly transaction error, potential 422')
|
|
|
|
HttpErrorHandler.unprocessableEntity(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
err.message,
|
|
|
|
OError.getFullInfo(err).public
|
|
|
|
)
|
|
|
|
} else {
|
2021-04-14 09:17:21 -04:00
|
|
|
logger.warn(
|
|
|
|
{ err, user_id: user._id },
|
|
|
|
'something went wrong creating subscription'
|
2019-12-16 05:52:21 -05:00
|
|
|
)
|
2021-04-14 09:17:21 -04:00
|
|
|
next(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function successfulSubscription(req, res, next) {
|
2021-07-28 04:51:20 -04:00
|
|
|
const user = SessionManager.getSessionUser(req.session)
|
2021-05-26 08:26:59 -04:00
|
|
|
return SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel(
|
|
|
|
user,
|
|
|
|
function (error, { personalSubscription }) {
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
if (personalSubscription == null) {
|
|
|
|
res.redirect('/user/subscription/plans')
|
|
|
|
} else {
|
|
|
|
res.render('subscriptions/successful_subscription', {
|
2019-05-29 05:21:06 -04:00
|
|
|
title: 'thank_you',
|
2021-04-27 03:52:58 -04:00
|
|
|
personalSubscription,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function cancelSubscription(req, res, next) {
|
2021-07-28 04:51:20 -04:00
|
|
|
const user = SessionManager.getSessionUser(req.session)
|
2021-05-26 08:26:59 -04:00
|
|
|
logger.log({ user_id: user._id }, 'canceling subscription')
|
|
|
|
SubscriptionHandler.cancelSubscription(user, function (err) {
|
|
|
|
if (err) {
|
|
|
|
OError.tag(err, 'something went wrong canceling subscription', {
|
|
|
|
user_id: user._id,
|
|
|
|
})
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
// Note: this redirect isn't used in the main flow as the redirection is
|
|
|
|
// handled by Angular
|
|
|
|
res.redirect('/user/subscription/canceled')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function canceledSubscription(req, res, next) {
|
|
|
|
return res.render('subscriptions/canceled_subscription', {
|
|
|
|
title: 'subscription_canceled',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function cancelV1Subscription(req, res, next) {
|
2021-07-28 04:51:20 -04:00
|
|
|
const userId = SessionManager.getLoggedInUserId(req.session)
|
2021-05-26 08:26:59 -04:00
|
|
|
logger.log({ userId }, 'canceling v1 subscription')
|
|
|
|
V1SubscriptionManager.cancelV1Subscription(userId, function (err) {
|
|
|
|
if (err) {
|
|
|
|
OError.tag(err, 'something went wrong canceling v1 subscription', {
|
|
|
|
userId,
|
|
|
|
})
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
res.redirect('/user/subscription')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateSubscription(req, res, next) {
|
|
|
|
const origin = req && req.query ? req.query.origin : null
|
2021-07-28 04:51:20 -04:00
|
|
|
const user = SessionManager.getSessionUser(req.session)
|
2021-05-26 08:26:59 -04:00
|
|
|
const planCode = req.body.plan_code
|
|
|
|
if (planCode == null) {
|
|
|
|
const err = new Error('plan_code is not defined')
|
|
|
|
logger.warn(
|
|
|
|
{ user_id: user._id, err, planCode, origin, body: req.body },
|
|
|
|
'[Subscription] error in updateSubscription form'
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2021-05-26 08:26:59 -04:00
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
logger.log({ planCode, user_id: user._id }, 'updating subscription')
|
|
|
|
SubscriptionHandler.updateSubscription(user, planCode, null, function (err) {
|
|
|
|
if (err) {
|
|
|
|
OError.tag(err, 'something went wrong updating subscription', {
|
|
|
|
user_id: user._id,
|
|
|
|
})
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
res.redirect('/user/subscription')
|
|
|
|
})
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
function cancelPendingSubscriptionChange(req, res, next) {
|
2021-07-28 04:51:20 -04:00
|
|
|
const user = SessionManager.getSessionUser(req.session)
|
2021-05-26 08:26:59 -04:00
|
|
|
logger.log({ user_id: user._id }, 'canceling pending subscription change')
|
|
|
|
SubscriptionHandler.cancelPendingSubscriptionChange(user, function (err) {
|
|
|
|
if (err) {
|
|
|
|
OError.tag(
|
|
|
|
err,
|
|
|
|
'something went wrong canceling pending subscription change',
|
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
user_id: user._id,
|
2021-05-26 08:26:59 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
res.redirect('/user/subscription')
|
|
|
|
})
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
function updateAccountEmailAddress(req, res, next) {
|
2021-07-28 04:51:20 -04:00
|
|
|
const user = SessionManager.getSessionUser(req.session)
|
2021-05-26 08:26:59 -04:00
|
|
|
RecurlyWrapper.updateAccountEmailAddress(
|
|
|
|
user._id,
|
|
|
|
user.email,
|
|
|
|
function (error) {
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
function reactivateSubscription(req, res, next) {
|
2021-07-28 04:51:20 -04:00
|
|
|
const user = SessionManager.getSessionUser(req.session)
|
2021-05-26 08:26:59 -04:00
|
|
|
logger.log({ user_id: user._id }, 'reactivating subscription')
|
|
|
|
SubscriptionHandler.reactivateSubscription(user, function (err) {
|
|
|
|
if (err) {
|
|
|
|
OError.tag(err, 'something went wrong reactivating subscription', {
|
|
|
|
user_id: user._id,
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(err)
|
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
res.redirect('/user/subscription')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function recurlyCallback(req, res, next) {
|
|
|
|
logger.log({ data: req.body }, 'received recurly callback')
|
|
|
|
const event = Object.keys(req.body)[0]
|
|
|
|
const eventData = req.body[event]
|
2021-06-10 04:04:30 -04:00
|
|
|
|
|
|
|
RecurlyEventHandler.sendRecurlyAnalyticsEvent(event, eventData)
|
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
if (
|
|
|
|
[
|
|
|
|
'new_subscription_notification',
|
|
|
|
'updated_subscription_notification',
|
|
|
|
'expired_subscription_notification',
|
|
|
|
].includes(event)
|
|
|
|
) {
|
|
|
|
const recurlySubscription = eventData.subscription
|
|
|
|
SubscriptionHandler.syncSubscription(
|
|
|
|
recurlySubscription,
|
|
|
|
{ ip: req.ip },
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err) {
|
2021-05-26 08:26:59 -04:00
|
|
|
if (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(err)
|
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
res.sendStatus(200)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
2021-05-26 08:26:59 -04:00
|
|
|
} else if (event === 'billing_info_updated_notification') {
|
|
|
|
const recurlyAccountCode = eventData.account.account_code
|
|
|
|
SubscriptionHandler.attemptPaypalInvoiceCollection(
|
|
|
|
recurlyAccountCode,
|
|
|
|
function (err) {
|
|
|
|
if (err) {
|
|
|
|
return next(err)
|
2021-04-14 09:17:21 -04:00
|
|
|
}
|
|
|
|
res.sendStatus(200)
|
2020-02-20 11:08:30 -05:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
)
|
2021-05-26 08:26:59 -04:00
|
|
|
} else {
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 11:08:30 -05:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
function renderUpgradeToAnnualPlanPage(req, res, next) {
|
2021-07-28 04:51:20 -04:00
|
|
|
const user = SessionManager.getSessionUser(req.session)
|
2021-05-26 08:26:59 -04:00
|
|
|
LimitationsManager.userHasV2Subscription(
|
|
|
|
user,
|
|
|
|
function (err, hasSubscription, subscription) {
|
|
|
|
let planName
|
|
|
|
if (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(err)
|
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
const planCode = subscription
|
|
|
|
? subscription.planCode.toLowerCase()
|
|
|
|
: undefined
|
|
|
|
if ((planCode ? planCode.indexOf('annual') : undefined) !== -1) {
|
|
|
|
planName = 'annual'
|
|
|
|
} else if ((planCode ? planCode.indexOf('student') : undefined) !== -1) {
|
|
|
|
planName = 'student'
|
|
|
|
} else if (
|
|
|
|
(planCode ? planCode.indexOf('collaborator') : undefined) !== -1
|
|
|
|
) {
|
|
|
|
planName = 'collaborator'
|
|
|
|
}
|
|
|
|
if (hasSubscription) {
|
|
|
|
res.render('subscriptions/upgradeToAnnual', {
|
2021-04-14 09:17:21 -04:00
|
|
|
title: 'Upgrade to annual',
|
2021-04-27 03:52:58 -04:00
|
|
|
planName,
|
2021-04-14 09:17:21 -04:00
|
|
|
})
|
2021-05-26 08:26:59 -04:00
|
|
|
} else {
|
|
|
|
res.redirect('/user/subscription/plans')
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
function processUpgradeToAnnualPlan(req, res, next) {
|
2021-07-28 04:51:20 -04:00
|
|
|
const user = SessionManager.getSessionUser(req.session)
|
2021-05-26 08:26:59 -04:00
|
|
|
const { planName } = req.body
|
|
|
|
const couponCode = Settings.coupon_codes.upgradeToAnnualPromo[planName]
|
|
|
|
const annualPlanName = `${planName}-annual`
|
|
|
|
logger.log(
|
|
|
|
{ user_id: user._id, planName: annualPlanName },
|
|
|
|
'user is upgrading to annual billing with discount'
|
|
|
|
)
|
|
|
|
return SubscriptionHandler.updateSubscription(
|
|
|
|
user,
|
|
|
|
annualPlanName,
|
|
|
|
couponCode,
|
|
|
|
function (err) {
|
|
|
|
if (err) {
|
|
|
|
OError.tag(err, 'error updating subscription', {
|
|
|
|
user_id: user._id,
|
|
|
|
})
|
|
|
|
return next(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
async function extendTrial(req, res) {
|
2021-07-28 04:51:20 -04:00
|
|
|
const user = SessionManager.getSessionUser(req.session)
|
2021-05-26 08:26:59 -04:00
|
|
|
const {
|
|
|
|
subscription,
|
|
|
|
} = await LimitationsManager.promises.userHasV2Subscription(user)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
try {
|
|
|
|
await SubscriptionHandler.promises.extendTrial(subscription, 14)
|
|
|
|
} catch (error) {
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
function recurlyNotificationParser(req, res, next) {
|
|
|
|
let xml = ''
|
|
|
|
req.on('data', chunk => (xml += chunk))
|
|
|
|
req.on('end', () =>
|
|
|
|
RecurlyWrapper._parseXml(xml, function (error, body) {
|
|
|
|
if (error) {
|
|
|
|
return next(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
req.body = body
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function refreshUserFeatures(req, res) {
|
|
|
|
const { user_id: userId } = req.params
|
|
|
|
await FeaturesUpdater.promises.refreshFeatures(userId)
|
|
|
|
res.sendStatus(200)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
module.exports = {
|
|
|
|
plansPage: expressify(plansPage),
|
|
|
|
paymentPage: expressify(paymentPage),
|
2021-05-26 08:37:15 -04:00
|
|
|
userSubscriptionPage: expressify(userSubscriptionPage),
|
2021-05-26 08:26:59 -04:00
|
|
|
createSubscription,
|
|
|
|
successfulSubscription,
|
|
|
|
cancelSubscription,
|
|
|
|
canceledSubscription,
|
|
|
|
cancelV1Subscription,
|
|
|
|
updateSubscription,
|
|
|
|
cancelPendingSubscriptionChange,
|
|
|
|
updateAccountEmailAddress,
|
|
|
|
reactivateSubscription,
|
|
|
|
recurlyCallback,
|
|
|
|
renderUpgradeToAnnualPlanPage,
|
|
|
|
processUpgradeToAnnualPlan,
|
|
|
|
extendTrial: expressify(extendTrial),
|
|
|
|
recurlyNotificationParser,
|
|
|
|
refreshUserFeatures: expressify(refreshUserFeatures),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|