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