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