Skip to content

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

feat(hCaptcha): captcha for subscription/payment method changes

Kevin Grüneberg13c070e81d17
diff --git a/studio/components/interfaces/Billing/AddNewPaymentMethodModal/AddNewPaymentMethodModal.tsx b/studio/components/interfaces/Billing/AddNewPaymentMethodModal/AddNewPaymentMethodModal.tsx
index 8e8e7e865b6..7808f106114 100644
--- a/studio/components/interfaces/Billing/AddNewPaymentMethodModal/AddNewPaymentMethodModal.tsx
+++ b/studio/components/interfaces/Billing/AddNewPaymentMethodModal/AddNewPaymentMethodModal.tsx
@@ -1,4 +1,4 @@
-import { FC, useEffect, useState } from 'react'
+import { FC, useEffect, useRef, useState } from 'react'
import { IconLoader, Modal } from 'ui'
import { loadStripe } from '@stripe/stripe-js'
import { Elements } from '@stripe/react-stripe-js'
@@ -7,6 +7,7 @@ import { useStore } from 'hooks'
import { post } from 'lib/common/fetch'
import { API_URL, STRIPE_PUBLIC_KEY } from 'lib/constants'
import AddPaymentMethodForm from './AddPaymentMethodForm'
+import HCaptcha from '@hcaptcha/react-hcaptcha'
interface Props {
visible: boolean
@@ -20,18 +21,38 @@ const AddNewPaymentMethodModal: FC<Props> = ({ visible, returnUrl, onCancel }) =
const { ui } = useStore()
const [intent, setIntent] = useState<any>()
+ const [captchaLoaded, setCaptchaLoaded] = useState(false)
+
+ const [captchaToken, setCaptchaToken] = useState<string | null>(null)
+ const captchaRef = useRef<HCaptcha>(null)
+
useEffect(() => {
- if (visible) {
- setupIntent()
+ if (visible && captchaLoaded) {
+ let token = captchaToken
+ if (!token) {
+ captchaRef.current?.execute({ async: true }).then((captchaResponse) => {
+ return setupIntent(captchaResponse?.response ?? undefined)
+ })
+ }
}
- }, [visible])
+ }, [visible, captchaLoaded])
+
+ const onCaptchaLoaded = () => {
+ setCaptchaLoaded(true)
+ }
- const setupIntent = async () => {
+ const setupIntent = async (hcaptchaToken: string | undefined) => {
setIntent(undefined)
+
const orgSlug = ui.selectedOrganization?.slug ?? ''
- const intent = await post(`${API_URL}/organizations/${orgSlug}/payments/setup-intent`, {})
+ const intent = await post(`${API_URL}/organizations/${orgSlug}/payments/setup-intent`, {
+ hcaptchaToken,
+ })
if (intent.error) {
+ setCaptchaToken(null)
+ captchaRef.current?.resetCaptcha()
+
return ui.setNotification({
category: 'error',
message: intent.error.message,
@@ -56,6 +77,18 @@ const AddNewPaymentMethodModal: FC<Props> = ({ visible, returnUrl, onCancel }) =
onCancel={onCancel}
className="PAYMENT"
>
+ <HCaptcha
+ ref={captchaRef}
+ sitekey={process.env.NEXT_PUBLIC_HCAPTCHA_SITE_KEY!}
+ size="invisible"
+ onLoad={onCaptchaLoaded}
+ onVerify={(token) => {
+ setCaptchaToken(token)
+ }}
+ onExpire={() => {
+ setCaptchaToken(null)
+ }}
+ />
<div className="space-y-4 py-4">
{intent !== undefined ? (
<Elements stripe={stripePromise} options={options}>
diff --git a/studio/components/interfaces/Billing/Billing.utils.ts b/studio/components/interfaces/Billing/Billing.utils.ts
index 84e1e448694..67f98f93e7b 100644
--- a/studio/components/interfaces/Billing/Billing.utils.ts
+++ b/studio/components/interfaces/Billing/Billing.utils.ts
@@ -52,7 +52,8 @@ export const formSubscriptionUpdatePayload = (
},
nonChangeableAddons: SubscriptionAddon[],
selectedPaymentMethod: string,
- region: string
+ region: string,
+ hcaptchaToken: string | undefined
) => {
const { computeSize, pitrDuration, customDomains } = selectedAddons
@@ -78,6 +79,7 @@ export const formSubscriptionUpdatePayload = (
addons,
proration_date,
payment_method: selectedPaymentMethod,
+ hcaptchaToken
}
}
diff --git a/studio/components/interfaces/Billing/EnterpriseUpdate.tsx b/studio/components/interfaces/Billing/EnterpriseUpdate.tsx
index bf98cd7e4aa..430b8dfcc68 100644
--- a/studio/components/interfaces/Billing/EnterpriseUpdate.tsx
+++ b/studio/components/interfaces/Billing/EnterpriseUpdate.tsx
@@ -1,4 +1,4 @@
-import { FC, useState, useEffect } from 'react'
+import { FC, useState, useEffect, useRef } from 'react'
import { useRouter } from 'next/router'
import { Transition } from '@headlessui/react'
@@ -6,6 +6,7 @@ import { useStore, useFlag } from 'hooks'
import { getURL } from 'lib/helpers'
import { post, patch } from 'lib/common/fetch'
import { API_URL, PROJECT_STATUS } from 'lib/constants'
+import HCaptcha from '@hcaptcha/react-hcaptcha'
import Divider from 'components/ui/Divider'
import { SubscriptionAddon } from './AddOns/AddOns.types'
@@ -45,6 +46,9 @@ const EnterpriseUpdate: FC<Props> = ({
const isCustomDomainsEnabled = useFlag('customDomains')
const isPITRSelfServeEnabled = useFlag('pitrSelfServe')
+ const [captchaToken, setCaptchaToken] = useState<string | null>(null)
+ const captchaRef = useRef<HCaptcha>(null)
+
const projectId = ui.selectedProject?.id ?? -1
const projectRef = ui.selectedProject?.ref ?? 'default'
const projectRegion = ui.selectedProject?.region ?? ''
@@ -110,7 +114,8 @@ const EnterpriseUpdate: FC<Props> = ({
selectedAddons,
nonChangeableAddons,
selectedPaymentMethodId,
- projectRegion
+ projectRegion,
+ undefined
),
tier: currentSubscription.tier.price_id,
}
@@ -129,6 +134,12 @@ const EnterpriseUpdate: FC<Props> = ({
// Last todo to support enterprise billing on dashboard + E2E test
const onConfirmPayment = async () => {
+ let token = captchaToken
+ if (!token) {
+ const captchaResponse = await captchaRef.current?.execute({ async: true })
+ token = captchaResponse?.response ?? null
+ }
+
const payload = {
...formSubscriptionUpdatePayload(
currentSubscription,
@@ -136,7 +147,8 @@ const EnterpriseUpdate: FC<Props> = ({
selectedAddons,
nonChangeableAddons,
selectedPaymentMethodId,
- projectRegion
+ projectRegion,
+ token ?? undefined
),
tier: currentSubscription.tier.price_id,
}
@@ -273,6 +285,19 @@ const EnterpriseUpdate: FC<Props> = ({
setShowAddPaymentMethodModal(true)
}}
onConfirmPayment={onConfirmPayment}
+ captcha={
+ <HCaptcha
+ ref={captchaRef}
+ sitekey={process.env.NEXT_PUBLIC_HCAPTCHA_SITE_KEY!}
+ size="invisible"
+ onVerify={(token) => {
+ setCaptchaToken(token)
+ }}
+ onExpire={() => {
+ setCaptchaToken(null)
+ }}
+ />
+ }
/>
</div>
</Transition>
diff --git a/studio/components/interfaces/Billing/ExitSurvey/ExitSurvey.tsx b/studio/components/interfaces/Billing/ExitSurvey/ExitSurvey.tsx
index c1838f7c233..7d7a02e26b1 100644
--- a/studio/components/interfaces/Billing/ExitSurvey/ExitSurvey.tsx
+++ b/studio/components/interfaces/Billing/ExitSurvey/ExitSurvey.tsx
@@ -11,6 +11,7 @@ import { CANCELLATION_REASONS } from '../Billing.constants'
import { UpdateSuccess } from '../'
import { SubscriptionPreview } from '../Billing.types'
import { StripeSubscription } from 'components/interfaces/Billing'
+import HCaptcha from '@hcaptcha/react-hcaptcha'
interface Props {
freeTier: any
@@ -24,6 +25,9 @@ const ExitSurvey: FC<Props> = ({ freeTier, subscription, onSelectBack }) => {
const projectId = ui.selectedProject?.id ?? -1
const projectRef = ui.selectedProject?.ref
+ const [captchaToken, setCaptchaToken] = useState<string | null>(null)
+ const captchaRef = useRef<HCaptcha>(null)
+
const formRef = useRef<any>()
const initialValues = { message: '' }
@@ -99,6 +103,12 @@ const ExitSurvey: FC<Props> = ({ freeTier, subscription, onSelectBack }) => {
const downgradeProject = async (values?: any) => {
const downgradeMessage = values?.message ?? message
+ let token = captchaToken
+ if (!token) {
+ const captchaResponse = await captchaRef.current?.execute({ async: true })
+ token = captchaResponse?.response ?? null
+ }
+
try {
setIsSubmitting(true)
@@ -110,6 +120,7 @@ const ExitSurvey: FC<Props> = ({ freeTier, subscription, onSelectBack }) => {
tier,
addons,
proration_date,
+ hcaptchaToken: token ?? undefined
})
if (res?.error) {
@@ -139,9 +150,9 @@ const ExitSurvey: FC<Props> = ({ freeTier, subscription, onSelectBack }) => {
}
const feedbackRes = await post(`${API_URL}/feedback/downgrade`, {
projectRef,
- reasons: selectedReasons.reduce((a, b) => `${a}- ${b}\n`, ''),
- additionalFeedback: downgradeMessage,
- exitAction: 'downgrade',
+ reasons: selectedReasons.reduce((a, b) => `${a}- ${b}\n`, ''),
+ additionalFeedback: downgradeMessage,
+ exitAction: 'downgrade',
})
if (feedbackRes.error) throw feedbackRes.error
} catch (error: any) {
@@ -245,6 +256,19 @@ const ExitSurvey: FC<Props> = ({ freeTier, subscription, onSelectBack }) => {
credits
</p>
</div>
+ <div className="self-center">
+ <HCaptcha
+ ref={captchaRef}
+ sitekey={process.env.NEXT_PUBLIC_HCAPTCHA_SITE_KEY!}
+ size="invisible"
+ onVerify={(token) => {
+ setCaptchaToken(token)
+ }}
+ onExpire={() => {
+ setCaptchaToken(null)
+ }}
+ />
+ </div>
</div>
)}
</Form>
diff --git a/studio/components/interfaces/Billing/PaymentSummaryPanel/PaymentSummaryPanel.tsx b/studio/components/interfaces/Billing/PaymentSummaryPanel/PaymentSummaryPanel.tsx
index 078d2a067d2..9e0db842011 100644
--- a/studio/components/interfaces/Billing/PaymentSummaryPanel/PaymentSummaryPanel.tsx
+++ b/studio/components/interfaces/Billing/PaymentSummaryPanel/PaymentSummaryPanel.tsx
@@ -1,4 +1,4 @@
-import { FC, useState } from 'react'
+import { FC, useRef, useState } from 'react'
import * as Tooltip from '@radix-ui/react-tooltip'
import { Listbox, IconLoader, Button, IconPlus, IconAlertCircle, IconCreditCard } from 'ui'
import { PermissionAction } from '@supabase/shared-types/out/constants'
@@ -44,6 +44,8 @@ interface Props {
onSelectAddNewPaymentMethod: () => void
onConfirmPayment: () => void
isSubmitting: boolean
+
+ captcha: React.ReactNode
}
// Use case of this panel is actually only for upgrading from Free to Pro
@@ -72,6 +74,7 @@ const PaymentSummaryPanel: FC<Props> = ({
onSelectAddNewPaymentMethod,
onConfirmPayment,
isSubmitting,
+ captcha,
}) => {
const { ui } = useStore()
const projectRegion = ui.selectedProject?.region
@@ -120,7 +123,7 @@ const PaymentSummaryPanel: FC<Props> = ({
).toFixed(2)
}
- const validateOrder = () => {
+ const validateOrder = async () => {
const error = validateSubscriptionUpdatePayload(selectedAddons)
if (error) {
return ui.setNotification({
@@ -382,6 +385,8 @@ const PaymentSummaryPanel: FC<Props> = ({
)}
</div>
+ <div className="self-center">{captcha}</div>
+
<div className="flex items-center justify-end">
<Tooltip.Root delayDuration={0}>
<Tooltip.Trigger className="w-full">
diff --git a/studio/components/interfaces/Billing/ProUpgrade.tsx b/studio/components/interfaces/Billing/ProUpgrade.tsx
index 7fe2e78c578..68ff40f839e 100644
--- a/studio/components/interfaces/Billing/ProUpgrade.tsx
+++ b/studio/components/interfaces/Billing/ProUpgrade.tsx
@@ -1,4 +1,4 @@
-import { FC, useEffect, useState } from 'react'
+import { FC, useEffect, useRef, useState } from 'react'
import { Transition } from '@headlessui/react'
import { useRouter } from 'next/router'
import * as Tooltip from '@radix-ui/react-tooltip'
@@ -29,6 +29,7 @@ import {
} from './AddOns/AddOns.utils'
import BackButton from 'components/ui/BackButton'
import SupportPlan from './AddOns/SupportPlan'
+import HCaptcha from '@hcaptcha/react-hcaptcha'
// Do not allow compute size changes for af-south-1
@@ -52,6 +53,9 @@ const ProUpgrade: FC<Props> = ({
const isCustomDomainsEnabled = useFlag('customDomains')
const isPITRSelfServeEnabled = useFlag('pitrSelfServe')
+ const [captchaToken, setCaptchaToken] = useState<string | null>(null)
+ const captchaRef = useRef<HCaptcha>(null)
+
const { addons } = products
const computeSizes = formatComputeSizes(addons)
const pitrDurationOptions = formatPITROptions(addons)
@@ -133,7 +137,8 @@ const ProUpgrade: FC<Props> = ({
selectedAddons,
nonChangeableAddons,
selectedPaymentMethodId,
- projectRegion
+ projectRegion,
+ undefined
)
setIsRefreshingPreview(true)
@@ -149,13 +154,19 @@ const ProUpgrade: FC<Props> = ({
}
const onConfirmPayment = async () => {
+ let token = captchaToken
+ if (!token) {
+ const captchaResponse = await captchaRef.current?.execute({ async: true })
+ token = captchaResponse?.response ?? null
+ }
const payload = formSubscriptionUpdatePayload(
currentSubscription,
selectedTier,
selectedAddons,
nonChangeableAddons,
selectedPaymentMethodId,
- projectRegion
+ projectRegion,
+ token ?? undefined,
)
setIsSubmitting(true)
@@ -165,6 +176,8 @@ const ProUpgrade: FC<Props> = ({
category: 'error',
message: `Failed to update subscription: ${res?.error?.message}`,
})
+ setCaptchaToken(null)
+ captchaRef.current?.resetCaptcha()
} else {
if (isChangingComputeSize) {
app.onProjectStatusUpdated(projectId, PROJECT_STATUS.RESTORING)
@@ -320,6 +333,19 @@ const ProUpgrade: FC<Props> = ({
}}
onConfirmPayment={onConfirmPayment}
isSubmitting={isSubmitting}
+ captcha={
+ <HCaptcha
+ ref={captchaRef}
+ sitekey={process.env.NEXT_PUBLIC_HCAPTCHA_SITE_KEY!}
+ size="invisible"
+ onVerify={(token) => {
+ setCaptchaToken(token)
+ }}
+ onExpire={() => {
+ setCaptchaToken(null)
+ }}
+ />
+ }
/>
</div>
</Transition>