"use client"; import { MoreVertical } from "lucide-react"; import { useEffect, useState } from "react"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, Badge, Button, Card, CardContent, CardHeader, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "../../../../../shadcnui"; import { PaymentMethodInterface, StripeCustomerInterface, StripeCustomerService } from "../../data"; type PaymentMethodCardProps = { paymentMethod: PaymentMethodInterface; onUpdate: () => void; }; // Card brand icons mapping const brandIcons: Record = { visa: "💳", mastercard: "💳", amex: "💳", discover: "💳", }; export function PaymentMethodCard({ paymentMethod, onUpdate }: PaymentMethodCardProps) { const [loading, setLoading] = useState(false); const [customer, setCustomer] = useState(null); const [showRemoveDialog, setShowRemoveDialog] = useState(false); // Load customer to check default payment method useEffect(() => { const loadCustomer = async () => { try { const fetchedCustomer = await StripeCustomerService.getCustomer(); setCustomer(fetchedCustomer); } catch (error) { console.error("[PaymentMethodCard] Failed to load customer:", error); } }; loadCustomer(); }, []); const isDefault = customer?.defaultPaymentMethodId === paymentMethod.id; const brand = paymentMethod.card?.brand || "card"; const last4 = paymentMethod.card?.last4 || "****"; const expMonth = paymentMethod.card?.expMonth || 0; const expYear = paymentMethod.card?.expYear || 0; const brandIcon = brandIcons[brand.toLowerCase()] || "💳"; const handleSetDefault = async () => { setLoading(true); try { await StripeCustomerService.setDefaultPaymentMethod({ paymentMethodId: paymentMethod.id }); onUpdate(); } catch (error) { console.error("[PaymentMethodCard] Failed to set as default:", error); } finally { setLoading(false); } }; const handleRemove = async () => { setLoading(true); try { await StripeCustomerService.removePaymentMethod({ paymentMethodId: paymentMethod.id }); setShowRemoveDialog(false); onUpdate(); } catch (error) { console.error("[PaymentMethodCard] Failed to remove:", error); setLoading(false); } }; return ( <> {/* Default Badge */} {isDefault && ( Default )}
{brandIcon} {brand}
{!isDefault && ( Set as Default )} setShowRemoveDialog(true)} disabled={loading} className="text-destructive" > Remove

•••• {last4}

Expires {String(expMonth).padStart(2, "0")}/{expYear}

{/* Remove Confirmation Dialog */} Remove Payment Method Are you sure you want to remove this payment method? This action cannot be undone. {isDefault && " This is your default payment method."} Cancel {loading ? "Removing..." : "Remove"} ); }