subscription-dashboard.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.$watch('plan', function (plan) {
  99. if (!plan) return
  100. const planCode = plan.planCode
  101. const subscription = getMeta('ol-subscription')
  102. const { currency, taxRate } = subscription.recurly
  103. $scope.price = '...' // Placeholder while we talk to recurly
  104. RecurlyPricing.loadDisplayPriceWithTax(planCode, currency, taxRate).then(
  105. price => {
  106. $scope.price = price
  107. }
  108. )
  109. })
  110. }
  111. )
  112. App.controller(
  113. 'ConfirmChangePlanController',
  114. function ($scope, $modalInstance, $http) {
  115. $scope.confirmChangePlan = function () {
  116. const body = {
  117. plan_code: $scope.plan.planCode,
  118. _csrf: window.csrfToken
  119. }
  120. $scope.inflight = true
  121. return $http
  122. .post(`${SUBSCRIPTION_URL}?origin=confirmChangePlan`, body)
  123. .then(() => location.reload())
  124. .catch(() => console.log('something went wrong changing plan'))
  125. }
  126. return ($scope.cancel = () => $modalInstance.dismiss('cancel'))
  127. }
  128. )
  129. App.controller(
  130. 'LeaveGroupModalController',
  131. function ($scope, $modalInstance, $http) {
  132. $scope.confirmLeaveGroup = function () {
  133. $scope.inflight = true
  134. return $http({
  135. url: '/subscription/group/user',
  136. method: 'DELETE',
  137. params: {
  138. subscriptionId: $scope.subscriptionId,
  139. _csrf: window.csrfToken
  140. }
  141. })
  142. .then(() => location.reload())
  143. .catch(() => console.log('something went wrong changing plan'))
  144. }
  145. return ($scope.cancel = () => $modalInstance.dismiss('cancel'))
  146. }
  147. )
  148. App.controller('GroupMembershipController', function ($scope, $modal) {
  149. $scope.removeSelfFromGroup = function (subscriptionId) {
  150. $scope.subscriptionId = subscriptionId
  151. return $modal.open({
  152. templateUrl: 'LeaveGroupModalTemplate',
  153. controller: 'LeaveGroupModalController',
  154. scope: $scope
  155. })
  156. }
  157. })
  158. App.controller('RecurlySubscriptionController', function ($scope) {
  159. const recurlyIsSetup = ensureRecurlyIsSetup()
  160. const subscription = getMeta('ol-subscription')
  161. $scope.showChangePlanButton = recurlyIsSetup && !subscription.groupPlan
  162. if (
  163. window.subscription.recurly.account.has_past_due_invoice &&
  164. window.subscription.recurly.account.has_past_due_invoice._ === 'true'
  165. ) {
  166. $scope.showChangePlanButton = false
  167. }
  168. $scope.recurlyLoadError = !recurlyIsSetup
  169. $scope.switchToDefaultView = () => {
  170. $scope.showCancellation = false
  171. $scope.showChangePlan = false
  172. }
  173. $scope.switchToDefaultView()
  174. $scope.switchToCancellationView = () => {
  175. $scope.showCancellation = true
  176. $scope.showChangePlan = false
  177. }
  178. $scope.switchToChangePlanView = () => {
  179. $scope.showCancellation = false
  180. $scope.showChangePlan = true
  181. }
  182. })
  183. App.controller(
  184. 'RecurlyCancellationController',
  185. function ($scope, RecurlyPricing, $http) {
  186. if (!ensureRecurlyIsSetup()) return
  187. const subscription = getMeta('ol-subscription')
  188. const sevenDaysTime = new Date()
  189. sevenDaysTime.setDate(sevenDaysTime.getDate() + 7)
  190. const freeTrialEndDate = new Date(subscription.recurly.trial_ends_at)
  191. const freeTrialInFuture = freeTrialEndDate > new Date()
  192. const freeTrialExpiresUnderSevenDays = freeTrialEndDate < sevenDaysTime
  193. const isMonthlyCollab =
  194. subscription.plan.planCode.indexOf('collaborator') !== -1 &&
  195. subscription.plan.planCode.indexOf('ann') === -1 &&
  196. !subscription.groupPlan
  197. const stillInFreeTrial = freeTrialInFuture && freeTrialExpiresUnderSevenDays
  198. if (isMonthlyCollab && stillInFreeTrial) {
  199. $scope.showExtendFreeTrial = true
  200. } else if (isMonthlyCollab && !stillInFreeTrial) {
  201. $scope.showDowngradeToStudent = true
  202. } else {
  203. $scope.showBasicCancel = true
  204. }
  205. const { currency, taxRate } = subscription.recurly
  206. $scope.studentPrice = '...' // Placeholder while we talk to recurly
  207. RecurlyPricing.loadDisplayPriceWithTax('student', currency, taxRate).then(
  208. price => {
  209. $scope.studentPrice = price
  210. }
  211. )
  212. $scope.downgradeToStudent = function () {
  213. const body = {
  214. plan_code: 'student',
  215. _csrf: window.csrfToken
  216. }
  217. $scope.inflight = true
  218. return $http
  219. .post(`${SUBSCRIPTION_URL}?origin=downgradeToStudent`, body)
  220. .then(() => location.reload())
  221. .catch(() => console.log('something went wrong changing plan'))
  222. }
  223. $scope.cancelSubscription = function () {
  224. const body = { _csrf: window.csrfToken }
  225. $scope.inflight = true
  226. return $http
  227. .post('/user/subscription/cancel', body)
  228. .then(() => (location.href = '/user/subscription/canceled'))
  229. .catch(() => console.log('something went wrong changing plan'))
  230. }
  231. $scope.extendTrial = function () {
  232. const body = { _csrf: window.csrfToken }
  233. $scope.inflight = true
  234. return $http
  235. .put('/user/subscription/extend', body)
  236. .then(() => location.reload())
  237. .catch(() => console.log('something went wrong changing plan'))
  238. }
  239. }
  240. )