SubscriptionController.coffee 10 KB

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