SubscriptionController.coffee 9.7 KB

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