SubscriptionController.coffee 10 KB

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