80 lines
2.0 KiB
TypeScript
80 lines
2.0 KiB
TypeScript
/**
|
|
* 查询订单支付状态 API
|
|
* GET /api/payment/status/{orderSn}
|
|
* 从数据库 orders 表查询真实订单状态
|
|
*/
|
|
|
|
import { type NextRequest, NextResponse } from "next/server"
|
|
import { query } from "@/lib/db"
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ orderSn: string }> }
|
|
) {
|
|
try {
|
|
const { orderSn } = await params
|
|
|
|
if (!orderSn) {
|
|
return NextResponse.json(
|
|
{ code: 400, message: "缺少订单号", data: null },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const rows = await query(
|
|
"SELECT order_sn, status, amount, pay_time, transaction_id, product_type FROM orders WHERE order_sn = ?",
|
|
[orderSn]
|
|
) as any[]
|
|
|
|
if (!rows || rows.length === 0) {
|
|
return NextResponse.json({
|
|
code: 200,
|
|
message: "success",
|
|
data: {
|
|
orderSn,
|
|
status: "created",
|
|
paidAmount: null,
|
|
paidAt: null,
|
|
paymentMethod: null,
|
|
tradeSn: null,
|
|
},
|
|
})
|
|
}
|
|
|
|
const order = rows[0]
|
|
const statusMap: Record<string, string> = {
|
|
created: "created",
|
|
pending: "paying",
|
|
paid: "paid",
|
|
cancelled: "closed",
|
|
refunded: "refunded",
|
|
expired: "closed",
|
|
}
|
|
const frontStatus = statusMap[order.status] || order.status
|
|
|
|
return NextResponse.json({
|
|
code: 200,
|
|
message: "success",
|
|
data: {
|
|
orderSn: order.order_sn,
|
|
status: frontStatus,
|
|
paidAmount: order.status === "paid" ? Number(order.amount) : null,
|
|
paidAt: order.pay_time || null,
|
|
paymentMethod: "wechat",
|
|
tradeSn: order.transaction_id || null,
|
|
productType: order.product_type,
|
|
},
|
|
})
|
|
} catch (error) {
|
|
console.error("[Payment] Query status error:", error)
|
|
return NextResponse.json(
|
|
{
|
|
code: 500,
|
|
message: error instanceof Error ? error.message : "服务器错误",
|
|
data: null,
|
|
},
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|