SubscriptionController.coffee 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. 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. AuthenticationController.getLoggedInUser req, (error, user) ->
  167. return next(error) if error?
  168. logger.log user_id:user._id, "reactivating subscription"
  169. SubscriptionHandler.reactivateSubscription user, (err)->
  170. if err?
  171. logger.err err:err, user_id:user._id, "something went wrong reactivating subscription"
  172. return next(err)
  173. res.redirect "/user/subscription"
  174. recurlyCallback: (req, res, next)->
  175. logger.log data: req.body, "received recurly callback"
  176. # we only care if a subscription has exipired
  177. if req.body? and req.body["expired_subscription_notification"]?
  178. recurlySubscription = req.body["expired_subscription_notification"].subscription
  179. SubscriptionHandler.recurlyCallback recurlySubscription, (err)->
  180. return next(err) if err?
  181. res.sendStatus 200
  182. else
  183. res.sendStatus 200
  184. renderUpgradeToAnnualPlanPage: (req, res, next)->
  185. AuthenticationController.getLoggedInUser req, (error, user) ->
  186. return next(error) if error?
  187. LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
  188. return next(err) if err?
  189. planCode = subscription?.planCode.toLowerCase()
  190. if planCode?.indexOf("annual") != -1
  191. planName = "annual"
  192. else if planCode?.indexOf("student") != -1
  193. planName = "student"
  194. else if planCode?.indexOf("collaborator") != -1
  195. planName = "collaborator"
  196. if !hasSubscription
  197. return res.redirect("/user/subscription/plans")
  198. logger.log planName:planName, user_id:user._id, "rendering upgrade to annual page"
  199. res.render "subscriptions/upgradeToAnnual",
  200. title: "Upgrade to annual"
  201. planName: planName
  202. processUpgradeToAnnualPlan: (req, res, next)->
  203. AuthenticationController.getLoggedInUser req, (error, user) ->
  204. return next(error) if error?
  205. {planName} = req.body
  206. coupon_code = Settings.coupon_codes.upgradeToAnnualPromo[planName]
  207. annualPlanName = "#{planName}-annual"
  208. logger.log user_id:user._id, planName:annualPlanName, "user is upgrading to annual billing with discount"
  209. SubscriptionHandler.updateSubscription user, annualPlanName, coupon_code, (err)->
  210. if err?
  211. logger.err err:err, user_id:user._id, "error updating subscription"
  212. return next(err)
  213. res.sendStatus 200
  214. extendTrial: (req, res, next)->
  215. AuthenticationController.getLoggedInUser req, (error, user) ->
  216. return next(error) if error?
  217. LimitationsManager.userHasSubscription user, (err, hasSubscription, subscription)->
  218. return next(err) if err?
  219. SubscriptionHandler.extendTrial subscription, 14, (err)->
  220. if err?
  221. res.send 500
  222. else
  223. res.send 200
  224. recurlyNotificationParser: (req, res, next) ->
  225. xml = ""
  226. req.on "data", (chunk) ->
  227. xml += chunk
  228. req.on "end", () ->
  229. RecurlyWrapper._parseXml xml, (error, body) ->
  230. return next(error) if error?
  231. req.body = body
  232. next()