SubscriptionController.coffee 9.7 KB

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