SubscriptionController.coffee 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. subscription: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. res.redirect "/user/subscription"
  152. cancelV1Subscription: (req, res, next) ->
  153. user_id = AuthenticationController.getLoggedInUserId(req)
  154. logger.log {user_id}, "canceling v1 subscription"
  155. V1SubscriptionManager.cancelV1Subscription user_id, (err)->
  156. if err?
  157. logger.err err:err, user_id:user_id, "something went wrong canceling v1 subscription"
  158. return next(err)
  159. res.redirect "/user/subscription"
  160. updateSubscription: (req, res, next)->
  161. _origin = req?.query?.origin || null
  162. user = AuthenticationController.getSessionUser(req)
  163. planCode = req.body.plan_code
  164. if !planCode?
  165. err = new Error('plan_code is not defined')
  166. logger.err {user_id: user._id, err, planCode, origin: _origin, body: req.body}, "[Subscription] error in updateSubscription form"
  167. return next(err)
  168. logger.log planCode: planCode, user_id:user._id, "updating subscription"
  169. SubscriptionHandler.updateSubscription user, planCode, null, (err)->
  170. if err?
  171. logger.err err:err, user_id:user._id, "something went wrong updating subscription"
  172. return next(err)
  173. res.redirect "/user/subscription"
  174. reactivateSubscription: (req, res, next)->
  175. user = AuthenticationController.getSessionUser(req)
  176. logger.log user_id:user._id, "reactivating subscription"
  177. SubscriptionHandler.reactivateSubscription user, (err)->
  178. if err?
  179. logger.err err:err, user_id:user._id, "something went wrong reactivating subscription"
  180. return next(err)
  181. res.redirect "/user/subscription"
  182. recurlyCallback: (req, res, next)->
  183. logger.log data: req.body, "received recurly callback"
  184. # we only care if a subscription has exipired
  185. if req.body? and req.body["expired_subscription_notification"]?
  186. recurlySubscription = req.body["expired_subscription_notification"].subscription
  187. SubscriptionHandler.recurlyCallback recurlySubscription, (err)->
  188. return next(err) if err?
  189. res.sendStatus 200
  190. else
  191. res.sendStatus 200
  192. renderUpgradeToAnnualPlanPage: (req, res, next)->
  193. user = AuthenticationController.getSessionUser(req)
  194. LimitationsManager.userHasV2Subscription user, (err, hasSubscription, subscription)->
  195. return next(err) if err?
  196. planCode = subscription?.planCode.toLowerCase()
  197. if planCode?.indexOf("annual") != -1
  198. planName = "annual"
  199. else if planCode?.indexOf("student") != -1
  200. planName = "student"
  201. else if planCode?.indexOf("collaborator") != -1
  202. planName = "collaborator"
  203. if !hasSubscription
  204. return res.redirect("/user/subscription/plans")
  205. logger.log planName:planName, user_id:user._id, "rendering upgrade to annual page"
  206. res.render "subscriptions/upgradeToAnnual",
  207. title: "Upgrade to annual"
  208. planName: planName
  209. processUpgradeToAnnualPlan: (req, res, next)->
  210. user = AuthenticationController.getSessionUser(req)
  211. {planName} = req.body
  212. coupon_code = Settings.coupon_codes.upgradeToAnnualPromo[planName]
  213. annualPlanName = "#{planName}-annual"
  214. logger.log user_id:user._id, planName:annualPlanName, "user is upgrading to annual billing with discount"
  215. SubscriptionHandler.updateSubscription user, annualPlanName, coupon_code, (err)->
  216. if err?
  217. logger.err err:err, user_id:user._id, "error updating subscription"
  218. return next(err)
  219. res.sendStatus 200
  220. extendTrial: (req, res, next)->
  221. user = AuthenticationController.getSessionUser(req)
  222. LimitationsManager.userHasV2Subscription user, (err, hasSubscription, subscription)->
  223. return next(err) if err?
  224. SubscriptionHandler.extendTrial subscription, 14, (err)->
  225. if err?
  226. res.send 500
  227. else
  228. res.send 200
  229. recurlyNotificationParser: (req, res, next) ->
  230. xml = ""
  231. req.on "data", (chunk) ->
  232. xml += chunk
  233. req.on "end", () ->
  234. RecurlyWrapper._parseXml xml, (error, body) ->
  235. return next(error) if error?
  236. req.body = body
  237. next()
  238. refreshUserFeatures: (req, res, next) ->
  239. {user_id} = req.params
  240. FeaturesUpdater.refreshFeatures user_id, (error) ->
  241. return next(error) if error?
  242. res.sendStatus 200