2025-06-22 14:42:21 +08:00
|
|
|
'use client'
|
|
|
|
|
import {MobilePayment} from './mobile-payment'
|
|
|
|
|
import {DesktopPayment} from './desktop-payment'
|
2025-06-23 11:20:54 +08:00
|
|
|
import {TradePlatform} from '@/lib/models/trade'
|
|
|
|
|
import {Dialog} from '@/components/ui/dialog'
|
|
|
|
|
import {PaymentProps} from './type'
|
2025-08-16 11:41:07 +08:00
|
|
|
import {payClose} from '@/actions/resource'
|
|
|
|
|
import {useEffect} from 'react'
|
|
|
|
|
import {useRouter} from 'next/navigation'
|
2025-06-22 14:42:21 +08:00
|
|
|
|
2025-06-23 11:20:54 +08:00
|
|
|
export type PaymentModalProps = {
|
2025-08-16 11:41:07 +08:00
|
|
|
onConfirm: (showFail: boolean) => Promise<void>
|
|
|
|
|
onClose: () => void
|
2025-06-23 11:20:54 +08:00
|
|
|
} & PaymentProps
|
|
|
|
|
|
|
|
|
|
export function PaymentModal(props: PaymentModalProps) {
|
2025-08-16 11:41:07 +08:00
|
|
|
// 手动关闭时的处理
|
|
|
|
|
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])
|
|
|
|
|
|
2025-06-22 14:42:21 +08:00
|
|
|
return (
|
2025-06-23 11:20:54 +08:00
|
|
|
<Dialog
|
|
|
|
|
defaultOpen={true}
|
|
|
|
|
onOpenChange={(open) => {
|
2025-08-16 11:41:07 +08:00
|
|
|
if (!open) handleClose()
|
2025-06-23 11:20:54 +08:00
|
|
|
}}>
|
|
|
|
|
{props.platform === TradePlatform.Mobile ? (
|
2025-06-22 14:42:21 +08:00
|
|
|
<MobilePayment
|
2025-06-23 11:20:54 +08:00
|
|
|
{...props}
|
2025-08-16 11:41:07 +08:00
|
|
|
onClose={handleClose}
|
2025-06-22 14:42:21 +08:00
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<DesktopPayment
|
2025-06-23 11:20:54 +08:00
|
|
|
{...props}
|
2025-08-16 11:41:07 +08:00
|
|
|
onClose={handleClose}
|
2025-06-22 14:42:21 +08:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</Dialog>
|
|
|
|
|
)
|
|
|
|
|
}
|