SubscriptionController.coffee 10 KB

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