SubscriptionController.coffee 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. AuthenticationController.getLoggedInUser req, (error, user) ->
  157. return next(error) if error?
  158. planCode = req.body.plan_code
  159. if !planCode?
  160. err = new Error('plan_code not defined')
  161. logger.err {user_id: user._id, err}, "error updating subscription"
  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. AuthenticationController.getLoggedInUser req, (error, user) ->
  171. return next(error) if error?
  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. AuthenticationController.getLoggedInUser req, (error, user) ->
  190. return next(error) if error?
  191. LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
  192. return next(err) if err?
  193. planCode = subscription?.planCode.toLowerCase()
  194. if planCode?.indexOf("annual") != -1
  195. planName = "annual"
  196. else if planCode?.indexOf("student") != -1
  197. planName = "student"
  198. else if planCode?.indexOf("collaborator") != -1
  199. planName = "collaborator"
  200. if !hasSubscription
  201. return res.redirect("/user/subscription/plans")
  202. logger.log planName:planName, user_id:user._id, "rendering upgrade to annual page"
  203. res.render "subscriptions/upgradeToAnnual",
  204. title: "Upgrade to annual"
  205. planName: planName
  206. processUpgradeToAnnualPlan: (req, res, next)->
  207. AuthenticationController.getLoggedInUser req, (error, user) ->
  208. return next(error) if error?
  209. {planName} = req.body
  210. coupon_code = Settings.coupon_codes.upgradeToAnnualPromo[planName]
  211. annualPlanName = "#{planName}-annual"
  212. logger.log user_id:user._id, planName:annualPlanName, "user is upgrading to annual billing with discount"
  213. SubscriptionHandler.updateSubscription user, annualPlanName, coupon_code, (err)->
  214. if err?
  215. logger.err err:err, user_id:user._id, "error updating subscription"
  216. return next(err)
  217. res.sendStatus 200
  218. extendTrial: (req, res, next)->
  219. AuthenticationController.getLoggedInUser req, (error, user) ->
  220. return next(error) if error?
  221. LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
  222. return next(err) if err?
  223. SubscriptionHandler.extendTrial subscription, 14, (err)->
  224. if err?
  225. res.send 500
  226. else
  227. res.send 200
  228. recurlyNotificationParser: (req, res, next) ->
  229. xml = ""
  230. req.on "data", (chunk) ->
  231. xml += chunk
  232. req.on "end", () ->
  233. RecurlyWrapper._parseXml xml, (error, body) ->
  234. return next(error) if error?
  235. req.body = body
  236. next()