Payment provider interface: Yoco behind it, Site settings → Payments
- src/payments: provider registry and PaymentProvider interface (createCheckout, verifyWebhook/parseWebhook, getTransaction, refund, listTransactions as capabilities), Yoco as the first provider. All four direct Yoco checkout calls go through payments.createCheckout. - Webhook split into the provider part (signature, parsing) and shared processing; same URL (/api/webhooks/yoco), same behaviour. - YocoTransaction renamed in place to ProviderTransaction (+ provider, unique per provider + externalId); Payment.provider added and backfilled to "yoco". Payment.method stays the wallet type so cashups are unchanged. - Site settings → Payments: Enabled toggle and order per provider; Yoco's settings shown while it's enabled. R15 minimum for all online payments. - New endpoint names (/api/payments/checkout, /api/provider-transactions); the old /yoco-checkout and /yoco-transactions paths still work. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
"use client";
|
||||
|
||||
import React, { useCallback, useEffect, useState } from "react";
|
||||
import { ArrowDown, ArrowUp, Wallet } from "lucide-react";
|
||||
import { useAuth } from "@/hooks/useAuth";
|
||||
import { apiFetch } from "@/lib/api";
|
||||
import { useDismissingState } from "@/hooks/useDismissingState";
|
||||
import { errorMessage } from "@/lib/utils";
|
||||
import { YocoTab } from "@/components/admin/YocoTab";
|
||||
|
||||
type ProviderSetting = { id: string; label: string; enabled: boolean; configured: boolean };
|
||||
|
||||
/**
|
||||
* Site settings → Payments: which online payment providers people can use, in what order
|
||||
* (the first one is the default), and below that the settings of each enabled provider.
|
||||
*/
|
||||
export function PaymentsTab({ active }: { active: boolean }) {
|
||||
const { token } = useAuth();
|
||||
const [providers, setProviders] = useState<ProviderSetting[] | null>(null);
|
||||
const [minimum, setMinimum] = useState(15);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [message, setMessage] = useDismissingState<{ type: "ok" | "err"; text: string } | null>(null);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
if (!token) return;
|
||||
try {
|
||||
const res = await apiFetch<{ providers: ProviderSetting[]; minimumAmount: number }>("/api/payments/providers/settings", { authToken: token });
|
||||
setProviders(res.providers);
|
||||
setMinimum(res.minimumAmount);
|
||||
} catch (e) {
|
||||
setMessage({ type: "err", text: errorMessage(e) || "Failed to load payment providers" });
|
||||
}
|
||||
}, [token, setMessage]);
|
||||
|
||||
useEffect(() => { if (active) load(); }, [active, load]);
|
||||
|
||||
const save = async (next: ProviderSetting[]) => {
|
||||
if (!token) return;
|
||||
setProviders(next);
|
||||
setSaving(true);
|
||||
try {
|
||||
const res = await apiFetch<{ message: string; providers: ProviderSetting[] }>("/api/payments/providers/settings", {
|
||||
method: "PUT", authToken: token, body: { providers: next.map(p => ({ id: p.id, enabled: p.enabled })) },
|
||||
});
|
||||
setProviders(res.providers);
|
||||
setMessage({ type: "ok", text: res.message });
|
||||
} catch (e) {
|
||||
setMessage({ type: "err", text: errorMessage(e) || "Could not save" });
|
||||
load();
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const move = (i: number, by: number) => {
|
||||
if (!providers) return;
|
||||
const next = [...providers];
|
||||
const [p] = next.splice(i, 1);
|
||||
next.splice(i + by, 0, p);
|
||||
save(next);
|
||||
};
|
||||
const toggle = (i: number) => providers && save(providers.map((p, j) => (j === i ? { ...p, enabled: !p.enabled } : p)));
|
||||
|
||||
const usable = (providers || []).filter(p => p.enabled && p.configured);
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="w-9 h-9 rounded-lg bg-brand-50 flex items-center justify-center shrink-0">
|
||||
<Wallet className="w-4 h-4 text-brand-600" />
|
||||
</div>
|
||||
<div>
|
||||
<h2 className="text-lg font-semibold text-gray-900">Payments</h2>
|
||||
<p className="text-xs text-gray-500">Which online payment providers people can use. The first one in the list is used by default.</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{message && (
|
||||
<div className={`text-sm p-2.5 rounded-lg ${message.type === "ok" ? "bg-green-50 text-green-700" : "bg-red-50 text-red-700"}`}>
|
||||
{message.text}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!providers ? (
|
||||
<p className="text-sm text-gray-400">Loading…</p>
|
||||
) : (
|
||||
<div className="border rounded-lg divide-y" role="list" aria-label="Payment providers">
|
||||
{providers.map((p, i) => (
|
||||
<div key={p.id} role="listitem" className="flex items-center gap-3 px-3 py-2.5">
|
||||
<label className="flex items-center gap-2 flex-1 min-w-0">
|
||||
<input type="checkbox" checked={p.enabled} disabled={saving} onChange={() => toggle(i)} aria-label={`Enable ${p.label}`} />
|
||||
<span className="text-sm font-medium text-gray-900">{p.label}</span>
|
||||
{i === 0 && p.enabled && <span className="text-[11px] px-1.5 py-0.5 rounded bg-brand-50 text-brand-700">Default</span>}
|
||||
{p.enabled && !p.configured && <span className="text-[11px] px-1.5 py-0.5 rounded bg-amber-100 text-amber-800">Not set up</span>}
|
||||
</label>
|
||||
<div className="flex items-center gap-1">
|
||||
<button type="button" className="p-1 rounded hover:bg-gray-100 disabled:opacity-30" disabled={saving || i === 0} onClick={() => move(i, -1)} aria-label={`Move ${p.label} up`}><ArrowUp className="w-4 h-4" /></button>
|
||||
<button type="button" className="p-1 rounded hover:bg-gray-100 disabled:opacity-30" disabled={saving || i === providers.length - 1} onClick={() => move(i, 1)} aria-label={`Move ${p.label} down`}><ArrowDown className="w-4 h-4" /></button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{providers && usable.length === 0 && (
|
||||
<p className="text-sm text-amber-800 bg-amber-50 border border-amber-200 rounded-lg p-2.5">
|
||||
Online payments are off: no provider is both enabled and set up. People can still pay at the door.
|
||||
</p>
|
||||
)}
|
||||
<p className="text-xs text-gray-500">
|
||||
Online payments must be at least R{minimum}. Payments are recorded at the full amount paid; provider fees aren't deducted or added to the payer's total — deal with them at cashup.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Each provider's own settings, only while it's enabled */}
|
||||
{providers?.some(p => p.id === "yoco" && p.enabled) && (
|
||||
<div className="border-t pt-5">
|
||||
<YocoTab active={active} onChanged={load} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -73,7 +73,8 @@ function Card({ icon: Icon, title, subtitle, children }: {
|
||||
);
|
||||
}
|
||||
|
||||
export function YocoTab({ active }: { active: boolean }) {
|
||||
// onChanged: called after a save, so the Payments tab can refresh whether Yoco is set up.
|
||||
export function YocoTab({ active, onChanged }: { active: boolean; onChanged?: () => void }) {
|
||||
const { token } = useAuth();
|
||||
const [cfg, setCfg] = useState<YocoConfig | null>(null);
|
||||
const [hooks, setHooks] = useState<YocoWebhook[]>([]);
|
||||
@@ -131,6 +132,7 @@ export function YocoTab({ active }: { active: boolean }) {
|
||||
const text = await fn();
|
||||
setMessage({ type: "ok", text });
|
||||
await load();
|
||||
onChanged?.();
|
||||
} catch (e) {
|
||||
setMessage({ type: "err", text: errorMessage(e) || "Something went wrong" });
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user