Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Improve payment method UI

Joshen Lim23f2e9edf703
diff --git a/studio/components/interfaces/Organization/BillingSettingsV2/PaymentMethods/ChangePaymentMethodModal.tsx b/studio/components/interfaces/Organization/BillingSettingsV2/PaymentMethods/ChangePaymentMethodModal.tsx
new file mode 100644
index 00000000000..3ad2afd3d22
--- /dev/null
+++ b/studio/components/interfaces/Organization/BillingSettingsV2/PaymentMethods/ChangePaymentMethodModal.tsx
@@ -0,0 +1,89 @@
+import { useParams } from 'common'
+import { OrganizationPaymentMethod } from 'data/organizations/organization-payment-methods-query'
+import { useOrgSubscriptionQuery } from 'data/subscriptions/org-subscription-query'
+import {
+ SubscriptionTier,
+ useOrgSubscriptionUpdateMutation,
+} from 'data/subscriptions/org-subscription-update-mutation'
+import { useStore } from 'hooks'
+import { Button, Modal } from 'ui'
+
+export interface ChangePaymentMethodModalProps {
+ selectedPaymentMethod?: OrganizationPaymentMethod
+ onClose: () => void
+}
+
+const ChangePaymentMethodModal = ({
+ selectedPaymentMethod,
+ onClose,
+}: ChangePaymentMethodModalProps) => {
+ const { ui } = useStore()
+ const { slug } = useParams()
+ const { data: subscription } = useOrgSubscriptionQuery({ orgSlug: slug })
+ const { mutateAsync: updateOrgSubscription, isLoading: isUpdating } =
+ useOrgSubscriptionUpdateMutation()
+
+ const onConfirmUpdate = async () => {
+ if (!slug) return console.error('Slug is required')
+ if (!subscription) return console.error('Subscription is required')
+ if (!selectedPaymentMethod) return console.error('Card ID is required')
+
+ const selectedTier =
+ subscription.plan.id === 'pro' && subscription.usage_billing_enabled
+ ? 'tier_payg'
+ : `tier_${subscription.plan.id}`
+
+ try {
+ await updateOrgSubscription({
+ slug,
+ tier: selectedTier as SubscriptionTier,
+ paymentMethod: selectedPaymentMethod.id,
+ })
+ ui.setNotification({
+ category: 'success',
+ message: `Successfully removed payment method ending with ${selectedPaymentMethod.card.last4}`,
+ })
+ onClose()
+ } catch (error: any) {
+ ui.setNotification({
+ category: 'error',
+ message: `Failed to delete payment method: ${error.message}`,
+ })
+ }
+ }
+
+ return (
+ <Modal
+ visible={selectedPaymentMethod !== undefined}
+ size="medium"
+ header={`Confirm to use payment method ending with ${selectedPaymentMethod?.card.last4}`}
+ onCancel={() => onClose()}
+ customFooter={
+ <div className="flex items-center gap-2">
+ <Button type="default" disabled={isUpdating} onClick={() => onClose()}>
+ Cancel
+ </Button>
+ <Button
+ type="primary"
+ disabled={isUpdating}
+ loading={isUpdating}
+ onClick={onConfirmUpdate}
+ >
+ Confirm
+ </Button>
+ </div>
+ }
+ >
+ <div className="py-4">
+ <Modal.Content>
+ <p className="text-sm">
+ Upon clicking confirm, all future charges will be deducted from the card ending with{' '}
+ {selectedPaymentMethod?.card.last4}. There are no immediate charges.
+ </p>
+ </Modal.Content>
+ </div>
+ </Modal>
+ )
+}
+
+export default ChangePaymentMethodModal
diff --git a/studio/components/interfaces/Organization/BillingSettingsV2/PaymentMethods/PaymentMethods.tsx b/studio/components/interfaces/Organization/BillingSettingsV2/PaymentMethods/PaymentMethods.tsx
index 7e0dc88e4f2..c49c416c316 100644
--- a/studio/components/interfaces/Organization/BillingSettingsV2/PaymentMethods/PaymentMethods.tsx
+++ b/studio/components/interfaces/Organization/BillingSettingsV2/PaymentMethods/PaymentMethods.tsx
@@ -8,30 +8,34 @@ import { FormPanel, FormSection, FormSectionContent, FormSectionLabel } from 'co
import NoPermission from 'components/ui/NoPermission'
import { organizationKeys } from 'data/organizations/keys'
import { useOrganizationPaymentMethodsQuery } from 'data/organizations/organization-payment-methods-query'
+import { useOrgSubscriptionQuery } from 'data/subscriptions/org-subscription-query'
import { useCheckPermissions, useStore } from 'hooks'
-import { getURL } from 'lib/helpers'
-import { Badge, Button, Dropdown, IconCreditCard, IconMoreHorizontal, IconPlus, Input } from 'ui'
import { BASE_PATH } from 'lib/constants'
-import { useOrgSubscriptionQuery } from 'data/subscriptions/org-subscription-query'
+import { getURL } from 'lib/helpers'
+import { Badge, Button, Dropdown, IconCreditCard, IconMoreHorizontal, IconPlus } from 'ui'
+import ChangePaymentMethodModal from './ChangePaymentMethodModal'
import DeletePaymentMethodModal from './DeletePaymentMethodModal'
-
-// [TODO] Implement change credit card for org subscription logic
+import AlertError from 'components/ui/AlertError'
const PaymentMethods = () => {
const { ui } = useStore()
const { slug } = useParams()
const queryClient = useQueryClient()
- const [selectedMethodForDefault, setSelectedMethodForDefault] = useState<any>()
+ const [selectedMethodForUse, setSelectedMethodForUse] = useState<any>()
const [selectedMethodToDelete, setSelectedMethodToDelete] = useState<any>()
const [showAddPaymentMethodModal, setShowAddPaymentMethodModal] = useState(false)
const { data: subscription } = useOrgSubscriptionQuery({ orgSlug: slug })
const {
data: paymentMethods,
+ error,
isLoading,
isError,
isSuccess,
} = useOrganizationPaymentMethodsQuery({ slug })
+ const sortedPaymentMethods =
+ paymentMethods?.sort((a, b) => (a.id === subscription?.payment_method_id ? -1 : 0)) ?? []
+ console.log({ sortedPaymentMethods })
const canReadPaymentMethods = useCheckPermissions(
PermissionAction.BILLING_READ,
@@ -65,92 +69,102 @@ const PaymentMethods = () => {
{!canReadPaymentMethods ? (
<NoPermission resourceText="view this organization's payment methods" />
) : (
- <FormPanel
- footer={
- <div className="flex items-center justify-between py-4 px-8">
- {!canUpdatePaymentMethods ? (
- <p className="text-sm text-scale-1000">
- You need additional permissions to manage payment methods
- </p>
- ) : (
- <div />
- )}
- <Button
- type="default"
- icon={<IconPlus />}
- disabled={!canUpdatePaymentMethods}
- onClick={() => setShowAddPaymentMethodModal(true)}
- >
- Add new card
- </Button>
- </div>
- }
- >
- <FormSection>
- <FormSectionContent fullWidth loading={false}>
- {(paymentMethods?.length ?? 0) === 0 ? (
- <div className="flex items-center space-x-2 opacity-50">
- <IconCreditCard />
- <p className="text-sm">No payment methods</p>
+ <>
+ {isError && (
+ <AlertError subject="Unable to retrieve payment methods" error={error as any} />
+ )}
+ {isSuccess && (
+ <FormPanel
+ footer={
+ <div className="flex items-center justify-between py-4 px-8">
+ {!canUpdatePaymentMethods ? (
+ <p className="text-sm text-scale-1000">
+ You need additional permissions to manage payment methods
+ </p>
+ ) : (
+ <div />
+ )}
+ <Button
+ type="default"
+ icon={<IconPlus />}
+ disabled={!canUpdatePaymentMethods}
+ onClick={() => setShowAddPaymentMethodModal(true)}
+ >
+ Add new card
+ </Button>
</div>
- ) : (
- <div className="space-y-3">
- {paymentMethods?.map((paymentMethod) => {
- const activePaymentMethod =
- subscription?.payment_method_id === paymentMethod.id
- return (
- <div key={paymentMethod.id} className="flex items-center justify-between">
- <div className="flex items-center space-x-8">
- <img
- alt="Credit card brand"
- src={`${BASE_PATH}/img/payment-methods/${paymentMethod.card.brand
- .replace(' ', '-')
- .toLowerCase()}.png`}
- width="32"
- />
- <p className="prose text-sm font-mono">
- **** **** **** {paymentMethod.card.last4}
- </p>
- <p className="text-sm tabular-nums">
- Expires: {paymentMethod.card.exp_month}/
- {paymentMethod.card.exp_year}
- </p>
- {activePaymentMethod && <Badge color="green">Active</Badge>}
- </div>
- {canUpdatePaymentMethods ? (
- <Dropdown
- size="tiny"
- align="end"
- overlay={[
- <Dropdown.Item
- key="make-default"
- onClick={() => setSelectedMethodForDefault(paymentMethod)}
- >
- Make default
- </Dropdown.Item>,
- <Dropdown.Item
- key="delete-method"
- onClick={() => setSelectedMethodToDelete(paymentMethod)}
- >
- Delete
- </Dropdown.Item>,
- ]}
+ }
+ >
+ <FormSection>
+ <FormSectionContent fullWidth loading={false}>
+ {(paymentMethods?.length ?? 0) === 0 ? (
+ <div className="flex items-center space-x-2 opacity-50">
+ <IconCreditCard />
+ <p className="text-sm">No payment methods</p>
+ </div>
+ ) : (
+ <div className="space-y-3">
+ {sortedPaymentMethods?.map((paymentMethod) => {
+ const isActive = subscription?.payment_method_id === paymentMethod.id
+ return (
+ <div
+ key={paymentMethod.id}
+ className="flex items-center justify-between"
>
- <Button
- type="outline"
- icon={<IconMoreHorizontal />}
- className="hover:border-gray-500 px-1"
- />
- </Dropdown>
- ) : null}
- </div>
- )
- })}
- </div>
- )}
- </FormSectionContent>
- </FormSection>
- </FormPanel>
+ <div className="flex items-center space-x-8">
+ <img
+ alt="Credit card brand"
+ src={`${BASE_PATH}/img/payment-methods/${paymentMethod.card.brand
+ .replace(' ', '-')
+ .toLowerCase()}.png`}
+ width="32"
+ />
+ <p className="prose text-sm font-mono">
+ **** **** **** {paymentMethod.card.last4}
+ </p>
+ <p className="text-sm tabular-nums">
+ Expires: {paymentMethod.card.exp_month}/
+ {paymentMethod.card.exp_year}
+ </p>
+ {isActive && <Badge color="green">Active</Badge>}
+ </div>
+ {canUpdatePaymentMethods && !isActive ? (
+ <Dropdown
+ size="tiny"
+ align="end"
+ overlay={[
+ <Dropdown.Item
+ key="make-default"
+ onClick={() => setSelectedMethodForUse(paymentMethod)}
+ >
+ Use this card
+ </Dropdown.Item>,
+ <Dropdown.Separator key="card-separator" />,
+ <Dropdown.Item
+ key="delete-method"
+ onClick={() => setSelectedMethodToDelete(paymentMethod)}
+ >
+ Delete card
+ </Dropdown.Item>,
+ ]}
+ >
+ <Button
+ type="outline"
+ icon={<IconMoreHorizontal />}
+ className="hover:border-gray-500 px-1"
+ />
+ </Dropdown>
+ ) : null}
+ </div>
+ )
+ })}
+ </div>
+ )}
+ </FormSectionContent>
+ </FormSection>
+ </FormPanel>
+ )}
+ </>
)}
</FormSectionContent>
</FormSection>
@@ -169,6 +183,11 @@ const PaymentMethods = () => {
}}
/>
+ <ChangePaymentMethodModal
+ selectedPaymentMethod={selectedMethodForUse}
+ onClose={() => setSelectedMethodForUse(undefined)}
+ />
+
<DeletePaymentMethodModal
selectedPaymentMethod={selectedMethodToDelete}
onClose={() => setSelectedMethodToDelete(undefined)}