import { CreditCardIcon, MoreHorizontalIcon } from 'lucide-react'
import {
Badge,
Button,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
Tooltip,
TooltipContent,
TooltipTrigger,
} from 'ui'
import { DropdownMenuItemTooltip } from '@/components/ui/DropdownMenuItemTooltip'
import PartnerIcon from '@/components/ui/PartnerIcon'
import type { OrganizationPaymentMethod } from '@/data/organizations/organization-payment-methods-query'
import type { PlanId } from '@/data/subscriptions/types'
import { BASE_PATH } from '@/lib/constants'
import { MANAGED_BY } from '@/lib/constants/infrastructure'
const BRANDS_WITH_ICON = new Set(['amex', 'discover', 'jcb', 'mastercard', 'visa'])
export const CardBrandIcon = ({ brand }: { brand: string | null | undefined }) => {
if (!brand || !BRANDS_WITH_ICON.has(brand)) {
return (
}
interface CreditCardProps {
paymentMethod: OrganizationPaymentMethod
canUpdatePaymentMethods?: boolean
paymentMethodType?: string
paymentMethodCount: number
subscriptionPlan?: PlanId
setSelectedMethodForUse?: (paymentMethod: OrganizationPaymentMethod) => void
setSelectedMethodToDelete?: (paymentMethod: OrganizationPaymentMethod) => void
}
const CreditCard = ({
paymentMethod,
canUpdatePaymentMethods = true,
paymentMethodType,
subscriptionPlan,
paymentMethodCount,
setSelectedMethodForUse,
setSelectedMethodToDelete,
}: CreditCardProps) => {
const isSpt = paymentMethod.type === 'shared_payment_token'
const spt = paymentMethod.shared_payment_token
const isActive = paymentMethod.is_default
const isRemovable =
!paymentMethod.is_default || (subscriptionPlan === 'free' && paymentMethodCount === 1)
const expiryYear = paymentMethod.card?.exp_year ?? 0
const expiryMonth = paymentMethod.card?.exp_month ?? 0
const currentMonth = new Date().getMonth() + 1
const currentYear = new Date().getFullYear()
const isCardExpiringSoon = expiryYear === currentYear && expiryMonth === currentMonth
const isCardExpired =
expiryYear < currentYear || (expiryYear === currentYear && expiryMonth < currentMonth)
const isTokenExpired = spt?.is_expired ?? false
const tokenExpiry = spt?.expires_at
? `${new Date(spt.expires_at * 1000).getMonth() + 1}/${new Date(spt.expires_at * 1000).getFullYear()}`
: undefined
const stripeStatus = (() => {
if (!isSpt) return null
if (isTokenExpired) {
return {
label: 'Token expired',
variant: 'destructive' as const,
description: 'Stripe Projects token has expired',
}
}
if (isCardExpired) {
return {
label: 'Needs review',
variant: 'warning' as const,
description: 'Underlying card has expired',
}
}
if (isCardExpiringSoon) {
return {
label: 'Needs review',
variant: 'warning' as const,
description: 'Underlying card expires soon',
}
}
return {
label: 'Active',
variant: 'success' as const,
description: 'Stripe Projects token is active',
}
})()
if (!paymentMethod.card) return null
return (
**** **** **** {paymentMethod.card.last4}
Expires: {paymentMethod.card.exp_month}/{paymentMethod.card.exp_year}
Managed via Stripe Projects · Token ending in {spt.last4}
{tokenExpiry && · Expires {tokenExpiry}}
Use this card
Delete card