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-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)
|
|
|
|
.catch(function (error) {
|
|
|
|
console.error(error)
|
|
|
|
})
|
|
|
|
.done(response => {
|
|
|
|
if (response) {
|
|
|
|
resolve(response)
|
|
|
|
} else {
|
|
|
|
resolve(undefined)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-21 10:24:49 -05:00
|
|
|
function priceToWithCents(price: number) {
|
2023-02-21 10:24:37 -05:00
|
|
|
return price % 1 !== 0 ? price.toFixed(2) : price
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatPriceForDisplayData(
|
|
|
|
price: string,
|
|
|
|
taxRate: number,
|
|
|
|
currencyCode: CurrencyCode
|
2023-02-21 10:24:49 -05:00
|
|
|
): PriceForDisplayData {
|
2023-02-07 10:38:20 -05:00
|
|
|
const currencySymbol = currencies[currencyCode]
|
|
|
|
|
|
|
|
const totalPriceExTax = parseFloat(price)
|
|
|
|
let taxAmount = totalPriceExTax * taxRate
|
|
|
|
if (isNaN(taxAmount)) {
|
|
|
|
taxAmount = 0
|
|
|
|
}
|
|
|
|
const totalWithTax = totalPriceExTax + taxAmount
|
|
|
|
|
|
|
|
return {
|
2023-02-21 10:24:49 -05:00
|
|
|
totalForDisplay: `${currencySymbol}${priceToWithCents(totalWithTax)}`,
|
2023-02-07 10:38:20 -05:00
|
|
|
totalAsNumber: totalWithTax,
|
|
|
|
subtotal: `${currencySymbol}${totalPriceExTax.toFixed(2)}`,
|
|
|
|
tax: `${currencySymbol}${taxAmount.toFixed(2)}`,
|
|
|
|
includesTax: taxAmount !== 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 10:24:49 -05:00
|
|
|
function getPerUserDisplayPrice(
|
|
|
|
totalPrice: number,
|
|
|
|
currencySymbol: string,
|
|
|
|
size: string
|
|
|
|
): string {
|
|
|
|
return `${currencySymbol}${priceToWithCents(totalPrice / parseInt(size))}`
|
|
|
|
}
|
|
|
|
|
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,
|
2023-02-07 10:38:20 -05:00
|
|
|
taxRate: number
|
|
|
|
) {
|
|
|
|
if (!recurly) return
|
|
|
|
|
2023-02-21 10:24:37 -05:00
|
|
|
const price = (await queryRecurlyPlanPrice(
|
|
|
|
planCode,
|
|
|
|
currencyCode
|
|
|
|
)) as SubscriptionPricingState['price']
|
|
|
|
if (price)
|
|
|
|
return formatPriceForDisplayData(price.next.total, taxRate, currencyCode)
|
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,
|
|
|
|
usage: string
|
|
|
|
) {
|
|
|
|
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,
|
|
|
|
taxRate
|
|
|
|
)
|
|
|
|
|
|
|
|
if (price) {
|
|
|
|
const currencySymbol = currencies[currencyCode]
|
|
|
|
price.perUserDisplayPrice = getPerUserDisplayPrice(
|
|
|
|
price.totalAsNumber,
|
|
|
|
currencySymbol,
|
|
|
|
size
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return price
|
|
|
|
}
|