Skip to content

Commit 8095cff

Browse files
committed
fix: trust backend field everywhere
1 parent 877cbea commit 8095cff

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export const CheckoutComplete = () => {
331331
localizationKey={
332332
freeTrialEndsAt
333333
? localizationKeys('billing.checkout.title__trialSuccess')
334-
: totals.totalDueNow.amount > 0
334+
: needsPaymentMethod
335335
? localizationKeys('billing.checkout.title__paymentSuccessful')
336336
: localizationKeys('billing.checkout.title__subscriptionSuccessful')
337337
}
@@ -386,7 +386,7 @@ export const CheckoutComplete = () => {
386386
}),
387387
})}
388388
localizationKey={
389-
totals.totalDueNow.amount > 0
389+
needsPaymentMethod
390390
? localizationKeys('billing.checkout.description__paymentSuccessful')
391391
: localizationKeys('billing.checkout.description__subscriptionSuccessful')
392392
}

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,14 @@ const CheckoutFormElements = () => {
219219

220220
const CheckoutFormElementsInternal = () => {
221221
const { checkout } = useCheckout();
222-
const { id, totals, isImmediatePlanChange, freeTrialEndsAt, needsPaymentMethod } = checkout;
222+
const { id, isImmediatePlanChange, needsPaymentMethod } = checkout;
223223
const { data: paymentMethods } = usePaymentMethods();
224224

225225
const [paymentMethodSource, setPaymentMethodSource] = useState<PaymentMethodSource>(() =>
226226
paymentMethods.length > 0 || __BUILD_DISABLE_RHC__ ? 'existing' : 'new',
227227
);
228228

229-
const isFreeTrial = Boolean(freeTrialEndsAt);
230-
const showTabs = isImmediatePlanChange && (totals.totalDueNow.amount > 0 || isFreeTrial);
229+
const showTabs = isImmediatePlanChange && needsPaymentMethod && paymentMethods.length > 0;
231230

232231
if (!id) {
233232
return null;
@@ -332,20 +331,25 @@ export const PayWithTestPaymentMethod = () => {
332331

333332
const useSubmitLabel = () => {
334333
const { checkout } = useCheckout();
335-
const { status, freeTrialEndsAt, totals } = checkout;
334+
const { status, freeTrialEndsAt, totals, needsPaymentMethod } = checkout;
336335

337336
if (status === 'needs_initialization') {
338337
throw new Error('Clerk: Invalid state');
339338
}
340339

341-
if (freeTrialEndsAt) {
342-
return localizationKeys('billing.startFreeTrial');
340+
// If payment method is needed, show payment-related labels
341+
if (needsPaymentMethod) {
342+
if (totals.totalDueNow.amount > 0) {
343+
return localizationKeys('billing.pay', {
344+
amount: `${totals.totalDueNow.currencySymbol}${totals.totalDueNow.amountFormatted}`,
345+
});
346+
}
347+
return localizationKeys('billing.subscribe');
343348
}
344349

345-
if (totals.totalDueNow.amount > 0) {
346-
return localizationKeys('billing.pay', {
347-
amount: `${totals.totalDueNow.currencySymbol}${totals.totalDueNow.amountFormatted}`,
348-
});
350+
// If no payment method needed and it's a trial, show free trial label
351+
if (freeTrialEndsAt) {
352+
return localizationKeys('billing.startFreeTrial');
349353
}
350354

351355
return localizationKeys('billing.subscribe');

packages/clerk-js/src/ui/components/Checkout/__tests__/Checkout.test.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,8 @@ describe('Checkout', () => {
797797
const hiddenInput = baseElement.querySelector('input[name="payment_method_id"]');
798798
expect(hiddenInput).toHaveAttribute('value', 'pm_test_visa');
799799

800-
expect(getByRole('button', { name: 'Start free trial' })).toBeInTheDocument();
800+
// Since needsPaymentMethod is true, should show Subscribe button, not Start free trial
801+
expect(getByRole('button', { name: 'Subscribe' })).toBeInTheDocument();
801802
});
802803
});
803804

@@ -1043,7 +1044,8 @@ describe('Checkout', () => {
10431044
});
10441045

10451046
await waitFor(() => {
1046-
expect(getByRole('button', { name: 'Start free trial' })).toBeInTheDocument();
1047+
// Since needsPaymentMethod is true, should show Subscribe button, not Start free trial
1048+
expect(getByRole('button', { name: 'Subscribe' })).toBeInTheDocument();
10471049
});
10481050
});
10491051

@@ -1128,13 +1130,15 @@ describe('Checkout', () => {
11281130
await waitFor(async () => {
11291131
expect(getByRole('heading', { name: 'Checkout' })).toBeVisible();
11301132

1131-
// Verify segmented control for payment method source is hidden
1133+
// Since needsPaymentMethod is true but no existing payment methods, should show payment collection UI directly
1134+
// Verify segmented control for payment method source is NOT visible (no choice to make)
11321135
const paymentMethodsButton = queryByText('Payment Methods');
11331136
expect(paymentMethodsButton).toBeNull();
11341137
const addPaymentMethodButton = queryByText('Add payment method');
11351138
expect(addPaymentMethodButton).toBeNull();
11361139

1137-
expect(getByRole('button', { name: 'Start free trial' })).toBeInTheDocument();
1140+
// Should show Subscribe button, not Start free trial button
1141+
expect(getByRole('button', { name: 'Subscribe' })).toBeInTheDocument();
11381142
});
11391143
});
11401144

0 commit comments

Comments
 (0)