/*
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, { useEffect, useState } from 'react';
import { Card, Spin } from '@/components/ui';
import SettingsGeneralPayment from '../../pages/Setting/Payment/SettingsGeneralPayment';
import SettingsPaymentGateway from '../../pages/Setting/Payment/SettingsPaymentGateway';
import SettingsPaymentGatewayStripe from '../../pages/Setting/Payment/SettingsPaymentGatewayStripe';
import SettingsPaymentGatewayPayPal from '../../pages/Setting/Payment/SettingsPaymentGatewayPayPal';
import SettingsPaymentGatewayCreem from '../../pages/Setting/Payment/SettingsPaymentGatewayCreem';
import SettingsPaymentWechat from '../../pages/Setting/Payment/SettingsPaymentWechat';
import SettingsPaymentAlipay from '../../pages/Setting/Payment/SettingsPaymentAlipay';
import SettingsNuonuoInvoice from '../../pages/Setting/Payment/SettingsNuonuoInvoice';
import { API, showError, toBoolean } from '../../helpers';
import { useTranslation } from 'react-i18next';
const PaymentSetting = () => {
const { t } = useTranslation();
let [inputs, setInputs] = useState({
ServerAddress: '',
PayAddress: '',
EpayId: '',
EpayKey: '',
Price: 7.3,
MinTopUp: 1,
TopupGroupRatio: '',
CustomCallbackAddress: '',
PayMethods: '',
AmountOptions: '',
AmountDiscount: '',
ShowDefaultPayMethods: true,
DisabledPayMethods: [],
StripeApiSecret: '',
StripeWebhookSecret: '',
StripePriceId: '',
StripeUnitPrice: 8.0,
StripeMinTopUp: 1,
StripePromotionCodesEnabled: false,
PayPalClientID: '',
PayPalClientSecret: '',
PayPalWebhookID: '',
PayPalSandbox: true,
PayPalMinTopUp: 1,
PayPalFXMode: 'manual',
PayPalFXProvider: 'open_exchange_rates',
PayPalFXAPIKey: '',
PayPalFXAdjustmentBps: 0,
// WeChat Pay Settings
WechatMchId: '',
WechatAppId: '',
WechatAPIv3Key: '',
WechatSerialNo: '',
WechatPrivateKey: '',
WechatNativeEnabled: false,
WechatH5Enabled: false,
WechatNotifyUrl: '',
WechatDescription: '平台充值',
WechatExpireMinutes: 30,
// Alipay Settings
AlipayAppId: '',
AlipayPrivateKey: '',
AlipayPublicKey: '',
AlipayPCEnabled: false,
AlipayWAPEnabled: false,
AlipayQREnabled: false,
AlipayIsSandbox: false,
AlipayNotifyUrl: '',
AlipayReturnUrl: '',
AlipaySubject: '平台充值',
AlipayExpireMinutes: 30,
// Nuonuo Invoice Settings
NuonuoEnabled: false,
NuonuoAppKey: '',
NuonuoAppSecret: '',
NuonuoApiUrl: 'https://sdk.nuonuo.com/open/v1/services',
NuonuoCallbackUrl: '',
NuonuoTaxNum: '',
NuonuoSalerName: '',
NuonuoSalerAddress: '',
NuonuoSalerTel: '',
NuonuoSalerBank: '',
NuonuoClerk: '',
NuonuoExtensionNum: '',
NuonuoItemName: '信息技术服务*信息技术服务费',
NuonuoTaxRate: '0.06',
NuonuoMinAmount: 200,
NuonuoMonthlyLimit: 5,
});
let [loading, setLoading] = useState(false);
const getOptions = async () => {
const res = await API.get('/api/option/');
const { success, message, data } = res.data;
if (success) {
let newInputs = {};
data.forEach((item) => {
switch (item.key) {
case 'TopupGroupRatio':
try {
newInputs[item.key] = JSON.stringify(
JSON.parse(item.value),
null,
2,
);
} catch (error) {
console.error('解析TopupGroupRatio出错:', error);
newInputs[item.key] = item.value;
}
break;
case 'payment_setting.amount_options':
try {
newInputs['AmountOptions'] = JSON.stringify(
JSON.parse(item.value),
null,
2,
);
} catch (error) {
console.error('解析AmountOptions出错:', error);
newInputs['AmountOptions'] = item.value;
}
break;
case 'payment_setting.amount_discount':
try {
newInputs['AmountDiscount'] = JSON.stringify(
JSON.parse(item.value),
null,
2,
);
} catch (error) {
console.error('解析AmountDiscount出错:', error);
newInputs['AmountDiscount'] = item.value;
}
break;
case 'payment_setting.show_default_pay_methods':
newInputs['ShowDefaultPayMethods'] = toBoolean(item.value);
break;
case 'payment_setting.disabled_pay_methods':
try {
const parsed = JSON.parse(item.value);
newInputs['DisabledPayMethods'] = Array.isArray(parsed)
? parsed
: [];
} catch (error) {
console.error('解析DisabledPayMethods出错:', error);
newInputs['DisabledPayMethods'] = [];
}
break;
case 'Price':
case 'MinTopUp':
case 'StripeUnitPrice':
case 'StripeMinTopUp':
case 'PayPalMinTopUp':
case 'PayPalFXAdjustmentBps':
newInputs[item.key] = parseFloat(item.value);
break;
case 'WechatExpireMinutes':
case 'AlipayExpireMinutes':
newInputs[item.key] = parseInt(item.value) || 30;
break;
// Nuonuo Invoice Settings
case 'nuonuo_setting.enabled':
newInputs['NuonuoEnabled'] = toBoolean(item.value);
break;
case 'nuonuo_setting.app_key':
newInputs['NuonuoAppKey'] = item.value;
break;
case 'nuonuo_setting.app_secret':
newInputs['NuonuoAppSecret'] = item.value;
break;
case 'nuonuo_setting.api_url':
newInputs['NuonuoApiUrl'] = item.value;
break;
case 'nuonuo_setting.callback_url':
newInputs['NuonuoCallbackUrl'] = item.value;
break;
case 'nuonuo_setting.tax_num':
newInputs['NuonuoTaxNum'] = item.value;
break;
case 'nuonuo_setting.saler_name':
newInputs['NuonuoSalerName'] = item.value;
break;
case 'nuonuo_setting.saler_address':
newInputs['NuonuoSalerAddress'] = item.value;
break;
case 'nuonuo_setting.saler_tel':
newInputs['NuonuoSalerTel'] = item.value;
break;
case 'nuonuo_setting.saler_bank':
newInputs['NuonuoSalerBank'] = item.value;
break;
case 'nuonuo_setting.clerk':
newInputs['NuonuoClerk'] = item.value;
break;
case 'nuonuo_setting.extension_num':
newInputs['NuonuoExtensionNum'] = item.value;
break;
case 'nuonuo_setting.item_name':
newInputs['NuonuoItemName'] = item.value;
break;
case 'nuonuo_setting.tax_rate':
newInputs['NuonuoTaxRate'] = item.value;
break;
case 'nuonuo_setting.min_amount':
newInputs['NuonuoMinAmount'] = parseFloat(item.value) || 200;
break;
case 'nuonuo_setting.monthly_limit':
newInputs['NuonuoMonthlyLimit'] = parseInt(item.value) || 5;
break;
default:
// 处理所有布尔值字段
if (
item.key.endsWith('Enabled') ||
item.key === 'WechatUsePublicKey' ||
item.key === 'WechatUseManualCert' ||
item.key === 'AlipayIsSandbox' ||
item.key === 'PayPalSandbox'
) {
newInputs[item.key] = toBoolean(item.value);
} else {
newInputs[item.key] = item.value;
}
break;
}
});
// 使用合并方式设置状态,保留初始值
setInputs((prevInputs) => ({ ...prevInputs, ...newInputs }));
} else {
showError(t(message));
}
};
async function onRefresh() {
try {
setLoading(true);
await getOptions();
} catch (error) {
showError(t('刷新失败'));
} finally {
setLoading(false);
}
}
const onMount = useEffectEvent(() => {
onRefresh();
});
useEffect(() => onMount(), [onMount]);
return (
<>
>
);
};
export default PaymentSetting;