recurly-pricing.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { SubscriptionPricingState } from '@recurly/recurly-js'
  2. import { PriceForDisplayData } from '../../../../../types/subscription/plan'
  3. import { currencies, CurrencyCode } from '../data/currency'
  4. import { getRecurlyGroupPlanCode } from './recurly-group-plan-code'
  5. import { debugConsole } from '@/utils/debugging'
  6. function queryRecurlyPlanPrice(planCode: string, currency: CurrencyCode) {
  7. return new Promise(resolve => {
  8. recurly.Pricing.Subscription()
  9. .plan(planCode, { quantity: 1 })
  10. .currency(currency)
  11. .catch(debugConsole.error)
  12. .done(response => {
  13. if (response) {
  14. resolve(response)
  15. } else {
  16. resolve(undefined)
  17. }
  18. })
  19. })
  20. }
  21. type FormatCurrency = (
  22. price: number,
  23. currency: CurrencyCode,
  24. locale: string,
  25. stripIfInteger?: boolean
  26. ) => string
  27. export const formatCurrencyDefault: FormatCurrency = (
  28. price: number,
  29. currency: CurrencyCode,
  30. _locale: string,
  31. stripIfInteger = false
  32. ) => {
  33. const currencySymbol = currencies[currency]
  34. const number =
  35. stripIfInteger && price % 1 === 0 ? Number(price) : price.toFixed(2)
  36. return `${currencySymbol}${number}`
  37. }
  38. export function formatPriceForDisplayData(
  39. price: string,
  40. taxRate: number,
  41. currencyCode: CurrencyCode,
  42. locale: string,
  43. formatCurrency: FormatCurrency
  44. ): PriceForDisplayData {
  45. const totalPriceExTax = parseFloat(price)
  46. let taxAmount = totalPriceExTax * taxRate
  47. if (isNaN(taxAmount)) {
  48. taxAmount = 0
  49. }
  50. const totalWithTax = totalPriceExTax + taxAmount
  51. return {
  52. totalForDisplay: formatCurrency(totalWithTax, currencyCode, locale, true),
  53. totalAsNumber: totalWithTax,
  54. subtotal: formatCurrency(totalPriceExTax, currencyCode, locale),
  55. tax: formatCurrency(taxAmount, currencyCode, locale),
  56. includesTax: taxAmount !== 0,
  57. }
  58. }
  59. function getPerUserDisplayPrice(
  60. totalPrice: number,
  61. currency: CurrencyCode,
  62. size: string,
  63. locale: string,
  64. formatCurrency: FormatCurrency
  65. ): string {
  66. return formatCurrency(totalPrice / parseInt(size), currency, locale, true)
  67. }
  68. export async function loadDisplayPriceWithTaxPromise(
  69. planCode: string,
  70. currencyCode: CurrencyCode,
  71. taxRate: number,
  72. locale: string,
  73. formatCurrency: FormatCurrency
  74. ) {
  75. if (!recurly) return
  76. const price = (await queryRecurlyPlanPrice(
  77. planCode,
  78. currencyCode
  79. )) as SubscriptionPricingState['price']
  80. if (price)
  81. return formatPriceForDisplayData(
  82. price.next.total,
  83. taxRate,
  84. currencyCode,
  85. locale,
  86. formatCurrency
  87. )
  88. }
  89. export async function loadGroupDisplayPriceWithTaxPromise(
  90. groupPlanCode: string,
  91. currencyCode: CurrencyCode,
  92. taxRate: number,
  93. size: string,
  94. usage: string,
  95. locale: string,
  96. formatCurrency: FormatCurrency
  97. ) {
  98. if (!recurly) return
  99. const planCode = getRecurlyGroupPlanCode(groupPlanCode, size, usage)
  100. const price = await loadDisplayPriceWithTaxPromise(
  101. planCode,
  102. currencyCode,
  103. taxRate,
  104. locale,
  105. formatCurrency
  106. )
  107. if (price) {
  108. price.perUserDisplayPrice = getPerUserDisplayPrice(
  109. price.totalAsNumber,
  110. currencyCode,
  111. size,
  112. locale,
  113. formatCurrency
  114. )
  115. }
  116. return price
  117. }