"use client"; import React, { useCallback, useEffect, useState } from "react"; import { useAuth } from "@/hooks/useAuth"; import { apiFetch } from "@/lib/api"; import { useDismissingState } from "@/hooks/useDismissingState"; import { Copy, CreditCard, KeyRound, Trash2, Webhook } from "lucide-react"; import { errorMessage } from "@/lib/utils"; type Source = "db" | "env" | "none"; interface YocoConfig { secretKeyMasked: string; secretKeySource: Source; mode: "test" | "live" | "unknown"; webhookSecretMasked: string; webhookSecretSource: Source; webhookId: string; webhookUrl: string; configured: boolean; } interface YocoWebhook { id: string; name: string; url: string; mode: "test" | "live"; isActive: boolean; pointsHere: boolean; } const inputCls = "w-full border rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-brand-400"; const primaryBtn = "px-3 py-1.5 text-sm rounded-lg bg-brand-600 hover:bg-brand-700 disabled:opacity-50 text-white font-medium"; const secondaryBtn = "px-3 py-1.5 text-sm rounded-lg bg-gray-100 hover:bg-gray-200 disabled:opacity-50"; function ModeBadge({ mode }: { mode: string }) { if (mode !== "test" && mode !== "live") return null; return ( {mode === "live" ? "Live" : "Test"} ); } function SourceNote({ source }: { source: Source }) { if (source === "env") { return

From the server's .env file. Save it here to store it in the database instead.

; } if (source === "none") return

Not set.

; return null; } function Card({ icon: Icon, title, subtitle, children }: { icon: React.ElementType; title: string; subtitle: string; children: React.ReactNode; }) { return (

{title}

{subtitle}

{children}
); } // 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(null); const [hooks, setHooks] = useState([]); const [hooksError, setHooksError] = useState(null); const [loading, setLoading] = useState(true); const [busy, setBusy] = useState(null); const [message, setMessage] = useDismissingState<{ type: "ok" | "err"; text: string } | null>(null); // API key editor const [editingKey, setEditingKey] = useState(false); const [keyInput, setKeyInput] = useState(""); // Webhook secret: generate (register at Yoco) or enter by hand const [secretMode, setSecretMode] = useState<"generate" | "enter">("generate"); const [hookName, setHookName] = useState("hope-events"); const [hookUrl, setHookUrl] = useState(""); const [secretInput, setSecretInput] = useState(""); const [confirmDelete, setConfirmDelete] = useState(null); const loadHooks = useCallback(async () => { if (!token) return; try { const res = await apiFetch<{ webhooks: YocoWebhook[] }>("/api/yoco/webhooks", { authToken: token }); setHooks(res.webhooks || []); setHooksError(null); } catch (e) { setHooks([]); setHooksError(errorMessage(e) || "Could not load webhooks from Yoco"); } }, [token]); const load = useCallback(async () => { if (!token) return; setLoading(true); try { const c = await apiFetch("/api/yoco/config", { authToken: token }); setCfg(c); setHookUrl((prev) => prev || c.webhookUrl); if (c.secretKeySource !== "none") await loadHooks(); else { setHooks([]); setHooksError(null); } } catch (e) { setMessage({ type: "err", text: errorMessage(e) || "Failed to load Yoco settings" }); } finally { setLoading(false); } }, [token, loadHooks, setMessage]); useEffect(() => { if (active) load(); }, [active, load]); const run = async (label: string, fn: () => Promise) => { setBusy(label); setMessage(null); try { const text = await fn(); setMessage({ type: "ok", text }); await load(); onChanged?.(); } catch (e) { setMessage({ type: "err", text: errorMessage(e) || "Something went wrong" }); } finally { setBusy(null); } }; const saveKey = (value: string) => run("key", async () => { await apiFetch("/api/yoco/config", { method: "PUT", authToken: token!, body: { secretKey: value } }); setEditingKey(false); setKeyInput(""); return value ? "Secret key saved." : "Secret key removed."; }); const saveSecret = (value: string) => run("secret", async () => { await apiFetch("/api/yoco/config", { method: "PUT", authToken: token!, body: { webhookSecret: value } }); setSecretInput(""); return value ? "Webhook secret saved." : "Webhook secret removed."; }); const generate = () => run("generate", async () => { const res = await apiFetch<{ message: string }>("/api/yoco/webhooks", { method: "POST", authToken: token!, body: { name: hookName.trim(), url: hookUrl.trim() }, }); return res.message; }); const removeHook = (id: string) => run(`delete:${id}`, async () => { const res = await apiFetch<{ message: string }>(`/api/yoco/webhooks/${encodeURIComponent(id)}`, { method: "DELETE", authToken: token!, }); setConfirmDelete(null); return res.message; }); const copy = (text: string) => { navigator.clipboard?.writeText(text).then( () => setMessage({ type: "ok", text: "Copied to clipboard." }), () => setMessage({ type: "err", text: "Could not copy." }), ); }; const hasKey = !!cfg && cfg.secretKeySource !== "none"; return (

Yoco payments

Online card payments. Secrets are stored encrypted and never shown in full.

{message && (
{message.text}
)} {loading && !cfg ? (

Loading…

) : cfg && ( <> {/* ── API key ─────────────────────────────────────────────────── */}
{cfg.secretKeyMasked || "—"} {!editingKey && ( )}
{editingKey && (
setKeyInput(e.target.value)} />
{cfg.secretKeySource === "db" && ( )}

The key is checked with Yoco before it is saved.

)}
{/* ── Webhook secret ──────────────────────────────────────────── */}
{cfg.webhookSecretMasked || "—"} {cfg.webhookSecretSource === "db" && ( )}
{cfg.webhookUrl || "—"} {cfg.webhookUrl && ( )}
{(["generate", "enter"] as const).map((m) => ( ))}
{secretMode === "generate" ? ( hasKey ? (

Registers a webhook with Yoco and saves the secret it returns. Yoco only shows the secret once, so it is stored straight away.

setHookName(e.target.value)} placeholder="Name" /> setHookUrl(e.target.value)} placeholder="https://…/api/webhooks/yoco" />
) : (

Add the secret key first.

) ) : (

Paste the secret of a webhook you registered yourself.

setSecretInput(e.target.value)} />
)}
{/* ── Registered webhooks ─────────────────────────────────────── */} {!hasKey ? (

Add the secret key to see the webhooks.

) : hooksError ? (

{hooksError}

) : hooks.length === 0 ? (

No webhooks registered.

) : (
    {hooks.map((h) => (
  • {h.name} {h.isActive && Active}
    {confirmDelete === h.id ? ( <> ) : ( )}

    {h.url}

    {!h.pointsHere &&

    This URL isn't this server's webhook endpoint.

    } {confirmDelete === h.id && h.isActive && (

    This is the active webhook. Deleting it also removes the stored secret, and payments won't be confirmed until a new one is set up.

    )}
  • ))}
)}
)}
); }