2023-02-07 15:38:20 +00:00
|
|
|
import { SubscriptionPricingState } from '@recurly/recurly-js'
|
2023-02-21 15:24:49 +00:00
|
|
|
import { PriceForDisplayData } from '../../../../../types/subscription/plan'
|
2024-06-18 10:01:37 +00:00
|
|
|
import {
|
|
|
|
currencies,
|
|
|
|
CurrencyCode,
|
|
|
|
} from '../../../../../types/subscription/currency'
|
2023-02-21 15:24:57 +00:00
|
|
|
import { getRecurlyGroupPlanCode } from './recurly-group-plan-code'
|
2023-09-27 09:45:49 +00:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2023-02-07 15:38:20 +00:00
|
|
|
|
2023-02-21 15:24:37 +00:00
|
|
|
function queryRecurlyPlanPrice(planCode: string, currency: CurrencyCode) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
recurly.Pricing.Subscription()
|
|
|
|
.plan(planCode, { quantity: 1 })
|
|
|
|
.currency(currency)
|
2023-09-27 09:45:49 +00:00
|
|
|
.catch(debugConsole.error)
|
2023-02-21 15:24:37 +00:00
|
|
|
.done(response => {
|
|
|
|
if (response) {
|
|
|
|
resolve(response)
|
|
|
|
} else {
|
|
|
|
resolve(undefined)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-18 08:13:51 +00:00
|
|
|
type FormatCurrency = (
|
|
|
|
price: number,
|
|
|
|
currency: CurrencyCode,
|
|
|
|
locale: string,
|
|
|
|
stripIfInteger?: boolean
|
|
|
|
) => string
|
|
|
|
|
|
|
|
export const formatCurrencyDefault: FormatCurrency = (
|
|
|
|
price: number,
|
|
|
|
currency: CurrencyCode,
|
|
|
|
_locale: string,
|
|
|
|
stripIfInteger = false
|
|
|
|
) => {
|
|
|
|
const currencySymbol = currencies[currency]
|
|
|
|
const number =
|
|
|
|
stripIfInteger && price % 1 === 0 ? Number(price) : price.toFixed(2)
|
|
|
|
return `${currencySymbol}${number}`
|
2023-02-21 15:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function formatPriceForDisplayData(
|
|
|
|
price: string,
|
|
|
|
taxRate: number,
|
2024-04-18 08:13:51 +00:00
|
|
|
currencyCode: CurrencyCode,
|
|
|
|
locale: string,
|
|
|
|
formatCurrency: FormatCurrency
|
2023-02-21 15:24:49 +00:00
|
|
|
): PriceForDisplayData {
|
2023-02-07 15:38:20 +00:00
|
|
|
const totalPriceExTax = parseFloat(price)
|
|
|
|
let taxAmount = totalPriceExTax * taxRate
|
|
|
|
if (isNaN(taxAmount)) {
|
|
|
|
taxAmount = 0
|
|
|
|
}
|
|
|
|
const totalWithTax = totalPriceExTax + taxAmount
|
|
|
|
|
|
|
|
return {
|
2024-04-18 08:13:51 +00:00
|
|
|
totalForDisplay: formatCurrency(totalWithTax, currencyCode, locale, true),
|
2023-02-07 15:38:20 +00:00
|
|
|
totalAsNumber: totalWithTax,
|
2024-04-18 08:13:51 +00:00
|
|
|
subtotal: formatCurrency(totalPriceExTax, currencyCode, locale),
|
|
|
|
tax: formatCurrency(taxAmount, currencyCode, locale),
|
2023-02-07 15:38:20 +00:00
|
|
|
includesTax: taxAmount !== 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 15:24:49 +00:00
|
|
|
function getPerUserDisplayPrice(
|
|
|
|
totalPrice: number,
|
2024-04-18 08:13:51 +00:00
|
|
|
currency: CurrencyCode,
|
|
|
|
size: string,
|
|
|
|
locale: string,
|
|
|
|
formatCurrency: FormatCurrency
|
2023-02-21 15:24:49 +00:00
|
|
|
): string {
|
2024-04-18 08:13:51 +00:00
|
|
|
return formatCurrency(totalPrice / parseInt(size), currency, locale, true)
|
2023-02-21 15:24:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 15:24:37 +00:00
|
|
|
export async function loadDisplayPriceWithTaxPromise(
|
2023-02-07 15:38:20 +00:00
|
|
|
planCode: string,
|
2023-02-21 15:24:37 +00:00
|
|
|
currencyCode: CurrencyCode,
|
2024-04-18 08:13:51 +00:00
|
|
|
taxRate: number,
|
|
|
|
locale: string,
|
|
|
|
formatCurrency: FormatCurrency
|
2023-02-07 15:38:20 +00:00
|
|
|
) {
|
|
|
|
if (!recurly) return
|
|
|
|
|
2023-02-21 15:24:37 +00:00
|
|
|
const price = (await queryRecurlyPlanPrice(
|
|
|
|
planCode,
|
|
|
|
currencyCode
|
|
|
|
)) as SubscriptionPricingState['price']
|
|
|
|
if (price)
|
2024-04-18 08:13:51 +00:00
|
|
|
return formatPriceForDisplayData(
|
|
|
|
price.next.total,
|
|
|
|
taxRate,
|
|
|
|
currencyCode,
|
|
|
|
locale,
|
|
|
|
formatCurrency
|
|
|
|
)
|
2023-02-07 15:38:20 +00:00
|
|
|
}
|
2023-02-21 15:24:49 +00:00
|
|
|
|
|
|
|
export async function loadGroupDisplayPriceWithTaxPromise(
|
|
|
|
groupPlanCode: string,
|
|
|
|
currencyCode: CurrencyCode,
|
|
|
|
taxRate: number,
|
|
|
|
size: string,
|
2024-04-18 08:13:51 +00:00
|
|
|
usage: string,
|
|
|
|
locale: string,
|
|
|
|
formatCurrency: FormatCurrency
|
2023-02-21 15:24:49 +00:00
|
|
|
) {
|
|
|
|
if (!recurly) return
|
|
|
|
|
2023-02-21 15:24:57 +00:00
|
|
|
const planCode = getRecurlyGroupPlanCode(groupPlanCode, size, usage)
|
2023-02-21 15:24:49 +00:00
|
|
|
const price = await loadDisplayPriceWithTaxPromise(
|
|
|
|
planCode,
|
|
|
|
currencyCode,
|
2024-04-18 08:13:51 +00:00
|
|
|
taxRate,
|
|
|
|
locale,
|
|
|
|
formatCurrency
|
2023-02-21 15:24:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if (price) {
|
|
|
|
price.perUserDisplayPrice = getPerUserDisplayPrice(
|
|
|
|
price.totalAsNumber,
|
2024-04-18 08:13:51 +00:00
|
|
|
currencyCode,
|
|
|
|
size,
|
|
|
|
locale,
|
|
|
|
formatCurrency
|
2023-02-21 15:24:49 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return price
|
|
|
|
}
|