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'
|
2026-03-13 14:13:06 +08:00
|
|
|
import {UniversalDesktopPayment} from './universal-desktop-payment'
|
2026-04-14 11:34:28 +08:00
|
|
|
import {useAppStore} from '@/components/stores/app'
|
2026-06-18 18:13:30 +08:00
|
|
|
import {toast} from 'sonner'
|
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 () => {
|
2026-06-18 18:13:30 +08:00
|
|
|
try {
|
|
|
|
|
const res = await payClose({
|
|
|
|
|
trade_no: props.inner_no,
|
|
|
|
|
method: props.method,
|
|
|
|
|
})
|
2025-08-16 11:41:07 +08:00
|
|
|
|
2026-06-18 18:13:30 +08:00
|
|
|
if (!res.success) {
|
|
|
|
|
throw new Error(res.message || '请求失败')
|
|
|
|
|
}
|
|
|
|
|
if (res.data.status === 1) {
|
|
|
|
|
toast.success('已支付成功!')
|
2025-08-16 11:41:07 +08:00
|
|
|
}
|
2025-12-04 14:43:13 +08:00
|
|
|
}
|
2026-06-18 18:13:30 +08:00
|
|
|
catch (error) {
|
|
|
|
|
console.error('关闭订单失败:', error)
|
2025-12-04 14:43:13 +08:00
|
|
|
}
|
2026-06-18 18:13:30 +08:00
|
|
|
finally {
|
|
|
|
|
props.onClose?.()
|
2025-08-16 11:41:07 +08:00
|
|
|
}
|
2026-06-18 18:13:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SSE处理方式检查支付状态
|
|
|
|
|
// const apiUrl = useAppStore('apiUrl')
|
|
|
|
|
// useEffect(() => {
|
|
|
|
|
// const eventSource = new EventSource(
|
|
|
|
|
// `${apiUrl}/api/trade/check?trade_no=${props.inner_no}&method=${props.method}`,
|
|
|
|
|
// )
|
|
|
|
|
// eventSource.onmessage = async (event) => {
|
|
|
|
|
// switch (event.data) {
|
|
|
|
|
// case '1':
|
|
|
|
|
// props.onConfirm?.(true)
|
|
|
|
|
// case '2':
|
|
|
|
|
// props.onClose?.()
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// eventSource.onerror = (error) => {
|
|
|
|
|
// console.error('SSE 连接错误:', error)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return () => {
|
|
|
|
|
// eventSource.close()
|
|
|
|
|
// }
|
|
|
|
|
// }, [apiUrl, props])
|
2025-08-16 11:41:07 +08:00
|
|
|
|
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
|
|
|
}}>
|
2026-03-13 14:13:06 +08:00
|
|
|
|
2026-03-31 16:11:36 +08:00
|
|
|
{props.platform === TradePlatform.Mobile
|
2026-03-13 14:13:06 +08:00
|
|
|
? <MobilePayment {...props} onClose={handleClose}/>
|
|
|
|
|
: <DesktopPayment {...props} onClose={handleClose}/>
|
2026-03-31 16:11:36 +08:00
|
|
|
}
|
2026-03-13 14:13:06 +08:00
|
|
|
|
2026-03-31 16:11:36 +08:00
|
|
|
{/* <UniversalDesktopPayment {...props} onClose={handleClose}/> */}
|
2025-06-22 14:42:21 +08:00
|
|
|
</Dialog>
|
|
|
|
|
)
|
|
|
|
|
}
|