group-plan-pricing.js 2.0 KB

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