Files
monkey-ai-web/src/pages/Setting/Payment/SettingsPaymentWechat.jsx
T
2026-11-19 10:48:17 +08:00

380 lines
13 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 { Trans } from 'react-i18next';
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 SettingsPaymentWechat = ({ options, refresh }) => {
const { t } = useTranslation();
const [loading, setLoading] = useState(false);
const onSubmit = async (values) => {
try {
setLoading(true);
// 保存微信支付配置
const configs = [
{ key: 'WechatMchId', value: values.WechatMchId || '' },
{ key: 'WechatAppId', value: values.WechatAppId || '' },
// 如果敏感字段未修改(仍然是空或占位符),则使用占位符让后端跳过更新
{
key: 'WechatAPIv3Key',
value:
values.WechatAPIv3Key && values.WechatAPIv3Key.trim() !== ''
? values.WechatAPIv3Key
: '***ALREADY_SET***',
},
{ key: 'WechatSerialNo', value: values.WechatSerialNo || '' },
{
key: 'WechatPrivateKey',
value:
values.WechatPrivateKey && values.WechatPrivateKey.trim() !== ''
? values.WechatPrivateKey
: '***ALREADY_SET***',
},
{
key: 'WechatNativeEnabled',
value: values.WechatNativeEnabled ? 'true' : 'false',
},
{
key: 'WechatH5Enabled',
value: values.WechatH5Enabled ? 'true' : 'false',
},
{ key: 'WechatNotifyUrl', value: values.WechatNotifyUrl || '' },
{
key: 'WechatDescription',
value: values.WechatDescription || '平台充值',
},
{
key: 'WechatExpireMinutes',
value: String(values.WechatExpireMinutes || 30),
},
// 公钥模式配置(2024年新商户)
{
key: 'WechatUsePublicKey',
value: values.WechatUsePublicKey ? 'true' : 'false',
},
{ key: 'WechatPublicKeyID', value: values.WechatPublicKeyID || '' },
{
key: 'WechatPublicKeyPEM',
value:
values.WechatPublicKeyPEM && values.WechatPublicKeyPEM.trim() !== ''
? values.WechatPublicKeyPEM
: '***ALREADY_SET***',
},
// 手动证书模式配置
{
key: 'WechatUseManualCert',
value: values.WechatUseManualCert ? 'true' : 'false',
},
{
key: 'WechatPlatformCertPath',
value: values.WechatPlatformCertPath || '',
},
{
key: 'WechatPlatformCertPEM',
value:
values.WechatPlatformCertPEM &&
values.WechatPlatformCertPEM.trim() !== ''
? values.WechatPlatformCertPEM
: '***ALREADY_SET***',
},
];
for (const config of configs) {
const res = await API.put('/api/option/', config);
if (!res.data.success) {
throw new Error(
t('保存 {{key}} 失败:{{message}}', {
key: config.key,
message: 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'>
<Trans
t={t}
i18nKey='配置微信支付 Native(扫码)和 H5 支付方式。请前往 <part0>微信支付商户平台</part0> 获取相关配置信息。'
components={{
part0: (
<a
href='https://pay.weixin.qq.com'
target='_blank'
rel='noopener noreferrer'
/>
),
}}
/>
</Text>
<Divider margin='12px' />
<Form
labelPosition='left'
labelAlign='end'
labelWidth={180}
onSubmit={onSubmit}
initValues={{
WechatMchId: options.WechatMchId || '',
WechatAppId: options.WechatAppId || '',
WechatAPIv3Key:
options.WechatAPIv3Key &&
options.WechatAPIv3Key !== '***ALREADY_SET***'
? options.WechatAPIv3Key
: '',
WechatSerialNo: options.WechatSerialNo || '',
WechatPrivateKey:
options.WechatPrivateKey &&
options.WechatPrivateKey !== '***ALREADY_SET***'
? options.WechatPrivateKey
: '',
WechatNativeEnabled: options.WechatNativeEnabled || false,
WechatH5Enabled: options.WechatH5Enabled || false,
WechatNotifyUrl: options.WechatNotifyUrl || '',
WechatDescription: options.WechatDescription || '平台充值',
WechatExpireMinutes: options.WechatExpireMinutes || 30,
// 公钥模式
WechatUsePublicKey: options.WechatUsePublicKey || false,
WechatPublicKeyID: options.WechatPublicKeyID || '',
WechatPublicKeyPEM:
options.WechatPublicKeyPEM &&
options.WechatPublicKeyPEM !== '***ALREADY_SET***'
? options.WechatPublicKeyPEM
: '',
// 手动证书模式
WechatUseManualCert: options.WechatUseManualCert || false,
WechatPlatformCertPath: options.WechatPlatformCertPath || '',
WechatPlatformCertPEM:
options.WechatPlatformCertPEM &&
options.WechatPlatformCertPEM !== '***ALREADY_SET***'
? options.WechatPlatformCertPEM
: '',
}}
>
<Form.Input
field='WechatMchId'
label={t('商户号 (Mch ID)')}
placeholder={t('请输入微信支付商户号')}
rules={[{ required: true, message: t('请输入商户号') }]}
/>
<Form.Input
field='WechatAppId'
label={t('应用 ID (App ID)')}
placeholder={t('请输入微信应用 ID')}
rules={[{ required: true, message: t('请输入应用 ID') }]}
/>
<Form.Input
field='WechatAPIv3Key'
label={t('APIv3 密钥')}
placeholder={
options.WechatAPIv3Key === '***ALREADY_SET***'
? t('已设置(留空不修改,填写新内容则更新)')
: t('请输入 APIv3 密钥')
}
mode='password'
extraText={
options.WechatAPIv3Key === '***ALREADY_SET***'
? t('✓ 已配置 APIv3 密钥(留空则保持不变)')
: ''
}
/>
<Form.Input
field='WechatSerialNo'
label={t('证书序列号')}
placeholder={t('请输入证书序列号')}
rules={[{ required: true, message: t('请输入证书序列号') }]}
/>
<Form.TextArea
field='WechatPrivateKey'
label={t('商户私钥')}
placeholder={
options.WechatPrivateKey === '***ALREADY_SET***'
? t('已设置(留空不修改,填写新内容则更新)')
: t(
'请粘贴完整的 PEM 格式私钥内容(包含 -----BEGIN PRIVATE KEY----- 等)',
)
}
rows={6}
extraText={
options.WechatPrivateKey === '***ALREADY_SET***'
? t('✓ 已配置商户私钥(留空则保持不变)')
: t('支持直接粘贴 PEM 格式内容或填写文件路径')
}
/>
<Divider margin='12px' />
<Title heading={6} style={{ marginTop: 16, marginBottom: 8 }}>
{t('公钥模式(2024年新商户必选)')}
</Title>
<Text type='tertiary' style={{ display: 'block', marginBottom: 12 }}>
{t(
'2024年后注册的微信支付商户默认使用公钥模式。如果初始化失败提示"无可用的平台证书",请从商户平台下载微信支付公钥并启用此模式。',
)}
</Text>
<Form.Switch
field='WechatUsePublicKey'
label={t('启用公钥模式')}
extraText={t('2024年新商户必须开启此选项')}
/>
<Form.Input
field='WechatPublicKeyID'
label={t('微信支付公钥 ID')}
placeholder='PUB_KEY_ID_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
extraText={t(
'从商户平台【API安全】→【微信支付公钥】获取,格式如 PUB_KEY_ID_xxx',
)}
/>
<Form.TextArea
field='WechatPublicKeyPEM'
label={t('微信支付公钥内容')}
placeholder={
options.WechatPublicKeyPEM === '***ALREADY_SET***'
? t('已设置(留空不修改,填写新内容则更新)')
: t(
'请粘贴完整的微信支付公钥 PEM 内容(包含 -----BEGIN PUBLIC KEY----- 等)',
)
}
rows={6}
extraText={
options.WechatPublicKeyPEM === '***ALREADY_SET***'
? t('✓ 已配置微信支付公钥(留空则保持不变)')
: t('从商户平台下载的公钥文件内容')
}
/>
<Divider margin='12px' />
<Title heading={6} style={{ marginTop: 16, marginBottom: 8 }}>
{t('高级配置:手动平台证书模式(可选)')}
</Title>
<Text type='tertiary' style={{ display: 'block', marginBottom: 12 }}>
{t(
'默认情况下,SDK 会自动下载微信平台证书,无需手动配置。仅在离线环境或网络受限时启用此选项。',
)}
</Text>
<Form.Switch
field='WechatUseManualCert'
label={t('启用手动证书模式')}
extraText={t(
'开启后需要手动配置平台证书,关闭则使用自动下载模式(推荐)',
)}
/>
<Form.Input
field='WechatPlatformCertPath'
label={t('平台证书文件路径')}
placeholder='/data/certs/wechatpay_platform_cert.pem'
extraText={t('平台证书文件的绝对路径(与下方 PEM 内容二选一)')}
/>
<Form.TextArea
field='WechatPlatformCertPEM'
label={t('平台证书 PEM 内容')}
placeholder={
options.WechatPlatformCertPEM === '***ALREADY_SET***'
? t('已设置(留空不修改,填写新内容则更新)')
: t(
'请粘贴完整的平台证书 PEM 内容(包含 -----BEGIN CERTIFICATE----- 等)',
)
}
rows={6}
extraText={
options.WechatPlatformCertPEM === '***ALREADY_SET***'
? t('✓ 已配置平台证书(留空则保持不变)')
: t('与上方文件路径二选一,优先使用文件路径')
}
/>
<Divider margin='12px' />
<Form.Switch
field='WechatNativeEnabled'
label={t('启用 Native 扫码支付')}
extraText={t('用户扫描二维码完成支付')}
/>
<Form.Switch
field='WechatH5Enabled'
label={t('启用 H5 支付')}
extraText={t('在手机浏览器中唤起微信支付')}
/>
<Divider margin='12px' />
<Form.Input
field='WechatNotifyUrl'
label={t('回调通知 URL')}
placeholder={t('留空则使用默认地址')}
extraText={t('默认: {服务器地址}/api/wechat/notify')}
/>
<Form.Input
field='WechatDescription'
label={t('商品描述')}
placeholder={t('平台充值')}
extraText={t('显示在微信支付界面的商品描述')}
/>
<Form.InputNumber
field='WechatExpireMinutes'
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 SettingsPaymentWechat;