Home / Function/ AddNewPaymentMethodModal() — supabase Function Reference

AddNewPaymentMethodModal() — supabase Function Reference

Architecture documentation for the AddNewPaymentMethodModal() function in AddNewPaymentMethodModal.tsx from the supabase codebase.

Entity Profile

Dependency Diagram

graph TD
  1ea504e4_0a4b_3887_3533_ac3ed2db4184["AddNewPaymentMethodModal()"]
  9d242dbc_4a69_3d5f_9baa_f2441bca1172["getStripeElementsAppearanceOptions()"]
  1ea504e4_0a4b_3887_3533_ac3ed2db4184 -->|calls| 9d242dbc_4a69_3d5f_9baa_f2441bca1172
  style 1ea504e4_0a4b_3887_3533_ac3ed2db4184 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

apps/studio/components/interfaces/Billing/Payment/AddNewPaymentMethodModal.tsx lines 24–144

const AddNewPaymentMethodModal = ({
  visible,
  returnUrl,
  onCancel,
  onConfirm,
}: AddNewPaymentMethodModalProps) => {
  const { resolvedTheme } = useTheme()
  const [intent, setIntent] = useState<any>()
  const { data: selectedOrganization } = useSelectedOrganizationQuery()

  const [captchaToken, setCaptchaToken] = useState<string | null>(null)
  const [captchaRef, setCaptchaRef] = useState<HCaptcha | null>(null)

  const { mutate: setupIntent } = useOrganizationPaymentMethodSetupIntent({
    onSuccess: (intent) => {
      setIntent(intent)
    },
    onError: (error) => {
      toast.error(`Failed to setup intent: ${error.message}`)
    },
  })

  const captchaRefCallback = useCallback((node: any) => {
    setCaptchaRef(node)
  }, [])

  useEffect(() => {
    const initSetupIntent = async (hcaptchaToken: string | undefined) => {
      const slug = selectedOrganization?.slug
      if (!slug) return console.error('Slug is required')
      if (!hcaptchaToken) return console.error('HCaptcha token required')

      setIntent(undefined)
      setupIntent({ slug, hcaptchaToken })
    }

    const loadPaymentForm = async () => {
      if (visible && captchaRef) {
        let token = captchaToken

        try {
          if (!token) {
            const captchaResponse = await captchaRef.execute({ async: true })
            token = captchaResponse?.response ?? null
          }
        } catch (error) {
          return
        }

        await initSetupIntent(token ?? undefined)
        resetCaptcha()
      }
    }

    loadPaymentForm()
  }, [visible, captchaRef])

  const resetCaptcha = () => {
    setCaptchaToken(null)
    captchaRef?.resetCaptcha()
  }

  const options = {
    clientSecret: intent ? intent.client_secret : '',
    appearance: getStripeElementsAppearanceOptions(resolvedTheme),
  } as any

  const onLocalCancel = () => {
    setIntent(undefined)
    return onCancel()
  }

  const onLocalConfirm = () => {
    setIntent(undefined)
    return onConfirm()
  }

  return (
    // We cant display the hCaptcha in the modal, as the modal auto-closes when clicking the captcha
    // So we only show the modal if the captcha has been executed successfully (intent loaded)
    <>
      <HCaptcha
        ref={captchaRefCallback}
        sitekey={process.env.NEXT_PUBLIC_HCAPTCHA_SITE_KEY!}
        size="invisible"
        onOpen={() => {
          // [Joshen] This is to ensure that hCaptcha popup remains clickable
          if (document !== undefined) document.body.classList.add('!pointer-events-auto')
        }}
        onClose={() => {
          onLocalCancel()
          if (document !== undefined) document.body.classList.remove('!pointer-events-auto')
        }}
        onVerify={(token) => {
          setCaptchaToken(token)
          if (document !== undefined) document.body.classList.remove('!pointer-events-auto')
        }}
        onExpire={() => {
          setCaptchaToken(null)
        }}
      />

      <Modal
        hideFooter
        size="medium"
        visible={visible && intent !== undefined}
        header="Add new payment method"
        onCancel={onLocalCancel}
        className="PAYMENT"
      >
        <Elements stripe={stripePromise} options={options}>
          <AddPaymentMethodForm
            returnUrl={returnUrl}
            onCancel={onLocalCancel}
            onConfirm={onLocalConfirm}
          />
        </Elements>
      </Modal>
    </>
  )
}

Subdomains

Frequently Asked Questions

What does AddNewPaymentMethodModal() do?
AddNewPaymentMethodModal() is a function in the supabase codebase.
What does AddNewPaymentMethodModal() call?
AddNewPaymentMethodModal() calls 1 function(s): getStripeElementsAppearanceOptions.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free