Skip to content

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

feat(billing): show cancel subscription button on billing page (#46500)

Julian Domkedfaf59010b00
diff --git a/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/CancellationFlow.tsx b/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/CancellationFlow.tsx
new file mode 100644
index 00000000000..c7067c8bffc
--- /dev/null
+++ b/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/CancellationFlow.tsx
@@ -0,0 +1,173 @@
+import { PermissionAction } from '@supabase/shared-types/out/constants'
+import { useParams } from 'common'
+import React, { ComponentProps, useEffect, useMemo, useState } from 'react'
+
+import { DowngradeModal } from './DowngradeModal'
+import { ExitSurveyModal } from './ExitSurveyModal'
+import MembersExceedLimitModal from './MembersExceedLimitModal'
+import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
+import { useFreeProjectLimitCheckQuery } from '@/data/organizations/free-project-limit-check-query'
+import { useOrgProjectsInfiniteQuery } from '@/data/projects/org-projects-infinite-query'
+import { useOrgSubscriptionQuery } from '@/data/subscriptions/org-subscription-query'
+import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
+import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
+import { MANAGED_BY } from '@/lib/constants'
+import { useTrack } from '@/lib/telemetry/track'
+
+type CancellationFlowProps = {
+ onDowngrade?: () => void
+ onCancel?: () => void
+ visible: boolean
+}
+
+type CancellationFlowState =
+ | 'closed'
+ | 'show-downgrade-modal'
+ | 'show-downgrade-error'
+ | 'show-exit-survey'
+
+export const CancellationFlow = (props: CancellationFlowProps) => {
+ const { slug } = useParams()
+
+ const [flowState, setFlowState] = useState<CancellationFlowState>(
+ props.visible ? 'show-downgrade-modal' : 'closed'
+ )
+ useEffect(() => {
+ setFlowState(props.visible ? 'show-downgrade-modal' : 'closed')
+ }, [props.visible])
+
+ const { data: membersExceededLimit, isLoading: projectLimitQueryLoading } =
+ useFreeProjectLimitCheckQuery({ slug }, { enabled: flowState !== 'closed' })
+
+ const { data: orgProjectsData, isLoading: orgProjectsQueryLoading } = useOrgProjectsInfiniteQuery(
+ { slug },
+ { enabled: flowState !== 'closed' }
+ )
+
+ const { data: subscription, isLoading: orgSubscriptionQueryLoading } = useOrgSubscriptionQuery({
+ orgSlug: slug,
+ })
+
+ const isLoading =
+ projectLimitQueryLoading || orgProjectsQueryLoading || orgSubscriptionQueryLoading
+
+ const orgProjects =
+ useMemo(
+ () => orgProjectsData?.pages.flatMap((page) => page.projects),
+ [orgProjectsData?.pages]
+ ) || []
+
+ // [Joshen] Note that orgProjects is paginated so there's a chance this may omit certain projects
+ // Although I don't foresee this affecting a majority of users. Ideally perhaps we could return
+ // this data from the organization query
+ const hasRunningProjects =
+ orgProjects.filter((it) => it.status !== 'INACTIVE' && it.status !== 'GOING_DOWN').length > 0
+
+ const hasMembersExceedingFreeTierLimit =
+ (membersExceededLimit || []).length > 0 && hasRunningProjects
+
+ const onConfirmDowngrade = () => {
+ if (hasMembersExceedingFreeTierLimit) {
+ setFlowState('show-downgrade-error')
+ } else {
+ setFlowState('show-exit-survey')
+ }
+ }
+
+ const onCancelFlow = () => {
+ setFlowState('closed')
+ props.onCancel?.()
+ }
+
+ return (
+ <>
+ <DowngradeModal
+ visible={flowState === 'show-downgrade-modal'}
+ subscription={subscription}
+ onClose={onCancelFlow}
+ confirmDisabled={isLoading}
+ onConfirm={onConfirmDowngrade}
+ projects={orgProjects}
+ />
+
+ <MembersExceedLimitModal
+ visible={flowState === 'show-downgrade-error'}
+ onClose={onCancelFlow}
+ />
+
+ <ExitSurveyModal
+ visible={flowState === 'show-exit-survey'}
+ projects={orgProjects}
+ onClose={(success?: boolean) => {
+ if (success) {
+ setFlowState('closed')
+ props.onDowngrade?.()
+ } else {
+ onCancelFlow()
+ }
+ }}
+ />
+ </>
+ )
+}
+
+type InitiateCancellationFlowButtonProps = Pick<
+ ComponentProps<typeof ButtonTooltip>,
+ 'children' | 'type'
+>
+
+export const InitiateCancellationFlowButton = (props: InitiateCancellationFlowButtonProps) => {
+ const { slug } = useParams()
+ const track = useTrack()
+
+ const { data: selectedOrganization } = useSelectedOrganizationQuery()
+ const isAwsManaged = selectedOrganization?.managed_by === MANAGED_BY.AWS_MARKETPLACE
+
+ const { can: canUpdateSubscription } = useAsyncCheckPermissions(
+ PermissionAction.BILLING_WRITE,
+ 'stripe.subscriptions'
+ )
+
+ const { data: subscription } = useOrgSubscriptionQuery({
+ orgSlug: slug,
+ })
+
+ const isDowngradeablePlan = !subscription || ['pro', 'team'].includes(subscription.plan.id)
+
+ const [visible, setVisible] = useState(false)
+
+ const tooltipText = [
+ [isAwsManaged, 'You cannot change the plan for an organization managed by AWS Marketplace.'],
+ [!isDowngradeablePlan, 'Reach out to us via support to update your plan.'],
+ [!canUpdateSubscription, "You need additional permissions to change your organization's plan."],
+ ].find(([cond]) => cond)?.[1]
+
+ return (
+ <>
+ <ButtonTooltip
+ type={props.type}
+ disabled={!isDowngradeablePlan || !canUpdateSubscription || isAwsManaged}
+ tooltip={{
+ content: {
+ side: 'bottom',
+ text: tooltipText,
+ },
+ }}
+ onClick={() => {
+ track('studio_billing_cancel_subscription_clicked', {
+ currentPlan: subscription?.plan.id || 'free',
+ })
+ setVisible(true)
+ }}
+ >
+ {props.children ?? 'Cancel Subscription'}
+ </ButtonTooltip>
+
+ <CancellationFlow
+ onCancel={() => setVisible(false)}
+ onDowngrade={() => setVisible(false)}
+ visible={visible}
+ />
+ </>
+ )
+}
diff --git a/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/DowngradeModal.tsx b/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/DowngradeModal.tsx
index 149f7c27486..194495f8bf5 100644
--- a/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/DowngradeModal.tsx
+++ b/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/DowngradeModal.tsx
@@ -23,6 +23,7 @@ export interface DowngradeModalProps {
onClose: () => void
onConfirm: () => void
projects: OrgProject[]
+ confirmDisabled?: boolean
}
const ProjectDowngradeListItem = ({ projectAddon }: { projectAddon: ProjectAddon }) => {
@@ -64,6 +65,7 @@ export const DowngradeModal = ({
onClose,
onConfirm,
projects,
+ confirmDisabled,
}: DowngradeModalProps) => {
const selectedPlan = useMemo(() => subscriptionsPlans.find((tier) => tier.id === 'tier_free'), [])
@@ -183,7 +185,14 @@ export const DowngradeModal = ({
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
- <AlertDialogAction variant="warning" onClick={onConfirm}>
+ <AlertDialogAction
+ disabled={confirmDisabled ?? false}
+ variant="warning"
+ onClick={(e) => {
+ e.preventDefault()
+ onConfirm()
+ }}
+ >
Confirm
</AlertDialogAction>
</AlertDialogFooter>
diff --git a/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/ExitSurveyModal.tsx b/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/ExitSurveyModal.tsx
index 18a44901681..523119d7df7 100644
--- a/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/ExitSurveyModal.tsx
+++ b/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/ExitSurveyModal.tsx
@@ -201,7 +201,7 @@ export const ExitSurveyModal = ({ visible, projects, onClose }: ExitSurveyModalP
disabled={subscriptionUpdateDisabled || isSubmitting}
onClick={onSubmit}
>
- Confirm downgrade
+ Downgrade Now
</Button>
</ProjectUpdateDisabledTooltip>
</DialogFooter>
diff --git a/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/PlanUpdateSidePanel.tsx b/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/PlanUpdateSidePanel.tsx
index 59a65fc5b23..22645551a3f 100644
--- a/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/PlanUpdateSidePanel.tsx
+++ b/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/PlanUpdateSidePanel.tsx
@@ -10,10 +10,8 @@ import { plans as subscriptionsPlans } from 'shared-data/plans'
import { Button, cn, SidePanel } from 'ui'
import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
-import { DowngradeModal } from './DowngradeModal'
+import { CancellationFlow } from './CancellationFlow'
import { EnterpriseCard } from './EnterpriseCard'
-import { ExitSurveyModal } from './ExitSurveyModal'
-import MembersExceedLimitModal from './MembersExceedLimitModal'
import { SubscriptionPlanUpdateDialog } from './SubscriptionPlanUpdateDialog'
import UpgradeSurveyModal from './UpgradeModal'
import { STRIPE_PROJECTS_DOCS_URL } from '@/components/interfaces/Billing/Payment/PaymentMethods/StripePaymentConnection'
@@ -21,7 +19,6 @@ import { getPlanChangeType } from '@/components/interfaces/Billing/Subscription/
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import PartnerManagedResource from '@/components/ui/PartnerManagedResource'
import { RequestUpgradeToBillingOwners } from '@/components/ui/RequestUpgradeToBillingOwners'
-import { useFreeProjectLimitCheckQuery } from '@/data/organizations/free-project-limit-check-query'
import { isPartnerBillingOrganization } from '@/data/organizations/managed-by-utils'
import { useOrganizationBillingSubscriptionPreview } from '@/data/organizations/organization-billing-subscription-preview'
import { useOrganizationQuery } from '@/data/organizations/organization-query'
@@ -72,9 +69,7 @@ export const PlanUpdateSidePanel = () => {
const originalPlanRef = useRef<string>(undefined)
- const [showExitSurvey, setShowExitSurvey] = useState(false)
const [showUpgradeSurvey, setShowUpgradeSurvey] = useState(false)
- const [showDowngradeError, setShowDowngradeError] = useState(false)
const [selectedTier, setSelectedTier] = useState<'tier_free' | 'tier_pro' | 'tier_team'>()
const [latestAddress, setLatestAddress] = useState<CustomerAddress>()
const [latestTaxId, setLatestTaxId] = useState<CustomerTaxId | null>()
@@ -130,10 +125,6 @@ export const PlanUpdateSidePanel = () => {
{ orgSlug: slug },
{ enabled: visible }
)
- const { data: membersExceededLimit } = useFreeProjectLimitCheckQuery(
- { slug },
- { enabled: visible }
- )
const subscriptionPreviewData = useOrganizationBillingSubscriptionPreview({
tier: selectedTier,
@@ -143,12 +134,6 @@ export const PlanUpdateSidePanel = () => {
})
const availablePlans: OrgPlan[] = plans?.plans ?? []
- const hasMembersExceedingFreeTierLimit =
- (membersExceededLimit || []).length > 0 &&
- // [Joshen] Note that orgProjects is paginated so there's a chance this may omit certain projects
- // Although I don't foresee this affecting a majority of users. Ideally perhaps we could return
- // this data from the organization query
- orgProjects.filter((it) => it.status !== 'INACTIVE' && it.status !== 'GOING_DOWN').length > 0
const onPanelOpened = useEffectEvent(
(properties: StudioPricingSidePanelOpenedEvent['properties']) => {
@@ -182,15 +167,6 @@ export const PlanUpdateSidePanel = () => {
}
}, [visible, isSuccessSubscription, subscription?.plan.id])
- const onConfirmDowngrade = () => {
- setSelectedTier(undefined)
- if (hasMembersExceedingFreeTierLimit) {
- setShowDowngradeError(true)
- } else {
- setShowExitSurvey(true)
- }
- }
-
const planMeta = selectedTier
? availablePlans.find((p) => p.id === selectedTier.split('tier_')[1])
: null
@@ -395,12 +371,10 @@ export const PlanUpdateSidePanel = () => {
</SidePanel.Content>
</SidePanel>
- <DowngradeModal
+ <CancellationFlow
visible={selectedTier === 'tier_free'}
- subscription={subscription}
- onClose={() => setSelectedTier(undefined)}
- onConfirm={onConfirmDowngrade}
- projects={orgProjects}
+ onCancel={() => setSelectedTier(undefined)}
+ onDowngrade={() => setSelectedTier(undefined)}
/>
<SubscriptionPlanUpdateDialog
@@ -416,20 +390,6 @@ export const PlanUpdateSidePanel = () => {
onUseAsDefaultBillingAddressChange={handleUseAsDefaultBillingAddressChange}
/>
- <MembersExceedLimitModal
- visible={showDowngradeError}
- onClose={() => setShowDowngradeError(false)}
- />
-
- <ExitSurveyModal
- visible={showExitSurvey}
- projects={orgProjects}
- onClose={(success?: boolean) => {
- setShowExitSurvey(false)
- if (success) onClose()
- }}
- />
-
<UpgradeSurveyModal
visible={showUpgradeSurvey}
originalPlan={originalPlanRef.current}
diff --git a/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/Subscription.tsx b/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/Subscription.tsx
index 30817aa61a7..07389bf35c8 100644
--- a/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/Subscription.tsx
+++ b/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/Subscription.tsx
@@ -5,8 +5,8 @@ import { Button } from 'ui'
import { Admonition } from 'ui-patterns/admonition'
import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
-import { ProjectUpdateDisabledTooltip } from '../ProjectUpdateDisabledTooltip'
import { Restriction } from '../Restriction'
+import { InitiateCancellationFlowButton } from './CancellationFlow'
import { PlanUpdateSidePanel } from './PlanUpdateSidePanel'
import { SupportLink } from '@/components/interfaces/Support/SupportLink'
import {
@@ -77,53 +77,63 @@ const Subscription = () => {
{isError && <AlertError subject="Failed to retrieve subscription" error={error} />}
{isSuccess && (
- <div className="space-y-6">
- <div>
- <p className="text-2xl text-brand">{currentPlan?.name ?? 'Unknown'} Plan</p>
- </div>
+ <div className="space-y-6 w-full">
+ <div className="flex justify-between items-center">
+ <p className="text-2xl text-brand leading-none">
+ {currentPlan?.name ?? 'Unknown'} Plan
+ </p>
- <div>
- {canChangeTier ? (
- <ProjectUpdateDisabledTooltip projectUpdateDisabled={projectUpdateDisabled}>
+ {canChangeTier && (
+ <div className="flex space-x-2">
<Button
type="default"
className="pointer-events-auto"
- disabled={!canChangeTier}
onClick={() => snap.setPanelKey('subscriptionPlan')}
>
Change subscription plan
</Button>
- </ProjectUpdateDisabledTooltip>
- ) : projectUpdateDisabled ? (
- <Admonition
- type="default"
- layout="horizontal"
- title={`Unable to update plan from ${planName}`}
- description="We have temporarily disabled project and subscription changes - our
- engineers are working on a fix."
- />
- ) : (
- <Admonition
- type="default"
- layout="horizontal"
- title={`Unable to update plan from ${planName}`}
- description="Please contact us if you'd like to change your plan."
- actions={
- <Button asChild key="contact-support" type="default">
- <SupportLink
- queryParams={{
- category: SupportCategories.SALES_ENQUIRY,
- subject: `Change plan away from ${planName}`,
- }}
- >
- Contact support
- </SupportLink>
- </Button>
- }
- />
+ {currentPlan && currentPlan.id !== 'free' && (
+ <InitiateCancellationFlowButton type="danger">
+ Cancel Subscription
+ </InitiateCancellationFlowButton>
+ )}
+ </div>
)}
</div>
+ {!canChangeTier && (
+ <div>
+ {projectUpdateDisabled ? (
+ <Admonition
+ type="default"
+ layout="horizontal"
+ title={`Unable to update plan from ${planName}`}
+ description="We have temporarily disabled project and subscription changes - our
+ engineers are working on a fix."
+ />
+ ) : (
+ <Admonition
+ type="default"
+ layout="horizontal"
+ title={`Unable to update plan from ${planName}`}
+ description="Please contact us if you'd like to change your plan."
+ actions={
+ <Button asChild key="contact-support" type="default">
+ <SupportLink
+ queryParams={{
+ category: SupportCategories.SALES_ENQUIRY,
+ subject: `Change plan away from ${planName}`,
+ }}
+ >
+ Contact support
+ </SupportLink>
+ </Button>
+ }
+ />
+ )}
+ </div>
+ )}
+
{!subscription?.usage_billing_enabled && (
<Admonition
type="default"
diff --git a/packages/common/telemetry-constants.ts b/packages/common/telemetry-constants.ts
index 4899cfce013..c55879f79ef 100644
--- a/packages/common/telemetry-constants.ts
+++ b/packages/common/telemetry-constants.ts
@@ -1150,6 +1150,24 @@ export interface SqlEditorQueryRunButtonClickedEvent {
groups: TelemetryGroups
}
+/**
+ * User clicked on the "Cancel Subscription" Button on the billing settings page.
+ *
+ * @group Events
+ * @source studio
+ * @page /billing
+ */
+export interface StudioBillingCancelSubscriptionClickedEvent {
+ action: 'studio_billing_cancel_subscription_clicked'
+ properties: {
+ /**
+ * The plan type the org is currently on.
+ */
+ currentPlan: string
+ }
+ groups: Omit<TelemetryGroups, 'project'>
+}
+
/**
* User clicked on the CTA button on a plan in the pricing side panel in studio.
*
@@ -3341,6 +3359,7 @@ export type TelemetryEvent =
| StoragePublicBucketSelectPolicyRemovedEvent
| StoragePublicBucketSelectPolicyWarningDismissButtonClickedEvent
| StudioPricingPlanCtaClickedEvent
+ | StudioBillingCancelSubscriptionClickedEvent
| StudioPricingSidePanelOpenedEvent
| ReportsDatabaseGrafanaBannerClickedEvent
| MetricsAPIBannerCtaButtonClickedEvent