fix(payment): prevent PayPal settings crash
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
|
||||
*/
|
||||
|
||||
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import React, {
|
||||
useCallback,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
} from 'react';
|
||||
import {
|
||||
Banner,
|
||||
Button,
|
||||
@@ -111,6 +117,16 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
||||
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(() => {
|
||||
const values = {
|
||||
PayPalClientID: props.options?.PayPalClientID || '',
|
||||
@@ -124,15 +140,18 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
||||
PayPalFXAPIKey: props.options?.PayPalFXAPIKey || '',
|
||||
PayPalFXAdjustmentBps: Number(props.options?.PayPalFXAdjustmentBps) || 0,
|
||||
};
|
||||
setInputs((current) => {
|
||||
const next = {
|
||||
setInputs((current) => ({
|
||||
...values,
|
||||
PayPalUserPaysFee: current.PayPalUserPaysFee,
|
||||
PayPalFeeRatePercent: current.PayPalFeeRatePercent,
|
||||
};
|
||||
formApiRef.current?.setValues(next);
|
||||
return next;
|
||||
}));
|
||||
// setValues invokes onValueChange, so it must run outside state updaters.
|
||||
if (formApiRef.current) {
|
||||
formApiRef.current.setValues({
|
||||
...formApiRef.current.getValues(),
|
||||
...values,
|
||||
});
|
||||
}
|
||||
}, [props.options]);
|
||||
|
||||
const refreshFXStatus = useCallback(async () => {
|
||||
@@ -216,7 +235,8 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
||||
refreshOperations(1);
|
||||
}, [refreshOperations]);
|
||||
|
||||
const inspectOperationOrder = async (tradeNo) => {
|
||||
const inspectOperationOrder = useCallback(
|
||||
async (tradeNo) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const response = await API.post(
|
||||
@@ -237,9 +257,12 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
},
|
||||
[t, refreshOperations, operationsPage],
|
||||
);
|
||||
|
||||
const reconcileOperationOrder = async (tradeNo) => {
|
||||
const reconcileOperationOrder = useCallback(
|
||||
async (tradeNo) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const response = await API.post(
|
||||
@@ -259,7 +282,9 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
},
|
||||
[t, refreshOperations, operationsPage],
|
||||
);
|
||||
|
||||
const expireOperationOrder = async () => {
|
||||
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
|
||||
? removeTrailingSlash(props.options.ServerAddress)
|
||||
: t('网站地址');
|
||||
@@ -520,8 +642,8 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
||||
return (
|
||||
<Spin spinning={loading}>
|
||||
<Form
|
||||
initValues={inputs}
|
||||
onValueChange={(values) => setInputs({ ...values })}
|
||||
initValues={initialValues}
|
||||
onValueChange={handleValuesChange}
|
||||
getFormApi={(api) => (formApiRef.current = api)}
|
||||
>
|
||||
<Form.Section text={t('PayPal 设置')}>
|
||||
@@ -576,39 +698,13 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
||||
currentPage: feeAuditPage,
|
||||
pageSize: 20,
|
||||
total: feeAuditTotal,
|
||||
showTotal: true,
|
||||
showTotal: (total) => t('共 {{total}} 条', { total }),
|
||||
onPageChange: (page) => refreshFeePolicy(page, true),
|
||||
}}
|
||||
dataSource={feeAudits}
|
||||
rowKey='id'
|
||||
style={{ marginTop: 16 }}
|
||||
columns={[
|
||||
{
|
||||
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() : '-',
|
||||
},
|
||||
]}
|
||||
columns={feeAuditColumns}
|
||||
/>
|
||||
<Banner
|
||||
type={operations?.pending_count ? 'warning' : 'success'}
|
||||
@@ -682,7 +778,7 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
||||
currentPage: operationsPage,
|
||||
pageSize: PAYPAL_OPERATIONS_PAGE_SIZE,
|
||||
total: Number(operations?.pending_count || 0),
|
||||
showTotal: true,
|
||||
showTotal: (total) => t('共 {{total}} 条', { total }),
|
||||
onPageChange: (page) => {
|
||||
setSelectedOperationOrderKeys([]);
|
||||
refreshOperations(page);
|
||||
@@ -690,70 +786,7 @@ export default function SettingsPaymentGatewayPayPal(props) {
|
||||
}}
|
||||
scroll={{ x: 900 }}
|
||||
style={{ marginTop: 12 }}
|
||||
columns={[
|
||||
{ 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>
|
||||
),
|
||||
},
|
||||
]}
|
||||
columns={operationColumns}
|
||||
/>
|
||||
)}
|
||||
<Button
|
||||
|
||||
Reference in New Issue
Block a user