2025-04-16 18:51:17 +08:00
|
|
|
|
'use client'
|
|
|
|
|
|
import {Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger} from '@/components/ui/dialog'
|
|
|
|
|
|
import {Button} from '@/components/ui/button'
|
2025-05-22 14:59:22 +08:00
|
|
|
|
import alipay from './_assets/alipay.svg'
|
|
|
|
|
|
import wechat from './_assets/wechat.svg'
|
|
|
|
|
|
import balance from './_assets/balance.svg'
|
2025-04-16 18:51:17 +08:00
|
|
|
|
import Image from 'next/image'
|
2025-05-22 14:59:22 +08:00
|
|
|
|
import {useEffect, useRef, useState} from 'react'
|
2025-06-22 10:58:41 +08:00
|
|
|
|
import {useProfileStore} from '@/components/stores-provider'
|
2025-06-16 11:27:34 +08:00
|
|
|
|
import {Alert, AlertTitle} from '@/components/ui/alert'
|
2025-05-22 14:59:22 +08:00
|
|
|
|
import {ApiResponse, ExtraResp, ExtraReq} from '@/lib/api'
|
2025-04-16 18:51:17 +08:00
|
|
|
|
import {toast} from 'sonner'
|
|
|
|
|
|
import {Loader} from 'lucide-react'
|
|
|
|
|
|
import {useRouter} from 'next/navigation'
|
2025-04-18 16:22:50 +08:00
|
|
|
|
import * as qrcode from 'qrcode'
|
2025-05-22 14:59:22 +08:00
|
|
|
|
import {completeResource, createResource, prepareResource} from '@/actions/resource'
|
2025-06-18 19:05:38 +08:00
|
|
|
|
import {PaymentMethod, Platform, usePlatformType} from '@/lib/models/trade'
|
2025-04-16 18:51:17 +08:00
|
|
|
|
|
|
|
|
|
|
export type PayProps = {
|
|
|
|
|
|
method: 'alipay' | 'wechat' | 'balance'
|
2025-05-22 14:59:22 +08:00
|
|
|
|
amount: string
|
|
|
|
|
|
resource: ExtraReq<typeof createResource>
|
2025-04-16 18:51:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default function Pay(props: PayProps) {
|
2025-05-19 11:04:40 +08:00
|
|
|
|
const profile = useProfileStore(store => store.profile)
|
|
|
|
|
|
const refreshProfile = useProfileStore(store => store.refreshProfile)
|
2025-06-18 19:05:38 +08:00
|
|
|
|
const platform = usePlatformType() // 获取当前平台类型
|
2025-04-16 18:51:17 +08:00
|
|
|
|
const [open, setOpen] = useState(false)
|
2025-05-22 14:59:22 +08:00
|
|
|
|
const [payInfo, setPayInfo] = useState<ExtraResp<typeof prepareResource> | undefined>()
|
2025-04-18 16:22:50 +08:00
|
|
|
|
const canvas = useRef<HTMLCanvasElement>(null)
|
2025-05-09 16:37:05 +08:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (canvas.current && payInfo) {
|
|
|
|
|
|
qrcode.toCanvas(canvas.current, payInfo.pay_url, {
|
|
|
|
|
|
width: 200,
|
|
|
|
|
|
margin: 0,
|
2025-05-22 14:59:22 +08:00
|
|
|
|
}).then()
|
2025-05-09 16:37:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
}, [payInfo])
|
2025-04-16 18:51:17 +08:00
|
|
|
|
|
|
|
|
|
|
const onOpen = async () => {
|
|
|
|
|
|
setOpen(true)
|
|
|
|
|
|
|
|
|
|
|
|
if (props.method === 'balance') {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-18 19:05:38 +08:00
|
|
|
|
// 根据平台类型设置支付方法
|
|
|
|
|
|
let paymentMethod: PaymentMethod
|
|
|
|
|
|
if (platform === Platform.Desktop) {
|
|
|
|
|
|
paymentMethod = PaymentMethod.Sft
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
// 移动端根据选择的支付方式设置
|
|
|
|
|
|
paymentMethod = props.method === 'alipay'
|
|
|
|
|
|
? PaymentMethod.SftAlipay
|
|
|
|
|
|
: PaymentMethod.SftWeChat
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-22 14:59:22 +08:00
|
|
|
|
const method = {
|
2025-06-18 19:05:38 +08:00
|
|
|
|
alipay: PaymentMethod.Alipay,
|
|
|
|
|
|
wechat: PaymentMethod.WeChat,
|
2025-05-22 14:59:22 +08:00
|
|
|
|
}[props.method]
|
|
|
|
|
|
|
2025-06-18 19:05:38 +08:00
|
|
|
|
const res = {
|
2025-05-22 14:59:22 +08:00
|
|
|
|
...props.resource,
|
2025-06-18 19:05:38 +08:00
|
|
|
|
payment_method: paymentMethod,
|
|
|
|
|
|
payment_platform: platform, // 使用检测到的平台类型
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log(res, 'reresp')
|
|
|
|
|
|
const resp = await prepareResource(res)
|
|
|
|
|
|
console.log(resp, 'resp')
|
|
|
|
|
|
|
2025-04-16 18:51:17 +08:00
|
|
|
|
if (!resp.success) {
|
|
|
|
|
|
toast.error(`创建订单失败: ${resp.message}`)
|
|
|
|
|
|
setOpen(false)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setPayInfo(resp.data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
|
const onSubmit = async () => {
|
|
|
|
|
|
let resp: ApiResponse
|
|
|
|
|
|
try {
|
|
|
|
|
|
switch (props.method) {
|
2025-05-09 16:37:05 +08:00
|
|
|
|
case 'alipay':
|
|
|
|
|
|
case 'wechat':
|
2025-05-22 14:59:22 +08:00
|
|
|
|
if (!payInfo) {
|
|
|
|
|
|
toast.error('无法读取支付信息', {
|
|
|
|
|
|
description: `请联系客服确认支付状态`,
|
|
|
|
|
|
})
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
resp = await completeResource({
|
|
|
|
|
|
trade_no: payInfo.trade_no,
|
2025-05-09 16:37:05 +08:00
|
|
|
|
})
|
|
|
|
|
|
break
|
2025-05-22 14:59:22 +08:00
|
|
|
|
|
2025-05-09 16:37:05 +08:00
|
|
|
|
case 'balance':
|
2025-05-22 14:59:22 +08:00
|
|
|
|
resp = await createResource(props.resource)
|
2025-05-09 16:37:05 +08:00
|
|
|
|
break
|
2025-04-16 18:51:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!resp.success) {
|
|
|
|
|
|
throw new Error(resp.message)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
toast.success('购买成功', {
|
|
|
|
|
|
duration: 10 * 1000,
|
|
|
|
|
|
closeButton: true,
|
|
|
|
|
|
action: {
|
|
|
|
|
|
label: `去提取`,
|
|
|
|
|
|
onClick: () => router.push('/admin/extract'),
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
setOpen(false)
|
2025-04-23 19:00:53 +08:00
|
|
|
|
await refreshProfile()
|
2025-04-16 18:51:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (e) {
|
|
|
|
|
|
console.log(e)
|
|
|
|
|
|
toast.error('购买失败', {
|
|
|
|
|
|
description: (e as Error).message,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-22 14:59:22 +08:00
|
|
|
|
const balanceEnough = profile && profile.balance >= Number(props.amount)
|
|
|
|
|
|
|
2025-04-16 18:51:17 +08:00
|
|
|
|
return (
|
|
|
|
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
|
|
|
|
<DialogTrigger asChild>
|
2025-06-07 11:49:57 +08:00
|
|
|
|
<Button className="mt-4 h-12" onClick={onOpen}>
|
2025-04-16 18:51:17 +08:00
|
|
|
|
立即支付
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</DialogTrigger>
|
|
|
|
|
|
<DialogContent>
|
|
|
|
|
|
<DialogHeader>
|
2025-06-07 11:49:57 +08:00
|
|
|
|
<DialogTitle className="flex gap-2 items-center">
|
|
|
|
|
|
{props.method === 'alipay' && (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<Image src={alipay} alt="支付宝" width={20} height={20}/>
|
|
|
|
|
|
<span>支付宝</span>
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
|
|
|
|
|
{props.method === 'wechat' && (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<Image src={wechat} alt="微信" width={20} height={20}/>
|
|
|
|
|
|
<span>微信</span>
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
|
|
|
|
|
{props.method === 'balance' && (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<Image src={balance} alt="余额" width={20} height={20}/>
|
|
|
|
|
|
<span>余额支付</span>
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
2025-04-16 18:51:17 +08:00
|
|
|
|
</DialogTitle>
|
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
|
|
|
|
|
|
|
{props.method === 'balance' ? (
|
2025-04-23 19:00:53 +08:00
|
|
|
|
profile && (
|
2025-04-16 18:51:17 +08:00
|
|
|
|
<div className="flex flex-col gap-4">
|
|
|
|
|
|
<div className="flex flex-col gap-1">
|
|
|
|
|
|
<div className="flex justify-between items-center">
|
|
|
|
|
|
<span className="text-weak text-sm">账户余额</span>
|
2025-06-07 11:49:57 +08:00
|
|
|
|
<span className="text-lg">
|
|
|
|
|
|
{profile.balance}
|
|
|
|
|
|
元
|
|
|
|
|
|
</span>
|
2025-04-16 18:51:17 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<div className="flex justify-between items-center">
|
|
|
|
|
|
<span className="text-weak text-sm">支付金额</span>
|
2025-06-07 11:49:57 +08:00
|
|
|
|
<span className="text-lg text-accent">
|
|
|
|
|
|
-
|
|
|
|
|
|
{props.amount}
|
|
|
|
|
|
元
|
|
|
|
|
|
</span>
|
2025-04-16 18:51:17 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<hr className="my-2"/>
|
|
|
|
|
|
<div className="flex justify-between items-center">
|
|
|
|
|
|
<span className="text-weak text-sm">支付后余额</span>
|
2025-05-22 14:59:22 +08:00
|
|
|
|
<span className={`text-lg ${balanceEnough ? 'text-done' : `text-fail`}`}>
|
2025-06-07 11:49:57 +08:00
|
|
|
|
{(profile.balance - Number(props.amount)).toFixed(2)}
|
|
|
|
|
|
元
|
2025-04-16 18:51:17 +08:00
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-05-22 14:59:22 +08:00
|
|
|
|
{balanceEnough ? (
|
2025-06-16 11:27:34 +08:00
|
|
|
|
<Alert variant="done">
|
|
|
|
|
|
<AlertTitle>
|
2025-05-22 14:59:22 +08:00
|
|
|
|
检查无误后,点击确认支付按钮完成支付
|
2025-06-16 11:27:34 +08:00
|
|
|
|
</AlertTitle>
|
2025-04-16 18:51:17 +08:00
|
|
|
|
</Alert>
|
2025-05-22 14:59:22 +08:00
|
|
|
|
) : (
|
|
|
|
|
|
<Alert variant="fail">
|
2025-06-16 11:27:34 +08:00
|
|
|
|
<AlertTitle>
|
2025-05-22 14:59:22 +08:00
|
|
|
|
余额不足,请先充值或选择其他支付方式
|
2025-06-16 11:27:34 +08:00
|
|
|
|
</AlertTitle>
|
2025-04-16 18:51:17 +08:00
|
|
|
|
</Alert>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<div className="flex flex-col items-center gap-4">
|
|
|
|
|
|
<div className="flex flex-col items-center gap-3">
|
2025-05-09 16:37:05 +08:00
|
|
|
|
<div className="bg-gray-100 size-50 flex items-center justify-center">
|
2025-04-16 18:51:17 +08:00
|
|
|
|
{payInfo ? (
|
2025-04-18 16:22:50 +08:00
|
|
|
|
props.method === 'alipay'
|
2025-05-19 11:04:40 +08:00
|
|
|
|
? <iframe src={payInfo.pay_url} className="w-full h-full"/>
|
2025-04-18 16:22:50 +08:00
|
|
|
|
: <canvas ref={canvas} className="w-full h-full"/>
|
2025-04-16 18:51:17 +08:00
|
|
|
|
) : (
|
2025-06-07 11:49:57 +08:00
|
|
|
|
<Loader size={40} className="animate-spin text-weak"/>
|
2025-04-16 18:51:17 +08:00
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<p className="text-sm text-gray-600 text-center">
|
2025-06-07 11:49:57 +08:00
|
|
|
|
请使用
|
|
|
|
|
|
{props.method === 'alipay' ? '支付宝' : '微信'}
|
|
|
|
|
|
扫码支付
|
2025-04-16 18:51:17 +08:00
|
|
|
|
</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="text-center space-y-1">
|
2025-06-07 11:49:57 +08:00
|
|
|
|
<p className="font-medium">
|
|
|
|
|
|
支付金额:
|
|
|
|
|
|
<span className="text-accent">
|
|
|
|
|
|
{props.amount}
|
|
|
|
|
|
元
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</p>
|
|
|
|
|
|
<p className="text-xs text-gray-500">
|
|
|
|
|
|
订单号:
|
|
|
|
|
|
{payInfo?.trade_no || '创建订单中...'}
|
|
|
|
|
|
</p>
|
2025-04-16 18:51:17 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="button"
|
2025-05-22 14:59:22 +08:00
|
|
|
|
disabled={props.method === 'balance' && !!profile && !balanceEnough}
|
2025-04-16 18:51:17 +08:00
|
|
|
|
onClick={onSubmit}
|
|
|
|
|
|
>
|
|
|
|
|
|
{props.method === 'balance' ? '确认支付' : '已完成支付'}
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
theme="outline"
|
|
|
|
|
|
onClick={() => setOpen(false)}
|
|
|
|
|
|
>
|
|
|
|
|
|
取消
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
|
</DialogContent>
|
|
|
|
|
|
</Dialog>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|