Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/billing-manage-subscription-seat-based.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/ui": patch
---

Fix the Manage Subscription button in `<UserProfile />` / `<OrganizationProfile />` and the Cancel / Re-subscribe actions in `<SubscriptionDetails />` so they are shown for paid seat-based plans that have no base fee. A shared `isManageableSubscriptionItem` helper now drives both places, treating "free / unmanageable" as "the instance's default plan" instead of "the plan has no base fee".
14 changes: 6 additions & 8 deletions packages/ui/src/components/SubscriptionDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { __internal_useOrganizationBase, useClerk } from '@clerk/shared/react';
import type {
__internal_CheckoutProps,
__internal_SubscriptionDetailsProps,
BillingPlanResource,
BillingSubscriptionItemResource,
} from '@clerk/shared/types';
import * as React from 'react';
import { useCallback, useContext, useState } from 'react';

import { Users } from '@/icons';
import { common } from '@/styledSystem';
import { useProtect } from '@/ui/common/Gate';
import {
SubscriptionDetailsContext,
Expand All @@ -19,6 +19,7 @@ import { CardAlert } from '@/ui/elements/Card/CardAlert';
import { useCardState, withCardStateProvider } from '@/ui/elements/contexts';
import { Drawer, useDrawerContext } from '@/ui/elements/Drawer';
import { LineItems } from '@/ui/elements/LineItems';
import { isManageableSubscriptionItem } from '@/ui/utils/billingSubscription';
import { handleError } from '@/ui/utils/errorHandler';
import { formatDate } from '@/ui/utils/formatDate';

Expand All @@ -44,9 +45,6 @@ import {
useLocalizations,
} from '../../customizables';
import { SubscriptionBadge } from '../Subscriptions/badge';
import { common } from '@/styledSystem';

const isFreePlan = (plan: BillingPlanResource) => !plan.hasBaseFee;

// We cannot derive the state of confirmation modal from the existence subscription, as it will make the animation laggy when the confirmation closes.
const SubscriptionForCancellationContext = React.createContext<{
Expand Down Expand Up @@ -376,14 +374,14 @@ const SubscriptionCardActions = ({ subscription }: { subscription: BillingSubscr
const canOrgManageBilling = useProtect(has => has({ permission: 'org:sys_billing:manage' }));
const canManageBilling = subscriberType === 'user' || canOrgManageBilling;

const isManageable = isManageableSubscriptionItem(subscription);
const isSwitchable =
((subscription.planPeriod === 'month' && Boolean(subscription.plan.annualMonthlyFee)) ||
(subscription.planPeriod === 'annual' && Boolean(subscription.plan.fee))) &&
subscription.status !== 'past_due' &&
!subscription.plan.isDefault;
const isFree = isFreePlan(subscription.plan);
const isCancellable = subscription.canceledAt === null && !isFree;
const isReSubscribable = subscription.canceledAt !== null && !isFree;
isManageable;
const isCancellable = subscription.canceledAt === null && isManageable;
const isReSubscribable = subscription.canceledAt !== null && isManageable;

const openCheckout = useCallback(
(params?: __internal_CheckoutProps) => {
Expand Down
14 changes: 7 additions & 7 deletions packages/ui/src/components/Subscriptions/SubscriptionsList.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { BillingPlanResource, BillingSubscriptionItemResource } from '@clerk/shared/types';
import type { BillingSubscriptionItemResource } from '@clerk/shared/types';
import { Fragment, useMemo } from 'react';

import { useProtect } from '@/ui/common/Gate';
import { ProfileSection } from '@/ui/elements/Section';
import { common } from '@/ui/styledSystem';
import { isManageableSubscriptionItem } from '@/ui/utils/billingSubscription';

import {
normalizeFormatted,
Expand All @@ -19,8 +20,6 @@ import { ArrowsUpDown, CogFilled, Plans, Plus, Users } from '../../icons';
import { useRouter } from '../../router';
import { SubscriptionBadge } from './badge';

const isFreePlan = (plan: BillingPlanResource) => !plan.hasBaseFee;

export function SubscriptionsList({
title,
switchPlansLabel,
Expand All @@ -45,11 +44,12 @@ export function SubscriptionsList({
(commerceSettings.billing.user.hasPaidPlans && subscriberType === 'user') ||
(commerceSettings.billing.organization.hasPaidPlans && subscriberType === 'organization');

const hasActiveFreePlan = useMemo(() => {
return subscriptionItems.some(sub => isFreePlan(sub.plan) && sub.status === 'active');
}, [subscriptionItems]);
const hasManageableSubscription = useMemo(
() => subscriptionItems.some(isManageableSubscriptionItem),
[subscriptionItems],
);

const isManageButtonVisible = canManageBilling && !hasActiveFreePlan && subscriptionItems.length > 0;
const isManageButtonVisible = canManageBilling && hasManageableSubscription;

const sortedSubscriptionItems = useMemo(
() =>
Expand Down
16 changes: 16 additions & 0 deletions packages/ui/src/utils/billingSubscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { BillingSubscriptionItemResource } from '@clerk/shared/types';

/**
* Returns `true` when a subscription item exposes at least one management
* action to the user (switch period, cancel, or re-subscribe).
*
* The only subscription a user cannot act on is the one backed by the
* instance's default plan, because every user is implicitly subscribed to it
* and cannot opt out.
*
* Intentionally not based on `plan.hasBaseFee`: a plan can have no base fee
* and still be a real paid subscription (e.g seat-based billing where the
* cost is driven entirely by a seat unit price).
*/
export const isManageableSubscriptionItem = (subscriptionItem: BillingSubscriptionItemResource): boolean =>
!subscriptionItem.plan.isDefault;
Comment thread
mauricioabreu marked this conversation as resolved.
Loading