2016-03-10 12:17:26 -05:00
|
|
|
AuthenticationController = require '../Authentication/AuthenticationController'
|
2014-02-12 05:23:40 -05:00
|
|
|
SubscriptionHandler = require './SubscriptionHandler'
|
|
|
|
PlansLocator = require("./PlansLocator")
|
|
|
|
SubscriptionViewModelBuilder = require('./SubscriptionViewModelBuilder')
|
|
|
|
LimitationsManager = require("./LimitationsManager")
|
|
|
|
RecurlyWrapper = require './RecurlyWrapper'
|
|
|
|
Settings = require 'settings-sharelatex'
|
|
|
|
logger = require('logger-sharelatex')
|
2014-10-13 09:10:15 -04:00
|
|
|
GeoIpLookup = require("../../infrastructure/GeoIpLookup")
|
2015-05-27 15:57:54 -04:00
|
|
|
SubscriptionDomainHandler = require("./SubscriptionDomainHandler")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = SubscriptionController =
|
|
|
|
|
|
|
|
plansPage: (req, res, next) ->
|
|
|
|
plans = SubscriptionViewModelBuilder.buildViewModel()
|
2014-07-07 09:05:07 -04:00
|
|
|
if !req.session.user?
|
|
|
|
baseUrl = "/register?redir="
|
|
|
|
else
|
|
|
|
baseUrl = ""
|
2014-02-12 05:23:40 -05:00
|
|
|
viewName = "subscriptions/plans"
|
2014-08-22 09:20:39 -04:00
|
|
|
if req.query.v?
|
|
|
|
viewName = "#{viewName}_#{req.query.v}"
|
2014-02-12 05:23:40 -05:00
|
|
|
logger.log viewName:viewName, "showing plans page"
|
2014-10-15 06:58:35 -04:00
|
|
|
GeoIpLookup.getCurrencyCode req.query?.ip || req.ip, (err, recomendedCurrency)->
|
2014-10-13 09:10:15 -04:00
|
|
|
res.render viewName,
|
|
|
|
title: "plans_and_pricing"
|
|
|
|
plans: plans
|
|
|
|
baseUrl: baseUrl
|
|
|
|
gaExperiments: Settings.gaExperiments.plansPage
|
|
|
|
recomendedCurrency:recomendedCurrency
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
#get to show the recurly.js page
|
|
|
|
paymentPage: (req, res, next) ->
|
2016-03-10 12:17:26 -05:00
|
|
|
AuthenticationController.getLoggedInUser req, (error, user) =>
|
2014-02-12 05:23:40 -05:00
|
|
|
return next(error) if error?
|
|
|
|
plan = PlansLocator.findLocalPlanInSettings(req.query.planCode)
|
|
|
|
LimitationsManager.userHasSubscription user, (err, hasSubscription)->
|
2014-10-14 10:46:44 -04:00
|
|
|
return next(err) if err?
|
2014-02-12 05:23:40 -05:00
|
|
|
if hasSubscription or !plan?
|
|
|
|
res.redirect "/user/subscription"
|
|
|
|
else
|
2014-10-13 12:39:44 -04:00
|
|
|
currency = req.query.currency?.toUpperCase()
|
2015-01-07 08:16:19 -05:00
|
|
|
GeoIpLookup.getCurrencyCode req.query?.ip || req.ip, (err, recomendedCurrency, countryCode)->
|
2014-10-14 10:46:44 -04:00
|
|
|
return next(err) if err?
|
2014-10-13 12:39:44 -04:00
|
|
|
if recomendedCurrency? and !currency?
|
|
|
|
currency = recomendedCurrency
|
|
|
|
RecurlyWrapper.sign {
|
|
|
|
subscription:
|
|
|
|
plan_code : req.query.planCode
|
|
|
|
currency: currency
|
|
|
|
account_code: user.id
|
|
|
|
}, (error, signature) ->
|
|
|
|
return next(error) if error?
|
|
|
|
res.render "subscriptions/new",
|
|
|
|
title : "subscribe"
|
|
|
|
plan_code: req.query.planCode
|
2014-09-11 06:45:00 -04:00
|
|
|
currency: currency
|
2015-01-07 08:16:19 -05:00
|
|
|
countryCode:countryCode
|
2014-10-14 07:14:24 -04:00
|
|
|
plan:plan
|
2014-11-24 11:21:03 -05:00
|
|
|
showStudentPlan: req.query.ssp
|
2014-10-13 12:39:44 -04:00
|
|
|
recurlyConfig: JSON.stringify
|
|
|
|
currency: currency
|
|
|
|
subdomain: Settings.apis.recurly.subdomain
|
2015-02-02 12:18:06 -05:00
|
|
|
showCouponField: req.query.scf
|
2015-03-04 13:08:06 -05:00
|
|
|
showVatField: req.query.svf
|
2015-02-02 12:18:06 -05:00
|
|
|
couponCode: req.query.cc or ""
|
2014-10-13 12:39:44 -04:00
|
|
|
subscriptionFormOptions: JSON.stringify
|
|
|
|
acceptedCards: ['discover', 'mastercard', 'visa']
|
|
|
|
target : "#subscribeForm"
|
|
|
|
signature : signature
|
|
|
|
planCode : req.query.planCode
|
|
|
|
successURL : "#{Settings.siteUrl}/user/subscription/create?_csrf=#{req.session._csrf}"
|
|
|
|
accountCode : user.id
|
|
|
|
enableCoupons: true
|
|
|
|
acceptPaypal: true
|
|
|
|
account :
|
|
|
|
firstName : user.first_name
|
|
|
|
lastName : user.last_name
|
|
|
|
email : user.email
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
userSubscriptionPage: (req, res, next) ->
|
2016-03-10 12:17:26 -05:00
|
|
|
AuthenticationController.getLoggedInUser req, (error, user) =>
|
2014-02-12 05:23:40 -05:00
|
|
|
return next(error) if error?
|
2014-10-10 10:11:22 -04:00
|
|
|
LimitationsManager.userHasSubscriptionOrIsGroupMember user, (err, hasSubOrIsGroupMember, subscription)->
|
2015-05-27 15:57:54 -04:00
|
|
|
groupLicenceInviteUrl = SubscriptionDomainHandler.getDomainLicencePage(user)
|
2014-10-10 10:11:22 -04:00
|
|
|
if subscription?.customAccount
|
2016-03-22 10:19:46 -04:00
|
|
|
logger.log user: user, "redirecting to custom account page"
|
2015-05-27 10:07:46 -04:00
|
|
|
res.redirect "/user/subscription/custom_account"
|
|
|
|
else if groupLicenceInviteUrl? and !hasSubOrIsGroupMember
|
|
|
|
logger.log user:user, "redirecting to group subscription invite page"
|
|
|
|
res.redirect groupLicenceInviteUrl
|
2014-10-10 10:11:22 -04:00
|
|
|
else if !hasSubOrIsGroupMember
|
2014-02-12 05:23:40 -05:00
|
|
|
logger.log user: user, "redirecting to plans"
|
|
|
|
res.redirect "/user/subscription/plans"
|
|
|
|
else
|
2014-08-07 10:29:06 -04:00
|
|
|
SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel user, (error, subscription, groups) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
return next(error) if error?
|
2016-02-19 07:27:01 -05:00
|
|
|
logger.log user: user, subscription:subscription, hasSubOrIsGroupMember:hasSubOrIsGroupMember, groups:groups, "showing subscription dashboard"
|
2014-02-12 05:23:40 -05:00
|
|
|
plans = SubscriptionViewModelBuilder.buildViewModel()
|
|
|
|
res.render "subscriptions/dashboard",
|
2014-08-01 08:47:14 -04:00
|
|
|
title: "your_subscription"
|
2014-11-04 05:53:03 -05:00
|
|
|
recomendedCurrency: subscription?.currency
|
2015-02-12 04:21:45 -05:00
|
|
|
taxRate:subscription?.taxRate
|
2014-02-12 05:23:40 -05:00
|
|
|
plans: plans
|
2016-02-03 12:41:05 -05:00
|
|
|
subscription: subscription || {}
|
2014-08-07 10:29:06 -04:00
|
|
|
groups: groups
|
2014-02-12 05:23:40 -05:00
|
|
|
subscriptionTabActive: true
|
|
|
|
|
|
|
|
|
2014-10-10 10:44:53 -04:00
|
|
|
userCustomSubscriptionPage: (req, res, next)->
|
2016-03-10 12:17:26 -05:00
|
|
|
AuthenticationController.getLoggedInUser req, (error, user) ->
|
2014-10-10 10:44:53 -04:00
|
|
|
LimitationsManager.userHasSubscriptionOrIsGroupMember user, (err, hasSubOrIsGroupMember, subscription)->
|
2016-03-22 10:19:46 -04:00
|
|
|
if !subscription?
|
|
|
|
err = new Error("subscription null for custom account, user:#{user?._id}")
|
|
|
|
logger.warn err:err, "subscription is null for custom accounts page"
|
|
|
|
return next(err)
|
2014-10-10 10:44:53 -04:00
|
|
|
res.render "subscriptions/custom_account",
|
|
|
|
title: "your_subscription"
|
|
|
|
subscription: subscription
|
|
|
|
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
editBillingDetailsPage: (req, res, next) ->
|
2016-03-10 12:17:26 -05:00
|
|
|
AuthenticationController.getLoggedInUser req, (error, user) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
return next(error) if error?
|
|
|
|
LimitationsManager.userHasSubscription user, (err, hasSubscription)->
|
|
|
|
if !hasSubscription
|
|
|
|
res.redirect "/user/subscription"
|
|
|
|
else
|
|
|
|
RecurlyWrapper.sign {
|
|
|
|
account_code: user.id
|
|
|
|
}, (error, signature) ->
|
|
|
|
return next(error) if error?
|
|
|
|
res.render "subscriptions/edit-billing-details",
|
2014-08-01 08:47:14 -04:00
|
|
|
title : "update_billing_details"
|
2014-02-12 05:23:40 -05:00
|
|
|
recurlyConfig: JSON.stringify
|
|
|
|
currency: "USD"
|
|
|
|
subdomain: Settings.apis.recurly.subdomain
|
|
|
|
signature : signature
|
|
|
|
successURL : "#{Settings.siteUrl}/user/subscription/update"
|
|
|
|
user :
|
|
|
|
id : user.id
|
|
|
|
|
|
|
|
createSubscription: (req, res, next)->
|
2016-03-10 12:17:26 -05:00
|
|
|
AuthenticationController.getLoggedInUser req, (error, user) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error) if error?
|
2014-12-18 13:59:29 -05:00
|
|
|
recurly_token_id = req.body.recurly_token_id
|
2014-12-19 06:17:35 -05:00
|
|
|
subscriptionDetails = req.body.subscriptionDetails
|
|
|
|
logger.log recurly_token_id: recurly_token_id, user_id:user._id, subscriptionDetails:subscriptionDetails, "creating subscription"
|
|
|
|
SubscriptionHandler.createSubscription user, subscriptionDetails, recurly_token_id, (err)->
|
2014-02-12 05:23:40 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, user_id:user._id, "something went wrong creating subscription"
|
2015-07-08 11:56:38 -04:00
|
|
|
return res.sendStatus 500
|
|
|
|
res.sendStatus 201
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
successful_subscription: (req, res)->
|
2016-03-10 12:17:26 -05:00
|
|
|
AuthenticationController.getLoggedInUser req, (error, user) =>
|
2014-02-12 05:23:40 -05:00
|
|
|
SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel user, (error, subscription) ->
|
|
|
|
res.render "subscriptions/successful_subscription",
|
2014-08-01 08:47:14 -04:00
|
|
|
title: "thank_you"
|
2014-02-12 05:23:40 -05:00
|
|
|
subscription:subscription
|
|
|
|
|
|
|
|
cancelSubscription: (req, res, next) ->
|
2016-03-10 12:17:26 -05:00
|
|
|
AuthenticationController.getLoggedInUser req, (error, user) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
logger.log user_id:user._id, "canceling subscription"
|
|
|
|
return next(error) if error?
|
|
|
|
SubscriptionHandler.cancelSubscription user, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, user_id:user._id, "something went wrong canceling subscription"
|
|
|
|
res.redirect "/user/subscription"
|
|
|
|
|
|
|
|
updateSubscription: (req, res)->
|
2016-03-10 12:17:26 -05:00
|
|
|
AuthenticationController.getLoggedInUser req, (error, user) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
return next(error) if error?
|
|
|
|
planCode = req.body.plan_code
|
|
|
|
logger.log planCode: planCode, user_id:user._id, "updating subscription"
|
2014-08-27 12:51:10 -04:00
|
|
|
SubscriptionHandler.updateSubscription user, planCode, null, (err)->
|
2014-02-12 05:23:40 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, user_id:user._id, "something went wrong updating subscription"
|
|
|
|
res.redirect "/user/subscription"
|
|
|
|
|
|
|
|
reactivateSubscription: (req, res)->
|
2016-03-10 12:17:26 -05:00
|
|
|
AuthenticationController.getLoggedInUser req, (error, user) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
logger.log user_id:user._id, "reactivating subscription"
|
|
|
|
return next(error) if error?
|
|
|
|
SubscriptionHandler.reactivateSubscription user, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, user_id:user._id, "something went wrong reactivating subscription"
|
|
|
|
res.redirect "/user/subscription"
|
|
|
|
|
|
|
|
recurlyCallback: (req, res)->
|
|
|
|
logger.log data: req.body, "received recurly callback"
|
|
|
|
# we only care if a subscription has exipired
|
|
|
|
if req.body? and req.body["expired_subscription_notification"]?
|
|
|
|
recurlySubscription = req.body["expired_subscription_notification"].subscription
|
|
|
|
SubscriptionHandler.recurlyCallback recurlySubscription, ->
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus 200
|
2014-02-12 05:23:40 -05:00
|
|
|
else
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus 200
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-08-27 12:51:10 -04:00
|
|
|
renderUpgradeToAnnualPlanPage: (req, res)->
|
2016-03-10 12:17:26 -05:00
|
|
|
AuthenticationController.getLoggedInUser req, (error, user) ->
|
2014-08-28 13:14:31 -04:00
|
|
|
LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
|
|
|
|
planCode = subscription?.planCode.toLowerCase()
|
2014-08-29 09:13:05 -04:00
|
|
|
if planCode?.indexOf("annual") != -1
|
|
|
|
planName = "annual"
|
|
|
|
else if planCode?.indexOf("student") != -1
|
2014-08-28 13:14:31 -04:00
|
|
|
planName = "student"
|
|
|
|
else if planCode?.indexOf("collaborator") != -1
|
|
|
|
planName = "collaborator"
|
2014-08-27 12:51:10 -04:00
|
|
|
if !hasSubscription
|
|
|
|
return res.redirect("/user/subscription/plans")
|
2014-08-28 13:14:31 -04:00
|
|
|
logger.log planName:planName, user_id:user._id, "rendering upgrade to annual page"
|
2014-08-27 12:51:10 -04:00
|
|
|
res.render "subscriptions/upgradeToAnnual",
|
|
|
|
title: "Upgrade to annual"
|
2014-08-28 13:14:31 -04:00
|
|
|
planName: planName
|
2014-08-27 12:51:10 -04:00
|
|
|
|
|
|
|
processUpgradeToAnnualPlan: (req, res)->
|
2016-03-10 12:17:26 -05:00
|
|
|
AuthenticationController.getLoggedInUser req, (error, user) ->
|
2014-08-28 13:14:31 -04:00
|
|
|
{planName} = req.body
|
|
|
|
coupon_code = Settings.coupon_codes.upgradeToAnnualPromo[planName]
|
|
|
|
annualPlanName = "#{planName}-annual"
|
|
|
|
logger.log user_id:user._id, planName:annualPlanName, "user is upgrading to annual billing with discount"
|
|
|
|
SubscriptionHandler.updateSubscription user, annualPlanName, coupon_code, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, user_id:user._id, "error updating subscription"
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus 500
|
2014-08-28 13:14:31 -04:00
|
|
|
else
|
2015-07-08 11:56:38 -04:00
|
|
|
res.sendStatus 200
|
2014-08-28 13:14:31 -04:00
|
|
|
|
2015-12-08 13:23:28 -05:00
|
|
|
extendTrial: (req, res)->
|
2016-03-10 12:17:26 -05:00
|
|
|
AuthenticationController.getLoggedInUser req, (error, user) ->
|
2015-12-08 13:23:28 -05:00
|
|
|
LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
|
|
|
|
SubscriptionHandler.extendTrial subscription, 14, (err)->
|
|
|
|
if err?
|
|
|
|
res.send 500
|
|
|
|
else
|
|
|
|
res.send 200
|
2014-08-27 12:51:10 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
recurlyNotificationParser: (req, res, next) ->
|
|
|
|
xml = ""
|
|
|
|
req.on "data", (chunk) ->
|
|
|
|
xml += chunk
|
|
|
|
req.on "end", () ->
|
|
|
|
RecurlyWrapper._parseXml xml, (error, body) ->
|
|
|
|
return next(error) if error?
|
|
|
|
req.body = body
|
|
|
|
next()
|