|
|
@@ -1,9 +1,33 @@
|
|
|
import { SubscriptionPricingState } from '@recurly/recurly-js'
|
|
|
-import getMeta from '../../../utils/meta'
|
|
|
import { currencies, CurrencyCode } from '../data/currency'
|
|
|
|
|
|
-export function formatPriceForDisplayData(price: string, taxRate: number) {
|
|
|
- const currencyCode: CurrencyCode = getMeta('ol-recommendedCurrency')
|
|
|
+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)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+function priceWithCents(price: number): string | number {
|
|
|
+ return price % 1 !== 0 ? price.toFixed(2) : price
|
|
|
+}
|
|
|
+
|
|
|
+export function formatPriceForDisplayData(
|
|
|
+ price: string,
|
|
|
+ taxRate: number,
|
|
|
+ currencyCode: CurrencyCode
|
|
|
+) {
|
|
|
const currencySymbol = currencies[currencyCode]
|
|
|
|
|
|
const totalPriceExTax = parseFloat(price)
|
|
|
@@ -14,9 +38,7 @@ export function formatPriceForDisplayData(price: string, taxRate: number) {
|
|
|
const totalWithTax = totalPriceExTax + taxAmount
|
|
|
|
|
|
return {
|
|
|
- totalForDisplay: `${currencySymbol}${
|
|
|
- totalWithTax % 1 !== 0 ? totalWithTax.toFixed(2) : totalWithTax
|
|
|
- }`,
|
|
|
+ totalForDisplay: `${currencySymbol}${priceWithCents(totalWithTax)}`,
|
|
|
totalAsNumber: totalWithTax,
|
|
|
subtotal: `${currencySymbol}${totalPriceExTax.toFixed(2)}`,
|
|
|
tax: `${currencySymbol}${taxAmount.toFixed(2)}`,
|
|
|
@@ -24,33 +46,17 @@ export function formatPriceForDisplayData(price: string, taxRate: number) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export function loadDisplayPriceWithTaxPromise(
|
|
|
+export async function loadDisplayPriceWithTaxPromise(
|
|
|
planCode: string,
|
|
|
- currency: CurrencyCode,
|
|
|
+ currencyCode: CurrencyCode,
|
|
|
taxRate: number
|
|
|
) {
|
|
|
if (!recurly) return
|
|
|
|
|
|
- return new Promise<ReturnType<typeof formatPriceForDisplayData> | undefined>(
|
|
|
- resolve => {
|
|
|
- recurly.Pricing.Subscription()
|
|
|
- .plan(planCode, { quantity: 1 })
|
|
|
- .currency(currency)
|
|
|
- .catch(function (error) {
|
|
|
- console.error(error)
|
|
|
- })
|
|
|
- .done(response => {
|
|
|
- if (response) {
|
|
|
- const price =
|
|
|
- response as unknown as SubscriptionPricingState['price']
|
|
|
- // type expects response to be {price: {next: ...}}
|
|
|
- // but the real response is {next: ...}
|
|
|
- const data = formatPriceForDisplayData(price.next.total, taxRate)
|
|
|
- resolve(data)
|
|
|
- } else {
|
|
|
- resolve(undefined)
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
- )
|
|
|
+ const price = (await queryRecurlyPlanPrice(
|
|
|
+ planCode,
|
|
|
+ currencyCode
|
|
|
+ )) as SubscriptionPricingState['price']
|
|
|
+ if (price)
|
|
|
+ return formatPriceForDisplayData(price.next.total, taxRate, currencyCode)
|
|
|
}
|