SubscriptionController.coffee 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. { personalSubscription, groupSubscriptions, v1Subscriptions } = results
  88. logger.log {user, personalSubscription, groupSubscriptions, v1Subscriptions}, "showing subscription dashboard"
  89. plans = SubscriptionViewModelBuilder.buildViewModel()
  90. data =
  91. title: "your_subscription"
  92. plans: plans
  93. user: user
  94. personalSubscription: personalSubscription
  95. groupSubscriptions: groupSubscriptions
  96. v1Subscriptions: v1Subscriptions
  97. res.render "subscriptions/dashboard", data
  98. createSubscription: (req, res, next)->
  99. user = AuthenticationController.getSessionUser(req)
  100. recurly_token_id = req.body.recurly_token_id
  101. subscriptionDetails = req.body.subscriptionDetails
  102. logger.log recurly_token_id: recurly_token_id, user_id:user._id, subscriptionDetails:subscriptionDetails, "creating subscription"
  103. LimitationsManager.userHasV1OrV2Subscription user, (err, hasSubscription)->
  104. return next(err) if err?
  105. if hasSubscription
  106. logger.warn {user_id: user._id}, 'user already has subscription'
  107. res.sendStatus 409 # conflict
  108. SubscriptionHandler.createSubscription user, subscriptionDetails, recurly_token_id, (err)->
  109. if err?
  110. logger.err err:err, user_id:user._id, "something went wrong creating subscription"
  111. return next(err)
  112. res.sendStatus 201
  113. successful_subscription: (req, res, next)->
  114. user = AuthenticationController.getSessionUser(req)
  115. SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel user, (error, {personalSubscription}) ->
  116. return next(error) if error?
  117. if !personalSubscription?
  118. return res.redirect '/user/subscription/plans'
  119. res.render "subscriptions/successful_subscription",
  120. title: "thank_you"
  121. subscription:personalSubscription
  122. cancelSubscription: (req, res, next) ->
  123. user = AuthenticationController.getSessionUser(req)
  124. logger.log user_id:user._id, "canceling subscription"
  125. SubscriptionHandler.cancelSubscription user, (err)->
  126. if err?
  127. logger.err err:err, user_id:user._id, "something went wrong canceling subscription"
  128. return next(err)
  129. res.redirect "/user/subscription"
  130. updateSubscription: (req, res, next)->
  131. _origin = req?.query?.origin || null
  132. user = AuthenticationController.getSessionUser(req)
  133. planCode = req.body.plan_code
  134. if !planCode?
  135. err = new Error('plan_code is not defined')
  136. logger.err {user_id: user._id, err, planCode, origin: _origin, body: req.body}, "[Subscription] error in updateSubscription form"
  137. return next(err)
  138. logger.log planCode: planCode, user_id:user._id, "updating subscription"
  139. SubscriptionHandler.updateSubscription user, planCode, null, (err)->
  140. if err?
  141. logger.err err:err, user_id:user._id, "something went wrong updating subscription"
  142. return next(err)
  143. res.redirect "/user/subscription"
  144. reactivateSubscription: (req, res, next)->
  145. user = AuthenticationController.getSessionUser(req)
  146. logger.log user_id:user._id, "reactivating subscription"
  147. SubscriptionHandler.reactivateSubscription user, (err)->
  148. if err?
  149. logger.err err:err, user_id:user._id, "something went wrong reactivating subscription"
  150. return next(err)
  151. res.redirect "/user/subscription"
  152. recurlyCallback: (req, res, next)->
  153. logger.log data: req.body, "received recurly callback"
  154. # we only care if a subscription has exipired
  155. if req.body? and req.body["expired_subscription_notification"]?
  156. recurlySubscription = req.body["expired_subscription_notification"].subscription
  157. SubscriptionHandler.recurlyCallback recurlySubscription, (err)->
  158. return next(err) if err?
  159. res.sendStatus 200
  160. else
  161. res.sendStatus 200
  162. renderUpgradeToAnnualPlanPage: (req, res, next)->
  163. user = AuthenticationController.getSessionUser(req)
  164. LimitationsManager.userHasV2Subscription user, (err, hasSubscription, subscription)->
  165. return next(err) if err?
  166. planCode = subscription?.planCode.toLowerCase()
  167. if planCode?.indexOf("annual") != -1
  168. planName = "annual"
  169. else if planCode?.indexOf("student") != -1
  170. planName = "student"
  171. else if planCode?.indexOf("collaborator") != -1
  172. planName = "collaborator"
  173. if !hasSubscription
  174. return res.redirect("/user/subscription/plans")
  175. logger.log planName:planName, user_id:user._id, "rendering upgrade to annual page"
  176. res.render "subscriptions/upgradeToAnnual",
  177. title: "Upgrade to annual"
  178. planName: planName
  179. processUpgradeToAnnualPlan: (req, res, next)->
  180. user = AuthenticationController.getSessionUser(req)
  181. {planName} = req.body
  182. coupon_code = Settings.coupon_codes.upgradeToAnnualPromo[planName]
  183. annualPlanName = "#{planName}-annual"
  184. logger.log user_id:user._id, planName:annualPlanName, "user is upgrading to annual billing with discount"
  185. SubscriptionHandler.updateSubscription user, annualPlanName, coupon_code, (err)->
  186. if err?
  187. logger.err err:err, user_id:user._id, "error updating subscription"
  188. return next(err)
  189. res.sendStatus 200
  190. extendTrial: (req, res, next)->
  191. user = AuthenticationController.getSessionUser(req)
  192. LimitationsManager.userHasV2Subscription user, (err, hasSubscription, subscription)->
  193. return next(err) if err?
  194. SubscriptionHandler.extendTrial subscription, 14, (err)->
  195. if err?
  196. res.send 500
  197. else
  198. res.send 200
  199. recurlyNotificationParser: (req, res, next) ->
  200. xml = ""
  201. req.on "data", (chunk) ->
  202. xml += chunk
  203. req.on "end", () ->
  204. RecurlyWrapper._parseXml xml, (error, body) ->
  205. return next(error) if error?
  206. req.body = body
  207. next()
  208. refreshUserFeatures: (req, res, next) ->
  209. {user_id} = req.params
  210. FeaturesUpdater.refreshFeatures user_id, (error) ->
  211. return next(error) if error?
  212. res.sendStatus 200