SubscriptionController.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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) ->
  95. return next(error) if error?
  96. logger.log user: user, subscription:subscription, hasSubOrIsGroupMember:hasSubOrIsGroupMember, groupSubscriptions:groupSubscriptions, "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. userCustomSubscriptionPage: (req, res, next)->
  109. user = AuthenticationController.getSessionUser(req)
  110. LimitationsManager.userHasSubscriptionOrIsGroupMember user, (err, hasSubOrIsGroupMember, subscription)->
  111. return next(err) if err?
  112. if !subscription?
  113. err = new Error("subscription null for custom account, user:#{user?._id}")
  114. logger.warn err:err, "subscription is null for custom accounts page"
  115. return next(err)
  116. res.render "subscriptions/custom_account",
  117. title: "your_subscription"
  118. subscription: subscription
  119. editBillingDetailsPage: (req, res, next) ->
  120. user = AuthenticationController.getSessionUser(req)
  121. LimitationsManager.userHasSubscription user, (err, hasSubscription)->
  122. return next(err) if err?
  123. if !hasSubscription
  124. res.redirect "/user/subscription"
  125. else
  126. RecurlyWrapper.sign {
  127. account_code: user._id
  128. }, (error, signature) ->
  129. return next(error) if error?
  130. res.render "subscriptions/edit-billing-details",
  131. title : "update_billing_details"
  132. recurlyConfig: JSON.stringify
  133. currency: "USD"
  134. subdomain: Settings.apis.recurly.subdomain
  135. signature : signature
  136. successURL : "#{Settings.siteUrl}/user/subscription/billing-details/update"
  137. user :
  138. id : user._id
  139. updateBillingDetails: (req, res, next) ->
  140. res.redirect "/user/subscription?saved_billing_details=true"
  141. createSubscription: (req, res, next)->
  142. user = AuthenticationController.getSessionUser(req)
  143. recurly_token_id = req.body.recurly_token_id
  144. subscriptionDetails = req.body.subscriptionDetails
  145. logger.log recurly_token_id: recurly_token_id, user_id:user._id, subscriptionDetails:subscriptionDetails, "creating subscription"
  146. SubscriptionHandler.createSubscription user, subscriptionDetails, recurly_token_id, (err)->
  147. if err?
  148. logger.err err:err, user_id:user._id, "something went wrong creating subscription"
  149. return res.sendStatus 500
  150. res.sendStatus 201
  151. successful_subscription: (req, res, next)->
  152. user = AuthenticationController.getSessionUser(req)
  153. SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel user, (error, subscription) ->
  154. return next(error) if error?
  155. res.render "subscriptions/successful_subscription",
  156. title: "thank_you"
  157. subscription:subscription
  158. cancelSubscription: (req, res, next) ->
  159. user = AuthenticationController.getSessionUser(req)
  160. logger.log user_id:user._id, "canceling subscription"
  161. SubscriptionHandler.cancelSubscription user, (err)->
  162. if err?
  163. logger.err err:err, user_id:user._id, "something went wrong canceling subscription"
  164. return next(err)
  165. res.redirect "/user/subscription"
  166. updateSubscription: (req, res, next)->
  167. _origin = req?.query?.origin || null
  168. user = AuthenticationController.getSessionUser(req)
  169. planCode = req.body.plan_code
  170. if !planCode?
  171. err = new Error('plan_code is not defined')
  172. logger.err {user_id: user._id, err, planCode, origin: _origin, body: req.body}, "[Subscription] error in updateSubscription form"
  173. return next(err)
  174. logger.log planCode: planCode, user_id:user._id, "updating subscription"
  175. SubscriptionHandler.updateSubscription user, planCode, null, (err)->
  176. if err?
  177. logger.err err:err, user_id:user._id, "something went wrong updating subscription"
  178. return next(err)
  179. res.redirect "/user/subscription"
  180. reactivateSubscription: (req, res, next)->
  181. user = AuthenticationController.getSessionUser(req)
  182. logger.log user_id:user._id, "reactivating subscription"
  183. SubscriptionHandler.reactivateSubscription user, (err)->
  184. if err?
  185. logger.err err:err, user_id:user._id, "something went wrong reactivating subscription"
  186. return next(err)
  187. res.redirect "/user/subscription"
  188. recurlyCallback: (req, res, next)->
  189. logger.log data: req.body, "received recurly callback"
  190. # we only care if a subscription has exipired
  191. if req.body? and req.body["expired_subscription_notification"]?
  192. recurlySubscription = req.body["expired_subscription_notification"].subscription
  193. SubscriptionHandler.recurlyCallback recurlySubscription, (err)->
  194. return next(err) if err?
  195. res.sendStatus 200
  196. else
  197. res.sendStatus 200
  198. renderUpgradeToAnnualPlanPage: (req, res, next)->
  199. user = AuthenticationController.getSessionUser(req)
  200. LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
  201. return next(err) if err?
  202. planCode = subscription?.planCode.toLowerCase()
  203. if planCode?.indexOf("annual") != -1
  204. planName = "annual"
  205. else if planCode?.indexOf("student") != -1
  206. planName = "student"
  207. else if planCode?.indexOf("collaborator") != -1
  208. planName = "collaborator"
  209. if !hasSubscription
  210. return res.redirect("/user/subscription/plans")
  211. logger.log planName:planName, user_id:user._id, "rendering upgrade to annual page"
  212. res.render "subscriptions/upgradeToAnnual",
  213. title: "Upgrade to annual"
  214. planName: planName
  215. processUpgradeToAnnualPlan: (req, res, next)->
  216. user = AuthenticationController.getSessionUser(req)
  217. {planName} = req.body
  218. coupon_code = Settings.coupon_codes.upgradeToAnnualPromo[planName]
  219. annualPlanName = "#{planName}-annual"
  220. logger.log user_id:user._id, planName:annualPlanName, "user is upgrading to annual billing with discount"
  221. SubscriptionHandler.updateSubscription user, annualPlanName, coupon_code, (err)->
  222. if err?
  223. logger.err err:err, user_id:user._id, "error updating subscription"
  224. return next(err)
  225. res.sendStatus 200
  226. extendTrial: (req, res, next)->
  227. user = AuthenticationController.getSessionUser(req)
  228. LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
  229. return next(err) if err?
  230. SubscriptionHandler.extendTrial subscription, 14, (err)->
  231. if err?
  232. res.send 500
  233. else
  234. res.send 200
  235. recurlyNotificationParser: (req, res, next) ->
  236. xml = ""
  237. req.on "data", (chunk) ->
  238. xml += chunk
  239. req.on "end", () ->
  240. RecurlyWrapper._parseXml xml, (error, body) ->
  241. return next(error) if error?
  242. req.body = body
  243. next()