subscription-dashboard.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. const SUBSCRIPTION_URL = '/user/subscription/update'
  21. const ensureRecurlyIsSetup = _.once(() => {
  22. if (typeof recurly === 'undefined' || !recurly) {
  23. return false
  24. }
  25. recurly.configure(window.recurlyApiKey)
  26. return true
  27. })
  28. App.controller('MetricsEmailController', function ($scope, $http) {
  29. $scope.institutionEmailSubscription = function (institutionId) {
  30. var inst = _.find(window.managedInstitutions, function (institution) {
  31. return institution.v1Id === parseInt(institutionId)
  32. })
  33. if (inst.metricsEmail.optedOutUserIds.includes(window.user_id)) {
  34. return 'Subscribe'
  35. } else {
  36. return 'Unsubscribe'
  37. }
  38. }
  39. $scope.changeInstitutionalEmailSubscription = function (institutionId) {
  40. $scope.subscriptionChanging = true
  41. return $http({
  42. method: 'POST',
  43. url: `/institutions/${institutionId}/emailSubscription`,
  44. headers: {
  45. 'X-CSRF-Token': window.csrfToken
  46. }
  47. }).then(function successCallback(response) {
  48. window.managedInstitutions = _.map(
  49. window.managedInstitutions,
  50. function (institution) {
  51. if (institution.v1Id === parseInt(institutionId)) {
  52. institution.metricsEmail.optedOutUserIds = response.data
  53. }
  54. return institution
  55. }
  56. )
  57. $scope.subscriptionChanging = false
  58. })
  59. }
  60. })
  61. App.factory('RecurlyPricing', function ($q, MultiCurrencyPricing) {
  62. return {
  63. loadDisplayPriceWithTax: function (planCode, currency, taxRate) {
  64. if (!ensureRecurlyIsSetup()) return
  65. const currencySymbol = MultiCurrencyPricing.plans[currency].symbol
  66. const pricing = recurly.Pricing()
  67. return $q(function (resolve, reject) {
  68. pricing
  69. .plan(planCode, { quantity: 1 })
  70. .currency(currency)
  71. .done(function (price) {
  72. const totalPriceExTax = parseFloat(price.next.total)
  73. let taxAmmount = totalPriceExTax * taxRate
  74. if (isNaN(taxAmmount)) {
  75. taxAmmount = 0
  76. }
  77. let total = totalPriceExTax + taxAmmount
  78. if (total % 1 !== 0) {
  79. total = total.toFixed(2)
  80. }
  81. resolve(`${currencySymbol}${total}`)
  82. })
  83. })
  84. }
  85. }
  86. })
  87. App.controller(
  88. 'ChangePlanFormController',
  89. function ($scope, $modal, RecurlyPricing) {
  90. if (!ensureRecurlyIsSetup()) return
  91. $scope.changePlan = () =>
  92. $modal.open({
  93. templateUrl: 'confirmChangePlanModalTemplate',
  94. controller: 'ConfirmChangePlanController',
  95. scope: $scope
  96. })
  97. $scope.$watch('plan', function (plan) {
  98. if (!plan) return
  99. const planCode = plan.planCode
  100. const { currency, taxRate } = window.subscription.recurly
  101. $scope.price = '...' // Placeholder while we talk to recurly
  102. RecurlyPricing.loadDisplayPriceWithTax(planCode, currency, taxRate).then(
  103. price => {
  104. $scope.price = price
  105. }
  106. )
  107. })
  108. }
  109. )
  110. App.controller(
  111. 'ConfirmChangePlanController',
  112. function ($scope, $modalInstance, $http) {
  113. $scope.confirmChangePlan = function () {
  114. const body = {
  115. plan_code: $scope.plan.planCode,
  116. _csrf: window.csrfToken
  117. }
  118. $scope.inflight = true
  119. return $http
  120. .post(`${SUBSCRIPTION_URL}?origin=confirmChangePlan`, body)
  121. .then(() => location.reload())
  122. .catch(() => console.log('something went wrong changing plan'))
  123. }
  124. return ($scope.cancel = () => $modalInstance.dismiss('cancel'))
  125. }
  126. )
  127. App.controller(
  128. 'LeaveGroupModalController',
  129. function ($scope, $modalInstance, $http) {
  130. $scope.confirmLeaveGroup = function () {
  131. $scope.inflight = true
  132. return $http({
  133. url: '/subscription/group/user',
  134. method: 'DELETE',
  135. params: {
  136. subscriptionId: $scope.subscriptionId,
  137. _csrf: window.csrfToken
  138. }
  139. })
  140. .then(() => location.reload())
  141. .catch(() => console.log('something went wrong changing plan'))
  142. }
  143. return ($scope.cancel = () => $modalInstance.dismiss('cancel'))
  144. }
  145. )
  146. App.controller('GroupMembershipController', function ($scope, $modal) {
  147. $scope.removeSelfFromGroup = function (subscriptionId) {
  148. $scope.subscriptionId = subscriptionId
  149. return $modal.open({
  150. templateUrl: 'LeaveGroupModalTemplate',
  151. controller: 'LeaveGroupModalController',
  152. scope: $scope
  153. })
  154. }
  155. })
  156. App.controller('RecurlySubscriptionController', function ($scope) {
  157. const recurlyIsSetup = ensureRecurlyIsSetup()
  158. $scope.showChangePlanButton = recurlyIsSetup && !window.subscription.groupPlan
  159. if (
  160. window.subscription.recurly.account.has_past_due_invoice &&
  161. window.subscription.recurly.account.has_past_due_invoice._ === 'true'
  162. ) {
  163. $scope.showChangePlanButton = false
  164. }
  165. $scope.recurlyLoadError = !recurlyIsSetup
  166. $scope.switchToDefaultView = () => {
  167. $scope.showCancellation = false
  168. $scope.showChangePlan = false
  169. }
  170. $scope.switchToDefaultView()
  171. $scope.switchToCancellationView = () => {
  172. $scope.showCancellation = true
  173. $scope.showChangePlan = false
  174. }
  175. $scope.switchToChangePlanView = () => {
  176. $scope.showCancellation = false
  177. $scope.showChangePlan = true
  178. }
  179. })
  180. App.controller(
  181. 'RecurlyCancellationController',
  182. function ($scope, RecurlyPricing, $http) {
  183. if (!ensureRecurlyIsSetup()) return
  184. const subscription = window.subscription
  185. const sevenDaysTime = new Date()
  186. sevenDaysTime.setDate(sevenDaysTime.getDate() + 7)
  187. const freeTrialEndDate = new Date(subscription.recurly.trial_ends_at)
  188. const freeTrialInFuture = freeTrialEndDate > new Date()
  189. const freeTrialExpiresUnderSevenDays = freeTrialEndDate < sevenDaysTime
  190. const isMonthlyCollab =
  191. subscription.plan.planCode.indexOf('collaborator') !== -1 &&
  192. subscription.plan.planCode.indexOf('ann') === -1 &&
  193. !subscription.groupPlan
  194. const stillInFreeTrial = freeTrialInFuture && freeTrialExpiresUnderSevenDays
  195. if (isMonthlyCollab && stillInFreeTrial) {
  196. $scope.showExtendFreeTrial = true
  197. } else if (isMonthlyCollab && !stillInFreeTrial) {
  198. $scope.showDowngradeToStudent = true
  199. } else {
  200. $scope.showBasicCancel = true
  201. }
  202. const { currency, taxRate } = window.subscription.recurly
  203. $scope.studentPrice = '...' // Placeholder while we talk to recurly
  204. RecurlyPricing.loadDisplayPriceWithTax('student', currency, taxRate).then(
  205. price => {
  206. $scope.studentPrice = price
  207. }
  208. )
  209. $scope.downgradeToStudent = function () {
  210. const body = {
  211. plan_code: 'student',
  212. _csrf: window.csrfToken
  213. }
  214. $scope.inflight = true
  215. return $http
  216. .post(`${SUBSCRIPTION_URL}?origin=downgradeToStudent`, body)
  217. .then(() => location.reload())
  218. .catch(() => console.log('something went wrong changing plan'))
  219. }
  220. $scope.cancelSubscription = function () {
  221. const body = { _csrf: window.csrfToken }
  222. $scope.inflight = true
  223. return $http
  224. .post('/user/subscription/cancel', body)
  225. .then(() => (location.href = '/user/subscription/canceled'))
  226. .catch(() => console.log('something went wrong changing plan'))
  227. }
  228. $scope.extendTrial = function () {
  229. const body = { _csrf: window.csrfToken }
  230. $scope.inflight = true
  231. return $http
  232. .put('/user/subscription/extend', body)
  233. .then(() => location.reload())
  234. .catch(() => console.log('something went wrong changing plan'))
  235. }
  236. }
  237. )