/*
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 { useEffectEvent } from '../../hooks/common/useEffectEvent';
import React, { useState, useEffect, useContext } from 'react';
import {
Card,
Button,
Radio,
Tag,
Toast,
Spin,
Row,
Col,
Space,
Typography,
} from '@/components/ui';
import { useTranslation } from 'react-i18next';
import { UserContext } from '../../context/User';
import { API, showError, showSuccess, renderQuota } from '../../helpers';
import {
formatCurrency,
formatNumber,
toFiniteNumber,
} from '../../helpers/safeDisplay';
import WechatQRModal from '../../components/topup/modals/WechatQRModal';
import './RechargeV2.css';
const { Title, Text } = Typography;
const RechargeV2 = () => {
const { t } = useTranslation();
const [userState, userDispatch] = useContext(UserContext);
const [loading, setLoading] = useState(false);
const [config, setConfig] = useState(null);
const [selectedAmountOption, setSelectedAmountOption] = useState(null);
const [selectedPaymentMethod, setSelectedPaymentMethod] = useState(null);
const [selectedChannel, setSelectedChannel] = useState(null);
const [payInfo, setPayInfo] = useState(null);
const [calculating, setCalculating] = useState(false);
const [paying, setPaying] = useState(false);
// 微信支付二维码弹窗
const [wechatQRVisible, setWechatQRVisible] = useState(false);
const [wechatOrderData, setWechatOrderData] = useState(null);
// 加载支付配置
const onMount = useEffectEvent(() => {
loadPaymentConfig();
});
useEffect(() => onMount(), [onMount]);
// 当选择档位时自动计算
const onScopeChange2 = useEffectEvent(() => {
if (selectedAmountOption) {
calculateAmount();
}
});
useEffect(() => onScopeChange2(), [onScopeChange2, selectedAmountOption]);
const loadPaymentConfig = async () => {
try {
setLoading(true);
const res = await API.get('/api/user/payment/config');
if (res.data.success) {
setConfig(res.data.data);
// 设置默认选项
if (
res.data.data.amount_options &&
res.data.data.amount_options.length > 0
) {
const recommended = res.data.data.amount_options.find(
(opt) => opt.is_recommend,
);
setSelectedAmountOption(
recommended || res.data.data.amount_options[0],
);
}
if (
res.data.data.payment_methods &&
res.data.data.payment_methods.length > 0
) {
const defaultMethod = res.data.data.payment_methods[0];
setSelectedPaymentMethod(defaultMethod);
if (defaultMethod.channels && defaultMethod.channels.length > 0) {
setSelectedChannel(defaultMethod.channels[0].value);
}
}
} else {
showError(t('加载支付配置失败'));
}
} catch (error) {
showError(t('加载支付配置失败'));
} finally {
setLoading(false);
}
};
// 计算支付金额
const calculateAmount = async () => {
if (!selectedAmountOption) return;
try {
setCalculating(true);
const res = await API.post('/api/user/payment/calculate', {
amount_option_id: selectedAmountOption.id,
});
if (res.data.success) {
setPayInfo(res.data.data);
} else {
showError(res.data.message);
}
} catch (error) {
showError(t('计算失败'));
} finally {
setCalculating(false);
}
};
// 选择充值档位
const handleSelectAmount = (option) => {
setSelectedAmountOption(option);
};
// 选择支付方式
const handleSelectPaymentMethod = (method) => {
setSelectedPaymentMethod(method);
if (method.channels && method.channels.length > 0) {
setSelectedChannel(method.channels[0].value);
}
};
// 发起支付
const handlePay = async () => {
if (!selectedAmountOption || !selectedChannel) {
Toast.warning(t('请选择充值金额和支付方式'));
return;
}
try {
setPaying(true);
const endpoint = {
wechat_native: '/api/user/wechat/native',
wechat_h5: '/api/user/wechat/h5',
alipay_pc: '/api/user/alipay/pc',
alipay_wap: '/api/user/alipay/wap',
alipay_qr: '/api/user/alipay/qr',
}[selectedChannel];
if (!endpoint) {
Toast.error(t('不支持的支付方式'));
return;
}
const res = await API.post(endpoint, {
amount_option_id: selectedAmountOption.id,
});
if (res.data.success) {
const data = res.data.data;
if (selectedChannel === 'wechat_native') {
// 显示微信支付二维码
setWechatOrderData(data);
setWechatQRVisible(true);
} else if (selectedChannel.startsWith('alipay')) {
// 跳转到支付宝支付页面
window.location.href = data.pay_url;
} else if (selectedChannel === 'wechat_h5') {
// 跳转到微信H5支付
window.location.href = data.h5_url;
}
} else {
showError(res.data.message);
}
} catch (error) {
showError(t('创建支付订单失败'));
} finally {
setPaying(false);
}
};
// 微信支付完成回调
const handleWechatPayComplete = (success) => {
setWechatQRVisible(false);
if (success) {
// 刷新用户额度
if (userState.user) {
const updatedUser = {
...userState.user,
quota: userState.user.quota + payInfo.quota,
};
userDispatch({ type: 'login', payload: updatedUser });
}
showSuccess(t('充值成功!'));
}
};
if (loading && !config) {
return (
);
}
return (
{/* 充值档位选择 */}
{t('选择充值金额')}
{config?.amount_options?.map((option) => (
handleSelectAmount(option)}
hoverable
styles={{ body: { padding: 20 } }}
>
{option.badge && (
{option.badge}
)}
{option.is_recommend && (
{t('推荐')}
)}
{option.is_hot && (
{t('热门')}
)}
¥{option.amount}
{option.discount < 1.0 && (
{t('{{rate}}折优惠', {
rate: formatNumber(
(toFiniteNumber(option.discount) ?? 1) * 10,
1,
'10.0',
),
})}
)}
{option.bonus > 0 && (
{t('赠送')} {renderQuota(option.bonus)}
)}
))}
{/* 支付信息预览 */}
{payInfo && (
{t('支付信息')}
{t('原价:')}
{formatCurrency(payInfo.original_money)}
{t('折扣:')}
{t('{{rate}}折', {
rate: formatNumber(
(toFiniteNumber(payInfo.discount_rate) ?? 1) * 10,
1,
'10.0',
),
})}
{t('实付:')}
{formatCurrency(payInfo.pay_money)}
{t('获得额度:')}
{renderQuota(payInfo.quota)}
{(toFiniteNumber(payInfo.save_money) ?? 0) > 0 && (
{t('节省:')}
{formatCurrency(payInfo.save_money)}
)}
)}
{/* 支付方式选择 */}
{t('选择支付方式')}
{
const method = config.payment_methods.find(
(m) => m.type === e.target.value,
);
handleSelectPaymentMethod(method);
}}
>
{config?.payment_methods?.map((method) => (
{method.logo && (

)}
{method.name}
))}
{/* 支付渠道选择 */}
{selectedPaymentMethod &&
selectedPaymentMethod.channels &&
selectedPaymentMethod.channels.length > 1 && (
{t('支付渠道:')}
setSelectedChannel(e.target.value)}
style={{ marginLeft: 10 }}
>
{selectedPaymentMethod.channels.map((channel) => (
{channel.icon} {channel.name}
))}
)}
{/* 支付按钮 */}
{/* 微信支付二维码弹窗 */}
);
};
export default RechargeV2;