SubscriptionController.coffee 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. planFeatures = require './planFeatures'
  14. GroupPlansData = require './GroupPlansData'
  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"
  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"
  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. RecurlyWrapper.sign {
  64. subscription:
  65. plan_code : req.query.planCode
  66. currency: currency
  67. account_code: user._id
  68. }, (error, signature) ->
  69. return next(error) if error?
  70. res.render "subscriptions/new",
  71. title : "subscribe"
  72. plan_code: req.query.planCode
  73. currency: currency
  74. countryCode:countryCode
  75. plan:plan
  76. showStudentPlan: req.query.ssp
  77. recurlyConfig: JSON.stringify
  78. currency: currency
  79. subdomain: Settings.apis.recurly.subdomain
  80. showCouponField: req.query.scf
  81. showVatField: req.query.svf
  82. couponCode: req.query.cc or ""
  83. userSubscriptionPage: (req, res, next) ->
  84. user = AuthenticationController.getSessionUser(req)
  85. SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel user, (error, results) ->
  86. return next(error) if error?
  87. {
  88. personalSubscription,
  89. memberGroupSubscriptions,
  90. managedGroupSubscriptions,
  91. confirmedMemberInstitutions,
  92. managedInstitutions,
  93. v1Subscriptions
  94. } = results
  95. logger.log {
  96. user,
  97. personalSubscription,
  98. memberGroupSubscriptions,
  99. managedGroupSubscriptions,
  100. confirmedMemberInstitutions,
  101. managedInstitutions,
  102. v1Subscriptions
  103. }, "showing subscription dashboard"
  104. plans = SubscriptionViewModelBuilder.buildViewModel()
  105. data = {
  106. title: "your_subscription"
  107. plans,
  108. user,
  109. personalSubscription,
  110. memberGroupSubscriptions,
  111. managedGroupSubscriptions,
  112. confirmedMemberInstitutions,
  113. managedInstitutions,
  114. v1Subscriptions
  115. }
  116. res.render "subscriptions/dashboard", data
  117. createSubscription: (req, res, next)->
  118. user = AuthenticationController.getSessionUser(req)
  119. recurly_token_id = req.body.recurly_token_id
  120. subscriptionDetails = req.body.subscriptionDetails
  121. logger.log recurly_token_id: recurly_token_id, user_id:user._id, subscriptionDetails:subscriptionDetails, "creating subscription"
  122. LimitationsManager.userHasV1OrV2Subscription user, (err, hasSubscription)->
  123. return next(err) if err?
  124. if hasSubscription
  125. logger.warn {user_id: user._id}, 'user already has subscription'
  126. res.sendStatus 409 # conflict
  127. SubscriptionHandler.createSubscription user, subscriptionDetails, recurly_token_id, (err)->
  128. if err?
  129. logger.err err:err, user_id:user._id, "something went wrong creating subscription"
  130. return next(err)
  131. res.sendStatus 201
  132. successful_subscription: (req, res, next)->
  133. user = AuthenticationController.getSessionUser(req)
  134. SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel user, (error, {personalSubscription}) ->
  135. return next(error) if error?
  136. if !personalSubscription?
  137. return res.redirect '/user/subscription/plans'
  138. res.render "subscriptions/successful_subscription",
  139. title: "thank_you"
  140. subscription:personalSubscription
  141. cancelSubscription: (req, res, next) ->
  142. user = AuthenticationController.getSessionUser(req)
  143. logger.log user_id:user._id, "canceling subscription"
  144. SubscriptionHandler.cancelSubscription user, (err)->
  145. if err?
  146. logger.err err:err, user_id:user._id, "something went wrong canceling subscription"
  147. return next(err)
  148. res.redirect "/user/subscription"
  149. updateSubscription: (req, res, next)->
  150. _origin = req?.query?.origin || null
  151. user = AuthenticationController.getSessionUser(req)
  152. planCode = req.body.plan_code
  153. if !planCode?
  154. err = new Error('plan_code is not defined')
  155. logger.err {user_id: user._id, err, planCode, origin: _origin, body: req.body}, "[Subscription] error in updateSubscription form"
  156. return next(err)
  157. logger.log planCode: planCode, user_id:user._id, "updating subscription"
  158. SubscriptionHandler.updateSubscription user, planCode, null, (err)->
  159. if err?
  160. logger.err err:err, user_id:user._id, "something went wrong updating subscription"
  161. return next(err)
  162. res.redirect "/user/subscription"
  163. reactivateSubscription: (req, res, next)->
  164. user = AuthenticationController.getSessionUser(req)
  165. logger.log user_id:user._id, "reactivating subscription"
  166. SubscriptionHandler.reactivateSubscription user, (err)->
  167. if err?
  168. logger.err err:err, user_id:user._id, "something went wrong reactivating subscription"
  169. return next(err)
  170. res.redirect "/user/subscription"
  171. recurlyCallback: (req, res, next)->
  172. logger.log data: req.body, "received recurly callback"
  173. # we only care if a subscription has exipired
  174. if req.body? and req.body["expired_subscription_notification"]?
  175. recurlySubscription = req.body["expired_subscription_notification"].subscription
  176. SubscriptionHandler.recurlyCallback recurlySubscription, (err)->
  177. return next(err) if err?
  178. res.sendStatus 200
  179. else
  180. res.sendStatus 200
  181. renderUpgradeToAnnualPlanPage: (req, res, next)->
  182. user = AuthenticationController.getSessionUser(req)
  183. LimitationsManager.userHasV2Subscription user, (err, hasSubscription, subscription)->
  184. return next(err) if err?
  185. planCode = subscription?.planCode.toLowerCase()
  186. if planCode?.indexOf("annual") != -1
  187. planName = "annual"
  188. else if planCode?.indexOf("student") != -1
  189. planName = "student"
  190. else if planCode?.indexOf("collaborator") != -1
  191. planName = "collaborator"
  192. if !hasSubscription
  193. return res.redirect("/user/subscription/plans")
  194. logger.log planName:planName, user_id:user._id, "rendering upgrade to annual page"
  195. res.render "subscriptions/upgradeToAnnual",
  196. title: "Upgrade to annual"
  197. planName: planName
  198. processUpgradeToAnnualPlan: (req, res, next)->
  199. user = AuthenticationController.getSessionUser(req)
  200. {planName} = req.body
  201. coupon_code = Settings.coupon_codes.upgradeToAnnualPromo[planName]
  202. annualPlanName = "#{planName}-annual"
  203. logger.log user_id:user._id, planName:annualPlanName, "user is upgrading to annual billing with discount"
  204. SubscriptionHandler.updateSubscription user, annualPlanName, coupon_code, (err)->
  205. if err?
  206. logger.err err:err, user_id:user._id, "error updating subscription"
  207. return next(err)
  208. res.sendStatus 200
  209. extendTrial: (req, res, next)->
  210. user = AuthenticationController.getSessionUser(req)
  211. LimitationsManager.userHasV2Subscription user, (err, hasSubscription, subscription)->
  212. return next(err) if err?
  213. SubscriptionHandler.extendTrial subscription, 14, (err)->
  214. if err?
  215. res.send 500
  216. else
  217. res.send 200
  218. recurlyNotificationParser: (req, res, next) ->
  219. xml = ""
  220. req.on "data", (chunk) ->
  221. xml += chunk
  222. req.on "end", () ->
  223. RecurlyWrapper._parseXml xml, (error, body) ->
  224. return next(error) if error?
  225. req.body = body
  226. next()
  227. refreshUserFeatures: (req, res, next) ->
  228. {user_id} = req.params
  229. FeaturesUpdater.refreshFeatures user_id, (error) ->
  230. return next(error) if error?
  231. res.sendStatus 200