Skip to content

feat(clerk-js,types): Update logic for showing Billing nav within profiles #6315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 21, 2025
Merged
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
6 changes: 6 additions & 0 deletions .changeset/public-bags-stay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/clerk-js': patch
'@clerk/types': patch
---

Adjust the cases in which the Billing item shows within the `UserProfile` and `OrgProfile` components
8 changes: 4 additions & 4 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ import {
createAllowedRedirectOrigins,
createBeforeUnloadTracker,
createPageLifecycle,
disabledAllBillingFeatures,
disabledAPIKeysFeature,
disabledBillingFeature,
disabledOrganizationsFeature,
errorThrower,
generateSignatureWithCoinbaseWallet,
Expand Down Expand Up @@ -576,7 +576,7 @@ export class Clerk implements ClerkInterface {

public __internal_openCheckout = (props?: __internal_CheckoutProps): void => {
this.assertComponentsReady(this.#componentControls);
if (disabledBillingFeature(this, this.environment)) {
if (disabledAllBillingFeatures(this, this.environment)) {
if (this.#instanceType === 'development') {
throw new ClerkRuntimeError(warnings.cannotRenderAnyCommerceComponent('Checkout'), {
code: CANNOT_RENDER_BILLING_DISABLED_ERROR_CODE,
Expand Down Expand Up @@ -605,7 +605,7 @@ export class Clerk implements ClerkInterface {

public __internal_openPlanDetails = (props: __internal_PlanDetailsProps): void => {
this.assertComponentsReady(this.#componentControls);
if (disabledBillingFeature(this, this.environment)) {
if (disabledAllBillingFeatures(this, this.environment)) {
Copy link
Contributor Author

@aeliox aeliox Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thought process on these is that since we don't necessarily know the subscriberType in these functions (right?) we will just check if all billing is disabled.

if (this.#instanceType === 'development') {
throw new ClerkRuntimeError(warnings.cannotRenderAnyCommerceComponent('PlanDetails'), {
code: CANNOT_RENDER_BILLING_DISABLED_ERROR_CODE,
Expand Down Expand Up @@ -1060,7 +1060,7 @@ export class Clerk implements ClerkInterface {

public mountPricingTable = (node: HTMLDivElement, props?: PricingTableProps): void => {
this.assertComponentsReady(this.#componentControls);
if (disabledBillingFeature(this, this.environment)) {
if (disabledAllBillingFeatures(this, this.environment)) {
if (this.#instanceType === 'development') {
throw new ClerkRuntimeError(warnings.cannotRenderAnyCommerceComponent('PricingTable'), {
code: CANNOT_RENDER_BILLING_DISABLED_ERROR_CODE,
Expand Down
20 changes: 20 additions & 0 deletions packages/clerk-js/src/core/resources/CommerceSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ export class CommerceSettings extends BaseResource implements CommerceSettingsRe
enabled: false,
hasPaidUserPlans: false,
hasPaidOrgPlans: false,
organization: {
enabled: false,
hasPaidPlans: false,
},
user: {
enabled: false,
hasPaidPlans: false,
},
};

public constructor(data: CommerceSettingsJSON | CommerceSettingsJSONSnapshot | null = null) {
Expand All @@ -27,6 +35,10 @@ export class CommerceSettings extends BaseResource implements CommerceSettingsRe
this.billing.enabled = data.billing.enabled || false;
this.billing.hasPaidUserPlans = data.billing.has_paid_user_plans || false;
this.billing.hasPaidOrgPlans = data.billing.has_paid_org_plans || false;
this.billing.organization.enabled = data.billing.organization.enabled || false;
this.billing.organization.hasPaidPlans = data.billing.organization.has_paid_plans || false;
this.billing.user.enabled = data.billing.user.enabled || false;
this.billing.user.hasPaidPlans = data.billing.user.has_paid_plans || false;

return this;
}
Expand All @@ -38,6 +50,14 @@ export class CommerceSettings extends BaseResource implements CommerceSettingsRe
enabled: this.billing.enabled,
has_paid_user_plans: this.billing.hasPaidUserPlans,
has_paid_org_plans: this.billing.hasPaidOrgPlans,
organization: {
enabled: this.billing.organization.enabled,
has_paid_plans: this.billing.organization.hasPaidPlans,
},
user: {
enabled: this.billing.user.enabled,
has_paid_plans: this.billing.user.hasPaidPlans,
},
},
} as unknown as CommerceSettingsJSONSnapshot;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const OrganizationProfileRoutes = () => {
</Route>
</Switch>
</Route>
{commerceSettings.billing.enabled && commerceSettings.billing.hasPaidOrgPlans && (
{commerceSettings.billing.organization.enabled ? (
<Protect
condition={has =>
has({ permission: 'org:sys_billing:read' }) || has({ permission: 'org:sys_billing:manage' })
Expand All @@ -96,11 +96,13 @@ export const OrganizationProfileRoutes = () => {
<OrganizationBillingPage />
</Suspense>
</Route>
<Route path='plans'>
<Suspense fallback={''}>
<OrganizationPlansPage />
</Suspense>
</Route>
{commerceSettings.billing.organization.hasPaidPlans ? (
<Route path='plans'>
<Suspense fallback={''}>
<OrganizationPlansPage />
</Suspense>
</Route>
) : null}
Comment on lines +99 to +105
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Fix type safety and approve the conditional plans route logic.

The logic to conditionally render the plans route based on available paid plans is excellent and aligns with the PR objectives. However, the same type safety issue exists here with nested property access.

Apply this diff to use optional chaining:

-                {commerceSettings.billing.organization.hasPaidPlans ? (
+                {commerceSettings.billing.organization?.hasPaidPlans ? (
🤖 Prompt for AI Agents
In
packages/clerk-js/src/ui/components/OrganizationProfile/OrganizationProfileRoutes.tsx
around lines 99 to 105, the conditional rendering of the plans route accesses
nested properties without type safety, risking runtime errors. Fix this by using
optional chaining when accessing
commerceSettings.billing.organization.hasPaidPlans to safely handle cases where
any intermediate property might be undefined or null.

<Route path='statement/:statementId'>
<Suspense fallback={''}>
<OrganizationStatementPage />
Expand All @@ -114,7 +116,7 @@ export const OrganizationProfileRoutes = () => {
</Switch>
</Route>
</Protect>
)}
) : null}
{apiKeysSettings.enabled && (
<Protect
condition={has =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ProfileSection } from '@/ui/elements/Section';

import { useProtect } from '../../common';
import {
useEnvironment,
usePlansContext,
useSubscriberTypeContext,
useSubscriberTypeLocalizationRoot,
Expand Down Expand Up @@ -44,6 +45,7 @@ export function SubscriptionsList({
has => has({ permission: 'org:sys_billing:manage' }) || subscriberType === 'user',
);
const { navigate } = useRouter();
const { commerceSettings } = useEnvironment();

const sortedSubscriptions = subscriptions.sort((a, b) => {
// alway put active subscriptions first
Expand Down Expand Up @@ -188,22 +190,25 @@ export function SubscriptionsList({
</Table>
)}

<ProfileSection.ArrowButton
id='subscriptionsList'
textLocalizationKey={subscriptions.length > 0 ? arrowButtonText : arrowButtonEmptyText}
sx={[
t => ({
justifyContent: 'start',
height: t.sizes.$8,
}),
]}
leftIcon={subscriptions.length > 0 ? ArrowsUpDown : Plus}
leftIconSx={t => ({
width: t.sizes.$4,
height: t.sizes.$4,
})}
onClick={() => void navigate('plans')}
/>
{(commerceSettings.billing.user.hasPaidPlans && subscriberType === 'user') ||
(commerceSettings.billing.organization.hasPaidPlans && subscriberType === 'org') ? (
<ProfileSection.ArrowButton
id='subscriptionsList'
textLocalizationKey={subscriptions.length > 0 ? arrowButtonText : arrowButtonEmptyText}
sx={[
t => ({
justifyContent: 'start',
height: t.sizes.$8,
}),
]}
leftIcon={subscriptions.length > 0 ? ArrowsUpDown : Plus}
leftIconSx={t => ({
width: t.sizes.$4,
height: t.sizes.$4,
})}
onClick={() => void navigate('plans')}
/>
) : null}
</ProfileSection.Root>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,21 @@ export const UserProfileRoutes = () => {
</Route>
</Switch>
</Route>
{commerceSettings.billing.enabled && commerceSettings.billing.hasPaidUserPlans && (
{commerceSettings.billing.user.enabled ? (
<Route path={isBillingPageRoot ? undefined : 'billing'}>
<Switch>
<Route index>
<Suspense fallback={''}>
<BillingPage />
</Suspense>
</Route>
<Route path='plans'>
<Suspense fallback={''}>
<PlansPage />
</Suspense>
</Route>
{commerceSettings.billing.user.hasPaidPlans ? (
<Route path='plans'>
<Suspense fallback={''}>
<PlansPage />
</Suspense>
</Route>
) : null}
<Route path='statement/:statementId'>
<Suspense fallback={''}>
<StatementPage />
Expand All @@ -105,7 +107,7 @@ export const UserProfileRoutes = () => {
</Route>
</Switch>
</Route>
)}
) : null}
{apiKeysSettings.enabled && (
<Route path={isAPIKeysPageRoot ? undefined : 'api-keys'}>
<Switch>
Expand Down
11 changes: 5 additions & 6 deletions packages/clerk-js/src/ui/utils/createCustomPages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import type { CustomPage, EnvironmentResource, LoadedClerk } from '@clerk/types'
import {
canViewOrManageAPIKeys,
disabledAPIKeysFeature,
disabledBillingFeature,
hasPaidOrgPlans,
hasPaidUserPlans,
disabledOrganizationBillingFeature,
disabledUserBillingFeature,
isValidUrl,
} from '../../utils';
import { ORGANIZATION_PROFILE_NAVBAR_ROUTE_ID, USER_PROFILE_NAVBAR_ROUTE_ID } from '../constants';
Expand Down Expand Up @@ -97,9 +96,9 @@ const createCustomPages = (
organization?: boolean,
) => {
const { INITIAL_ROUTES, pageToRootNavbarRouteMap, validReorderItemLabels } = getDefaultRoutes({
commerce:
!disabledBillingFeature(clerk, environment) &&
(organization ? hasPaidOrgPlans(clerk, environment) : hasPaidUserPlans(clerk, environment)),
commerce: organization
? !disabledOrganizationBillingFeature(clerk, environment)
: !disabledUserBillingFeature(clerk, environment),
apiKeys: !disabledAPIKeysFeature(clerk, environment) && (organization ? canViewOrManageAPIKeys(clerk) : true),
});

Expand Down
16 changes: 12 additions & 4 deletions packages/clerk-js/src/utils/componentGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,24 @@ export const disabledOrganizationsFeature: ComponentGuard = (_, environment) =>
return !environment?.organizationSettings.enabled;
};

export const disabledBillingFeature: ComponentGuard = (_, environment) => {
return !environment?.commerceSettings.billing.enabled;
export const disabledUserBillingFeature: ComponentGuard = (_, environment) => {
return !environment?.commerceSettings.billing.user.enabled;
};

export const disabledOrganizationBillingFeature: ComponentGuard = (_, environment) => {
return !environment?.commerceSettings.billing.organization.enabled;
};

export const disabledAllBillingFeatures: ComponentGuard = (_, environment) => {
return disabledUserBillingFeature(_, environment) && disabledOrganizationBillingFeature(_, environment);
};

export const hasPaidOrgPlans: ComponentGuard = (_, environment) => {
return environment?.commerceSettings.billing.hasPaidOrgPlans || false;
return environment?.commerceSettings.billing.organization.hasPaidPlans || false;
};

export const hasPaidUserPlans: ComponentGuard = (_, environment) => {
return environment?.commerceSettings.billing.hasPaidUserPlans || false;
return environment?.commerceSettings.billing.user.hasPaidPlans || false;
};

export const disabledAPIKeysFeature: ComponentGuard = (_, environment) => {
Expand Down
16 changes: 16 additions & 0 deletions packages/types/src/commerceSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ export interface CommerceSettingsJSON extends ClerkResourceJSON {
stripe_publishable_key: string;
has_paid_user_plans: boolean;
has_paid_org_plans: boolean;
organization: {
enabled: boolean;
has_paid_plans: boolean;
};
user: {
enabled: boolean;
has_paid_plans: boolean;
};
};
}

Expand All @@ -18,6 +26,14 @@ export interface CommerceSettingsResource extends ClerkResource {
stripePublishableKey: string;
hasPaidUserPlans: boolean;
hasPaidOrgPlans: boolean;
organization: {
enabled: boolean;
hasPaidPlans: boolean;
};
user: {
enabled: boolean;
hasPaidPlans: boolean;
};
};

__internal_toSnapshot: () => CommerceSettingsJSONSnapshot;
Expand Down
Loading