subscription-dashboard.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import _ from 'lodash'
  2. /* global recurly */
  3. /* eslint-disable
  4. camelcase,
  5. max-len,
  6. no-return-assign,
  7. no-unused-vars,
  8. */
  9. // TODO: This file was created by bulk-decaffeinate.
  10. // Fix any style issues and re-enable lint.
  11. /*
  12. * decaffeinate suggestions:
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * DS104: Avoid inline assignments
  15. * DS204: Change includes calls to have a more natural evaluation order
  16. * DS207: Consider shorter variations of null checks
  17. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  18. */
  19. import App from '../base'
  20. import getMeta from '../utils/meta'
  21. const SUBSCRIPTION_URL = '/user/subscription/update'
  22. const ensureRecurlyIsSetup = _.once(() => {
  23. if (typeof recurly === 'undefined' || !recurly) {
  24. return false
  25. }
  26. recurly.configure(getMeta('ol-recurlyApiKey'))
  27. return true
  28. })
  29. App.controller('MetricsEmailController', function ($scope, $http) {
  30. $scope.institutionEmailSubscription = function (institutionId) {
  31. var inst = _.find(window.managedInstitutions, function (institution) {
  32. return institution.v1Id === parseInt(institutionId)
  33. })
  34. if (inst.metricsEmail.optedOutUserIds.includes(window.user_id)) {
  35. return 'Subscribe'
  36. } else {
  37. return 'Unsubscribe'
  38. }
  39. }
  40. $scope.changeInstitutionalEmailSubscription = function (institutionId) {
  41. $scope.subscriptionChanging = true
  42. return $http({
  43. method: 'POST',
  44. url: `/institutions/${institutionId}/emailSubscription`,
  45. headers: {
  46. 'X-CSRF-Token': window.csrfToken,
  47. },
  48. }).then(function successCallback(response) {
  49. window.managedInstitutions = _.map(
  50. window.managedInstitutions,
  51. function (institution) {
  52. if (institution.v1Id === parseInt(institutionId)) {
  53. institution.metricsEmail.optedOutUserIds = response.data
  54. }
  55. return institution
  56. }
  57. )
  58. $scope.subscriptionChanging = false
  59. })
  60. }
  61. })
  62. App.factory('RecurlyPricing', function ($q, MultiCurrencyPricing) {
  63. return {
  64. loadDisplayPriceWithTax: function (planCode, currency, taxRate) {
  65. if (!ensureRecurlyIsSetup()) return
  66. const currencySymbol = MultiCurrencyPricing.plans[currency].symbol
  67. const pricing = recurly.Pricing()
  68. return $q(function (resolve, reject) {
  69. pricing
  70. .plan(planCode, { quantity: 1 })
  71. .currency(currency)
  72. .done(function (price) {
  73. const totalPriceExTax = parseFloat(price.next.total)
  74. let taxAmmount = totalPriceExTax * taxRate
  75. if (isNaN(taxAmmount)) {
  76. taxAmmount = 0
  77. }
  78. let total = totalPriceExTax + taxAmmount
  79. if (total % 1 !== 0) {
  80. total = total.toFixed(2)
  81. }
  82. resolve(`${currencySymbol}${total}`)
  83. })
  84. })
  85. },
  86. }
  87. })
  88. App.controller(
  89. 'ChangePlanFormController',
  90. function ($scope, $modal, RecurlyPricing) {
  91. if (!ensureRecurlyIsSetup()) return
  92. $scope.changePlan = () =>
  93. $modal.open({
  94. templateUrl: 'confirmChangePlanModalTemplate',
  95. controller: 'ConfirmChangePlanController',
  96. scope: $scope,
  97. })
  98. $scope.cancelPendingPlanChange = () =>
  99. $modal.open({
  100. templateUrl: 'cancelPendingPlanChangeModalTemplate',
  101. controller: 'CancelPendingPlanChangeController',
  102. scope: $scope,
  103. })
  104. $scope.$watch('plan', function (plan) {
  105. if (!plan) return
  106. const planCodesChangingAtTermEnd = getMeta(
  107. 'ol-planCodesChangingAtTermEnd'
  108. )
  109. $scope.planChangesAtTermEnd = false
  110. if (
  111. planCodesChangingAtTermEnd &&
  112. planCodesChangingAtTermEnd.indexOf(plan.planCode) > -1
  113. ) {
  114. $scope.planChangesAtTermEnd = true
  115. }
  116. const planCode = plan.planCode
  117. const subscription = getMeta('ol-subscription')
  118. const { currency, taxRate } = subscription.recurly
  119. $scope.price = '...' // Placeholder while we talk to recurly
  120. RecurlyPricing.loadDisplayPriceWithTax(planCode, currency, taxRate).then(
  121. price => {
  122. $scope.price = price
  123. }
  124. )
  125. })
  126. }
  127. )
  128. App.controller(
  129. 'ConfirmChangePlanController',
  130. function ($scope, $modalInstance, $http) {
  131. $scope.confirmChangePlan = function () {
  132. const body = {
  133. plan_code: $scope.plan.planCode,
  134. _csrf: window.csrfToken,
  135. }
  136. $scope.genericError = false
  137. $scope.inflight = true
  138. return $http
  139. .post(`${SUBSCRIPTION_URL}?origin=confirmChangePlan`, body)
  140. .then(() => location.reload())
  141. .catch(() => {
  142. $scope.genericError = true
  143. $scope.inflight = false
  144. })
  145. }
  146. return ($scope.cancel = () => $modalInstance.dismiss('cancel'))
  147. }
  148. )
  149. App.controller(
  150. 'CancelPendingPlanChangeController',
  151. function ($scope, $modalInstance, $http) {
  152. $scope.confirmCancelPendingPlanChange = function () {
  153. const body = {
  154. _csrf: window.csrfToken,
  155. }
  156. $scope.genericError = false
  157. $scope.inflight = true
  158. return $http
  159. .post('/user/subscription/cancel-pending', body)
  160. .then(() => location.reload())
  161. .catch(() => {
  162. $scope.genericError = true
  163. $scope.inflight = false
  164. })
  165. }
  166. return ($scope.cancel = () => $modalInstance.dismiss('cancel'))
  167. }
  168. )
  169. App.controller(
  170. 'LeaveGroupModalController',
  171. function ($scope, $modalInstance, $http) {
  172. $scope.confirmLeaveGroup = function () {
  173. $scope.inflight = true
  174. return $http({
  175. url: '/subscription/group/user',
  176. method: 'DELETE',
  177. params: {
  178. subscriptionId: $scope.subscriptionId,
  179. _csrf: window.csrfToken,
  180. },
  181. })
  182. .then(() => location.reload())
  183. .catch(() => console.log('something went wrong changing plan'))
  184. }
  185. return ($scope.cancel = () => $modalInstance.dismiss('cancel'))
  186. }
  187. )
  188. App.controller('GroupMembershipController', function ($scope, $modal) {
  189. $scope.removeSelfFromGroup = function (subscriptionId) {
  190. $scope.subscriptionId = subscriptionId
  191. return $modal.open({
  192. templateUrl: 'LeaveGroupModalTemplate',
  193. controller: 'LeaveGroupModalController',
  194. scope: $scope,
  195. })
  196. }
  197. })
  198. App.controller('RecurlySubscriptionController', function ($scope) {
  199. const recurlyIsSetup = ensureRecurlyIsSetup()
  200. const subscription = getMeta('ol-subscription')
  201. $scope.showChangePlanButton = recurlyIsSetup && !subscription.groupPlan
  202. if (
  203. window.subscription.recurly.account.has_past_due_invoice &&
  204. window.subscription.recurly.account.has_past_due_invoice._ === 'true'
  205. ) {
  206. $scope.showChangePlanButton = false
  207. }
  208. $scope.recurlyLoadError = !recurlyIsSetup
  209. $scope.switchToDefaultView = () => {
  210. $scope.showCancellation = false
  211. $scope.showChangePlan = false
  212. }
  213. $scope.switchToDefaultView()
  214. $scope.switchToCancellationView = () => {
  215. $scope.showCancellation = true
  216. $scope.showChangePlan = false
  217. }
  218. $scope.switchToChangePlanView = () => {
  219. $scope.showCancellation = false
  220. $scope.showChangePlan = true
  221. }
  222. })
  223. App.controller(
  224. 'RecurlyCancellationController',
  225. function ($scope, RecurlyPricing, $http) {
  226. if (!ensureRecurlyIsSetup()) return
  227. const subscription = getMeta('ol-subscription')
  228. const sevenDaysTime = new Date()
  229. sevenDaysTime.setDate(sevenDaysTime.getDate() + 7)
  230. const freeTrialEndDate = new Date(subscription.recurly.trial_ends_at)
  231. const freeTrialInFuture = freeTrialEndDate > new Date()
  232. const freeTrialExpiresUnderSevenDays = freeTrialEndDate < sevenDaysTime
  233. const isMonthlyCollab =
  234. subscription.plan.planCode.indexOf('collaborator') !== -1 &&
  235. subscription.plan.planCode.indexOf('ann') === -1 &&
  236. !subscription.groupPlan
  237. const stillInFreeTrial = freeTrialInFuture && freeTrialExpiresUnderSevenDays
  238. if (isMonthlyCollab && stillInFreeTrial) {
  239. $scope.showExtendFreeTrial = true
  240. } else if (isMonthlyCollab && !stillInFreeTrial) {
  241. $scope.showDowngradeToStudent = true
  242. } else {
  243. $scope.showBasicCancel = true
  244. }
  245. const { currency, taxRate } = subscription.recurly
  246. $scope.studentPrice = '...' // Placeholder while we talk to recurly
  247. RecurlyPricing.loadDisplayPriceWithTax('student', currency, taxRate).then(
  248. price => {
  249. $scope.studentPrice = price
  250. }
  251. )
  252. $scope.downgradeToStudent = function () {
  253. const body = {
  254. plan_code: 'student',
  255. _csrf: window.csrfToken,
  256. }
  257. $scope.inflight = true
  258. return $http
  259. .post(`${SUBSCRIPTION_URL}?origin=downgradeToStudent`, body)
  260. .then(() => location.reload())
  261. .catch(() => console.log('something went wrong changing plan'))
  262. }
  263. $scope.cancelSubscription = function () {
  264. const body = { _csrf: window.csrfToken }
  265. $scope.inflight = true
  266. return $http
  267. .post('/user/subscription/cancel', body)
  268. .then(() => (location.href = '/user/subscription/canceled'))
  269. .catch(() => console.log('something went wrong changing plan'))
  270. }
  271. $scope.extendTrial = function () {
  272. const body = { _csrf: window.csrfToken }
  273. $scope.inflight = true
  274. return $http
  275. .put('/user/subscription/extend', body)
  276. .then(() => location.reload())
  277. .catch(() => console.log('something went wrong changing plan'))
  278. }
  279. }
  280. )