Skip to content
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
9 changes: 9 additions & 0 deletions .changeset/some-parents-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@clerk/clerk-js': minor
'@clerk/shared': minor
'@clerk/types': minor
---

[Billing Beta]: Replace `org` for `organization` as payer type for billing APIs.

This applies for all billing APIs, except the resources classes that represent data from Frontend API.
3 changes: 2 additions & 1 deletion .typedoc/__tests__/__snapshots__/file-structure.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ exports[`Typedoc output > should have a deliberate file structure 1`] = `
"types/commerce-initialized-payment-source-resource.mdx",
"types/commerce-money-json.mdx",
"types/commerce-money.mdx",
"types/commerce-payer-type.mdx",
"types/commerce-payer-resource-type.mdx",
"types/commerce-payment-charge-type.mdx",
"types/commerce-payment-json.mdx",
"types/commerce-payment-resource.mdx",
Expand Down Expand Up @@ -66,6 +66,7 @@ exports[`Typedoc output > should have a deliberate file structure 1`] = `
"types/experimental_checkout-button-props.mdx",
"types/experimental_plan-details-button-props.mdx",
"types/experimental_subscription-details-button-props.mdx",
"types/for-payer-type.mdx",
"types/get-payment-attempts-params.mdx",
"types/get-payment-sources-params.mdx",
"types/get-plans-params.mdx",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
export class CommerceBilling implements CommerceBillingNamespace {
getPlans = async (params?: GetPlansParams): Promise<ClerkPaginatedResponse<CommercePlanResource>> => {
const { for: forParam, ...safeParams } = params || {};
const searchParams = { ...safeParams, payer_type: forParam || 'user' };
const searchParams = { ...safeParams, payer_type: forParam === 'organization' ? 'org' : 'user' };
return await BaseResource._fetch({
path: `/commerce/plans`,
method: 'GET',
Expand Down
9 changes: 7 additions & 2 deletions packages/clerk-js/src/core/resources/CommercePlan.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { CommercePayerType, CommercePlanJSON, CommercePlanJSONSnapshot, CommercePlanResource } from '@clerk/types';
import type {
CommercePayerResourceType,
CommercePlanJSON,
CommercePlanJSONSnapshot,
CommercePlanResource,
} from '@clerk/types';

import { BaseResource, CommerceFeature } from './internal';

Expand All @@ -17,7 +22,7 @@ export class CommercePlan extends BaseResource implements CommercePlanResource {
isDefault!: boolean;
isRecurring!: boolean;
hasBaseFee!: boolean;
forPayerType!: CommercePayerType;
forPayerType!: CommercePayerResourceType;
publiclyVisible!: boolean;
slug!: string;
avatarUrl!: string;
Expand Down
5 changes: 3 additions & 2 deletions packages/clerk-js/src/ui/components/Checkout/CheckoutForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const CheckoutForm = withCardStateProvider(() => {

const useCheckoutMutations = () => {
const { organization } = useOrganization();
const { subscriberType, onSubscriptionComplete } = useCheckoutContext();
const { for: _for, onSubscriptionComplete } = useCheckoutContext();
const { checkout } = useCheckout();
const { id, confirm } = checkout;
const card = useCardState();
Expand All @@ -130,7 +130,8 @@ const useCheckoutMutations = () => {

const { data, error } = await confirm({
...params,
...(subscriberType === 'org' ? { orgId: organization?.id } : {}),
// TODO(@COMMERCE): Come back to this, this should not be needed
...(_for === 'organization' ? { orgId: organization?.id } : {}),
});

if (error) {
Expand Down
6 changes: 3 additions & 3 deletions packages/clerk-js/src/ui/components/Checkout/CheckoutPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ const Initiator = () => {
const { checkout } = useCheckout();

useEffect(() => {
checkout.start().catch(() => null);
void checkout.start();
return checkout.clear;
}, []);
return null;
};

const Root = ({ children }: { children: React.ReactNode }) => {
const { planId, planPeriod, subscriberType } = useCheckoutContext();
const { planId, planPeriod, for: _for } = useCheckoutContext();

return (
<CheckoutProvider
for={subscriberType === 'org' ? 'organization' : undefined}
for={_for}
planId={
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
planId!
Expand Down
2 changes: 1 addition & 1 deletion packages/clerk-js/src/ui/components/Checkout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const Checkout = (props: __internal_CheckoutProps) => {
return (
<Flow.Root flow='checkout'>
<Flow.Part>
<SubscriberTypeContext.Provider value={props.subscriberType || 'user'}>
<SubscriberTypeContext.Provider value={props.for || 'user'}>
<CheckoutContext.Provider
value={{
componentName: 'Checkout',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const OrganizationBillingPageInternal = withCardStateProvider(() => {

export const OrganizationBillingPage = () => {
return (
<SubscriberTypeContext.Provider value='org'>
<SubscriberTypeContext.Provider value='organization'>
<OrganizationBillingPageInternal />
</SubscriberTypeContext.Provider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PaymentAttemptPage } from '../PaymentAttempts';

export const OrganizationPaymentAttemptPage = () => {
return (
<SubscriberTypeContext.Provider value='org'>
<SubscriberTypeContext.Provider value='organization'>
<PaymentAttemptPage />
</SubscriberTypeContext.Provider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const OrganizationPlansPageInternal = () => {

export const OrganizationPlansPage = () => {
return (
<SubscriberTypeContext.Provider value='org'>
<SubscriberTypeContext.Provider value='organization'>
<OrganizationPlansPageInternal />
</SubscriberTypeContext.Provider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StatementPage } from '../Statements';

export const OrganizationStatementPage = () => {
return (
<SubscriberTypeContext.Provider value='org'>
<SubscriberTypeContext.Provider value='organization'>
<StatementPage />
</SubscriberTypeContext.Provider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const AddScreen = withCardStateProvider(({ onSuccess }: { onSuccess: () => void
const localizationRoot = useSubscriberTypeLocalizationRoot();

const onAddPaymentSourceSuccess = async (context: { gateway: 'stripe'; paymentToken: string }) => {
const resource = subscriberType === 'org' ? clerk?.organization : clerk.user;
const resource = subscriberType === 'organization' ? clerk?.organization : clerk.user;
await resource?.addPaymentSource(context);
onSuccess();
close();
Expand Down Expand Up @@ -72,7 +72,7 @@ const RemoveScreen = ({

const removePaymentSource = async () => {
await paymentSource
.remove({ orgId: subscriberType === 'org' ? organization?.id : undefined })
.remove({ orgId: subscriberType === 'organization' ? organization?.id : undefined })
.then(revalidate)
.catch((error: Error) => {
handleError(error, [], card.setError);
Expand Down Expand Up @@ -108,7 +108,7 @@ export const PaymentSources = withCardStateProvider(() => {
const clerk = useClerk();
const subscriberType = useSubscriberTypeContext();
const localizationRoot = useSubscriberTypeLocalizationRoot();
const resource = subscriberType === 'org' ? clerk?.organization : clerk.user;
const resource = subscriberType === 'organization' ? clerk?.organization : clerk.user;

const { data: paymentMethods, isLoading, revalidate: revalidatePaymentMethods } = usePaymentMethods();

Expand Down Expand Up @@ -209,7 +209,7 @@ const PaymentSourceMenu = ({
isDestructive: false,
onClick: () => {
paymentSource
.makeDefault({ orgId: subscriberType === 'org' ? organization?.id : undefined })
.makeDefault({ orgId: subscriberType === 'organization' ? organization?.id : undefined })
.then(revalidate)
.catch((error: Error) => {
handleError(error, [], card.setError);
Expand Down
2 changes: 1 addition & 1 deletion packages/clerk-js/src/ui/components/Plans/PlanDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const PlanDetailsInternal = ({
const hasFeatures = features.length > 0;

return (
<SubscriberTypeContext.Provider value={plan.forPayerType}>
<SubscriberTypeContext.Provider value={plan.forPayerType === 'org' ? 'organization' : 'user'}>
<Drawer.Header
sx={t =>
!hasFeatures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ const SubscriptionDetailsFooter = withCardStateProvider(() => {
setLoading();

await selectedSubscription
.cancel({ orgId: subscriberType === 'org' ? organization?.id : undefined })
.cancel({ orgId: subscriberType === 'organization' ? organization?.id : undefined })
.then(() => {
onSubscriptionCancel?.();
if (setIsOpen) {
Expand Down Expand Up @@ -381,7 +381,7 @@ const SubscriptionCardActions = ({ subscription }: { subscription: CommerceSubsc
openCheckout({
planId: subscription.plan.id,
planPeriod: subscription.planPeriod === 'month' ? 'annual' : 'month',
subscriberType,
for: subscriberType,
});
},
}
Expand All @@ -403,7 +403,7 @@ const SubscriptionCardActions = ({ subscription }: { subscription: CommerceSubsc
openCheckout({
planId: subscription.plan.id,
planPeriod: subscription.planPeriod,
subscriberType,
for: subscriberType,
});
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export function SubscriptionsList({
)}

{(commerceSettings.billing.user.hasPaidPlans && subscriberType === 'user') ||
(commerceSettings.billing.organization.hasPaidPlans && subscriberType === 'org') ? (
(commerceSettings.billing.organization.hasPaidPlans && subscriberType === 'organization') ? (
<ProfileSection.ArrowButton
id='subscriptionsList'
textLocalizationKey={subscriptionItems.length > 0 ? arrowButtonText : arrowButtonEmptyText}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function ComponentContextProvider({
);
case 'PricingTable':
return (
<SubscriberTypeContext.Provider value={(props as PricingTableProps).forOrganizations ? 'org' : 'user'}>
<SubscriberTypeContext.Provider value={(props as PricingTableProps).forOrganizations ? 'organization' : 'user'}>
<PricingTableContext.Provider value={{ componentName, ...(props as PricingTableProps) }}>
{children}
</PricingTableContext.Provider>
Expand Down
19 changes: 0 additions & 19 deletions packages/clerk-js/src/ui/contexts/components/Checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ import { isAllowedRedirect } from '../../../utils';
import type { CheckoutCtx } from '../../types';
import { useOptions } from '../OptionsContext';

function invariant(cond: any, msg: string): asserts cond {
if (!cond) {
throw Error(msg);
}
}

export const CheckoutContext = createContext<CheckoutCtx | null>(null);

export const useCheckoutContext = () => {
Expand All @@ -35,22 +29,9 @@ export const useCheckoutContext = () => {

const { componentName, ...ctx } = context;

const subscriber = () => {
if (ctx.subscriberType === 'org' && clerk.organization) {
invariant(clerk.organization, 'Clerk: subscriberType is "org" but no active organization was found');

return clerk.organization;
}

invariant(clerk.user, 'Clerk: no active user found');

return clerk.user;
};

return {
...ctx,
componentName,
newSubscriptionRedirectUrl,
subscriber,
};
};
12 changes: 6 additions & 6 deletions packages/clerk-js/src/ui/contexts/components/Plans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { useSubscriberTypeContext } from './SubscriberType';
export const usePaymentMethods = () => {
const subscriberType = useSubscriberTypeContext();
return __experimental_usePaymentMethods({
for: subscriberType === 'org' ? 'organization' : 'user',
for: subscriberType,
initialPage: 1,
pageSize: 10,
keepPreviousData: true,
Expand All @@ -35,7 +35,7 @@ export const usePaymentMethods = () => {
export const usePaymentAttempts = () => {
const subscriberType = useSubscriberTypeContext();
return __experimental_usePaymentAttempts({
for: subscriberType === 'org' ? 'organization' : 'user',
for: subscriberType,
initialPage: 1,
pageSize: 10,
keepPreviousData: true,
Expand All @@ -45,7 +45,7 @@ export const usePaymentAttempts = () => {
export const useStatements = (params?: { mode: 'cache' }) => {
const subscriberType = useSubscriberTypeContext();
return __experimental_useStatements({
for: subscriberType === 'org' ? 'organization' : 'user',
for: subscriberType,
initialPage: 1,
pageSize: 10,
keepPreviousData: true,
Expand All @@ -56,7 +56,7 @@ export const useStatements = (params?: { mode: 'cache' }) => {
export const useSubscription = () => {
const subscriberType = useSubscriberTypeContext();
const subscription = __experimental_useSubscription({
for: subscriberType === 'org' ? 'organization' : 'user',
for: subscriberType,
keepPreviousData: true,
});
const subscriptionItems = useMemo(
Expand All @@ -74,7 +74,7 @@ export const usePlans = (params?: { mode: 'cache' }) => {
const subscriberType = useSubscriberTypeContext();

return __experimental_usePlans({
for: subscriberType === 'org' ? 'organization' : 'user',
for: subscriberType,
initialPage: 1,
pageSize: 50,
keepPreviousData: true,
Expand Down Expand Up @@ -303,7 +303,7 @@ export const usePlansContext = () => {
planId: plan.id,
// if the plan doesn't support annual, use monthly
planPeriod: planPeriod === 'annual' && plan.annualMonthlyAmount === 0 ? 'month' : planPeriod,
subscriberType,
for: subscriberType,
onSubscriptionComplete: () => {
revalidateAll();
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { CommercePayerType } from '@clerk/types';
import type { ForPayerType } from '@clerk/types';
import { createContext, useContext } from 'react';

const DEFAUlT = 'user';
export const SubscriberTypeContext = createContext<CommercePayerType | undefined>(DEFAUlT);
export const SubscriberTypeContext = createContext<ForPayerType | undefined>(DEFAUlT);

export const useSubscriberTypeContext = (): CommercePayerType => useContext(SubscriberTypeContext) || DEFAUlT;
export const useSubscriberTypeContext = (): ForPayerType => useContext(SubscriberTypeContext) || DEFAUlT;

export const useSubscriberTypeLocalizationRoot = () => {
const subscriberType = useSubscriberTypeContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function MountedCheckoutDrawer({
<Checkout
planId={checkoutDrawer.props.planId}
planPeriod={checkoutDrawer.props.planPeriod}
subscriberType={checkoutDrawer.props.subscriberType}
for={checkoutDrawer.props.for}
onSubscriptionComplete={checkoutDrawer.props.onSubscriptionComplete}
portalRoot={checkoutDrawer.props.portalRoot}
appearance={checkoutDrawer.props.appearance}
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/components/CheckoutButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const CheckoutButton = withClerk(
const {
planId,
planPeriod,
subscriberType,
for: _for,
onSubscriptionComplete,
newSubscriptionRedirectUrl,
checkoutProps,
Expand All @@ -62,7 +62,7 @@ export const CheckoutButton = withClerk(
throw new Error('Ensure that `<CheckoutButton />` is rendered inside a `<SignedIn />` component.');
}

if (orgId === null && subscriberType === 'org') {
if (orgId === null && _for === 'organization') {
throw new Error('Wrap `<CheckoutButton for="organization" />` with a check for an active organization.');
}

Expand All @@ -77,7 +77,7 @@ export const CheckoutButton = withClerk(
return clerk.__internal_openCheckout({
planId,
planPeriod,
subscriberType,
for: _for,
onSubscriptionComplete,
newSubscriptionRedirectUrl,
...checkoutProps,
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/components/SubscriptionDetailsButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const SubscriptionDetailsButton = withClerk(
children,
...props
}: WithClerkProp<React.PropsWithChildren<__experimental_SubscriptionDetailsButtonProps>>) => {
const { for: forProp, subscriptionDetailsProps, onSubscriptionCancel, ...rest } = props;
const { for: _for, subscriptionDetailsProps, onSubscriptionCancel, ...rest } = props;
children = normalizeWithDefaultValue(children, 'Subscription details');
const child = assertSingleChild(children)('SubscriptionDetailsButton');

Expand All @@ -56,7 +56,7 @@ export const SubscriptionDetailsButton = withClerk(
throw new Error('Ensure that `<SubscriptionDetailsButton />` is rendered inside a `<SignedIn />` component.');
}

if (orgId === null && forProp === 'org') {
if (orgId === null && _for === 'organization') {
throw new Error(
'Wrap `<SubscriptionDetailsButton for="organization" />` with a check for an active organization.',
);
Expand All @@ -68,7 +68,7 @@ export const SubscriptionDetailsButton = withClerk(
}

return clerk.__internal_openSubscriptionDetails({
for: forProp,
for: _for,
onSubscriptionCancel,
...subscriptionDetailsProps,
});
Expand Down
Loading