修复支付取消的调用和规定时间内更新订单支付状态

This commit is contained in:
Eamon-meng
2025-08-16 11:41:07 +08:00
parent 1baa7c94dc
commit 99c3b9914e
7 changed files with 105 additions and 98 deletions

View File

@@ -4,26 +4,75 @@ import {DesktopPayment} from './desktop-payment'
import {TradePlatform} from '@/lib/models/trade'
import {Dialog} from '@/components/ui/dialog'
import {PaymentProps} from './type'
import {payClose} from '@/actions/resource'
import {useEffect} from 'react'
import {useRouter} from 'next/navigation'
export type PaymentModalProps = {
onConfirm?: () => Promise<void>
onClose?: () => void
onConfirm: (showFail: boolean) => Promise<void>
onClose: () => void
} & PaymentProps
export function PaymentModal(props: PaymentModalProps) {
// 手动关闭时的处理
const handleClose = async () => {
try {
const req = {
trade_no: props.inner_no,
method: props.method,
}
const res = await payClose(req)
}
catch (error) {
console.error('关闭订单失败:', error)
}
finally {
props.onClose?.()
}
}
// 轮询检查支付状态
useEffect(() => {
const pollInterval = 2000
const maxRetries = 30
let retries = 0
const interval = setInterval(async () => {
try {
await props.onConfirm(false)
return
}
catch (error) {
console.error('支付状态检查失败:', error)
}
finally {
retries++
if (retries >= maxRetries) {
clearInterval(interval)
}
}
}, pollInterval)
return () => {
clearInterval(interval)
}
}, [props])
return (
<Dialog
defaultOpen={true}
onOpenChange={(open) => {
if (!open) props.onClose?.()
if (!open) handleClose()
}}>
{props.platform === TradePlatform.Mobile ? (
<MobilePayment
{...props}
onClose={handleClose}
/>
) : (
<DesktopPayment
{...props}
onClose={handleClose}
/>
)}
</Dialog>