Skip to content

fix(clerk-js): Improve error handling for unexpected errors in checkout process #5805

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 4 commits into from
May 2, 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
5 changes: 5 additions & 0 deletions .changeset/mighty-worms-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/clerk-js": patch
---

Show a localized message for all errors returned from the backend if the user tries to perform an invalid plan change.
2 changes: 1 addition & 1 deletion packages/clerk-js/bundlewatch.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{ "path": "./dist/waitlist*.js", "maxSize": "1.3KB" },
{ "path": "./dist/keylessPrompt*.js", "maxSize": "6.5KB" },
{ "path": "./dist/pricingTable*.js", "maxSize": "4.02KB" },
{ "path": "./dist/checkout*.js", "maxSize": "5.2KB" },
{ "path": "./dist/checkout*.js", "maxSize": "5.3KB" },
{ "path": "./dist/paymentSources*.js", "maxSize": "8.9KB" },
{ "path": "./dist/up-billing-page*.js", "maxSize": "2.4KB" },
{ "path": "./dist/op-billing-page*.js", "maxSize": "2.4KB" },
Expand Down
37 changes: 24 additions & 13 deletions packages/clerk-js/src/ui/components/Checkout/CheckoutPage.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import type { __experimental_CheckoutProps, __experimental_CommerceCheckoutResource } from '@clerk/types';
import type {
__experimental_CheckoutProps,
__experimental_CommerceCheckoutResource,
ClerkAPIError,
} from '@clerk/types';
import { useEffect } from 'react';

import { Alert, Box, localizationKeys, Spinner } from '../../customizables';
import { Alert, Box, Flex, localizationKeys, Spinner, useLocalizations } from '../../customizables';
import { Drawer, useDrawerContext } from '../../elements';
import { useCheckout } from '../../hooks';
import { EmailForm } from '../UserProfile/EmailForm';
import { CheckoutComplete } from './CheckoutComplete';
import { CheckoutForm } from './CheckoutForm';

export const CheckoutPage = (props: __experimental_CheckoutProps) => {
const { translateError } = useLocalizations();
const { planId, planPeriod, subscriberType, onSubscriptionComplete } = props;
const { setIsOpen, isOpen } = useDrawerContext();

const { checkout, isLoading, invalidate, revalidate, updateCheckout, isMissingPayerEmail } = useCheckout({
const { checkout, isLoading, invalidate, revalidate, updateCheckout, errors } = useCheckout({
planId,
planPeriod,
subscriberType,
});

const isMissingPayerEmail = !!errors?.some((e: ClerkAPIError) => e.code === 'missing_payer_email');

const onCheckoutComplete = (newCheckout: __experimental_CommerceCheckoutResource) => {
invalidate(); // invalidate the initial checkout on complete
updateCheckout(newCheckout);
Expand Down Expand Up @@ -74,16 +81,20 @@ export const CheckoutPage = (props: __experimental_CheckoutProps) => {
}

return (
<>
{/* TODO(@COMMERCE): needs localization */}
<Alert
colorScheme='danger'
sx={{
margin: 'auto',
}}
<Drawer.Body>
<Flex
align={'center'}
justify={'center'}
sx={t => ({
height: '100%',
padding: t.space.$4,
fontSize: t.fontSizes.$md,
})}
>
There was a problem, please try again later.
</Alert>
</>
<Alert colorScheme='danger'>
{errors ? translateError(errors[0]) : 'There was a problem, please try again later.'}
</Alert>
</Flex>
</Drawer.Body>
);
};
13 changes: 6 additions & 7 deletions packages/clerk-js/src/ui/hooks/useCheckout.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { ClerkAPIResponseError } from '@clerk/shared/error';
import { useClerk, useOrganization, useUser } from '@clerk/shared/react';
import type {
__experimental_CheckoutProps,
__experimental_CommerceCheckoutResource,
ClerkAPIError,
} from '@clerk/types';
import type { __experimental_CheckoutProps, __experimental_CommerceCheckoutResource } from '@clerk/types';
import { useCallback, useEffect, useState } from 'react';

import { useFetch } from './useFetch';
Expand All @@ -20,7 +17,7 @@ export const useCheckout = (props: __experimental_CheckoutProps) => {
isLoading,
invalidate,
revalidate,
error,
error: _error,
} = useFetch(
__experimental_commerce?.__experimental_billing.startCheckout,
{
Expand All @@ -32,6 +29,8 @@ export const useCheckout = (props: __experimental_CheckoutProps) => {
`commerce-checkout-${user?.id}`,
);

const error = _error as ClerkAPIResponseError | undefined;

const updateCheckout = useCallback((newCheckout: __experimental_CommerceCheckoutResource) => {
setCurrentCheckout(newCheckout);
}, []);
Expand All @@ -48,6 +47,6 @@ export const useCheckout = (props: __experimental_CheckoutProps) => {
isLoading,
invalidate,
revalidate,
isMissingPayerEmail: error?.errors.some((e: ClerkAPIError) => e.code === 'missing_payer_email'),
errors: error?.errors,
};
};