import { PermissionAction, SupportCategories } from '@supabase/shared-types/out/constants' import { useParams } from 'common' import { CreditCardIcon, ExternalLink, Plus } from 'lucide-react' import { useState } from 'react' import { toast } from 'sonner' import { Button } from 'ui' import { Admonition } from 'ui-patterns/admonition' 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 { ScaffoldSection, ScaffoldSectionContent, ScaffoldSectionDetail, } from '@/components/layouts/Scaffold' 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 { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions' 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 { data: subscription } = useOrgSubscriptionQuery({ orgSlug: slug }) const { data: paymentMethods, error, isPending: isLoading, isError, isSuccess, } = useOrganizationPaymentMethodsQuery({ slug }) const { can: canReadPaymentMethods, isSuccess: isPermissionsLoaded } = useAsyncCheckPermissions( PermissionAction.BILLING_READ, 'stripe.payment_methods' ) const { can: canUpdatePaymentMethods } = useAsyncCheckPermissions( PermissionAction.BILLING_WRITE, 'stripe.payment_methods' ) 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