Files
monkey-ai-web/src/pages/Recharge/RechargePanel.jsx
T

351 lines
11 KiB
React

/*
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 <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { Check as ActionCheck } from '@/components/common/icons/ActionIcons';
import React from 'react';
import { Alert } from 'antd';
import { Button, Spin, Tooltip } from '@/components/ui';
import {
AlipayBrandIcon,
StripeBrandIcon,
PayPalBrandIcon,
} from '../../components/common/icons/BrandIcons';
import { getCurrencyConfig } from '../../helpers/render';
import { formatNumber, toFiniteNumber } from '../../helpers/safeDisplay';
import { CUSTOMER_SERVICE_OPEN_EVENT } from '../../components/customer-service';
import { showInfo } from '../../helpers';
import AmountCard from './AmountCard';
import { useMinimumLoadingTime } from '../../hooks/common/useMinimumLoadingTime';
const formatCny = (value) => {
const number = toFiniteNumber(value);
if (number === null) return '-';
return (
'¥' +
number.toLocaleString('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})
);
};
// 需要发票时的联系提示(原型:充值订单按钮下方)
export const InvoiceNote = ({ t }) => (
<p className='rc-invoice'>
{t('需要发票?')}
<button
type='button'
onClick={() => {
const handled = !window.dispatchEvent(
new CustomEvent(CUSTOMER_SERVICE_OPEN_EVENT, { cancelable: true }),
);
if (!handled) showInfo(t('暂无可用客服'));
}}
>
{t('联系在线客服')}
</button>
</p>
);
const RechargePanel = ({
t,
statusLoading,
enableOnlineTopUp,
enableWechatTopUp,
enableAlipayTopUp,
enableStripeTopUp,
enablePayPalTopUp,
enableCreemTopUp,
creemProducts,
creemPreTopUp,
presetAmounts,
selectedPreset,
selectedPresetObj,
selectPresetAmount,
priceRatio,
topUpCount,
amount,
amountLoading,
payMethods,
preTopUp,
paymentLoading,
payWay,
payPalQuote,
topupInfo,
}) => {
useMinimumLoadingTime(amountLoading);
// Creem 使用独立产品选择;其余渠道共用金额和支付方式区块。
const hasStandardOnlinePayment =
enableOnlineTopUp ||
enableWechatTopUp ||
enableAlipayTopUp ||
enableStripeTopUp ||
enablePayPalTopUp;
const hasAnyOnlinePayment = hasStandardOnlinePayment || enableCreemTopUp;
// 与原逻辑一致的支付方式可用性判断
const isMethodDisabled = (method) => {
const minTopupVal = Number(method.min_topup) || 0;
const isStripe = method.type === 'stripe';
const isPayPal = method.type === 'paypal';
const isWechat = method.type === 'wxpay';
const isAlipay = method.type === 'alipay';
return (
(!enableOnlineTopUp &&
!isStripe &&
!isPayPal &&
!isWechat &&
!isAlipay) ||
(!enableStripeTopUp && isStripe) ||
(!enablePayPalTopUp && isPayPal) ||
(!enableWechatTopUp && isWechat) ||
(!enableAlipayTopUp && isAlipay) ||
minTopupVal > Number(topUpCount || 0)
);
};
// 渲染支付方式图标(微信为原型中的绿色圆点)
const renderPayIcon = (type) => {
if (type === 'alipay') return <AlipayBrandIcon size={16} color='#1677FF' />;
if (type === 'wxpay') return <i className='rc-pay-dot' />;
if (type === 'stripe') return <StripeBrandIcon size={16} color='#635BFF' />;
if (type === 'paypal') return <PayPalBrandIcon size={16} />;
return null;
};
// ---- 充值订单:仅由已选档位、金额与支付方式派生展示 ----
const { symbol } = getCurrencyConfig();
const preset = selectedPreset !== null ? selectedPresetObj : null;
const presetValue = toFiniteNumber(preset?.value) ?? 0;
const discount =
toFiniteNumber(preset?.discount ?? topupInfo?.discount?.[preset?.value]) ??
1.0;
const isNewVersion = preset?.id !== undefined;
const originalPrice = preset
? isNewVersion
? presetValue
: presetValue * (toFiniteNumber(priceRatio) ?? 0)
: 0;
const arrivalText = preset
? preset.label ||
(isNewVersion
? formatCny(preset.value)
: `${symbol}${presetValue.toLocaleString('en-US')}`)
: '-';
const selectedMethod = payMethods?.find((method) => method.type === payWay);
const payPalQuoted = payWay === 'paypal' && payPalQuote;
// 应付金额:人民币取已计算的 amount;PayPal 取已获取的美元报价(原型中币种符号为小字)
const payable = !preset
? null
: payWay === 'paypal'
? payPalQuoted
? {
symbol: '$',
value: formatNumber(payPalQuote.payAmount, 2, '0.00'),
unit: 'USD',
}
: null
: { symbol: '¥', value: formatCny(amount).slice(1) };
const canPay =
Boolean(preset) &&
Boolean(selectedMethod) &&
!isMethodDisabled(selectedMethod) &&
Boolean(payable) &&
!amountLoading;
if (statusLoading) {
return (
<div className='rc-loading'>
<Spin size='large' />
</div>
);
}
if (!hasAnyOnlinePayment) {
return (
<>
<Alert
type='info'
showIcon
title={t(
'管理员未开启在线充值功能,请联系管理员开启或使用兑换码充值。',
)}
/>
<InvoiceNote t={t} />
</>
);
}
return (
<>
{hasStandardOnlinePayment && (
<section className='rc-section'>
<h3>{t('选择充值额度')}</h3>
<p className='rc-section-desc'>
{t('按需充值,余额可用于平台模型调用。')}
</p>
<div className='rc-amount-grid'>
{presetAmounts.slice(0, 8).map((item, index) => (
<AmountCard
key={index}
preset={item}
selected={selectedPreset === item.value}
onClick={() => selectPresetAmount(item)}
topupInfo={topupInfo}
priceRatio={priceRatio}
t={t}
/>
))}
</div>
<div className='rc-divider' />
<h3>{t('支付方式')}</h3>
{payMethods && payMethods.length > 0 ? (
<div className='rc-pay-methods'>
{payMethods.map((method) => {
const minTopupVal = Number(method.min_topup) || 0;
const disabled = isMethodDisabled(method);
const selected = payWay === method.type;
const button = (
<Button
key={method.type}
onClick={() => preTopUp(method.type)}
disabled={disabled}
loading={paymentLoading && selected}
aria-pressed={selected}
icon={renderPayIcon(method.type)}
className={`rc-pay-method ${selected ? 'is-selected' : ''}`}
>
{method.name}
{selected && <ActionCheck size={14} />}
</Button>
);
if (disabled && minTopupVal > Number(topUpCount || 0)) {
return (
<Tooltip
key={method.type}
content={t('此支付方式最低充值金额为 {{minTopupVal}}', {
minTopupVal: minTopupVal,
})}
>
{button}
</Tooltip>
);
}
return button;
})}
</div>
) : (
<div className='rc-pay-empty'>
{t('暂无可用的支付方式,请联系管理员配置')}
</div>
)}
</section>
)}
{/* Creem 充值 */}
{enableCreemTopUp && creemProducts.length > 0 && (
<section className='rc-section rc-section-gap'>
<h3>{t('Creem 充值')}</h3>
<div className='rc-amount-grid'>
{creemProducts.map((product, index) => (
<button
key={index}
type='button'
onClick={() => creemPreTopUp(product)}
className='rc-amount'
>
<span className='rc-amount-value'>{product.name}</span>
<span className='rc-amount-pay'>
{t('额度')}: {product.quota}
</span>
<span className='rc-amount-bonus'>
{product.currency === 'EUR' ? '€' : '¥'}
{product.price}
</span>
</button>
))}
</div>
</section>
)}
{hasStandardOnlinePayment && (
<section className='rc-order'>
<div className='rc-order-main'>
<h3 className='rc-order-title'>{t('充值订单')}</h3>
<dl className='rc-order-rows'>
<div>
<dt>{t('到账额度')}</dt>
<dd>{arrivalText}</dd>
</div>
<div>
<dt>{t('优惠金额')}</dt>
<dd>
{preset ? formatCny(originalPrice * (1 - discount)) : '-'}
</dd>
</div>
<div>
<dt>{t('支付方式')}</dt>
<dd>{selectedMethod?.name || '-'}</dd>
</div>
{payPalQuoted && (
<div>
<dt>{t('支付服务费')}</dt>
<dd>{`$${formatNumber(payPalQuote.serviceFee, 2, '0.00')} USD`}</dd>
</div>
)}
</dl>
</div>
<div className='rc-order-side'>
<span className='rc-label'>{t('应付金额')}</span>
<strong className='rc-order-amount'>
{payable ? (
<>
<small>{payable.symbol}</small>
{payable.value}
{payable.unit && (
<small className='rc-order-unit'>{payable.unit}</small>
)}
</>
) : (
'-'
)}
</strong>
{/* 与点击支付方式相同:进入原有的预充值校验与确认弹窗 */}
<Button
type='primary'
block
disabled={!canPay}
loading={paymentLoading}
onClick={() => preTopUp(payWay)}
>
{t('立即充值')}
</Button>
<InvoiceNote t={t} />
</div>
</section>
)}
</>
);
};
export default RechargePanel;