2021-07-07 05:38:56 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2019-05-29 05:21:06 -04:00
|
|
|
const RecurlyWrapper = require('./RecurlyWrapper')
|
|
|
|
const PlansLocator = require('./PlansLocator')
|
|
|
|
const SubscriptionFormatters = require('./SubscriptionFormatters')
|
|
|
|
const SubscriptionLocator = require('./SubscriptionLocator')
|
|
|
|
const V1SubscriptionManager = require('./V1SubscriptionManager')
|
|
|
|
const InstitutionsGetter = require('../Institutions/InstitutionsGetter')
|
|
|
|
const PublishersGetter = require('../Publishers/PublishersGetter')
|
|
|
|
const sanitizeHtml = require('sanitize-html')
|
|
|
|
const _ = require('underscore')
|
|
|
|
const async = require('async')
|
2021-04-27 10:17:39 -04:00
|
|
|
const SubscriptionHelper = require('./SubscriptionHelper')
|
2021-05-26 08:26:59 -04:00
|
|
|
const { promisify } = require('../../util/promises')
|
2022-05-19 10:25:28 -04:00
|
|
|
const {
|
|
|
|
InvalidError,
|
|
|
|
NotFoundError,
|
|
|
|
V1ConnectionError,
|
|
|
|
} = require('../Errors/Errors')
|
2021-12-14 08:26:07 -05:00
|
|
|
|
|
|
|
function buildHostedLink(type) {
|
|
|
|
return `/user/subscription/recurly/${type}`
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getRedirectToHostedPage(userId, pageType) {
|
|
|
|
if (!['billing-details', 'account-management'].includes(pageType)) {
|
|
|
|
throw new InvalidError('unexpected page type')
|
|
|
|
}
|
2022-01-10 05:23:05 -05:00
|
|
|
const personalSubscription =
|
|
|
|
await SubscriptionLocator.promises.getUsersSubscription(userId)
|
2021-12-14 08:26:07 -05:00
|
|
|
const recurlySubscriptionId = personalSubscription?.recurlySubscription_id
|
|
|
|
if (!recurlySubscriptionId) {
|
|
|
|
throw new NotFoundError('not a recurly subscription')
|
|
|
|
}
|
|
|
|
const recurlySubscription = await RecurlyWrapper.promises.getSubscription(
|
|
|
|
recurlySubscriptionId,
|
|
|
|
{ includeAccount: true }
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-07-15 09:09:04 -04:00
|
|
|
const recurlySubdomain = Settings.apis.recurly.subdomain
|
2019-08-20 08:44:59 -04:00
|
|
|
const hostedLoginToken = recurlySubscription.account.hosted_login_token
|
2021-12-14 08:26:07 -05:00
|
|
|
if (!hostedLoginToken) {
|
|
|
|
throw new Error('recurly account does not have hosted login token')
|
|
|
|
}
|
2019-08-20 08:44:59 -04:00
|
|
|
let path = ''
|
2021-12-14 08:26:07 -05:00
|
|
|
if (pageType === 'billing-details') {
|
2019-08-20 08:44:59 -04:00
|
|
|
path = 'billing_info/edit?ht='
|
|
|
|
}
|
2021-12-14 08:26:07 -05:00
|
|
|
return [
|
|
|
|
'https://',
|
|
|
|
recurlySubdomain,
|
|
|
|
'.recurly.com/account/',
|
|
|
|
path,
|
|
|
|
hostedLoginToken,
|
|
|
|
].join('')
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
function buildUsersSubscriptionViewModel(user, callback) {
|
|
|
|
async.auto(
|
|
|
|
{
|
|
|
|
personalSubscription(cb) {
|
|
|
|
SubscriptionLocator.getUsersSubscription(user, cb)
|
|
|
|
},
|
|
|
|
recurlySubscription: [
|
|
|
|
'personalSubscription',
|
|
|
|
(cb, { personalSubscription }) => {
|
|
|
|
if (
|
|
|
|
personalSubscription == null ||
|
|
|
|
personalSubscription.recurlySubscription_id == null ||
|
|
|
|
personalSubscription.recurlySubscription_id === ''
|
|
|
|
) {
|
|
|
|
return cb(null, null)
|
|
|
|
}
|
|
|
|
RecurlyWrapper.getSubscription(
|
|
|
|
personalSubscription.recurlySubscription_id,
|
|
|
|
{ includeAccount: true },
|
|
|
|
cb
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
2021-05-26 08:26:59 -04:00
|
|
|
],
|
|
|
|
recurlyCoupons: [
|
|
|
|
'recurlySubscription',
|
|
|
|
(cb, { recurlySubscription }) => {
|
|
|
|
if (!recurlySubscription) {
|
|
|
|
return cb(null, null)
|
|
|
|
}
|
|
|
|
const accountId = recurlySubscription.account.account_code
|
|
|
|
RecurlyWrapper.getAccountActiveCoupons(accountId, cb)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
2021-05-26 08:26:59 -04:00
|
|
|
],
|
|
|
|
plan: [
|
|
|
|
'personalSubscription',
|
|
|
|
(cb, { personalSubscription }) => {
|
|
|
|
if (personalSubscription == null) {
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
const plan = PlansLocator.findLocalPlanInSettings(
|
|
|
|
personalSubscription.planCode
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2021-05-26 08:26:59 -04:00
|
|
|
if (plan == null) {
|
|
|
|
return cb(
|
|
|
|
new Error(
|
|
|
|
`No plan found for planCode '${personalSubscription.planCode}'`
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
cb(null, plan)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2021-05-26 08:26:59 -04:00
|
|
|
],
|
|
|
|
memberGroupSubscriptions(cb) {
|
|
|
|
SubscriptionLocator.getMemberSubscriptions(user, cb)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
2021-05-26 08:26:59 -04:00
|
|
|
managedGroupSubscriptions(cb) {
|
|
|
|
SubscriptionLocator.getManagedGroupSubscriptions(user, cb)
|
|
|
|
},
|
2021-08-16 09:08:16 -04:00
|
|
|
currentInstitutionsWithLicence(cb) {
|
2022-05-19 10:25:28 -04:00
|
|
|
InstitutionsGetter.getCurrentInstitutionsWithLicence(
|
|
|
|
user._id,
|
|
|
|
(error, institutions) => {
|
|
|
|
if (error instanceof V1ConnectionError) {
|
|
|
|
return cb(null, false)
|
|
|
|
}
|
|
|
|
cb(null, institutions)
|
|
|
|
}
|
|
|
|
)
|
2021-05-26 08:26:59 -04:00
|
|
|
},
|
|
|
|
managedInstitutions(cb) {
|
|
|
|
InstitutionsGetter.getManagedInstitutions(user._id, cb)
|
|
|
|
},
|
|
|
|
managedPublishers(cb) {
|
|
|
|
PublishersGetter.getManagedPublishers(user._id, cb)
|
|
|
|
},
|
|
|
|
v1SubscriptionStatus(cb) {
|
|
|
|
V1SubscriptionManager.getSubscriptionStatusFromV1(
|
|
|
|
user._id,
|
|
|
|
(error, status, v1Id) => {
|
|
|
|
if (error) {
|
|
|
|
return cb(error)
|
|
|
|
}
|
|
|
|
cb(null, status)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
(err, results) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
let {
|
|
|
|
personalSubscription,
|
|
|
|
memberGroupSubscriptions,
|
|
|
|
managedGroupSubscriptions,
|
2021-08-16 09:08:16 -04:00
|
|
|
currentInstitutionsWithLicence,
|
2021-05-26 08:26:59 -04:00
|
|
|
managedInstitutions,
|
|
|
|
managedPublishers,
|
|
|
|
v1SubscriptionStatus,
|
|
|
|
recurlySubscription,
|
|
|
|
recurlyCoupons,
|
|
|
|
plan,
|
|
|
|
} = results
|
2021-08-16 09:08:16 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
if (memberGroupSubscriptions == null) {
|
|
|
|
memberGroupSubscriptions = []
|
|
|
|
}
|
|
|
|
if (managedGroupSubscriptions == null) {
|
|
|
|
managedGroupSubscriptions = []
|
|
|
|
}
|
|
|
|
if (managedInstitutions == null) {
|
|
|
|
managedInstitutions = []
|
|
|
|
}
|
|
|
|
if (v1SubscriptionStatus == null) {
|
|
|
|
v1SubscriptionStatus = {}
|
|
|
|
}
|
|
|
|
if (recurlyCoupons == null) {
|
|
|
|
recurlyCoupons = []
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
personalSubscription &&
|
|
|
|
typeof personalSubscription.toObject === 'function'
|
|
|
|
) {
|
|
|
|
// Downgrade from Mongoose object, so we can add a recurly and plan attribute
|
|
|
|
personalSubscription = personalSubscription.toObject()
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
if (plan != null) {
|
|
|
|
personalSubscription.plan = plan
|
|
|
|
}
|
|
|
|
|
|
|
|
if (personalSubscription && recurlySubscription) {
|
|
|
|
const tax = recurlySubscription.tax_in_cents || 0
|
|
|
|
// Some plans allow adding more seats than the base plan provides.
|
|
|
|
// This is recorded as a subscription add on.
|
|
|
|
// Note: tax_in_cents already includes the tax for any addon.
|
|
|
|
let addOnPrice = 0
|
|
|
|
let additionalLicenses = 0
|
2019-05-29 05:21:06 -04:00
|
|
|
if (
|
2021-05-26 08:26:59 -04:00
|
|
|
plan.membersLimitAddOn &&
|
|
|
|
Array.isArray(recurlySubscription.subscription_add_ons)
|
2019-05-29 05:21:06 -04:00
|
|
|
) {
|
2021-05-26 08:26:59 -04:00
|
|
|
recurlySubscription.subscription_add_ons.forEach(addOn => {
|
|
|
|
if (addOn.add_on_code === plan.membersLimitAddOn) {
|
|
|
|
addOnPrice += addOn.quantity * addOn.unit_amount_in_cents
|
|
|
|
additionalLicenses += addOn.quantity
|
|
|
|
}
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
const totalLicenses = (plan.membersLimit || 0) + additionalLicenses
|
|
|
|
personalSubscription.recurly = {
|
|
|
|
tax,
|
|
|
|
taxRate: recurlySubscription.tax_rate
|
|
|
|
? parseFloat(recurlySubscription.tax_rate._)
|
|
|
|
: 0,
|
2021-12-14 08:26:07 -05:00
|
|
|
billingDetailsLink: buildHostedLink('billing-details'),
|
|
|
|
accountManagementLink: buildHostedLink('account-management'),
|
2021-05-26 08:26:59 -04:00
|
|
|
additionalLicenses,
|
|
|
|
totalLicenses,
|
|
|
|
nextPaymentDueAt: SubscriptionFormatters.formatDate(
|
|
|
|
recurlySubscription.current_period_ends_at
|
|
|
|
),
|
|
|
|
currency: recurlySubscription.currency,
|
|
|
|
state: recurlySubscription.state,
|
|
|
|
trialEndsAtFormatted: SubscriptionFormatters.formatDate(
|
|
|
|
recurlySubscription.trial_ends_at
|
|
|
|
),
|
|
|
|
trial_ends_at: recurlySubscription.trial_ends_at,
|
|
|
|
activeCoupons: recurlyCoupons,
|
|
|
|
account: recurlySubscription.account,
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
if (recurlySubscription.pending_subscription) {
|
|
|
|
const pendingPlan = PlansLocator.findLocalPlanInSettings(
|
|
|
|
recurlySubscription.pending_subscription.plan.plan_code
|
|
|
|
)
|
|
|
|
if (pendingPlan == null) {
|
|
|
|
return callback(
|
|
|
|
new Error(
|
|
|
|
`No plan found for planCode '${personalSubscription.planCode}'`
|
|
|
|
)
|
2021-04-27 10:17:39 -04:00
|
|
|
)
|
2021-05-26 08:26:59 -04:00
|
|
|
}
|
|
|
|
let pendingAdditionalLicenses = 0
|
|
|
|
let pendingAddOnTax = 0
|
|
|
|
let pendingAddOnPrice = 0
|
|
|
|
if (recurlySubscription.pending_subscription.subscription_add_ons) {
|
|
|
|
if (
|
|
|
|
pendingPlan.membersLimitAddOn &&
|
|
|
|
Array.isArray(
|
|
|
|
recurlySubscription.pending_subscription.subscription_add_ons
|
2021-04-27 10:17:39 -04:00
|
|
|
)
|
2021-05-26 08:26:59 -04:00
|
|
|
) {
|
|
|
|
recurlySubscription.pending_subscription.subscription_add_ons.forEach(
|
|
|
|
addOn => {
|
|
|
|
if (addOn.add_on_code === pendingPlan.membersLimitAddOn) {
|
|
|
|
pendingAddOnPrice +=
|
|
|
|
addOn.quantity * addOn.unit_amount_in_cents
|
|
|
|
pendingAdditionalLicenses += addOn.quantity
|
2021-05-17 10:19:37 -04:00
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
}
|
|
|
|
)
|
2021-05-17 10:19:37 -04:00
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
// Need to calculate tax ourselves as we don't get tax amounts for pending subs
|
|
|
|
pendingAddOnTax =
|
|
|
|
personalSubscription.recurly.taxRate * pendingAddOnPrice
|
2021-04-27 10:17:39 -04:00
|
|
|
}
|
2021-05-26 08:26:59 -04:00
|
|
|
const pendingSubscriptionTax =
|
|
|
|
personalSubscription.recurly.taxRate *
|
|
|
|
recurlySubscription.pending_subscription.unit_amount_in_cents
|
2022-01-12 04:41:39 -05:00
|
|
|
personalSubscription.recurly.displayPrice =
|
2022-01-10 05:23:05 -05:00
|
|
|
SubscriptionFormatters.formatPrice(
|
|
|
|
recurlySubscription.pending_subscription.unit_amount_in_cents +
|
|
|
|
pendingAddOnPrice +
|
|
|
|
pendingAddOnTax +
|
|
|
|
pendingSubscriptionTax,
|
|
|
|
recurlySubscription.currency
|
|
|
|
)
|
2022-06-03 06:13:03 -04:00
|
|
|
personalSubscription.recurly.currentPlanDisplayPrice =
|
|
|
|
SubscriptionFormatters.formatPrice(
|
|
|
|
recurlySubscription.unit_amount_in_cents + addOnPrice + tax,
|
|
|
|
recurlySubscription.currency
|
|
|
|
)
|
2021-05-26 08:26:59 -04:00
|
|
|
const pendingTotalLicenses =
|
|
|
|
(pendingPlan.membersLimit || 0) + pendingAdditionalLicenses
|
2022-01-10 05:23:05 -05:00
|
|
|
personalSubscription.recurly.pendingAdditionalLicenses =
|
|
|
|
pendingAdditionalLicenses
|
|
|
|
personalSubscription.recurly.pendingTotalLicenses =
|
|
|
|
pendingTotalLicenses
|
2021-05-26 08:26:59 -04:00
|
|
|
personalSubscription.pendingPlan = pendingPlan
|
|
|
|
} else {
|
2022-01-12 04:41:39 -05:00
|
|
|
personalSubscription.recurly.displayPrice =
|
2022-01-10 05:23:05 -05:00
|
|
|
SubscriptionFormatters.formatPrice(
|
|
|
|
recurlySubscription.unit_amount_in_cents + addOnPrice + tax,
|
|
|
|
recurlySubscription.currency
|
|
|
|
)
|
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
|
|
|
for (const memberGroupSubscription of memberGroupSubscriptions) {
|
|
|
|
if (memberGroupSubscription.teamNotice) {
|
|
|
|
memberGroupSubscription.teamNotice = sanitizeHtml(
|
|
|
|
memberGroupSubscription.teamNotice
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
callback(null, {
|
|
|
|
personalSubscription,
|
|
|
|
managedGroupSubscriptions,
|
|
|
|
memberGroupSubscriptions,
|
2021-08-16 09:08:16 -04:00
|
|
|
currentInstitutionsWithLicence,
|
2021-05-26 08:26:59 -04:00
|
|
|
managedInstitutions,
|
|
|
|
managedPublishers,
|
|
|
|
v1SubscriptionStatus,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
function buildPlansList(currentPlan) {
|
|
|
|
const { plans } = Settings
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
const allPlans = {}
|
|
|
|
plans.forEach(plan => {
|
|
|
|
allPlans[plan.planCode] = plan
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
const result = { allPlans }
|
2021-04-27 10:17:39 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
if (currentPlan) {
|
|
|
|
result.planCodesChangingAtTermEnd = _.pluck(
|
|
|
|
_.filter(plans, plan => {
|
|
|
|
if (!plan.hideFromUsers) {
|
|
|
|
return SubscriptionHelper.shouldPlanChangeAtTermEnd(currentPlan, plan)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
'planCode'
|
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
|
|
|
result.studentAccounts = _.filter(
|
|
|
|
plans,
|
|
|
|
plan => plan.planCode.indexOf('student') !== -1
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
result.groupMonthlyPlans = _.filter(
|
|
|
|
plans,
|
|
|
|
plan => plan.groupPlan && !plan.annual
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
result.groupAnnualPlans = _.filter(
|
|
|
|
plans,
|
|
|
|
plan => plan.groupPlan && plan.annual
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
result.individualMonthlyPlans = _.filter(
|
|
|
|
plans,
|
|
|
|
plan =>
|
|
|
|
!plan.groupPlan &&
|
|
|
|
!plan.annual &&
|
|
|
|
plan.planCode !== 'personal' && // Prevent the personal plan from appearing on the change-plans page
|
|
|
|
plan.planCode.indexOf('student') === -1
|
|
|
|
)
|
|
|
|
|
|
|
|
result.individualAnnualPlans = _.filter(
|
|
|
|
plans,
|
|
|
|
plan =>
|
|
|
|
!plan.groupPlan && plan.annual && plan.planCode.indexOf('student') === -1
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-05-26 08:26:59 -04:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
buildUsersSubscriptionViewModel,
|
|
|
|
buildPlansList,
|
|
|
|
promises: {
|
|
|
|
buildUsersSubscriptionViewModel: promisify(buildUsersSubscriptionViewModel),
|
2021-12-14 08:26:07 -05:00
|
|
|
getRedirectToHostedPage,
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|