group-plan-pricing.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import getMeta from '../../../utils/meta'
  2. /**
  3. * @typedef {import('@/shared/utils/currency').CurrencyCode} CurrencyCode
  4. */
  5. // plan: 'collaborator' or 'professional'
  6. // the rest of available arguments can be seen in the groupPlans value
  7. /**
  8. * @param {'collaborator' | 'professional'} plan
  9. * @param {string} licenseSize
  10. * @param {CurrencyCode} currency
  11. * @param {'enterprise' | 'educational'} usage
  12. * @param {string?} locale
  13. * @param {(amount: number, currency: CurrencyCode, locale: string, includeSymbol: boolean) => string} formatCurrency
  14. * @returns {{localizedPrice: string, localizedPerUserPrice: string}}
  15. */
  16. export function createLocalizedGroupPlanPrice({
  17. plan,
  18. licenseSize,
  19. currency,
  20. usage,
  21. locale = getMeta('ol-i18n').currentLangCode || 'en',
  22. formatCurrency,
  23. }) {
  24. const groupPlans = getMeta('ol-groupPlans')
  25. const priceInCents =
  26. groupPlans[usage][plan][currency][licenseSize].price_in_cents
  27. const price = priceInCents / 100
  28. const perUserPrice = price / parseInt(licenseSize)
  29. /**
  30. * @param {number} price
  31. * @returns {string}
  32. */
  33. const formatPrice = price => formatCurrency(price, currency, locale, true)
  34. return {
  35. localizedPrice: formatPrice(price),
  36. localizedPerUserPrice: formatPrice(perUserPrice),
  37. }
  38. }
  39. const LOCALES = {
  40. BRL: 'pt-BR',
  41. MXN: 'es-MX',
  42. COP: 'es-CO',
  43. CLP: 'es-CL',
  44. PEN: 'es-PE',
  45. }
  46. /**
  47. * @param {number} amount
  48. * @param {string} currency
  49. */
  50. export function formatCurrencyDefault(amount, currency) {
  51. const currencySymbols = getMeta('ol-currencySymbols')
  52. const currencySymbol = currencySymbols[currency]
  53. switch (currency) {
  54. case 'BRL':
  55. case 'MXN':
  56. case 'COP':
  57. case 'CLP':
  58. case 'PEN':
  59. // Test using toLocaleString to format currencies for new LATAM regions
  60. return amount.toLocaleString(LOCALES[currency], {
  61. style: 'currency',
  62. currency,
  63. minimumFractionDigits: Number.isInteger(amount) ? 0 : null,
  64. })
  65. case 'CHF':
  66. return `${currencySymbol} ${amount}`
  67. case 'DKK':
  68. case 'SEK':
  69. case 'NOK':
  70. return `${amount} ${currencySymbol}`
  71. default: {
  72. return `${currencySymbol}${amount}`
  73. }
  74. }
  75. }