100 lines
3.2 KiB
TypeScript
100 lines
3.2 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
|
|
interface PaymentButtonProps {
|
|
planId: string;
|
|
planType: "setup" | "monthly";
|
|
label: string;
|
|
variant?: "primary" | "secondary";
|
|
}
|
|
|
|
export default function PaymentButton({ planId, planType, label, variant = "primary" }: PaymentButtonProps) {
|
|
const [loading, setLoading] = useState(false);
|
|
const [email, setEmail] = useState("");
|
|
const [showModal, setShowModal] = useState(false);
|
|
|
|
const handleCheckout = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setLoading(true);
|
|
|
|
try {
|
|
const response = await fetch("/api/stripe/checkout", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
planId,
|
|
planType,
|
|
email,
|
|
}),
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.url) {
|
|
window.location.href = data.url;
|
|
} else {
|
|
alert("Error initiating checkout. Please try again.");
|
|
}
|
|
} catch (error) {
|
|
console.error("Checkout error:", error);
|
|
alert("Error initiating checkout. Please try again.");
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const buttonClass = variant === "primary"
|
|
? "bg-brand-pink hover:bg-[#ff7bc0]"
|
|
: "border border-white/20 hover:bg-white/10";
|
|
|
|
return (
|
|
<>
|
|
<button
|
|
onClick={() => setShowModal(true)}
|
|
className={`w-full mt-6 py-2.5 rounded-lg font-semibold text-white transition ${buttonClass}`}
|
|
disabled={loading}
|
|
>
|
|
{loading ? "Processing..." : label}
|
|
</button>
|
|
|
|
{showModal && (
|
|
<div className="fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4">
|
|
<div className="bg-[#1a1625] border border-white/20 rounded-2xl p-6 max-w-md w-full">
|
|
<h3 className="text-xl font-bold text-white mb-4">Enter your email to continue</h3>
|
|
<p className="text-white/60 text-sm mb-4">
|
|
We'll send the payment link to your email.
|
|
</p>
|
|
<form onSubmit={handleCheckout}>
|
|
<input
|
|
type="email"
|
|
required
|
|
placeholder="your@email.com"
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
className="w-full px-4 py-3 rounded-lg bg-white/10 border border-white/20 text-white placeholder:text-white/40 focus:outline-none focus:border-brand-pink mb-4"
|
|
/>
|
|
<div className="flex gap-3">
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowModal(false)}
|
|
className="flex-1 px-4 py-2 rounded-lg border border-white/20 text-white/70 hover:bg-white/10 transition"
|
|
>
|
|
Cancel
|
|
</button>
|
|
<button
|
|
type="submit"
|
|
disabled={loading}
|
|
className="flex-1 px-4 py-2 rounded-lg bg-brand-pink text-white font-semibold hover:bg-[#ff7bc0] transition disabled:opacity-50"
|
|
>
|
|
{loading ? "Processing..." : "Continue to Payment"}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</>
|
|
);
|
|
}
|