PayFast provider, Pay online page, amount checks on payments
- PayFast (custom integration): signed form (PHP-style encoding, checked against a real sandbox), ITN checks (signature over all posted fields, PayFast host, VALID from /eng/query/validate, amount vs checkout). - ProviderCheckout records every checkout (amount, metadata, request); webhooks match payments to it and hold a different amount for review (ProviderTransaction.note). - /pay/<id>: payers choose a provider when several are on, and the browser posts PayFast's form from there, so payment links work for every provider. - Site settings → Payments: PayFast card (merchant ID/key, encrypted passphrase, sandbox/live) while PayFast is enabled. - Payer-facing wording no longer names Yoco; legal pages name both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
"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 { Wallet } from "lucide-react";
|
||||
import { errorMessage } from "@/lib/utils";
|
||||
|
||||
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 apiFetch<PayFastConfig>("/api/payfast/config", { authToken: token });
|
||||
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 apiFetch("/api/payfast/config", {
|
||||
method: "PUT", 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 removePassphrase = async () => {
|
||||
if (!token || !window.confirm("Remove the passphrase? Only do this if it's also removed on your PayFast dashboard.")) return;
|
||||
try {
|
||||
await apiFetch("/api/payfast/config", { method: "PUT", 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>
|
||||
<button type="submit" className={primaryBtn} disabled={saving}>{saving ? "Saving…" : "Save PayFast settings"}</button>
|
||||
</form>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { apiFetch } from "@/lib/api";
|
||||
import { useDismissingState } from "@/hooks/useDismissingState";
|
||||
import { errorMessage } from "@/lib/utils";
|
||||
import { YocoTab } from "@/components/admin/YocoTab";
|
||||
import { PayFastSettings } from "@/components/admin/PayFastSettings";
|
||||
|
||||
type ProviderSetting = { id: string; label: string; enabled: boolean; configured: boolean };
|
||||
|
||||
@@ -119,6 +120,11 @@ export function PaymentsTab({ active }: { active: boolean }) {
|
||||
<YocoTab active={active} onChanged={load} />
|
||||
</div>
|
||||
)}
|
||||
{providers?.some(p => p.id === "payfast" && p.enabled) && (
|
||||
<div className="border-t pt-5">
|
||||
<PayFastSettings active={active} onChanged={load} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user