SubscriptionController.coffee 9.6 KB

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