"use client"; import React, { useCallback, useEffect, useState } from "react"; import { useAuth } from "@/hooks/useAuth"; import { useDismissingState } from "@/hooks/useDismissingState"; import { Wallet } from "lucide-react"; import { errorMessage } from "@/lib/utils"; import { apiV2Data } from "@/lib/apiV2"; interface PayFastConfig { merchantId: string; merchantKey: string; passphraseMasked: string; hasPassphrase: boolean; mode: "sandbox" | "live"; notifyUrl: string; notifyUrlProblem: string | null; configured: 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"; /** * PayFast settings (Site settings → Payments, while PayFast is enabled): merchant ID and key, * the passphrase that signs payments (stored encrypted), and sandbox or live. * onChanged: called after a save, so the Payments tab can refresh whether PayFast is set up. */ export function PayFastSettings({ active, onChanged }: { active: boolean; onChanged?: () => void }) { const { token } = useAuth(); const [cfg, setCfg] = useState(null); const [merchantId, setMerchantId] = useState(""); const [merchantKey, setMerchantKey] = useState(""); const [passphrase, setPassphrase] = useState(""); const [mode, setMode] = useState<"sandbox" | "live">("sandbox"); 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 c = await (apiV2Data("get", "/payments/payfast/config", { authToken: token }) as Promise); setCfg(c); setMerchantId(c.merchantId); setMerchantKey(c.merchantKey); setMode(c.mode); setPassphrase(""); } catch (e) { setMessage({ type: "err", text: errorMessage(e) || "Failed to load PayFast settings" }); } }, [token, setMessage]); useEffect(() => { if (active) load(); }, [active, load]); const save = async (e: React.FormEvent) => { e.preventDefault(); if (!token) return; setSaving(true); try { await apiV2Data("put", "/payments/payfast/config", { authToken: token, // An empty passphrase box keeps the saved one. body: { merchantId, merchantKey, mode, ...(passphrase ? { passphrase } : {}) }, }); setMessage({ type: "ok", text: "PayFast settings saved." }); await load(); onChanged?.(); } catch (err) { setMessage({ type: "err", text: errorMessage(err) || "Could not save" }); } finally { setSaving(false); } }; const [testing, setTesting] = useState(false); // Checks the saved merchant ID and passphrase with PayFast (a signed API ping). const test = async () => { if (!token) return; setTesting(true); try { const res = await (apiV2Data("post", "/payments/payfast/test", { authToken: token, body: {} }) as Promise<{ message: string }>); setMessage({ type: "ok", text: res.message }); } catch (err) { setMessage({ type: "err", text: errorMessage(err) || "PayFast didn't accept the settings" }); } finally { setTesting(false); } }; const removePassphrase = async () => { if (!token || !window.confirm("Remove the passphrase? Only do this if it's also removed on your PayFast dashboard.")) return; try { await apiV2Data("put", "/payments/payfast/config", { authToken: token, body: { passphrase: "" } }); setMessage({ type: "ok", text: "Passphrase removed." }); await load(); } catch (err) { setMessage({ type: "err", text: errorMessage(err) || "Could not remove it" }); } }; return (

PayFast payments

Card, instant EFT and more through PayFast. Find these on your PayFast (or Sandbox) dashboard under Settings.

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

Loading…

) : (
setMerchantId(e.target.value)} inputMode="numeric" placeholder="From your PayFast dashboard" />
setMerchantKey(e.target.value)} placeholder="From your PayFast dashboard" />
setPassphrase(e.target.value)} placeholder={cfg.hasPassphrase ? `Saved (${cfg.passphraseMasked}) — type to replace` : "The passphrase set on your PayFast dashboard"} />

Must match the one on your PayFast dashboard exactly, or payments are refused. Stored encrypted. {cfg.hasPassphrase && <> }

Mode
{mode === "live" &&

Live takes real money. Use your live merchant ID, key and passphrase — the sandbox ones won't work.

}
Payment notifications go to {cfg.notifyUrl || "(not set)"}. {cfg.notifyUrlProblem &&

{cfg.notifyUrlProblem}

}
{cfg.configured && ( )}

Test connection checks the saved settings — save any changes first.

)}
); }