Skip to content

Commit a059017

Browse files
authored
fix(clerk-js): Hide payment selection if checkout results to downgrade (#6786)
1 parent 9d4a95c commit a059017

File tree

4 files changed

+441
-134
lines changed

4 files changed

+441
-134
lines changed

.changeset/slick-paths-write.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Hide payment methods from checkout if the new subscription does not result in an immediate change to the end user's plan.

packages/clerk-js/bundlewatch.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
{ "path": "./dist/waitlist*.js", "maxSize": "1.5KB" },
2424
{ "path": "./dist/keylessPrompt*.js", "maxSize": "6.5KB" },
2525
{ "path": "./dist/pricingTable*.js", "maxSize": "4.02KB" },
26-
{ "path": "./dist/checkout*.js", "maxSize": "8.75KB" },
26+
{ "path": "./dist/checkout*.js", "maxSize": "8.77KB" },
2727
{ "path": "./dist/up-billing-page*.js", "maxSize": "3.0KB" },
2828
{ "path": "./dist/op-billing-page*.js", "maxSize": "3.0KB" },
2929
{ "path": "./dist/up-plans-page*.js", "maxSize": "1.0KB" },

packages/clerk-js/src/ui/components/Checkout/CheckoutForm.tsx

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { handleError } from '@/ui/utils/errorHandler';
1313

1414
import { DevOnly } from '../../common/DevOnly';
1515
import { useCheckoutContext, usePaymentMethods } from '../../contexts';
16-
import { Box, Button, Col, descriptors, Flex, Form, localizationKeys, Text } from '../../customizables';
16+
import { Box, Button, Col, descriptors, Flex, Form, localizationKeys, Spinner, Text } from '../../customizables';
1717
import { ChevronUpDown, InformationCircle } from '../../icons';
1818
import * as AddPaymentSource from '../PaymentSources/AddPaymentSource';
1919
import { PaymentSourceRow } from '../PaymentSources/PaymentSourceRow';
@@ -187,13 +187,38 @@ const useCheckoutMutations = () => {
187187

188188
const CheckoutFormElements = () => {
189189
const { checkout } = useCheckout();
190-
const { id, totals, freeTrialEndsAt } = checkout;
190+
const { id } = checkout;
191+
192+
const { isLoading } = usePaymentMethods();
193+
194+
if (!id) {
195+
return null;
196+
}
197+
198+
if (isLoading) {
199+
return (
200+
<Spinner
201+
sx={{
202+
margin: 'auto',
203+
}}
204+
/>
205+
);
206+
}
207+
208+
return <CheckoutFormElementsInternal />;
209+
};
210+
211+
const CheckoutFormElementsInternal = () => {
212+
const { checkout } = useCheckout();
213+
const { id, totals, isImmediatePlanChange, freeTrialEndsAt } = checkout;
191214
const { data: paymentSources } = usePaymentMethods();
192215

193216
const [paymentMethodSource, setPaymentMethodSource] = useState<PaymentMethodSource>(() =>
194217
paymentSources.length > 0 || __BUILD_DISABLE_RHC__ ? 'existing' : 'new',
195218
);
196219

220+
const showPaymentMethods = isImmediatePlanChange && (totals.totalDueNow.amount > 0 || !!freeTrialEndsAt);
221+
197222
if (!id) {
198223
return null;
199224
}
@@ -206,8 +231,7 @@ const CheckoutFormElements = () => {
206231
>
207232
{__BUILD_DISABLE_RHC__ ? null : (
208233
<>
209-
{/* only show if there are payment sources and there is a total due now */}
210-
{paymentSources.length > 0 && (totals.totalDueNow.amount > 0 || !!freeTrialEndsAt) && (
234+
{paymentSources.length > 0 && showPaymentMethods && (
211235
<SegmentedControl.Root
212236
aria-label='Payment method source'
213237
value={paymentMethodSource}
@@ -347,7 +371,7 @@ const ExistingPaymentSourceForm = withCardStateProvider(
347371
}) => {
348372
const submitLabel = useSubmitLabel();
349373
const { checkout } = useCheckout();
350-
const { paymentSource, freeTrialEndsAt } = checkout;
374+
const { paymentSource, isImmediatePlanChange, freeTrialEndsAt } = checkout;
351375

352376
const { payWithExistingPaymentSource } = useCheckoutMutations();
353377
const card = useCardState();
@@ -369,7 +393,7 @@ const ExistingPaymentSourceForm = withCardStateProvider(
369393
});
370394
}, [paymentSources]);
371395

372-
const shouldDefaultBeUsed = totalDueNow.amount === 0 || !freeTrialEndsAt;
396+
const showPaymentMethods = isImmediatePlanChange && (totalDueNow.amount > 0 || !!freeTrialEndsAt);
373397

374398
return (
375399
<Form
@@ -380,7 +404,7 @@ const ExistingPaymentSourceForm = withCardStateProvider(
380404
rowGap: t.space.$4,
381405
})}
382406
>
383-
{shouldDefaultBeUsed ? (
407+
{showPaymentMethods ? (
384408
<Select
385409
elementId='paymentSource'
386410
options={options}

0 commit comments

Comments
 (0)