mirror of
https://github.com/supabase/supabase.git
synced 2026-09-08 19:08:44 +08:00
59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
import { useMutation } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { handleError, post } from '@/data/fetchers'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type OrganizationPaymentMethodSetupIntentVariables = {
|
|
slug: string
|
|
hcaptchaToken: string
|
|
}
|
|
|
|
export async function setupPaymentMethodIntent({
|
|
slug,
|
|
hcaptchaToken,
|
|
}: OrganizationPaymentMethodSetupIntentVariables) {
|
|
const { data, error } = await post('/platform/organizations/{slug}/payments/setup-intent', {
|
|
// @ts-ignore [Joshen] API seems to be having the wrong spec
|
|
params: { path: { slug } },
|
|
// @ts-ignore [Joshen] API seems to be having the wrong spec
|
|
body: { hcaptchaToken },
|
|
})
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type OrganizationPaymentMethodSetupIntentData = Awaited<ReturnType<typeof setupPaymentMethodIntent>>
|
|
|
|
export const useOrganizationPaymentMethodSetupIntent = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<
|
|
OrganizationPaymentMethodSetupIntentData,
|
|
ResponseError,
|
|
OrganizationPaymentMethodSetupIntentVariables
|
|
>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
return useMutation<
|
|
OrganizationPaymentMethodSetupIntentData,
|
|
ResponseError,
|
|
OrganizationPaymentMethodSetupIntentVariables
|
|
>({
|
|
mutationFn: (vars) => setupPaymentMethodIntent(vars),
|
|
async onSuccess(data, variables, context) {
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to setup intent: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|