Archived
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMdg2GYqbnKZmPaX1ot
166 lines
7.8 KiB
TypeScript
166 lines
7.8 KiB
TypeScript
"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<PayFastConfig | null>(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<PayFastConfig>);
|
|
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 (
|
|
<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">PayFast payments</h2>
|
|
<p className="text-xs text-gray-500">Card, instant EFT and more through PayFast. Find these on your PayFast (or Sandbox) dashboard under Settings.</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>
|
|
)}
|
|
|
|
{!cfg ? (
|
|
<p className="text-sm text-gray-400">Loading…</p>
|
|
) : (
|
|
<form onSubmit={save} className="border rounded-lg p-4 space-y-3">
|
|
<div className="grid sm:grid-cols-2 gap-3">
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 mb-1" htmlFor="pf-merchant-id">Merchant ID</label>
|
|
<input id="pf-merchant-id" className={inputCls} value={merchantId} onChange={e => setMerchantId(e.target.value)} inputMode="numeric" placeholder="From your PayFast dashboard" />
|
|
</div>
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 mb-1" htmlFor="pf-merchant-key">Merchant key</label>
|
|
<input id="pf-merchant-key" className={inputCls} value={merchantKey} onChange={e => setMerchantKey(e.target.value)} placeholder="From your PayFast dashboard" />
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<label className="block text-xs font-medium text-gray-700 mb-1" htmlFor="pf-passphrase">Salt passphrase</label>
|
|
<input id="pf-passphrase" type="password" autoComplete="off" className={inputCls} value={passphrase} onChange={e => setPassphrase(e.target.value)}
|
|
placeholder={cfg.hasPassphrase ? `Saved (${cfg.passphraseMasked}) — type to replace` : "The passphrase set on your PayFast dashboard"} />
|
|
<p className="text-xs text-gray-500 mt-1">
|
|
Must match the one on your PayFast dashboard exactly, or payments are refused. Stored encrypted.
|
|
{cfg.hasPassphrase && <> <button type="button" className="underline" onClick={removePassphrase}>Remove</button></>}
|
|
</p>
|
|
</div>
|
|
<div>
|
|
<span className="block text-xs font-medium text-gray-700 mb-1">Mode</span>
|
|
<div className="flex gap-4 text-sm" role="radiogroup" aria-label="PayFast mode">
|
|
<label className="flex items-center gap-1.5"><input type="radio" name="pf-mode" checked={mode === "sandbox"} onChange={() => setMode("sandbox")} /> Sandbox (testing)</label>
|
|
<label className="flex items-center gap-1.5"><input type="radio" name="pf-mode" checked={mode === "live"} onChange={() => setMode("live")} /> Live</label>
|
|
</div>
|
|
{mode === "live" && <p className="text-xs text-amber-700 mt-1">Live takes real money. Use your live merchant ID, key and passphrase — the sandbox ones won't work.</p>}
|
|
</div>
|
|
<div className="text-xs text-gray-600">
|
|
Payment notifications go to <span className="font-mono break-all">{cfg.notifyUrl || "(not set)"}</span>.
|
|
{cfg.notifyUrlProblem && <p className="text-amber-700 mt-1">{cfg.notifyUrlProblem}</p>}
|
|
</div>
|
|
<div className="flex flex-wrap gap-2">
|
|
<button type="submit" className={primaryBtn} disabled={saving}>{saving ? "Saving…" : "Save PayFast settings"}</button>
|
|
{cfg.configured && (
|
|
<button type="button" className="px-3 py-1.5 text-sm rounded-lg bg-gray-100 hover:bg-gray-200 disabled:opacity-50" disabled={testing} onClick={test}>
|
|
{testing ? "Testing…" : "Test connection"}
|
|
</button>
|
|
)}
|
|
</div>
|
|
<p className="text-xs text-gray-500">Test connection checks the saved settings — save any changes first.</p>
|
|
</form>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|