recurly-pricing.ts 3.1 KB

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