import { NextResponse } from 'next/server' import { FrappeError, METHODS, frappeCall } from '@/lib/lukenya-api' // Polled by /payment/return. The backend checks with Paystack itself before answering, // so the browser being sent back here never confirms a payment on its own. export async function GET(req: Request) { const reference = new URL(req.url).searchParams.get('reference') || '' if (!/^[A-Za-z0-9._-]{4,100}$/.test(reference)) { return NextResponse.json({ error: 'Missing or invalid payment reference.' }, { status: 400 }) } try { const result = await frappeCall>(METHODS.paymentStatus, { args: { reference } }) return NextResponse.json(result, { headers: { 'Cache-Control': 'no-store' } }) } catch (err) { const status = (err as FrappeError).status if (status === 404) return NextResponse.json({ error: 'We could not find this payment.' }, { status: 404 }) // The server re-checks with Paystack in the background; keep the runner on the waiting screen. return NextResponse.json({ status: 'Pending' }, { headers: { 'Cache-Control': 'no-store' } }) } }