import { SupportCategories } from '@supabase/shared-types/out/constants' import { useParams } from 'common' import { CreditCardIcon, ExternalLinkIcon, PlusIcon } from 'lucide-react' import { useState } from 'react' import { useInView } from 'react-intersection-observer' import { toast } from 'sonner' import { Button } from 'ui' import { Admonition } from 'ui-patterns/Admonition' import { PageSection, PageSectionContent, PageSectionDescription, PageSectionMeta, PageSectionSummary, PageSectionTitle, } from 'ui-patterns/PageSection' import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader' import ChangePaymentMethodModal from './ChangePaymentMethodModal' import CreditCard from './CreditCard' import DeletePaymentMethodModal from './DeletePaymentMethodModal' import AddNewPaymentMethodModal from '@/components/interfaces/Billing/Payment/AddNewPaymentMethodModal' import { SupportLink } from '@/components/interfaces/Support/SupportLink' import { AlertError } from '@/components/ui/AlertError' import { FormPanel } from '@/components/ui/Forms/FormPanel' import { FormSection, FormSectionContent } from '@/components/ui/Forms/FormSection' import { NoPermission } from '@/components/ui/NoPermission' import PartnerManagedResource from '@/components/ui/PartnerManagedResource' import { isPartnerBillingOrganization } from '@/data/organizations/managed-by-utils' import { useOrganizationPaymentMethodsQuery } from '@/data/organizations/organization-payment-methods-query' import { useOrgSubscriptionQuery } from '@/data/subscriptions/org-subscription-query' import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2' import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization' import { MANAGED_BY } from '@/lib/constants/infrastructure' import { getURL } from '@/lib/helpers' const PaymentMethods = () => { const { slug } = useParams() const { data: selectedOrganization } = useSelectedOrganizationQuery() const [selectedMethodForUse, setSelectedMethodForUse] = useState() const [selectedMethodToDelete, setSelectedMethodToDelete] = useState() const [showAddPaymentMethodModal, setShowAddPaymentMethodModal] = useState(false) const { ref, inView } = useInView({ triggerOnce: true }) const { data: subscription } = useOrgSubscriptionQuery({ orgSlug: slug }, { enabled: inView }) const { data: paymentMethods, error, isPending: isLoading, isError, isSuccess, } = useOrganizationPaymentMethodsQuery({ slug }, { enabled: inView }) const { can: canReadPaymentMethods, isSuccess: isPermissionsLoaded } = useAsyncCheckPermissionsV2( FGA_PERMISSIONS.ORGANIZATION.BILLING_READ ) const { can: canUpdatePaymentMethods } = useAsyncCheckPermissionsV2( FGA_PERMISSIONS.ORGANIZATION.BILLING_WRITE ) const isPartnerBilledOrganization = isPartnerBillingOrganization( selectedOrganization?.billing_partner ) const isStripeManagedOrganization = selectedOrganization?.managed_by === MANAGED_BY.STRIPE_PROJECTS return ( <> Payment Methods {isStripeManagedOrganization ? 'Billing for this organisation is handled through Stripe Projects.' : 'Payments for your subscription are made using the default card.'} {selectedOrganization && isPartnerBilledOrganization ? ( ) : isPermissionsLoaded && !canReadPaymentMethods ? ( ) : ( <> {isLoading && (
)} {isError && ( )} {isSuccess && ( <> {subscription?.payment_method_type === 'invoice' && ( } > View Stripe Projects docs ) : ( ) } /> )} {!canUpdatePaymentMethods ? (

You need additional permissions to manage payment methods

) : (
)}
) : undefined } > {(paymentMethods?.data?.length ?? 0) === 0 ? (

No payment methods

) : (
{paymentMethods?.data?.map((paymentMethod) => ( ))}
)}
)} )}
setShowAddPaymentMethodModal(false)} onConfirm={() => { setShowAddPaymentMethodModal(false) toast.success('Successfully added new payment method') }} /> setSelectedMethodForUse(undefined)} /> setSelectedMethodToDelete(undefined)} /> ) } export default PaymentMethods