import React, { useCallback, useEffect, useRef, useState } from "react" import PayPalTabs from "../_components/Tabs" import SectionCard from "../_components/SectionCard" import FieldRow from "../_components/FieldRow" import Toast, { ToastState } from "../_components/Toast" import { adminFetch } from "../_utils/adminFetch" type ButtonColor = "gold" | "blue" | "silver" | "black" | "white" type ButtonShape = "rect" | "pill" type ButtonWidth = "small" | "medium" | "large" | "responsive" type ButtonLabel = "paypal" | "checkout" | "buynow" | "pay" type PayPalSettingsForm = { enabled: boolean title: string description: string buttonColor: ButtonColor buttonShape: ButtonShape buttonWidth: ButtonWidth buttonHeight: number buttonLabel: ButtonLabel } const COLOR_OPTIONS: { value: ButtonColor; label: string }[] = [ { value: "gold", label: "Gold (Recommended)" }, { value: "blue", label: "Blue" }, { value: "silver", label: "Silver" }, { value: "black", label: "Black" }, { value: "white", label: "White" }, ] const SHAPE_OPTIONS: { value: ButtonShape; label: string }[] = [ { value: "rect", label: "Rect (Recommended)" }, { value: "pill", label: "Pill" }, ] const WIDTH_OPTIONS: { value: ButtonWidth; label: string }[] = [ { value: "small", label: "Small" }, { value: "medium", label: "Medium" }, { value: "large", label: "Large" }, { value: "responsive", label: "Responsive" }, ] const HEIGHT_OPTIONS: number[] = [32, 36, 40, 44, 48, 52, 56] const LABEL_OPTIONS: { value: ButtonLabel; label: string }[] = [ { value: "paypal", label: "PayPal" }, { value: "checkout", label: "Checkout" }, { value: "buynow", label: "Buy Now" }, { value: "pay", label: "Pay" }, ] /** Settings → PayPal → PayPal Settings (enable toggle, title, Smart Button styling). */ export default function PayPalSettingsTab() { const [form, setForm] = useState({ enabled: true, title: "PayPal", description: "Pay via PayPal; you can pay with your credit card if you don't have a PayPal account", buttonColor: "gold", buttonShape: "rect", buttonWidth: "medium", buttonHeight: 48, buttonLabel: "paypal", }) const [loading, setLoading] = useState(false) const [saving, setSaving] = useState(false) const [toast, setToast] = useState(null) const didInit = useRef(false) const dismissToast = useCallback(() => setToast(null), []) useEffect(() => { if (didInit.current) return didInit.current = true ;(async () => { try { setLoading(true) const json = await adminFetch<{ data?: { paypal_settings?: PayPalSettingsForm } paypal_settings?: PayPalSettingsForm }>("/admin/paypal/settings") const payload = (json?.data ?? json) as any const saved = payload?.paypal_settings if (saved && typeof saved === "object") { setForm((prev) => ({ ...prev, ...saved, })) } } catch { // Keep the defaults if settings can't be loaded; saving still works. } finally { setLoading(false) } })() }, []) async function onSave() { try { setSaving(true) const cleaned = { ...form } const json = await adminFetch<{ data?: { paypal_settings?: PayPalSettingsForm } paypal_settings?: PayPalSettingsForm }>("/admin/paypal/settings", { method: "POST", body: { paypal_settings: cleaned as unknown as Record, }, }) const payload = (json?.data ?? json) as any const saved = payload?.paypal_settings if (saved && typeof saved === "object") { setForm((prev) => ({ ...prev, ...saved, })) } setToast({ kind: "success", message: "Settings saved" }) } catch (e: unknown) { setToast({ kind: "error", message: (e instanceof Error ? e.message : "") || "Failed to save settings.", }) } finally { setSaving(false) } } return (

PayPal Gateway By Easy Payment

{loading ? ( Loading… ) : null}
} >
setForm((p) => ({ ...p, title: e.target.value })) } className="w-full rounded-md border border-ui-border-base bg-ui-bg-base px-3 py-2 text-sm text-ui-fg-base outline-none focus:ring-2 focus:ring-ui-border-interactive" placeholder="PayPal" />
) }