Skip to content

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

Use RQ mutation for update default payment method + delete payment method in Org V1 Billing settings

Joshen Lim6b2763fc1959
diff --git a/studio/components/interfaces/Organization/BillingSettings/BillingSettings.tsx b/studio/components/interfaces/Organization/BillingSettings/BillingSettings.tsx
index c18d3297dfd..b453da92d71 100644
--- a/studio/components/interfaces/Organization/BillingSettings/BillingSettings.tsx
+++ b/studio/components/interfaces/Organization/BillingSettings/BillingSettings.tsx
@@ -1,19 +1,19 @@
+import { PermissionAction } from '@supabase/shared-types/out/constants'
+import { useParams } from 'common'
import { useEffect, useState } from 'react'
-import { useParams } from 'common/hooks'
+import { ScaffoldContainerLegacy } from 'components/layouts/Scaffold'
import { useProjectsQuery } from 'data/projects/projects-query'
import { useCheckPermissions, useFlag, useSelectedOrganization, useStore } from 'hooks'
-import { PermissionAction } from '@supabase/shared-types/out/constants'
import { get } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
+import OrganizationBillingMigrationPanel from '../GeneralSettings/OrganizationBillingMigrationPanel'
import BillingAddress from './BillingAddress/BillingAddress'
import BillingEmail from './BillingEmail'
import CreditBalance from './CreditBalance'
import PaymentMethods from './PaymentMethods'
import ProjectsSummary from './ProjectsSummary'
import TaxID from './TaxID/TaxID'
-import OrganizationBillingMigrationPanel from '../GeneralSettings/OrganizationBillingMigrationPanel'
-import { ScaffoldContainer, ScaffoldContainerLegacy } from 'components/layouts/Scaffold'
const BillingSettings = () => {
const { ui } = useStore()
diff --git a/studio/components/interfaces/Organization/BillingSettings/PaymentMethods.tsx b/studio/components/interfaces/Organization/BillingSettings/PaymentMethods.tsx
index 39076b09d92..666ecb682b3 100644
--- a/studio/components/interfaces/Organization/BillingSettings/PaymentMethods.tsx
+++ b/studio/components/interfaces/Organization/BillingSettings/PaymentMethods.tsx
@@ -17,9 +17,10 @@ import {
import { AddNewPaymentMethodModal } from 'components/interfaces/BillingV2'
import NoPermission from 'components/ui/NoPermission'
import Panel from 'components/ui/Panel'
+import { useOrganizationCustomerProfileUpdateMutation } from 'data/organizations/organization-customer-profile-update-mutation'
+import { useOrganizationPaymentMethodDeleteMutation } from 'data/organizations/organization-payment-method-delete-mutation'
import { useCheckPermissions, useSelectedOrganization, useStore } from 'hooks'
-import { delete_, patch } from 'lib/common/fetch'
-import { API_URL, BASE_PATH } from 'lib/constants'
+import { BASE_PATH } from 'lib/constants'
import { getURL } from 'lib/helpers'
export interface PaymentMethodsProps {
@@ -46,7 +47,33 @@ const PaymentMethods = ({
const [selectedMethodForDefault, setSelectedMethodForDefault] = useState<any>()
const [selectedMethodToDelete, setSelectedMethodToDelete] = useState<any>()
const [showAddPaymentMethodModal, setShowAddPaymentMethodModal] = useState(false)
- const [isUpdatingPaymentMethod, setIsUpdatingPaymentMethod] = useState(false)
+
+ const { mutate: updateCustomerProfile, isLoading: isUpdating } =
+ useOrganizationCustomerProfileUpdateMutation({
+ onSuccess: (updatedCustomer) => {
+ onDefaultMethodUpdated(updatedCustomer)
+ setSelectedMethodForDefault(undefined)
+ ui.setNotification({
+ category: 'success',
+ message: 'Successfully updated default payment method',
+ })
+ },
+ onError: (error) => {
+ ui.setNotification({
+ category: 'error',
+ message: `Failed to make payment method default: ${error.message}`,
+ })
+ },
+ })
+
+ const { mutate: deletePaymentMethod, isLoading: isDeleting } =
+ useOrganizationPaymentMethodDeleteMutation({
+ onSuccess: () => {
+ onPaymentMethodsDeleted()
+ setSelectedMethodToDelete(undefined)
+ ui.setNotification({ category: 'success', message: 'Successfully deleted payment method' })
+ },
+ })
const canReadPaymentMethods = useCheckPermissions(
PermissionAction.BILLING_READ,
@@ -58,48 +85,16 @@ const PaymentMethods = ({
)
const onConfirmMakeDefaultPaymentMethod = async () => {
- try {
- setIsUpdatingPaymentMethod(true)
- const updatedCustomer = await patch(`${API_URL}/organizations/${orgSlug}/customer`, {
- invoice_settings: {
- default_payment_method: selectedMethodForDefault.id,
- },
- })
- if (updatedCustomer.error) throw updatedCustomer.error
- onDefaultMethodUpdated(updatedCustomer)
- setSelectedMethodForDefault(undefined)
- ui.setNotification({
- category: 'success',
- message: 'Successfully updated default payment method',
- })
- } catch (error: any) {
- ui.setNotification({
- category: 'error',
- message: `Failed to make payment method default: ${error.message}`,
- })
- } finally {
- setIsUpdatingPaymentMethod(false)
- }
+ updateCustomerProfile({
+ slug: orgSlug,
+ invoice_settings: {
+ default_payment_method: selectedMethodForDefault.id,
+ },
+ })
}
const onConfirmDetachPaymentMethod = async () => {
- try {
- setIsUpdatingPaymentMethod(true)
- const { error } = await delete_(`${API_URL}/organizations/${orgSlug}/payments`, {
- card_id: selectedMethodToDelete.id,
- })
- if (error) throw error
- onPaymentMethodsDeleted()
- setSelectedMethodToDelete(undefined)
- ui.setNotification({ category: 'success', message: 'Successfully deleted payment method' })
- } catch (error: any) {
- ui.setNotification({
- category: 'error',
- message: `Failed to delete payment method: ${error.message}`,
- })
- } finally {
- setIsUpdatingPaymentMethod(false)
- }
+ deletePaymentMethod({ slug: orgSlug, cardId: selectedMethodToDelete.id })
}
const onLocalPaymentMethodAdded = () => {
@@ -271,12 +266,17 @@ const PaymentMethods = ({
onCancel={() => setSelectedMethodForDefault(undefined)}
customFooter={
<div className="flex items-center gap-2">
- <Button type="default" onClick={() => setSelectedMethodForDefault(undefined)}>
+ <Button
+ type="default"
+ disabled={isUpdating}
+ onClick={() => setSelectedMethodForDefault(undefined)}
+ >
Cancel
</Button>
<Button
type="primary"
- loading={isUpdatingPaymentMethod}
+ disabled={isUpdating}
+ loading={isUpdating}
onClick={onConfirmMakeDefaultPaymentMethod}
>
Confirm
@@ -301,12 +301,17 @@ const PaymentMethods = ({
onCancel={() => setSelectedMethodToDelete(undefined)}
customFooter={
<div className="flex items-center gap-2">
- <Button type="default" onClick={() => setSelectedMethodToDelete(undefined)}>
+ <Button
+ type="default"
+ disabled={isDeleting}
+ onClick={() => setSelectedMethodToDelete(undefined)}
+ >
Cancel
</Button>
<Button
type="primary"
- loading={isUpdatingPaymentMethod}
+ disabled={isDeleting}
+ loading={isDeleting}
onClick={onConfirmDetachPaymentMethod}
>
Confirm
diff --git a/studio/data/organizations/organization-customer-profile-update-mutation.ts b/studio/data/organizations/organization-customer-profile-update-mutation.ts
index 50452598f1b..41f9b3724f8 100644
--- a/studio/data/organizations/organization-customer-profile-update-mutation.ts
+++ b/studio/data/organizations/organization-customer-profile-update-mutation.ts
@@ -16,15 +16,23 @@ export type OrganizationCustomerProfileUpdateVariables = {
postal_code: string | null
state: string | null
} | null
+ invoice_settings?: {
+ default_payment_method: string
+ }
}
export async function updateOrganizationCustomerProfile({
slug,
address,
+ invoice_settings,
}: OrganizationCustomerProfileUpdateVariables) {
if (!slug) return console.error('Slug is required')
- const response = await patch(`${API_URL}/organizations/${slug}/customer`, { address })
+ const payload: any = {}
+ if (address) payload.address = address
+ if (invoice_settings) payload.invoice_settings = invoice_settings
+
+ const response = await patch(`${API_URL}/organizations/${slug}/customer`, payload)
if (response.error) throw response.error
return response
}
diff --git a/studio/data/organizations/organization-payment-method-delete-mutation.ts b/studio/data/organizations/organization-payment-method-delete-mutation.ts
index aba96fdbfaf..34d8181755d 100644
--- a/studio/data/organizations/organization-payment-method-delete-mutation.ts
+++ b/studio/data/organizations/organization-payment-method-delete-mutation.ts
@@ -11,7 +11,7 @@ export type OrganizationPaymentMethodDeleteVariables = {
cardId: string
}
-export async function deleteOrganizationMember({
+export async function deletePaymentMethod({
slug,
cardId,
}: OrganizationPaymentMethodDeleteVariables) {
@@ -20,7 +20,7 @@ export async function deleteOrganizationMember({
return response
}
-type OrganizationPaymentMethodDeleteData = Awaited<ReturnType<typeof deleteOrganizationMember>>
+type OrganizationPaymentMethodDeleteData = Awaited<ReturnType<typeof deletePaymentMethod>>
export const useOrganizationPaymentMethodDeleteMutation = ({
onSuccess,
@@ -40,7 +40,7 @@ export const useOrganizationPaymentMethodDeleteMutation = ({
OrganizationPaymentMethodDeleteData,
ResponseError,
OrganizationPaymentMethodDeleteVariables
- >((vars) => deleteOrganizationMember(vars), {
+ >((vars) => deletePaymentMethod(vars), {
async onSuccess(data, variables, context) {
const { slug } = variables
await queryClient.invalidateQueries(organizationKeys.paymentMethods(slug))