79 lines
2.5 KiB
TypeScript
79 lines
2.5 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getApiUrlFromRequest, getPaymentConfig } from "@/config/services.config";
|
|
import { queryXorPayOrderStatus } from "@/app/lib/xorpayStatus";
|
|
import { queryZPayOrderStatus } from "@/app/lib/zpayStatus";
|
|
|
|
function resolvePayApiUrl(request: NextRequest): string {
|
|
const payConfig = getPaymentConfig();
|
|
const hostHeader = request.headers.get("host") || request.headers.get("x-forwarded-host");
|
|
return (
|
|
(hostHeader ? getApiUrlFromRequest(hostHeader) : null) ||
|
|
payConfig.apiUrl
|
|
).replace(/\/$/, "");
|
|
}
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const orderId = request.nextUrl.searchParams.get("order_id");
|
|
if (!orderId) {
|
|
return NextResponse.json({ ok: false, error: "missing order_id" }, { status: 400 });
|
|
}
|
|
|
|
const hostHeader = request.headers.get("host") || request.headers.get("x-forwarded-host");
|
|
const apiUrl = resolvePayApiUrl(request);
|
|
const url = `${apiUrl}/nomadvip/order_status?order_id=${encodeURIComponent(orderId)}`;
|
|
console.log("[pay/status] request order_id=%s host=%s url=%s", orderId, hostHeader, url);
|
|
|
|
try {
|
|
const controller = new AbortController();
|
|
const timeout = setTimeout(() => controller.abort(), 3000);
|
|
const res = await fetch(url, {
|
|
cache: "no-store",
|
|
signal: controller.signal,
|
|
}).finally(() => clearTimeout(timeout));
|
|
const data = await res.json().catch(() => ({}));
|
|
const paid = !!data?.paid;
|
|
console.log(
|
|
"[pay/status] result order_id=%s paid=%s status=%s",
|
|
orderId,
|
|
paid,
|
|
res.status
|
|
);
|
|
if (paid) {
|
|
return NextResponse.json({ ok: true, paid: true });
|
|
}
|
|
} catch (error) {
|
|
console.log(
|
|
"[pay/status] error order_id=%s error=%s",
|
|
orderId,
|
|
error instanceof Error ? error.message : String(error)
|
|
);
|
|
}
|
|
|
|
const xorpay = await queryXorPayOrderStatus(orderId, 5000);
|
|
console.log(
|
|
"[pay/status] xorpay fallback order_id=%s paid=%s status=%s error=%s url=%s",
|
|
orderId,
|
|
!!xorpay?.paid,
|
|
xorpay?.status || "",
|
|
xorpay?.error || "",
|
|
xorpay?.url || ""
|
|
);
|
|
if (xorpay?.paid) {
|
|
return NextResponse.json({ ok: true, paid: true });
|
|
}
|
|
|
|
const zpay = await queryZPayOrderStatus(orderId, 5000);
|
|
console.log(
|
|
"[pay/status] zpay fallback order_id=%s paid=%s status=%s error=%s url=%s",
|
|
orderId,
|
|
!!zpay?.paid,
|
|
zpay?.status || "",
|
|
zpay?.error || "",
|
|
zpay?.url || ""
|
|
);
|
|
if (zpay?.paid) {
|
|
return NextResponse.json({ ok: true, paid: true });
|
|
}
|
|
return NextResponse.json({ ok: true, paid: false });
|
|
}
|