SubscriptionController.coffee 10 KB

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