SubscriptionController.coffee 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. AuthenticationController = require '../Authentication/AuthenticationController'
  2. SubscriptionHandler = require './SubscriptionHandler'
  3. PlansLocator = require("./PlansLocator")
  4. SubscriptionViewModelBuilder = require('./SubscriptionViewModelBuilder')
  5. LimitationsManager = require("./LimitationsManager")
  6. RecurlyWrapper = require './RecurlyWrapper'
  7. Settings = require 'settings-sharelatex'
  8. logger = require('logger-sharelatex')
  9. GeoIpLookup = require("../../infrastructure/GeoIpLookup")
  10. SubscriptionDomainHandler = require("./SubscriptionDomainHandler")
  11. UserGetter = require "../User/UserGetter"
  12. FeaturesUpdater = require './FeaturesUpdater'
  13. module.exports = SubscriptionController =
  14. plansPage: (req, res, next) ->
  15. plans = SubscriptionViewModelBuilder.buildViewModel()
  16. viewName = "subscriptions/plans"
  17. if req.query.v?
  18. viewName = "#{viewName}_#{req.query.v}"
  19. logger.log viewName:viewName, "showing plans page"
  20. currentUser = null
  21. GeoIpLookup.getCurrencyCode req.query?.ip || req.ip, (err, recomendedCurrency)->
  22. return next(err) if err?
  23. render = () ->
  24. res.render viewName,
  25. title: "plans_and_pricing"
  26. plans: plans
  27. gaExperiments: Settings.gaExperiments.plansPage
  28. recomendedCurrency:recomendedCurrency
  29. shouldABTestPlans: currentUser == null or (currentUser?.signUpDate? and currentUser.signUpDate >= (new Date('2016-10-27')))
  30. user_id = AuthenticationController.getLoggedInUserId(req)
  31. if user_id?
  32. UserGetter.getUser user_id, {signUpDate: 1}, (err, user) ->
  33. return next(err) if err?
  34. currentUser = user
  35. render()
  36. else
  37. render()
  38. #get to show the recurly.js page
  39. paymentPage: (req, res, next) ->
  40. user = AuthenticationController.getSessionUser(req)
  41. plan = PlansLocator.findLocalPlanInSettings(req.query.planCode)
  42. LimitationsManager.userHasSubscription user, (err, hasSubscription)->
  43. return next(err) if err?
  44. if hasSubscription or !plan?
  45. res.redirect "/user/subscription"
  46. else
  47. # LimitationsManager.userHasSubscription only checks Mongo. Double check with
  48. # Recurly as well at this point (we don't do this most places for speed).
  49. SubscriptionHandler.validateNoSubscriptionInRecurly user._id, (error, valid) ->
  50. return next(error) if error?
  51. if !valid
  52. res.redirect "/user/subscription"
  53. return
  54. else
  55. currency = req.query.currency?.toUpperCase()
  56. GeoIpLookup.getCurrencyCode req.query?.ip || req.ip, (err, recomendedCurrency, countryCode)->
  57. return next(err) if err?
  58. if recomendedCurrency? and !currency?
  59. currency = recomendedCurrency
  60. RecurlyWrapper.sign {
  61. subscription:
  62. plan_code : req.query.planCode
  63. currency: currency
  64. account_code: user._id
  65. }, (error, signature) ->
  66. return next(error) if error?
  67. res.render "subscriptions/new",
  68. title : "subscribe"
  69. plan_code: req.query.planCode
  70. currency: currency
  71. countryCode:countryCode
  72. plan:plan
  73. showStudentPlan: req.query.ssp
  74. recurlyConfig: JSON.stringify
  75. currency: currency
  76. subdomain: Settings.apis.recurly.subdomain
  77. showCouponField: req.query.scf
  78. showVatField: req.query.svf
  79. couponCode: req.query.cc or ""
  80. userSubscriptionPage: (req, res, next) ->
  81. user = AuthenticationController.getSessionUser(req)
  82. LimitationsManager.userHasSubscriptionOrIsGroupMember user, (err, hasSubOrIsGroupMember, subscription)->
  83. return next(err) if err?
  84. groupLicenceInviteUrl = SubscriptionDomainHandler.getDomainLicencePage(user)
  85. if subscription?.customAccount
  86. logger.log user: user, "redirecting to custom account page"
  87. res.redirect "/user/subscription/custom_account"
  88. else if groupLicenceInviteUrl? and !hasSubOrIsGroupMember
  89. logger.log user:user, "redirecting to group subscription invite page"
  90. res.redirect groupLicenceInviteUrl
  91. else if !hasSubOrIsGroupMember
  92. logger.log user: user, "redirecting to plans"
  93. res.redirect "/user/subscription/plans"
  94. else
  95. SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel user, (error, subscription, groupSubscriptions, billingDetailsLink) ->
  96. return next(error) if error?
  97. logger.log {user, subscription, hasSubOrIsGroupMember, groupSubscriptions, billingDetailsLink}, "showing subscription dashboard"
  98. plans = SubscriptionViewModelBuilder.buildViewModel()
  99. res.render "subscriptions/dashboard",
  100. title: "your_subscription"
  101. recomendedCurrency: subscription?.currency
  102. taxRate:subscription?.taxRate
  103. plans: plans
  104. subscription: subscription || {}
  105. groupSubscriptions: groupSubscriptions
  106. subscriptionTabActive: true
  107. user:user
  108. saved_billing_details: req.query.saved_billing_details?
  109. billingDetailsLink: billingDetailsLink
  110. userCustomSubscriptionPage: (req, res, next)->
  111. user = AuthenticationController.getSessionUser(req)
  112. LimitationsManager.userHasSubscriptionOrIsGroupMember user, (err, hasSubOrIsGroupMember, subscription)->
  113. return next(err) if err?
  114. if !subscription?
  115. err = new Error("subscription null for custom account, user:#{user?._id}")
  116. logger.warn err:err, "subscription is null for custom accounts page"
  117. return next(err)
  118. res.render "subscriptions/custom_account",
  119. title: "your_subscription"
  120. subscription: subscription
  121. createSubscription: (req, res, next)->
  122. user = AuthenticationController.getSessionUser(req)
  123. recurly_token_id = req.body.recurly_token_id
  124. subscriptionDetails = req.body.subscriptionDetails
  125. logger.log recurly_token_id: recurly_token_id, user_id:user._id, subscriptionDetails:subscriptionDetails, "creating subscription"
  126. SubscriptionHandler.createSubscription user, subscriptionDetails, recurly_token_id, (err)->
  127. if err?
  128. logger.err err:err, user_id:user._id, "something went wrong creating subscription"
  129. return res.sendStatus 500
  130. res.sendStatus 201
  131. successful_subscription: (req, res, next)->
  132. user = AuthenticationController.getSessionUser(req)
  133. SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel user, (error, subscription) ->
  134. return next(error) if error?
  135. res.render "subscriptions/successful_subscription",
  136. title: "thank_you"
  137. subscription:subscription
  138. cancelSubscription: (req, res, next) ->
  139. user = AuthenticationController.getSessionUser(req)
  140. logger.log user_id:user._id, "canceling subscription"
  141. SubscriptionHandler.cancelSubscription user, (err)->
  142. if err?
  143. logger.err err:err, user_id:user._id, "something went wrong canceling subscription"
  144. return next(err)
  145. res.redirect "/user/subscription"
  146. updateSubscription: (req, res, next)->
  147. _origin = req?.query?.origin || null
  148. user = AuthenticationController.getSessionUser(req)
  149. planCode = req.body.plan_code
  150. if !planCode?
  151. err = new Error('plan_code is not defined')
  152. logger.err {user_id: user._id, err, planCode, origin: _origin, body: req.body}, "[Subscription] error in updateSubscription form"
  153. return next(err)
  154. logger.log planCode: planCode, user_id:user._id, "updating subscription"
  155. SubscriptionHandler.updateSubscription user, planCode, null, (err)->
  156. if err?
  157. logger.err err:err, user_id:user._id, "something went wrong updating subscription"
  158. return next(err)
  159. res.redirect "/user/subscription"
  160. reactivateSubscription: (req, res, next)->
  161. user = AuthenticationController.getSessionUser(req)
  162. logger.log user_id:user._id, "reactivating subscription"
  163. SubscriptionHandler.reactivateSubscription user, (err)->
  164. if err?
  165. logger.err err:err, user_id:user._id, "something went wrong reactivating subscription"
  166. return next(err)
  167. res.redirect "/user/subscription"
  168. recurlyCallback: (req, res, next)->
  169. logger.log data: req.body, "received recurly callback"
  170. # we only care if a subscription has exipired
  171. if req.body? and req.body["expired_subscription_notification"]?
  172. recurlySubscription = req.body["expired_subscription_notification"].subscription
  173. SubscriptionHandler.recurlyCallback recurlySubscription, (err)->
  174. return next(err) if err?
  175. res.sendStatus 200
  176. else
  177. res.sendStatus 200
  178. renderUpgradeToAnnualPlanPage: (req, res, next)->
  179. user = AuthenticationController.getSessionUser(req)
  180. LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
  181. return next(err) if err?
  182. planCode = subscription?.planCode.toLowerCase()
  183. if planCode?.indexOf("annual") != -1
  184. planName = "annual"
  185. else if planCode?.indexOf("student") != -1
  186. planName = "student"
  187. else if planCode?.indexOf("collaborator") != -1
  188. planName = "collaborator"
  189. if !hasSubscription
  190. return res.redirect("/user/subscription/plans")
  191. logger.log planName:planName, user_id:user._id, "rendering upgrade to annual page"
  192. res.render "subscriptions/upgradeToAnnual",
  193. title: "Upgrade to annual"
  194. planName: planName
  195. processUpgradeToAnnualPlan: (req, res, next)->
  196. user = AuthenticationController.getSessionUser(req)
  197. {planName} = req.body
  198. coupon_code = Settings.coupon_codes.upgradeToAnnualPromo[planName]
  199. annualPlanName = "#{planName}-annual"
  200. logger.log user_id:user._id, planName:annualPlanName, "user is upgrading to annual billing with discount"
  201. SubscriptionHandler.updateSubscription user, annualPlanName, coupon_code, (err)->
  202. if err?
  203. logger.err err:err, user_id:user._id, "error updating subscription"
  204. return next(err)
  205. res.sendStatus 200
  206. extendTrial: (req, res, next)->
  207. user = AuthenticationController.getSessionUser(req)
  208. LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
  209. return next(err) if err?
  210. SubscriptionHandler.extendTrial subscription, 14, (err)->
  211. if err?
  212. res.send 500
  213. else
  214. res.send 200
  215. recurlyNotificationParser: (req, res, next) ->
  216. xml = ""
  217. req.on "data", (chunk) ->
  218. xml += chunk
  219. req.on "end", () ->
  220. RecurlyWrapper._parseXml xml, (error, body) ->
  221. return next(error) if error?
  222. req.body = body
  223. next()
  224. refreshUserFeatures: (req, res, next) ->
  225. {user_id} = req.params
  226. FeaturesUpdater.refreshFeatures user_id, (error) ->
  227. return next(error) if error?
  228. res.sendStatus 200