Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ describe('SubscriptionPurchaseDialog Epay amount', () => {

const amountRow = screen.getByTestId('subscription-epay-amount')
expect(within(amountRow).getByText('Online payment amount')).toBeVisible()
expect(within(amountRow).getByText(/73/)).toBeVisible()
expect(within(amountRow).getByText('¥73')).toBeVisible()
expect(within(amountRow).queryByText(/\$10/)).not.toBeInTheDocument()
expect(screen.getByRole('button', { name: /Pay .*73/ })).toBeVisible()
expect(screen.getByRole('button', { name: 'Pay ¥73' })).toBeVisible()
// The USD list price is still what the balance path charges.
expect(screen.getByText('$10.00')).toBeVisible()
})
Expand All @@ -107,10 +107,34 @@ describe('SubscriptionPurchaseDialog Epay amount', () => {
)

const amountRow = screen.getByTestId('subscription-epay-amount')
expect(within(amountRow).getByText(/10/)).toBeVisible()
expect(within(amountRow).getByText('¥10')).toBeVisible()
expect(within(amountRow).queryByText(/73/)).not.toBeInTheDocument()
})

test('with the USD display type the Epay amount is still shown in ¥', () => {
setDisplayCurrency('USD')

renderWithQueryClient(
<SubscriptionPurchaseDialog
open
onOpenChange={() => {}}
plan={{
plan: { ...planRecord.plan, price_amount: 27.94 },
}}
enableOnlineTopUp
epayMethods={[{ type: 'alipay', name: 'Alipay' }]}
priceRatio={7.3}
userQuota={0}
/>
)

// 27.94 USD × 7.3 yuan per USD = 203.962 → charged as ¥203.96 by Epay.
const amountRow = screen.getByTestId('subscription-epay-amount')
expect(within(amountRow).getByText('¥203.96')).toBeVisible()
expect(within(amountRow).queryByText(/\$/)).not.toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Pay ¥203.96' })).toBeVisible()
})

test('without Epay no online payment amount is rendered', () => {
setDisplayCurrency('CNY')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { Button } from '@/components/ui/button'
import { Combobox } from '@/components/ui/combobox'
import { Separator } from '@/components/ui/separator'
import { useSystemConfig } from '@/hooks/use-system-config'
import { formatLocalCurrencyAmount } from '@/lib/currency'
import { formatPaymentCurrencyAmount } from '@/lib/currency'
import { formatQuota } from '@/lib/format'
import { handleServerError } from '@/lib/handle-server-error'
import { DEFAULT_CURRENCY_CONFIG } from '@/stores/system-config-store'
Expand Down Expand Up @@ -93,9 +93,11 @@ export function SubscriptionPurchaseDialog(props: Props) {
const priceAmount = Number(plan.price_amount || 0)
const price = priceAmount.toFixed(2)
// Mirrors the backend conversion for Epay orders: plan USD price × price per USD.
// The result is RMB charged by Epay, so it is always shown as ¥ regardless of
// the quota display type.
const priceRatio =
props.priceRatio && props.priceRatio > 0 ? props.priceRatio : 1
const epayPayAmount = formatLocalCurrencyAmount(
const epayPayAmount = formatPaymentCurrencyAmount(
Math.round(priceAmount * priceRatio * 100) / 100
)
const quotaPerUnit =
Expand Down
113 changes: 113 additions & 0 deletions web/src/lib/__tests__/payment-currency-amount.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
Copyright (C) 2023-2026 QuantumNous

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.

For commercial licensing, please contact support@quantumnous.com
*/
import { afterEach, describe, expect, test } from 'vitest'

import {
formatLocalCurrencyAmount,
formatPaymentCurrencyAmount,
} from '@/lib/currency'
import {
DEFAULT_CURRENCY_CONFIG,
useSystemConfigStore,
type CurrencyDisplayType,
} from '@/stores/system-config-store'

function setDisplayType(quotaDisplayType: CurrencyDisplayType) {
useSystemConfigStore.getState().setConfig({
currency: {
...DEFAULT_CURRENCY_CONFIG,
quotaDisplayType,
usdExchangeRate: 7,
customCurrencySymbol: '€',
customCurrencyExchangeRate: 0.9,
},
})
}

afterEach(() => {
useSystemConfigStore.getState().setConfig({
currency: { ...DEFAULT_CURRENCY_CONFIG },
})
})

describe('formatPaymentCurrencyAmount', () => {
test.each<CurrencyDisplayType>(['USD', 'CNY', 'CUSTOM', 'TOKENS'])(
'always renders Epay amounts as ¥ when the display type is %s',
(displayType) => {
setDisplayType(displayType)

expect(formatPaymentCurrencyAmount(203.93, { locale: 'en-US' })).toBe(
'¥203.93'
)
}
)

test('does not apply any exchange rate', () => {
setDisplayType('CNY')

expect(formatPaymentCurrencyAmount(73, { locale: 'en-US' })).toBe('¥73')
})

test('differs from formatLocalCurrencyAmount under the USD display type', () => {
setDisplayType('USD')

expect(formatLocalCurrencyAmount(203.93, { locale: 'en-US' })).toBe(
'$203.93'
)
expect(formatPaymentCurrencyAmount(203.93, { locale: 'en-US' })).toBe(
'¥203.93'
)
})

test('uses at most two fraction digits and does not abbreviate', () => {
expect(formatPaymentCurrencyAmount(203.926, { locale: 'en-US' })).toBe(
'¥203.93'
)
expect(formatPaymentCurrencyAmount(0.126, { locale: 'en-US' })).toBe(
'¥0.13'
)
expect(formatPaymentCurrencyAmount(12345.6, { locale: 'en-US' })).toBe(
'¥12,345.6'
)
})

test('respects explicit digit options', () => {
expect(
formatPaymentCurrencyAmount(1.23456, {
locale: 'en-US',
digitsLarge: 4,
})
).toBe('¥1.2346')
})

test('keeps the CNY currency for zh locales', () => {
setDisplayType('USD')

expect(formatPaymentCurrencyAmount(203.93, { locale: 'zh-CN' })).toBe(
'¥203.93'
)
expect(formatPaymentCurrencyAmount(203.93, { locale: 'zh-TW' })).toBe(
'¥203.93'
)
})

test.each([null, undefined, Number.NaN])('returns "-" for %s', (value) => {
expect(formatPaymentCurrencyAmount(value)).toBe('-')
})
})
58 changes: 58 additions & 0 deletions web/src/lib/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ For commercial licensing, please contact support@quantumnous.com
* - `formatCurrencyFromUSD()`: Use for quota/balance display (stored as USD, converted for display)
* - `formatBillingCurrencyFromUSD()`: Use for billing/pricing displays (never shows tokens)
* - `formatLocalCurrencyAmount()`: Use for payment amounts already in local currency
* (symbol follows the admin quota display type: USD → $, CNY → ¥, CUSTOM → custom symbol)
* - `formatPaymentCurrencyAmount()`: Use for the amount a payment gateway actually charges
* (Epay). Always CNY/¥, independent of the quota display type, because the gateway decides
* the settlement currency and Epay settles in RMB (amount × "price per USD", in yuan)
* - `formatQuotaWithCurrency()`: Use for raw quota values (converts to USD first)
*
* ## Example Scenario
Expand All @@ -65,6 +69,7 @@ For commercial licensing, please contact support@quantumnous.com
* | User balance display | USD (from DB) | `formatCurrencyFromUSD()` | Needs conversion to display currency |
* | Recharge option button | USD | `formatCurrencyFromUSD()` | Needs conversion to local currency |
* | Payment confirmation | Already local currency | `formatLocalCurrencyAmount()` | Already converted via priceRatio |
* | Epay amount charged (e.g. subscription online payment) | CNY from price per USD | `formatPaymentCurrencyAmount()` | Gateway currency is RMB, not the display type |
* | Billing history Amount | USD (from DB) | `formatCurrencyFromUSD()` | Historical USD needs conversion |
* | Billing history Payment | Local currency | `formatNumber()` | Just show number, no symbol |
* | Model pricing | USD | `formatBillingCurrencyFromUSD()` | Never show as tokens |
Expand Down Expand Up @@ -635,3 +640,56 @@ export function formatLocalCurrencyAmount(

return formatCurrencyValue(amount, merged, meta)
}

const PAYMENT_GATEWAY_DISPLAY_META: DisplayMeta = {
kind: 'currency',
symbol: '¥',
currencyCode: 'CNY',
exchangeRate: 1,
}

/**
* Format the amount a payment gateway (Epay) actually charges, in RMB.
*
* Epay amounts are computed on the backend as USD amount × `Price` (the admin
* "price per USD" setting, in yuan), so they are always CNY. Unlike
* formatLocalCurrencyAmount(), the symbol does NOT follow the quota display
* type: with quotaDisplayType 'USD' an Epay charge of 203.93 must render as
* "¥203.93", not "$203.93".
*
* Defaults to at most two fraction digits (also for values below 1) and never
* abbreviates. Number grouping and symbol placement follow `options.locale`
* (Intl CNY formatting), the currency itself is fixed.
*
* @param amount - Amount already converted to RMB by the gateway pricing
* @param options - Optional formatting configuration
* @returns Formatted CNY string, or '-' for null/undefined/NaN
*
* @example
* // With any quotaDisplayType (USD, CNY, CUSTOM, TOKENS)
* formatPaymentCurrencyAmount(203.93) → "¥203.93"
*
* @remarks
* Use this function for:
* - Epay online payment amounts (subscription purchase, Epay pay button)
*
* DO NOT use for:
* - Credit/top-up amounts shown in the display currency → use formatLocalCurrencyAmount()
* or formatCurrencyFromUSD()
* - Gateways that settle in another currency (Stripe, Creem, ...)
*/
export function formatPaymentCurrencyAmount(
amount: number | null | undefined,
options?: CurrencyFormatOptions
): string {
if (amount == null || Number.isNaN(amount)) return '-'

const merged = mergeOptions({
...options,
digitsLarge: options?.digitsLarge ?? 2,
digitsSmall: options?.digitsSmall ?? 2,
abbreviate: options?.abbreviate ?? false,
})

return formatCurrencyValue(amount, merged, PAYMENT_GATEWAY_DISPLAY_META)
}
Loading