"use client"; import React, { useCallback, useEffect, useState } from "react"; import { apiFetch } from "@/lib/api"; import { errorMessage } from "@/lib/utils"; type OpenCheckout = { id: string; provider: string; providerLabel: string; canCheck: boolean; amount: number; description: string; type: string | null; registrationId: string | null; payer: string | null; createdAt: string; }; type CheckResult = { status: "paid" | "recorded" | "needs_review" | "not_paid" | "cant_check"; note?: string }; const RESULT_TEXT: Record = { paid: "Already paid", recorded: "Payment found and recorded", needs_review: "Paid a different amount — see Unreconciled below", not_paid: "Not paid (yet)", cant_check: "Can't check — look in the provider's portal", }; /** * Online checkouts from the last 14 days with no payment yet (Payments → Reconcile). For when a * provider's notification never arrived: "Check status" asks the provider and records any * payment it received. A background job does the same every 15 minutes. */ export function WaitingForPayment({ token, onRecorded }: { token: string | null; onRecorded: () => void }) { const [list, setList] = useState(null); const [results, setResults] = useState>({}); const [busy, setBusy] = useState(null); const [message, setMessage] = useState<{ ok: boolean; text: string } | null>(null); const load = useCallback(async () => { if (!token) return; try { const res = await apiFetch<{ checkouts: OpenCheckout[] }>("/api/payments/checkouts/open", { authToken: token }); setList(res.checkouts); } catch (e) { setMessage({ ok: false, text: errorMessage(e) || "Couldn't load waiting payments" }); } }, [token]); useEffect(() => { load(); }, [load]); const check = async (ids?: string[]) => { if (!token) return; setBusy(ids?.[0] || "all"); setMessage(null); try { const res = await apiFetch<{ results: Record; recorded: number; message: string }>("/api/payments/checkouts/check", { method: "POST", authToken: token, body: ids ? { ids } : {}, }); setResults(r => ({ ...r, ...res.results })); setMessage({ ok: true, text: res.message }); if (res.recorded > 0) onRecorded(); } catch (e) { setMessage({ ok: false, text: errorMessage(e) || "Couldn't check" }); } finally { setBusy(null); } }; const checkable = (list || []).some(c => c.canCheck); return (
Waiting for payment
{checkable && ( )}

Online payments started in the last 14 days that haven't been confirmed. If the provider's notification didn't arrive, check the status here — PayFast payments are also checked automatically every 15 minutes.

{message &&
{message.text}
} {!list ? (
Loading…
) : list.length === 0 ? (
Nothing waiting.
) : (
{list.map(c => { const result = results[c.id]; return ( ); })}
Started Payer For Amount Provider Status
{new Date(c.createdAt).toLocaleString()} {c.payer || "—"} {c.description || (c.type === "donation" ? "Donation" : "—")} R {c.amount.toFixed(2)} {c.providerLabel} {result ? ( {RESULT_TEXT[result.status]} ) : c.canCheck ? ( ) : ( Can't check automatically — look in the {c.providerLabel} portal, then use Reconcile )}
)}
); }