Files
monkey-ai-web/src/pages/Setting/Payment/SettingsPaymentAlipay.jsx
T

246 lines
8.1 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 React, { useState } from 'react';
import { Form, Button, Typography, Space, Divider } from '@/components/ui';
import { API, showError, showSuccess } from '../../../helpers';
import { useTranslation } from 'react-i18next';
const { Title, Text } = Typography;
const SettingsPaymentAlipay = ({ options, refresh }) => {
const { t } = useTranslation();
const [loading, setLoading] = useState(false);
const onSubmit = async (values) => {
try {
setLoading(true);
// 保存支付宝配置
const configs = [
{ key: 'AlipayAppId', value: values.AlipayAppId || '' },
// 如果私钥/公钥未修改(仍然是空或占位符),则使用占位符让后端跳过更新
{
key: 'AlipayPrivateKey',
value:
values.AlipayPrivateKey && values.AlipayPrivateKey.trim() !== ''
? values.AlipayPrivateKey
: '***ALREADY_SET***',
},
{
key: 'AlipayPublicKey',
value:
values.AlipayPublicKey && values.AlipayPublicKey.trim() !== ''
? values.AlipayPublicKey
: '***ALREADY_SET***',
},
{
key: 'AlipayPCEnabled',
value: values.AlipayPCEnabled ? 'true' : 'false',
},
{
key: 'AlipayWAPEnabled',
value: values.AlipayWAPEnabled ? 'true' : 'false',
},
{
key: 'AlipayQREnabled',
value: values.AlipayQREnabled ? 'true' : 'false',
},
{
key: 'AlipayIsSandbox',
value: values.AlipayIsSandbox ? 'true' : 'false',
},
{ key: 'AlipayNotifyUrl', value: values.AlipayNotifyUrl || '' },
{ key: 'AlipayReturnUrl', value: values.AlipayReturnUrl || '' },
{ key: 'AlipaySubject', value: values.AlipaySubject || '平台充值' },
{
key: 'AlipayExpireMinutes',
value: String(values.AlipayExpireMinutes || 30),
},
];
for (const config of configs) {
const res = await API.put('/api/option/', config);
if (!res.data.success) {
throw new Error(`保存 ${config.key} 失败: ${res.data.message}`);
}
}
showSuccess(t('支付宝配置保存成功'));
if (refresh) refresh();
} catch (error) {
showError(error.message || t('保存失败'));
} finally {
setLoading(false);
}
};
return (
<>
<Title heading={5}>{t('支付宝配置')}</Title>
<Text type='tertiary'>
{t('配置支付宝电脑网站支付、手机网站支付和当面付扫码支付。请前往')}{' '}
<a
href='https://open.alipay.com'
target='_blank'
rel='noopener noreferrer'
>
{t('支付宝开放平台')}
</a>{' '}
{t('获取相关配置信息。')}
</Text>
<Divider margin='12px' />
<Form
labelPosition='left'
labelAlign='right'
labelWidth={180}
onSubmit={onSubmit}
initValues={{
AlipayAppId: options.AlipayAppId || '',
AlipayPrivateKey:
options.AlipayPrivateKey &&
options.AlipayPrivateKey !== '***ALREADY_SET***'
? options.AlipayPrivateKey
: '',
AlipayPublicKey:
options.AlipayPublicKey &&
options.AlipayPublicKey !== '***ALREADY_SET***'
? options.AlipayPublicKey
: '',
AlipayPCEnabled: options.AlipayPCEnabled || false,
AlipayWAPEnabled: options.AlipayWAPEnabled || false,
AlipayQREnabled: options.AlipayQREnabled || false,
AlipayIsSandbox: options.AlipayIsSandbox || false,
AlipayNotifyUrl: options.AlipayNotifyUrl || '',
AlipayReturnUrl: options.AlipayReturnUrl || '',
AlipaySubject: options.AlipaySubject || '平台充值',
AlipayExpireMinutes: options.AlipayExpireMinutes || 30,
}}
>
<Form.Input
field='AlipayAppId'
label={t('应用 ID (App ID)')}
placeholder={t('请输入支付宝应用 ID')}
rules={[{ required: true, message: t('请输入应用 ID') }]}
/>
<Form.TextArea
field='AlipayPrivateKey'
label={t('应用私钥')}
placeholder={
options.AlipayPrivateKey === '***ALREADY_SET***'
? t('已设置(留空不修改,填写新内容则更新)')
: t(
'请粘贴完整的 RSA2 私钥内容(包含 -----BEGIN PRIVATE KEY----- 等)',
)
}
rows={6}
extraText={
options.AlipayPrivateKey === '***ALREADY_SET***'
? t('✓ 已配置应用私钥(留空则保持不变)')
: t('应用私钥,用于生成签名')
}
/>
<Form.TextArea
field='AlipayPublicKey'
label={t('支付宝公钥')}
placeholder={
options.AlipayPublicKey === '***ALREADY_SET***'
? t('已设置(留空不修改,填写新内容则更新)')
: t('请粘贴支付宝公钥内容(包含 -----BEGIN PUBLIC KEY----- 等)')
}
rows={6}
extraText={
options.AlipayPublicKey === '***ALREADY_SET***'
? t('✓ 已配置支付宝公钥(留空则保持不变)')
: t('支付宝公钥(非应用公钥),用于验证签名')
}
/>
<Divider margin='12px' />
<Form.Switch
field='AlipayPCEnabled'
label={t('启用电脑网站支付')}
extraText={t('PC 端网页支付,跳转到支付宝页面')}
/>
<Form.Switch
field='AlipayWAPEnabled'
label={t('启用手机网站支付')}
extraText={t('手机浏览器唤起支付宝 APP 支付')}
/>
<Form.Switch
field='AlipayQREnabled'
label={t('启用当面付扫码支付')}
extraText={t('生成二维码,用户扫码支付')}
/>
<Form.Switch
field='AlipayIsSandbox'
label={t('沙箱模式')}
extraText={t('测试环境,使用支付宝沙箱进行调试')}
/>
<Divider margin='12px' />
<Form.Input
field='AlipayNotifyUrl'
label={t('异步通知 URL')}
placeholder={t('留空则使用默认地址')}
extraText={t('默认: {服务器地址}/api/alipay/notify')}
/>
<Form.Input
field='AlipayReturnUrl'
label={t('同步返回 URL')}
placeholder={t('留空则使用默认地址')}
extraText={t('默认: {服务器地址}/recharge')}
/>
<Form.Input
field='AlipaySubject'
label={t('商品标题')}
placeholder={t('平台充值')}
extraText={t('显示在支付宝支付界面的商品标题')}
/>
<Form.InputNumber
field='AlipayExpireMinutes'
label={t('订单过期时间(分钟)')}
min={1}
max={1440}
extraText={t('订单未支付的有效时间,默认 30 分钟')}
/>
<Space style={{ marginTop: 20 }}>
<Button type='primary' htmlType='submit' loading={loading}>
{t('保存配置')}
</Button>
</Space>
</Form>
</>
);
};
export default SettingsPaymentAlipay;