/* Copyright (C) 2025 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 React, { useRef } from 'react'; import { Typography, Card, Button, Banner, Form, Space, Spin, Tooltip, } from '@/components/ui'; import { CreditCard, Coins, Wallet, Receipt } from 'lucide-react'; import { AlipayBrandIcon, WechatBrandIcon, StripeBrandIcon, } from '../common/icons/BrandIcons'; import { IconGift } from '@/components/ui/icons'; import { useMinimumLoadingTime } from '../../hooks/common/useMinimumLoadingTime'; import { getCurrencyConfig } from '../../helpers/render'; const { Text } = Typography; const RechargeCard = ({ t, enableOnlineTopUp, enableStripeTopUp, enableCreemTopUp, creemProducts, creemPreTopUp, presetAmounts, selectedPreset, selectPresetAmount, formatLargeNumber, priceRatio, topUpCount, minTopUp, renderQuotaWithAmount, getAmount, setTopUpCount, setSelectedPreset, renderAmount, amountLoading, payMethods, preTopUp, paymentLoading, payWay, redemptionCode, setRedemptionCode, topUp, isSubmitting, topUpLink, openTopUpLink, userState, renderQuota, statusLoading, topupInfo, onOpenHistory, }) => { const onlineFormApiRef = useRef(null); const redeemFormApiRef = useRef(null); const _showAmountSkeleton = useMinimumLoadingTime(amountLoading); console.log( ' enabled screem ?', enableCreemTopUp, ' products ?', creemProducts, ); return ( {/* 卡片头部 */}
{t('账户充值')}
{t('多种充值方式,安全便捷')}
{/* 统计数据 */}
{t('账户概览')}
{renderQuota(userState?.user?.quota)}
{t('当前余额')}
{renderQuota(userState?.user?.used_quota)}
{t('历史消耗')}
{userState?.user?.request_count || 0}
{t('请求次数')}
{/* 在线充值表单 */} {statusLoading ? (
) : enableOnlineTopUp || enableStripeTopUp || enableCreemTopUp || (payMethods && payMethods.length > 0) ? (
(onlineFormApiRef.current = api)} initValues={{ topUpCount: topUpCount }} >
{/* 自定义金额输入区域 - 已隐藏,不允许用户自己输入金额 */} {(enableOnlineTopUp || enableStripeTopUp || (payMethods && payMethods.length > 0)) && (
{t('支付方式')} } > {payMethods && payMethods.length > 0 ? (
{payMethods.map((payMethod) => { const minTopupVal = Number(payMethod.min_topup) || 0; const isStripe = payMethod.type === 'stripe'; const disabled = (!enableOnlineTopUp && !isStripe) || (!enableStripeTopUp && isStripe) || minTopupVal > Number(topUpCount || 0); const buttonContent = (
{payMethod.type === 'alipay' ? ( ) : payMethod.type === 'wxpay' ? ( ) : payMethod.type === 'stripe' ? ( ) : ( )} {payMethod.name}
); const containerClass = `h-24 rounded-xl border-2 cursor-pointer transition-all duration-200 ${ payWay === payMethod.type ? 'border-blue-500 bg-blue-50 dark:bg-blue-900/20 shadow-md ring-2 ring-blue-200 dark:ring-blue-800' : 'border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 hover:border-blue-300 dark:hover:border-blue-600 hover:shadow-md' } ${disabled ? 'cursor-not-allowed grayscale' : 'group'}`; const buttonEl = (
!disabled && preTopUp(payMethod.type) } > {buttonContent}
); return disabled && minTopupVal > Number(topUpCount || 0) ? ( {buttonEl} ) : ( {buttonEl} ); })}
) : (
{t('暂无可用的支付方式')}
)}
)} {(enableOnlineTopUp || enableStripeTopUp || (payMethods && payMethods.length > 0)) && ( {t('快捷选择')} {(() => { const { symbol, rate, type } = getCurrencyConfig(); if (type === 'USD') return null; return ( 1 $ ≈ {rate.toFixed(2)} {symbol} ); })()}
} >
{presetAmounts.map((preset, index) => { const discount = preset.discount || topupInfo?.discount?.[preset.value] || 1.0; const originalPrice = preset.value * priceRatio; const discountedPrice = originalPrice * discount; const hasDiscount = discount < 1.0; const actualPay = discountedPrice; const save = originalPrice - discountedPrice; const { symbol, rate, type } = getCurrencyConfig(); const statusStr = localStorage.getItem('status'); let usdRate = 1; try { if (statusStr) { const s = JSON.parse(statusStr); // 注意:如果设置了汇率值(包括1),使用设置值;只有未设置(undefined/null)时才用默认值1 usdRate = s?.usd_exchange_rate !== undefined && s?.usd_exchange_rate !== null ? s.usd_exchange_rate : 1; } } catch (e) {} let displayValue = preset.value; let displayActualPay = actualPay; let displaySave = save; if (type === 'USD') { displayActualPay = actualPay / usdRate; displaySave = save / usdRate; } else if (type === 'CNY') { displayValue = preset.value * usdRate; } else if (type === 'CUSTOM') { displayValue = preset.value * rate; displayActualPay = (actualPay / usdRate) * rate; displaySave = (save / usdRate) * rate; } const isSelected = selectedPreset === preset.value; return (
{ selectPresetAmount(preset); onlineFormApiRef.current?.setValue( 'topUpCount', preset.value, ); }} > {hasDiscount && (
{t('优惠')}
)}
{formatLargeNumber(displayValue)}{' '} {symbol}
{hasDiscount && (
{t('省')} {symbol} {displaySave.toFixed(0)}
)}
{t('实付')}{' '} {symbol} {displayActualPay.toFixed(2)}
); })}
)} {/* Creem 充值区域 */} {enableCreemTopUp && creemProducts.length > 0 && ( Creem {t('套餐')} } >
{creemProducts.map((product, index) => (
creemPreTopUp(product)} className='cursor-pointer rounded-xl border-2 border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 p-5 hover:border-blue-400 hover:shadow-lg transition-all duration-300 group' >
{product.name}
{t('额度')}:{' '} {product.quota}
{product.currency === 'EUR' ? '€' : '¥'} {product.price}
))}
)}
) : ( )} {/* 兑换码充值 */}
{t('兑换码充值')}
(redeemFormApiRef.current = api)} initValues={{ redemptionCode: redemptionCode }} className='flex flex-col sm:flex-row gap-4' >
setRedemptionCode(value)} className='w-full' size='large' showClear />
{topUpLink && (
{t('没有兑换码?')} {t('去购买')} →
)}
); }; export default RechargeCard;