import { NextRequest, NextResponse } from "next/server"; import { getApiUrlFromRequest, getPaymentConfig } from "@/config/services.config"; function resolvePayApiUrl(request: NextRequest): string { const config = getPaymentConfig(); const hostHeader = request.headers.get("host") || request.headers.get("x-forwarded-host"); return (getApiUrlFromRequest(hostHeader) || config.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}/salon/order_status?order_id=${encodeURIComponent(orderId)}`; 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; 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) ); } return NextResponse.json({ ok: true, paid: false }); }