Files
crosscode-platform/apps/events/frontend/src/components/payments/WaitingForPayment.tsx
T
joshuaandClaude Opus 5.5 2064ba2e42 Payment status checks for notifications that never arrive
- src/payments/status.js: open checkouts, checkCheckouts and the
  payment-status job (every 15 min, checkouts 10 min - 3 days old). Found
  payments are settled exactly as their webhook would be (settlePayment,
  extracted from the webhook handler: amount check, record, emails).
- PayFast: signed API client (alphabetical signature, testing=true in
  sandbox), findPayments from the transaction history CSV matched on
  M Payment ID, and testConnection (signed /ping). Verified against a
  real sandbox account.
- Yoco documents no checkout lookup, so its checkouts are "can't check".
- Payments -> Reconcile: "Waiting for payment" with Check status /
  Check all; PayFast card: Test connection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 15:28:28 +02:00

138 lines
6.1 KiB
TypeScript

"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<CheckResult["status"], string> = {
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<OpenCheckout[] | null>(null);
const [results, setResults] = useState<Record<string, CheckResult>>({});
const [busy, setBusy] = useState<string | null>(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<string, CheckResult>; 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 (
<div className="border rounded-xl p-4 bg-white shadow-sm">
<div className="flex items-center justify-between gap-2 mb-1">
<div className="text-lg font-semibold">Waiting for payment</div>
<div className="flex gap-2">
{checkable && (
<button className="px-3 py-1.5 text-sm rounded bg-brand-600 hover:bg-brand-700 text-white disabled:opacity-50" disabled={!!busy} onClick={() => check()}>
{busy === "all" ? "Checking…" : "Check all"}
</button>
)}
<button className="px-3 py-1.5 text-sm rounded bg-gray-100 hover:bg-gray-200 text-gray-800" onClick={() => { setResults({}); load(); }}>Refresh</button>
</div>
</div>
<p className="text-xs text-gray-500 mb-3">
Online payments started in the last 14 days that haven&apos;t been confirmed. If the provider&apos;s notification didn&apos;t arrive, check the status here — PayFast payments are also checked automatically every 15 minutes.
</p>
{message && <div className={`text-sm mb-3 p-2 rounded ${message.ok ? "bg-green-50 text-green-800" : "bg-red-50 text-red-700"}`}>{message.text}</div>}
{!list ? (
<div className="text-sm text-gray-500">Loading…</div>
) : list.length === 0 ? (
<div className="text-sm text-gray-500">Nothing waiting.</div>
) : (
<div className="overflow-x-auto w-full">
<table className="min-w-full text-sm">
<thead>
<tr className="text-left border-b">
<th className="py-2 pr-3">Started</th>
<th className="py-2 pr-3">Payer</th>
<th className="py-2 pr-3">For</th>
<th className="py-2 pr-3">Amount</th>
<th className="py-2 pr-3">Provider</th>
<th className="py-2">Status</th>
</tr>
</thead>
<tbody>
{list.map(c => {
const result = results[c.id];
return (
<tr key={c.id} className="border-b align-top">
<td className="py-2 pr-3 whitespace-nowrap">{new Date(c.createdAt).toLocaleString()}</td>
<td className="py-2 pr-3">{c.payer || "—"}</td>
<td className="py-2 pr-3">{c.description || (c.type === "donation" ? "Donation" : "—")}</td>
<td className="py-2 pr-3 whitespace-nowrap">R {c.amount.toFixed(2)}</td>
<td className="py-2 pr-3">{c.providerLabel}</td>
<td className="py-2">
{result ? (
<span className={result.status === "recorded" || result.status === "paid" ? "text-green-700" : result.status === "needs_review" ? "text-amber-800" : "text-gray-600"}>
{RESULT_TEXT[result.status]}
</span>
) : c.canCheck ? (
<button className="px-2 py-1 rounded border border-brand-300 text-brand-700 hover:bg-brand-50 disabled:opacity-50" disabled={!!busy} onClick={() => check([c.id])}>
{busy === c.id ? "Checking…" : "Check status"}
</button>
) : (
<span className="text-xs text-gray-500">Can&apos;t check automatically — look in the {c.providerLabel} portal, then use Reconcile</span>
)}
</td>
</tr>
);
})}
</tbody>
</table>
</div>
)}
</div>
);
}