group-plan-pricing.js 2.2 KB

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