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')
|
2021-11-10 08:40:18 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2019-05-29 05:21:06 -04:00
|
|
|
const GeoIpLookup = require('../../infrastructure/GeoIpLookup')
|
|
|
|
const FeaturesUpdater = require('./FeaturesUpdater')
|
|
|
|
const planFeatures = require('./planFeatures')
|
2022-06-03 04:06:43 -04:00
|
|
|
const plansV2Config = require('./plansV2Config')
|
2019-05-29 05:21:06 -04:00
|
|
|
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 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')
|
2022-02-01 05:54:59 -05:00
|
|
|
const SplitTestHandler = require('../SplitTests/SplitTestHandler')
|
2022-05-19 05:12:48 -04:00
|
|
|
const SubscriptionHelper = require('./SubscriptionHelper')
|
2022-06-13 05:33:26 -04:00
|
|
|
const interstitialPaymentConfig = require('./interstitialPaymentConfig')
|
2021-09-22 07:13:18 -04:00
|
|
|
|
|
|
|
const groupPlanModalOptions = Settings.groupPlanModalOptions
|
|
|
|
const validGroupPlanModalOptions = {
|
|
|
|
plan_code: groupPlanModalOptions.plan_codes.map(item => item.code),
|
|
|
|
currency: groupPlanModalOptions.currencies.map(item => item.code),
|
|
|
|
size: groupPlanModalOptions.sizes,
|
|
|
|
usage: groupPlanModalOptions.usages.map(item => item.code),
|
|
|
|
}
|
2021-05-19 04:29:21 -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
|
|
|
|
2022-06-03 06:13:21 -04:00
|
|
|
let recommendedCurrency
|
|
|
|
if (req.query.currency) {
|
|
|
|
const queryCurrency = req.query.currency.toUpperCase()
|
|
|
|
if (GeoIpLookup.isValidCurrencyParam(queryCurrency)) {
|
|
|
|
recommendedCurrency = queryCurrency
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!recommendedCurrency) {
|
|
|
|
const currencyLookup = await GeoIpLookup.promises.getCurrencyCode(
|
2022-01-10 05:23:05 -05:00
|
|
|
(req.query ? req.query.ip : undefined) || req.ip
|
|
|
|
)
|
2022-06-03 06:13:21 -04:00
|
|
|
recommendedCurrency = currencyLookup.currencyCode
|
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
|
2021-09-22 07:13:18 -04:00
|
|
|
function getDefault(param, category, defaultValue) {
|
|
|
|
const v = req.query && req.query[param]
|
|
|
|
if (v && validGroupPlanModalOptions[category].includes(v)) {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return defaultValue
|
|
|
|
}
|
2022-05-19 07:57:36 -04:00
|
|
|
const newPlansPageAssignmentV2 =
|
|
|
|
await SplitTestHandler.promises.getAssignment(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
'plans-page-layout-v2'
|
|
|
|
)
|
|
|
|
|
|
|
|
const newPlansPageVariantV2 =
|
|
|
|
newPlansPageAssignmentV2 &&
|
|
|
|
newPlansPageAssignmentV2.variant === 'new-plans-page'
|
2021-09-22 07:13:18 -04:00
|
|
|
|
|
|
|
let defaultGroupPlanModalCurrency = 'USD'
|
|
|
|
if (validGroupPlanModalOptions.currency.includes(recommendedCurrency)) {
|
|
|
|
defaultGroupPlanModalCurrency = recommendedCurrency
|
|
|
|
}
|
|
|
|
const groupPlanModalDefaults = {
|
|
|
|
plan_code: getDefault('plan', 'plan_code', 'collaborator'),
|
2022-05-19 07:57:36 -04:00
|
|
|
size: getDefault('number', 'size', newPlansPageVariantV2 ? '2' : '10'),
|
2021-09-22 07:13:18 -04:00
|
|
|
currency: getDefault('currency', 'currency', defaultGroupPlanModalCurrency),
|
|
|
|
usage: getDefault('usage', 'usage', 'enterprise'),
|
|
|
|
}
|
|
|
|
|
2022-02-01 05:54:13 -05:00
|
|
|
AnalyticsManager.recordEventForSession(req.session, 'plans-page-view')
|
|
|
|
|
2022-03-17 09:10:19 -04:00
|
|
|
const standardPlanNameAssignment =
|
2022-04-22 06:42:57 -04:00
|
|
|
await SplitTestHandler.promises.getAssignment(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
'standard-plan-name'
|
|
|
|
)
|
2022-03-17 09:10:19 -04:00
|
|
|
|
|
|
|
const useNewPlanName =
|
|
|
|
standardPlanNameAssignment &&
|
|
|
|
standardPlanNameAssignment.variant === 'new-plan-name'
|
|
|
|
|
2022-05-12 07:38:02 -04:00
|
|
|
const template = newPlansPageVariantV2
|
|
|
|
? 'subscriptions/plans-marketing-v2'
|
|
|
|
: 'subscriptions/plans-marketing'
|
|
|
|
|
|
|
|
res.render(template, {
|
2021-05-26 08:26:59 -04:00
|
|
|
title: 'plans_and_pricing',
|
|
|
|
plans,
|
2021-09-22 07:13:18 -04:00
|
|
|
itm_content: req.query && req.query.itm_content,
|
|
|
|
recommendedCurrency,
|
2022-06-03 04:06:43 -04:00
|
|
|
planFeatures,
|
|
|
|
plansV2Config,
|
2021-05-26 08:26:59 -04:00
|
|
|
groupPlans: GroupPlansData,
|
2021-09-22 07:13:18 -04:00
|
|
|
groupPlanModalOptions,
|
|
|
|
groupPlanModalDefaults,
|
2022-05-19 05:13:25 -04:00
|
|
|
newPlansPageVariantV2,
|
2022-03-17 09:10:19 -04:00
|
|
|
useNewPlanName,
|
2022-05-19 05:12:48 -04:00
|
|
|
initialLocalizedGroupPrice:
|
|
|
|
SubscriptionHelper.generateInitialLocalizedGroupPrice(
|
|
|
|
recommendedCurrency
|
|
|
|
),
|
2021-05-26 08:26:59 -04:00
|
|
|
})
|
|
|
|
}
|
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')
|
|
|
|
}
|
2022-01-10 05:23:05 -05:00
|
|
|
const hasSubscription =
|
|
|
|
await LimitationsManager.promises.userHasV1OrV2Subscription(user)
|
2021-05-26 08:26:59 -04:00
|
|
|
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).
|
2022-01-10 05:23:05 -05:00
|
|
|
const valid =
|
|
|
|
await SubscriptionHandler.promises.validateNoSubscriptionInRecurly(
|
|
|
|
user._id
|
|
|
|
)
|
2021-05-26 08:26:59 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 05:23:05 -05:00
|
|
|
const { currencyCode: recommendedCurrency, countryCode } =
|
|
|
|
await GeoIpLookup.promises.getCurrencyCode(
|
|
|
|
(req.query ? req.query.ip : undefined) || req.ip
|
|
|
|
)
|
2021-05-26 08:26:59 -04:00
|
|
|
if (recommendedCurrency && currency == null) {
|
|
|
|
currency = recommendedCurrency
|
|
|
|
}
|
2022-02-10 04:52:03 -05:00
|
|
|
const assignment = await SplitTestHandler.promises.getAssignment(
|
|
|
|
req,
|
2022-04-22 06:42:57 -04:00
|
|
|
res,
|
2022-02-10 04:52:03 -05:00
|
|
|
'payment-page'
|
|
|
|
)
|
|
|
|
const template =
|
|
|
|
assignment && assignment.variant === 'updated-payment-page'
|
|
|
|
? 'subscriptions/new-updated'
|
|
|
|
: 'subscriptions/new'
|
|
|
|
res.render(template, {
|
2021-05-26 08:26:59 -04:00
|
|
|
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,
|
|
|
|
})
|
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)
|
2022-01-10 05:23:05 -05:00
|
|
|
const results =
|
|
|
|
await SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel(
|
|
|
|
user
|
|
|
|
)
|
2021-05-26 08:37:15 -04:00
|
|
|
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
|
2022-01-10 05:23:05 -05:00
|
|
|
const hasSubscription =
|
|
|
|
await LimitationsManager.promises.userHasV1OrV2Subscription(user)
|
2021-05-26 08:37:15 -04:00
|
|
|
const fromPlansPage = req.query.hasSubscription
|
|
|
|
const plans = SubscriptionViewModelBuilder.buildPlansList(
|
|
|
|
personalSubscription ? personalSubscription.plan : undefined
|
|
|
|
)
|
2021-05-19 04:29:21 -04:00
|
|
|
|
2021-09-10 04:30:01 -04:00
|
|
|
AnalyticsManager.recordEventForSession(req.session, 'subscription-page-view')
|
2021-05-26 08:37:15 -04:00
|
|
|
|
2022-06-22 08:24:48 -04:00
|
|
|
const cancelButtonAssignment = await SplitTestHandler.promises.getAssignment(
|
2022-02-10 04:48:25 -05:00
|
|
|
req,
|
2022-04-22 06:42:57 -04:00
|
|
|
res,
|
2022-02-10 04:48:25 -05:00
|
|
|
'subscription-cancel-button'
|
|
|
|
)
|
|
|
|
|
2022-06-22 08:24:48 -04:00
|
|
|
const cancelButtonNewCopy = cancelButtonAssignment?.variant === 'new-copy'
|
|
|
|
|
|
|
|
const premiumFeaturesDiscoverabilityAssignment =
|
|
|
|
await SplitTestHandler.promises.getAssignment(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
'premium-features-discoverability'
|
|
|
|
)
|
|
|
|
|
|
|
|
const premiumFeaturesDiscoverability =
|
|
|
|
premiumFeaturesDiscoverabilityAssignment?.variant === 'active'
|
2022-02-10 04:48:25 -05:00
|
|
|
|
2021-05-26 08:37:15 -04:00
|
|
|
const data = {
|
|
|
|
title: 'your_subscription',
|
|
|
|
plans,
|
2021-09-14 08:18:37 -04:00
|
|
|
groupPlans: GroupPlansData,
|
2021-05-26 08:37:15 -04:00
|
|
|
user,
|
|
|
|
hasSubscription,
|
|
|
|
fromPlansPage,
|
|
|
|
personalSubscription,
|
|
|
|
memberGroupSubscriptions,
|
|
|
|
managedGroupSubscriptions,
|
|
|
|
managedInstitutions,
|
|
|
|
managedPublishers,
|
|
|
|
v1SubscriptionStatus,
|
2021-08-16 09:08:16 -04:00
|
|
|
currentInstitutionsWithLicence,
|
2022-01-11 09:57:03 -05:00
|
|
|
groupPlanModalOptions,
|
2022-02-10 04:48:25 -05:00
|
|
|
cancelButtonNewCopy,
|
2022-06-22 08:24:48 -04:00
|
|
|
premiumFeaturesDiscoverability,
|
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
|
|
|
|
2022-06-13 05:33:26 -04:00
|
|
|
async function interstitialPaymentPage(req, res) {
|
|
|
|
const user = SessionManager.getSessionUser(req.session)
|
|
|
|
const { currencyCode: recommendedCurrency } =
|
|
|
|
await GeoIpLookup.promises.getCurrencyCode(
|
|
|
|
(req.query ? req.query.ip : undefined) || req.ip
|
|
|
|
)
|
|
|
|
|
|
|
|
const hasSubscription =
|
|
|
|
await LimitationsManager.promises.userHasV1OrV2Subscription(user)
|
|
|
|
|
|
|
|
if (hasSubscription) {
|
|
|
|
res.redirect('/user/subscription?hasSubscription=true')
|
|
|
|
} else {
|
|
|
|
res.render('subscriptions/interstitial-payment', {
|
|
|
|
title: 'subscribe',
|
|
|
|
itm_content: req.query && req.query.itm_content,
|
|
|
|
recommendedCurrency,
|
|
|
|
interstitialPaymentConfig,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:36:49 -04:00
|
|
|
async function successfulSubscription(req, res) {
|
2021-07-28 04:51:20 -04:00
|
|
|
const user = SessionManager.getSessionUser(req.session)
|
2022-06-14 05:36:49 -04:00
|
|
|
const { personalSubscription } =
|
|
|
|
await SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel(
|
|
|
|
user
|
|
|
|
)
|
|
|
|
|
2022-06-17 07:55:05 -04:00
|
|
|
const premiumFeaturesDiscoverabilityAssignment =
|
|
|
|
await SplitTestHandler.promises.getAssignment(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
'premium-features-discoverability'
|
|
|
|
)
|
|
|
|
|
|
|
|
const premiumFeaturesDiscoverability =
|
|
|
|
premiumFeaturesDiscoverabilityAssignment?.variant === 'active'
|
|
|
|
|
2022-06-14 05:36:49 -04:00
|
|
|
if (!personalSubscription) {
|
|
|
|
res.redirect('/user/subscription/plans')
|
|
|
|
} else {
|
|
|
|
res.render('subscriptions/successful_subscription', {
|
|
|
|
title: 'thank_you',
|
|
|
|
personalSubscription,
|
2022-06-17 07:55:05 -04:00
|
|
|
premiumFeaturesDiscoverability,
|
2022-06-14 05:36:49 -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)
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ user_id: user._id }, 'canceling subscription')
|
2021-05-26 08:26:59 -04:00
|
|
|
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)
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ userId }, 'canceling v1 subscription')
|
2021-05-26 08:26:59 -04:00
|
|
|
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)
|
|
|
|
}
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ planCode, user_id: user._id }, 'updating subscription')
|
2021-05-26 08:26:59 -04:00
|
|
|
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)
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ user_id: user._id }, 'canceling pending subscription change')
|
2021-05-26 08:26:59 -04:00
|
|
|
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)
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ user_id: user._id }, 'reactivating subscription')
|
2021-05-26 08:26:59 -04:00
|
|
|
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) {
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ data: req.body }, 'received recurly callback')
|
2021-05-26 08:26:59 -04:00
|
|
|
const event = Object.keys(req.body)[0]
|
|
|
|
const eventData = req.body[event]
|
2021-06-10 04:04:30 -04:00
|
|
|
|
2022-02-02 04:27:42 -05:00
|
|
|
RecurlyEventHandler.sendRecurlyAnalyticsEvent(event, eventData).catch(error =>
|
|
|
|
logger.error(
|
|
|
|
{ err: error },
|
|
|
|
'Failed to process analytics event on Recurly webhook'
|
|
|
|
)
|
|
|
|
)
|
2021-06-10 04:04:30 -04:00
|
|
|
|
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`
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
2021-05-26 08:26:59 -04:00
|
|
|
{ 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)
|
2022-01-10 05:23:05 -05: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)
|
2021-09-16 04:48:01 -04:00
|
|
|
AnalyticsManager.recordEventForSession(
|
|
|
|
req.session,
|
|
|
|
'subscription-trial-extended'
|
|
|
|
)
|
2021-05-26 08:26:59 -04:00
|
|
|
} 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
|
2021-09-20 08:20:14 -04:00
|
|
|
await FeaturesUpdater.promises.refreshFeatures(userId, 'acceptance-test')
|
2021-05-26 08:26:59 -04:00
|
|
|
res.sendStatus(200)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2021-12-14 08:26:07 -05:00
|
|
|
async function redirectToHostedPage(req, res) {
|
|
|
|
const userId = SessionManager.getLoggedInUserId(req.session)
|
|
|
|
const { pageType } = req.params
|
2022-01-10 05:23:05 -05:00
|
|
|
const url =
|
|
|
|
await SubscriptionViewModelBuilder.promises.getRedirectToHostedPage(
|
|
|
|
userId,
|
|
|
|
pageType
|
|
|
|
)
|
2021-12-14 08:26:07 -05:00
|
|
|
logger.warn({ userId, pageType }, 'redirecting to recurly hosted page')
|
|
|
|
res.redirect(url)
|
|
|
|
}
|
|
|
|
|
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),
|
2022-06-13 05:33:26 -04:00
|
|
|
interstitialPaymentPage: expressify(interstitialPaymentPage),
|
2021-05-26 08:26:59 -04:00
|
|
|
createSubscription,
|
2022-06-14 05:36:49 -04:00
|
|
|
successfulSubscription: expressify(successfulSubscription),
|
2021-05-26 08:26:59 -04:00
|
|
|
cancelSubscription,
|
|
|
|
canceledSubscription,
|
|
|
|
cancelV1Subscription,
|
|
|
|
updateSubscription,
|
|
|
|
cancelPendingSubscriptionChange,
|
|
|
|
updateAccountEmailAddress,
|
|
|
|
reactivateSubscription,
|
|
|
|
recurlyCallback,
|
|
|
|
renderUpgradeToAnnualPlanPage,
|
|
|
|
processUpgradeToAnnualPlan,
|
|
|
|
extendTrial: expressify(extendTrial),
|
|
|
|
recurlyNotificationParser,
|
|
|
|
refreshUserFeatures: expressify(refreshUserFeatures),
|
2021-12-14 08:26:07 -05:00
|
|
|
redirectToHostedPage: expressify(redirectToHostedPage),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|