SubscriptionHelper.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. const GroupPlansData = require('./GroupPlansData')
  2. /**
  3. * If the user changes to a less expensive plan, we shouldn't apply the change immediately.
  4. * This is to avoid unintended/artifical credits on users Recurly accounts.
  5. */
  6. function shouldPlanChangeAtTermEnd(oldPlan, newPlan) {
  7. return oldPlan.price_in_cents > newPlan.price_in_cents
  8. }
  9. /**
  10. * @import { CurrencyCode } from '../../../../frontend/js/shared/utils/currency'
  11. */
  12. /**
  13. * @param {CurrencyCode} recommendedCurrency
  14. * @param {string} locale
  15. * @param {(amount: number, currency: CurrencyCode, locale: string, stripIfInteger: boolean) => string} formatCurrency
  16. * @returns {{ price: { collaborator: string, professional: string }, pricePerUser: { collaborator: string, professional: string } }} - localized group price
  17. */
  18. function generateInitialLocalizedGroupPrice(
  19. recommendedCurrency,
  20. locale,
  21. formatCurrency
  22. ) {
  23. const INITIAL_LICENSE_SIZE = 2
  24. // the price is in cents, so divide by 100 to get the value
  25. const collaboratorPrice =
  26. GroupPlansData.enterprise.collaborator[recommendedCurrency][
  27. INITIAL_LICENSE_SIZE
  28. ].price_in_cents / 100
  29. const collaboratorPricePerUser = collaboratorPrice / INITIAL_LICENSE_SIZE
  30. const professionalPrice =
  31. GroupPlansData.enterprise.professional[recommendedCurrency][
  32. INITIAL_LICENSE_SIZE
  33. ].price_in_cents / 100
  34. const professionalPricePerUser = professionalPrice / INITIAL_LICENSE_SIZE
  35. /**
  36. * @param {number} price
  37. * @returns {string}
  38. */
  39. const formatPrice = price =>
  40. formatCurrency(price, recommendedCurrency, locale, true)
  41. return {
  42. price: {
  43. collaborator: formatPrice(collaboratorPrice),
  44. professional: formatPrice(professionalPrice),
  45. },
  46. pricePerUser: {
  47. collaborator: formatPrice(collaboratorPricePerUser),
  48. professional: formatPrice(professionalPricePerUser),
  49. },
  50. }
  51. }
  52. const currencies = {
  53. USD: {
  54. symbol: '$',
  55. placement: 'before',
  56. },
  57. EUR: {
  58. symbol: '€',
  59. placement: 'before',
  60. },
  61. GBP: {
  62. symbol: '£',
  63. placement: 'before',
  64. },
  65. SEK: {
  66. symbol: ' kr',
  67. placement: 'after',
  68. },
  69. CAD: {
  70. symbol: '$',
  71. placement: 'before',
  72. },
  73. NOK: {
  74. symbol: ' kr',
  75. placement: 'after',
  76. },
  77. DKK: {
  78. symbol: ' kr',
  79. placement: 'after',
  80. },
  81. AUD: {
  82. symbol: '$',
  83. placement: 'before',
  84. },
  85. NZD: {
  86. symbol: '$',
  87. placement: 'before',
  88. },
  89. CHF: {
  90. symbol: 'Fr ',
  91. placement: 'before',
  92. },
  93. SGD: {
  94. symbol: '$',
  95. placement: 'before',
  96. },
  97. INR: {
  98. symbol: '₹',
  99. placement: 'before',
  100. },
  101. BRL: {
  102. code: 'BRL',
  103. locale: 'pt-BR',
  104. symbol: 'R$ ',
  105. placement: 'before',
  106. },
  107. MXN: {
  108. code: 'MXN',
  109. locale: 'es-MX',
  110. symbol: '$ ',
  111. placement: 'before',
  112. },
  113. COP: {
  114. code: 'COP',
  115. locale: 'es-CO',
  116. symbol: '$ ',
  117. placement: 'before',
  118. },
  119. CLP: {
  120. code: 'CLP',
  121. locale: 'es-CL',
  122. symbol: '$ ',
  123. placement: 'before',
  124. },
  125. PEN: {
  126. code: 'PEN',
  127. locale: 'es-PE',
  128. symbol: 'S/ ',
  129. placement: 'before',
  130. },
  131. }
  132. function formatCurrencyDefault(amount, recommendedCurrency) {
  133. const currency = currencies[recommendedCurrency]
  134. // Test using toLocaleString to format currencies for new LATAM regions
  135. if (currency.locale && currency.code) {
  136. return amount.toLocaleString(currency.locale, {
  137. style: 'currency',
  138. currency: currency.code,
  139. minimumFractionDigits: 0,
  140. })
  141. }
  142. return currency.placement === 'before'
  143. ? `${currency.symbol}${amount}`
  144. : `${amount}${currency.symbol}`
  145. }
  146. module.exports = {
  147. formatCurrencyDefault,
  148. shouldPlanChangeAtTermEnd,
  149. generateInitialLocalizedGroupPrice,
  150. }