fix(payment): 修复 PayPal 支付设置页崩溃
This commit is contained in:
@@ -17,7 +17,13 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
For commercial licensing, please contact support@quantumnous.com
|
For commercial licensing, please contact support@quantumnous.com
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
import React, {
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useMemo,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
} from 'react';
|
||||||
import {
|
import {
|
||||||
Banner,
|
Banner,
|
||||||
Button,
|
Button,
|
||||||
@@ -111,6 +117,16 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
|||||||
PayPalFeeRatePercent: 0,
|
PayPalFeeRatePercent: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const initialValues = useRef(inputs).current;
|
||||||
|
const handleValuesChange = useCallback((values) => {
|
||||||
|
setInputs((current) =>
|
||||||
|
Object.keys(current).length === Object.keys(values).length &&
|
||||||
|
Object.keys(values).every((key) => Object.is(current[key], values[key]))
|
||||||
|
? current
|
||||||
|
: { ...values },
|
||||||
|
);
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const values = {
|
const values = {
|
||||||
PayPalClientID: props.options?.PayPalClientID || '',
|
PayPalClientID: props.options?.PayPalClientID || '',
|
||||||
@@ -124,15 +140,18 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
|||||||
PayPalFXAPIKey: props.options?.PayPalFXAPIKey || '',
|
PayPalFXAPIKey: props.options?.PayPalFXAPIKey || '',
|
||||||
PayPalFXAdjustmentBps: Number(props.options?.PayPalFXAdjustmentBps) || 0,
|
PayPalFXAdjustmentBps: Number(props.options?.PayPalFXAdjustmentBps) || 0,
|
||||||
};
|
};
|
||||||
setInputs((current) => {
|
setInputs((current) => ({
|
||||||
const next = {
|
|
||||||
...values,
|
...values,
|
||||||
PayPalUserPaysFee: current.PayPalUserPaysFee,
|
PayPalUserPaysFee: current.PayPalUserPaysFee,
|
||||||
PayPalFeeRatePercent: current.PayPalFeeRatePercent,
|
PayPalFeeRatePercent: current.PayPalFeeRatePercent,
|
||||||
};
|
}));
|
||||||
formApiRef.current?.setValues(next);
|
// setValues invokes onValueChange, so it must run outside state updaters.
|
||||||
return next;
|
if (formApiRef.current) {
|
||||||
|
formApiRef.current.setValues({
|
||||||
|
...formApiRef.current.getValues(),
|
||||||
|
...values,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}, [props.options]);
|
}, [props.options]);
|
||||||
|
|
||||||
const refreshFXStatus = useCallback(async () => {
|
const refreshFXStatus = useCallback(async () => {
|
||||||
@@ -216,7 +235,8 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
|||||||
refreshOperations(1);
|
refreshOperations(1);
|
||||||
}, [refreshOperations]);
|
}, [refreshOperations]);
|
||||||
|
|
||||||
const inspectOperationOrder = async (tradeNo) => {
|
const inspectOperationOrder = useCallback(
|
||||||
|
async (tradeNo) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
const response = await API.post(
|
const response = await API.post(
|
||||||
@@ -237,9 +257,12 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
|||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
},
|
||||||
|
[t, refreshOperations, operationsPage],
|
||||||
|
);
|
||||||
|
|
||||||
const reconcileOperationOrder = async (tradeNo) => {
|
const reconcileOperationOrder = useCallback(
|
||||||
|
async (tradeNo) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
const response = await API.post(
|
const response = await API.post(
|
||||||
@@ -259,7 +282,9 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
|||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
},
|
||||||
|
[t, refreshOperations, operationsPage],
|
||||||
|
);
|
||||||
|
|
||||||
const expireOperationOrder = async () => {
|
const expireOperationOrder = async () => {
|
||||||
const reason =
|
const reason =
|
||||||
@@ -492,6 +517,103 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const feeAuditColumns = useMemo(
|
||||||
|
() => [
|
||||||
|
{
|
||||||
|
title: t('版本'),
|
||||||
|
dataIndex: 'policy_version',
|
||||||
|
width: 80,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: t('承担方'),
|
||||||
|
dataIndex: 'new_user_pays_fee',
|
||||||
|
render: (value) => t(value ? '用户承担' : '平台承担'),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: t('费率'),
|
||||||
|
dataIndex: 'new_rate_bps',
|
||||||
|
render: (value) => `${(Number(value) / 100).toFixed(2)}%`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: t('操作人'),
|
||||||
|
dataIndex: 'operator_id',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: t('更新时间'),
|
||||||
|
dataIndex: 'created_at',
|
||||||
|
render: (value) =>
|
||||||
|
value ? new Date(value * 1000).toLocaleString() : '-',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
[t],
|
||||||
|
);
|
||||||
|
|
||||||
|
const operationColumns = useMemo(
|
||||||
|
() => [
|
||||||
|
{ title: '本地订单号', dataIndex: 'trade_no' },
|
||||||
|
{ title: 'PayPal Order ID', dataIndex: 'third_trade_no' },
|
||||||
|
{ title: '环境', dataIndex: 'payment_environment' },
|
||||||
|
{
|
||||||
|
title: '创建时间',
|
||||||
|
dataIndex: 'create_time',
|
||||||
|
render: (value) =>
|
||||||
|
value ? new Date(value * 1000).toLocaleString() : '-',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: '操作',
|
||||||
|
render: (_, row) => (
|
||||||
|
<div className='flex flex-wrap gap-2'>
|
||||||
|
<Button
|
||||||
|
size='small'
|
||||||
|
htmlType='button'
|
||||||
|
onClick={() => inspectOperationOrder(row.trade_no)}
|
||||||
|
>
|
||||||
|
查询远端状态
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
size='small'
|
||||||
|
theme='solid'
|
||||||
|
htmlType='button'
|
||||||
|
onClick={() => reconcileOperationOrder(row.trade_no)}
|
||||||
|
disabled={!row.third_trade_no}
|
||||||
|
>
|
||||||
|
触发对账
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
size='small'
|
||||||
|
type='danger'
|
||||||
|
htmlType='button'
|
||||||
|
onClick={() => {
|
||||||
|
setExpireOrder(row);
|
||||||
|
setForceExpire(false);
|
||||||
|
setForceExpireConfirmed(false);
|
||||||
|
setExpireReason('确认该订单未完成 PayPal 支付');
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
确认未付款并过期
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
size='small'
|
||||||
|
type='warning'
|
||||||
|
htmlType='button'
|
||||||
|
onClick={() => {
|
||||||
|
setExpireOrder(row);
|
||||||
|
setForceExpire(true);
|
||||||
|
setForceExpireConfirmed(false);
|
||||||
|
setExpireReason(
|
||||||
|
'管理员确认远端状态无法核验,强制关闭该 PayPal 订单',
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
强制过期
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
[inspectOperationOrder, reconcileOperationOrder],
|
||||||
|
);
|
||||||
|
|
||||||
const webhookBase = props.options?.ServerAddress
|
const webhookBase = props.options?.ServerAddress
|
||||||
? removeTrailingSlash(props.options.ServerAddress)
|
? removeTrailingSlash(props.options.ServerAddress)
|
||||||
: t('网站地址');
|
: t('网站地址');
|
||||||
@@ -520,8 +642,8 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
|||||||
return (
|
return (
|
||||||
<Spin spinning={loading}>
|
<Spin spinning={loading}>
|
||||||
<Form
|
<Form
|
||||||
initValues={inputs}
|
initValues={initialValues}
|
||||||
onValueChange={(values) => setInputs({ ...values })}
|
onValueChange={handleValuesChange}
|
||||||
getFormApi={(api) => (formApiRef.current = api)}
|
getFormApi={(api) => (formApiRef.current = api)}
|
||||||
>
|
>
|
||||||
<Form.Section text={t('PayPal 设置')}>
|
<Form.Section text={t('PayPal 设置')}>
|
||||||
@@ -576,39 +698,13 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
|||||||
currentPage: feeAuditPage,
|
currentPage: feeAuditPage,
|
||||||
pageSize: 20,
|
pageSize: 20,
|
||||||
total: feeAuditTotal,
|
total: feeAuditTotal,
|
||||||
showTotal: true,
|
showTotal: (total) => t('共 {{total}} 条', { total }),
|
||||||
onPageChange: (page) => refreshFeePolicy(page, true),
|
onPageChange: (page) => refreshFeePolicy(page, true),
|
||||||
}}
|
}}
|
||||||
dataSource={feeAudits}
|
dataSource={feeAudits}
|
||||||
rowKey='id'
|
rowKey='id'
|
||||||
style={{ marginTop: 16 }}
|
style={{ marginTop: 16 }}
|
||||||
columns={[
|
columns={feeAuditColumns}
|
||||||
{
|
|
||||||
title: t('版本'),
|
|
||||||
dataIndex: 'policy_version',
|
|
||||||
width: 80,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: t('承担方'),
|
|
||||||
dataIndex: 'new_user_pays_fee',
|
|
||||||
render: (value) => t(value ? '用户承担' : '平台承担'),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: t('费率'),
|
|
||||||
dataIndex: 'new_rate_bps',
|
|
||||||
render: (value) => `${(Number(value) / 100).toFixed(2)}%`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: t('操作人'),
|
|
||||||
dataIndex: 'operator_id',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: t('更新时间'),
|
|
||||||
dataIndex: 'created_at',
|
|
||||||
render: (value) =>
|
|
||||||
value ? new Date(value * 1000).toLocaleString() : '-',
|
|
||||||
},
|
|
||||||
]}
|
|
||||||
/>
|
/>
|
||||||
<Banner
|
<Banner
|
||||||
type={operations?.pending_count ? 'warning' : 'success'}
|
type={operations?.pending_count ? 'warning' : 'success'}
|
||||||
@@ -682,7 +778,7 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
|||||||
currentPage: operationsPage,
|
currentPage: operationsPage,
|
||||||
pageSize: PAYPAL_OPERATIONS_PAGE_SIZE,
|
pageSize: PAYPAL_OPERATIONS_PAGE_SIZE,
|
||||||
total: Number(operations?.pending_count || 0),
|
total: Number(operations?.pending_count || 0),
|
||||||
showTotal: true,
|
showTotal: (total) => t('共 {{total}} 条', { total }),
|
||||||
onPageChange: (page) => {
|
onPageChange: (page) => {
|
||||||
setSelectedOperationOrderKeys([]);
|
setSelectedOperationOrderKeys([]);
|
||||||
refreshOperations(page);
|
refreshOperations(page);
|
||||||
@@ -690,70 +786,7 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
|||||||
}}
|
}}
|
||||||
scroll={{ x: 900 }}
|
scroll={{ x: 900 }}
|
||||||
style={{ marginTop: 12 }}
|
style={{ marginTop: 12 }}
|
||||||
columns={[
|
columns={operationColumns}
|
||||||
{ title: '本地订单号', dataIndex: 'trade_no' },
|
|
||||||
{ title: 'PayPal Order ID', dataIndex: 'third_trade_no' },
|
|
||||||
{ title: '环境', dataIndex: 'payment_environment' },
|
|
||||||
{
|
|
||||||
title: '创建时间',
|
|
||||||
dataIndex: 'create_time',
|
|
||||||
render: (value) =>
|
|
||||||
value ? new Date(value * 1000).toLocaleString() : '-',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '操作',
|
|
||||||
render: (_, row) => (
|
|
||||||
<div className='flex flex-wrap gap-2'>
|
|
||||||
<Button
|
|
||||||
size='small'
|
|
||||||
htmlType='button'
|
|
||||||
onClick={() => inspectOperationOrder(row.trade_no)}
|
|
||||||
>
|
|
||||||
查询远端状态
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
size='small'
|
|
||||||
theme='solid'
|
|
||||||
htmlType='button'
|
|
||||||
onClick={() =>
|
|
||||||
reconcileOperationOrder(row.trade_no)
|
|
||||||
}
|
|
||||||
disabled={!row.third_trade_no}
|
|
||||||
>
|
|
||||||
触发对账
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
size='small'
|
|
||||||
type='danger'
|
|
||||||
htmlType='button'
|
|
||||||
onClick={() => {
|
|
||||||
setExpireOrder(row);
|
|
||||||
setForceExpire(false);
|
|
||||||
setForceExpireConfirmed(false);
|
|
||||||
setExpireReason('确认该订单未完成 PayPal 支付');
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
确认未付款并过期
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
size='small'
|
|
||||||
type='warning'
|
|
||||||
htmlType='button'
|
|
||||||
onClick={() => {
|
|
||||||
setExpireOrder(row);
|
|
||||||
setForceExpire(true);
|
|
||||||
setForceExpireConfirmed(false);
|
|
||||||
setExpireReason(
|
|
||||||
'管理员确认远端状态无法核验,强制关闭该 PayPal 订单',
|
|
||||||
);
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
强制过期
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
),
|
|
||||||
},
|
|
||||||
]}
|
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
Reference in New Issue
Block a user