Skip to content

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

chore: update invoice pay now redirect (#30565)

Alaister Young8c7236070701
diff --git a/apps/studio/components/interfaces/Organization/InvoicesSettings/InvoicePayButton.tsx b/apps/studio/components/interfaces/Organization/InvoicesSettings/InvoicePayButton.tsx
new file mode 100644
index 00000000000..3bf0e11cc87
--- /dev/null
+++ b/apps/studio/components/interfaces/Organization/InvoicesSettings/InvoicePayButton.tsx
@@ -0,0 +1,30 @@
+import { useInvoicePaymentLinkGetMutation } from 'data/invoices/invoice-payment-link-mutation'
+import { toast } from 'sonner'
+import { Button } from 'ui'
+
+interface InvoicePayButtonProps {
+ slug?: string
+ invoiceId: string
+}
+
+const InvoicePayButton = ({ slug, invoiceId }: InvoicePayButtonProps) => {
+ const { mutate, isLoading } = useInvoicePaymentLinkGetMutation({
+ onSuccess(data) {
+ toast.success('Redirecting to payment gateway...')
+
+ window.location.href = data.redirectUrl
+ },
+ })
+
+ function onPayNow() {
+ mutate({ slug, invoiceId })
+ }
+
+ return (
+ <Button onClick={onPayNow} loading={isLoading} disabled={isLoading}>
+ Pay Now
+ </Button>
+ )
+}
+
+export default InvoicePayButton
diff --git a/apps/studio/components/interfaces/Organization/InvoicesSettings/InvoicesSettings.tsx b/apps/studio/components/interfaces/Organization/InvoicesSettings/InvoicesSettings.tsx
index 4804764ae49..baf7142059c 100644
--- a/apps/studio/components/interfaces/Organization/InvoicesSettings/InvoicesSettings.tsx
+++ b/apps/studio/components/interfaces/Organization/InvoicesSettings/InvoicesSettings.tsx
@@ -21,6 +21,7 @@ import { formatCurrency } from 'lib/helpers'
import { Button } from 'ui'
import CurrentPaymentMethod from '../BillingSettings/PaymentMethods/CurrentPaymentMethod'
import { FileText, Download, ChevronLeft, ChevronRight } from 'lucide-react'
+import InvoicePayButton from './InvoicePayButton'
const PAGE_LIMIT = 10
@@ -152,15 +153,7 @@ const InvoicesSettings = () => {
InvoiceStatus.OPEN,
InvoiceStatus.ISSUED,
].includes(x.status as InvoiceStatus) && (
- <Button asChild>
- <Link
- href={`https://redirect.revops.supabase.com/pay-invoice/${x.id}`}
- target="_blank"
- rel="noreferrer"
- >
- Pay Now
- </Link>
- </Button>
+ <InvoicePayButton slug={slug} invoiceId={x.id} />
)}
<Button
diff --git a/apps/studio/data/invoices/invoice-payment-link-mutation.ts b/apps/studio/data/invoices/invoice-payment-link-mutation.ts
new file mode 100644
index 00000000000..35fcdbdd939
--- /dev/null
+++ b/apps/studio/data/invoices/invoice-payment-link-mutation.ts
@@ -0,0 +1,65 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'sonner'
+
+import { get, handleError } from 'data/fetchers'
+import type { ResponseError } from 'types'
+import { invoicesKeys } from './keys'
+
+export type InvoicePaymentLinkGetVariables = {
+ slug?: string
+ invoiceId?: string
+}
+
+export async function updateInvoicePaymentLink({
+ slug,
+ invoiceId,
+}: InvoicePaymentLinkGetVariables) {
+ if (!slug) throw new Error('Org slug is required')
+ if (!invoiceId) throw new Error('Invoice ID is required')
+
+ const { data, error } = await get(
+ '/platform/organizations/{slug}/billing/invoices/{invoiceId}/payment-link',
+ {
+ params: {
+ path: {
+ slug,
+ invoiceId,
+ },
+ },
+ }
+ )
+
+ if (error) handleError(error)
+ return data
+}
+
+type InvoicePaymentLinkGetData = Awaited<ReturnType<typeof updateInvoicePaymentLink>>
+
+export const useInvoicePaymentLinkGetMutation = ({
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<InvoicePaymentLinkGetData, ResponseError, InvoicePaymentLinkGetVariables>,
+ 'mutationFn'
+> = {}) => {
+ const queryClient = useQueryClient()
+
+ return useMutation<InvoicePaymentLinkGetData, ResponseError, InvoicePaymentLinkGetVariables>(
+ (vars) => updateInvoicePaymentLink(vars),
+ {
+ async onError(error, variables, context) {
+ // In case of an error, there is a good chance that the invoice status has changed, so we invalidate the cache to reflect the updated status
+ await Promise.all([
+ queryClient.invalidateQueries(invoicesKeys.listAndCount(variables.slug)),
+ ])
+
+ if (onError === undefined) {
+ toast.error(error.message)
+ } else {
+ onError(error, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}
diff --git a/apps/studio/data/invoices/keys.ts b/apps/studio/data/invoices/keys.ts
index e83637097ec..651dd5618f0 100644
--- a/apps/studio/data/invoices/keys.ts
+++ b/apps/studio/data/invoices/keys.ts
@@ -5,6 +5,7 @@ export const invoicesKeys = {
list: (slug: string | undefined, offset: number | undefined) =>
['invoices', slug, offset] as const,
count: (slug: string | undefined) => ['invoices', slug, 'count'] as const,
+ listAndCount: (slug: string | undefined) => ['invoices', slug] as const,
projectInvoices: (projectRef: string | undefined, offset: number | undefined) =>
['invoices', projectRef, offset] as const,