SubscriptionController.coffee 9.8 KB

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