SubscriptionController.coffee 10 KB

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