diff --git a/web/src/features/subscriptions/__tests__/purchase-dialog-epay-amount.test.tsx b/web/src/features/subscriptions/__tests__/purchase-dialog-epay-amount.test.tsx index af71c5d6a76e..d9ab0f91f3c7 100644 --- a/web/src/features/subscriptions/__tests__/purchase-dialog-epay-amount.test.tsx +++ b/web/src/features/subscriptions/__tests__/purchase-dialog-epay-amount.test.tsx @@ -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() }) @@ -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( + {}} + 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') diff --git a/web/src/features/subscriptions/components/dialogs/subscription-purchase-dialog.tsx b/web/src/features/subscriptions/components/dialogs/subscription-purchase-dialog.tsx index 9aeefca3a0ce..2a79998d4ada 100644 --- a/web/src/features/subscriptions/components/dialogs/subscription-purchase-dialog.tsx +++ b/web/src/features/subscriptions/components/dialogs/subscription-purchase-dialog.tsx @@ -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' @@ -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 = diff --git a/web/src/lib/__tests__/payment-currency-amount.test.ts b/web/src/lib/__tests__/payment-currency-amount.test.ts new file mode 100644 index 000000000000..ad0d0b3550f3 --- /dev/null +++ b/web/src/lib/__tests__/payment-currency-amount.test.ts @@ -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 . + +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(['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('-') + }) +}) diff --git a/web/src/lib/currency.ts b/web/src/lib/currency.ts index 15a3d345d05c..5f7c63c87d55 100644 --- a/web/src/lib/currency.ts +++ b/web/src/lib/currency.ts @@ -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 @@ -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 | @@ -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) +}