Skip to content

feat(clerk-js,localizations): Switching monthly to annual for existing subscription #5811

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 3 commits into from
May 3, 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
7 changes: 7 additions & 0 deletions .changeset/sad-teeth-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@clerk/localizations': patch
'@clerk/clerk-js': patch
'@clerk/types': patch
---

Allow switching from an existing monthly subscription to an annual subscription for the same plan
69 changes: 43 additions & 26 deletions packages/clerk-js/src/ui/components/Plans/PlanDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Badge,
Box,
Button,
Col,
descriptors,
Flex,
Heading,
Expand Down Expand Up @@ -90,12 +91,16 @@ const PlanDetailsInternal = ({
});
};

const openCheckout = () => {
type OpenCheckoutProps = {
planPeriod?: __experimental_CommerceSubscriptionPlanPeriod;
};

const openCheckout = (props?: OpenCheckoutProps) => {
handleClose();

// if the plan doesn't support annual, use monthly
let _planPeriod = planPeriod;
if (planPeriod === 'annual' && plan.annualMonthlyAmount === 0) {
let _planPeriod = props?.planPeriod || planPeriod;
if (_planPeriod === 'annual' && plan.annualMonthlyAmount === 0) {
_planPeriod = 'month';
}

Expand Down Expand Up @@ -211,24 +216,36 @@ const PlanDetailsInternal = ({
block
textVariant='buttonLarge'
{...buttonPropsForPlan({ plan })}
onClick={openCheckout}
onClick={() => openCheckout()}
/>
) : (
<Button
block
variant='bordered'
colorScheme='secondary'
textVariant='buttonLarge'
onClick={() => setShowConfirmation(true)}
localizationKey={localizationKeys('__experimental_commerce.cancelSubscription')}
/>
<Col gap={4}>
{!!subscription && subscription.planPeriod === 'month' && plan.annualMonthlyAmount > 0 ? (
<Button
block
variant='bordered'
colorScheme='secondary'
textVariant='buttonLarge'
onClick={() => openCheckout({ planPeriod: 'annual' })}
localizationKey={localizationKeys('__experimental_commerce.switchToAnnual')}
/>
) : null}
<Button
block
variant='bordered'
colorScheme='danger'
textVariant='buttonLarge'
onClick={() => setShowConfirmation(true)}
localizationKey={localizationKeys('__experimental_commerce.cancelSubscription')}
/>
</Col>
)
) : (
<Button
block
textVariant='buttonLarge'
{...buttonPropsForPlan({ plan })}
onClick={openCheckout}
onClick={() => openCheckout()}
/>
)}
</Drawer.Footer>
Expand Down Expand Up @@ -484,19 +501,7 @@ const Header = React.forwardRef<HTMLDivElement, HeaderProps>((props, ref) => {
</>
</Flex>

{!!subscription && (
<Text
elementDescriptor={descriptors.planDetailCaption}
variant={'caption'}
localizationKey={captionForSubscription(subscription)}
colorScheme='secondary'
sx={t => ({
marginTop: t.space.$3,
})}
/>
)}

{!subscription && plan.annualMonthlyAmount > 0 ? (
{!subscription || (subscription.planPeriod === 'month' && plan.annualMonthlyAmount > 0) ? (
<Box
elementDescriptor={descriptors.planDetailPeriodToggle}
sx={t => ({
Expand All @@ -522,6 +527,18 @@ const Header = React.forwardRef<HTMLDivElement, HeaderProps>((props, ref) => {
</SegmentedControl.Root>
</Box>
) : null}

{!!subscription && (
<Text
elementDescriptor={descriptors.planDetailCaption}
variant={'caption'}
localizationKey={captionForSubscription(subscription)}
colorScheme='secondary'
sx={t => ({
marginTop: t.space.$3,
})}
/>
)}
</Box>
);
});
1 change: 1 addition & 0 deletions packages/localizations/src/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const enUS: LocalizationResource = {
month: 'Month',
reSubscribe: 'Re-subscribe',
switchPlan: 'Switch to this plan',
switchToAnnual: 'Switch to annual',
defaultFreePlanActive: "You're currently on the Free plan",
availableFeatures: 'Available features',
viewFeatures: 'View features',
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/localization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ type _LocalizationResource = {
keepSubscription: LocalizationValue;
reSubscribe: LocalizationValue;
switchPlan: LocalizationValue;
switchToAnnual: LocalizationValue;
billedAnnually: LocalizationValue;
accountFunds: LocalizationValue;
defaultFreePlanActive: LocalizationValue;
Expand Down