Skip to content

Commit ca8c049

Browse files
committed
refactor: use new needs_payment_method field
1 parent a2b55d9 commit ca8c049

File tree

7 files changed

+15
-23
lines changed

7 files changed

+15
-23
lines changed

packages/clerk-js/src/core/resources/BillingCheckout.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class BillingCheckout extends BaseResource implements BillingCheckoutReso
2929
totals!: BillingCheckoutTotals;
3030
isImmediatePlanChange!: boolean;
3131
freeTrialEndsAt!: Date | null;
32+
needsPaymentMethod!: boolean;
3233
payer!: BillingPayerResource;
3334

3435
constructor(data: BillingCheckoutJSON) {
@@ -52,6 +53,7 @@ export class BillingCheckout extends BaseResource implements BillingCheckoutReso
5253
this.totals = billingTotalsFromJSON(data.totals);
5354
this.isImmediatePlanChange = data.is_immediate_plan_change;
5455
this.freeTrialEndsAt = data.free_trial_ends_at ? unixEpochToDate(data.free_trial_ends_at) : null;
56+
this.needsPaymentMethod = data.needs_payment_method;
5557
this.payer = new BillingPayer(data.payer);
5658
return this;
5759
}

packages/clerk-js/src/core/resources/CommerceSettings.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { BaseResource } from './internal';
88
export class CommerceSettings extends BaseResource implements CommerceSettingsResource {
99
billing: CommerceSettingsResource['billing'] = {
1010
stripePublishableKey: '',
11-
freeTrialRequiresPaymentMethod: true,
1211
organization: {
1312
enabled: false,
1413
hasPaidPlans: false,
@@ -30,7 +29,6 @@ export class CommerceSettings extends BaseResource implements CommerceSettingsRe
3029
}
3130

3231
this.billing.stripePublishableKey = data.billing.stripe_publishable_key || '';
33-
this.billing.freeTrialRequiresPaymentMethod = data.billing.free_trial_requires_payment_method ?? true;
3432
this.billing.organization.enabled = data.billing.organization.enabled || false;
3533
this.billing.organization.hasPaidPlans = data.billing.organization.has_paid_plans || false;
3634
this.billing.user.enabled = data.billing.user.enabled || false;
@@ -43,7 +41,6 @@ export class CommerceSettings extends BaseResource implements CommerceSettingsRe
4341
return {
4442
billing: {
4543
stripe_publishable_key: this.billing.stripePublishableKey,
46-
free_trial_requires_payment_method: this.billing.freeTrialRequiresPaymentMethod,
4744
organization: {
4845
enabled: this.billing.organization.enabled,
4946
has_paid_plans: this.billing.organization.hasPaidPlans,

packages/clerk-js/src/test/fixture-helpers.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,19 +367,16 @@ const createBillingSettingsFixtureHelpers = (environment: EnvironmentJSON) => {
367367
userHasPaidPlans = true,
368368
organizationEnabled = true,
369369
organizationHasPaidPlans = true,
370-
freeTrialRequiresPaymentMethod = true,
371370
}: {
372371
userEnabled?: boolean;
373372
userHasPaidPlans?: boolean;
374373
organizationEnabled?: boolean;
375374
organizationHasPaidPlans?: boolean;
376-
freeTrialRequiresPaymentMethod?: boolean;
377375
} = {}) => {
378376
os.user.enabled = userEnabled;
379377
os.user.has_paid_plans = userHasPaidPlans;
380378
os.organization.enabled = organizationEnabled;
381379
os.organization.has_paid_plans = organizationHasPaidPlans;
382-
os.free_trial_requires_payment_method = freeTrialRequiresPaymentMethod;
383380
};
384381

385382
return { withBilling };

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Drawer, useDrawerContext } from '@/ui/elements/Drawer';
55
import { LineItems } from '@/ui/elements/LineItems';
66
import { formatDate } from '@/ui/utils/formatDate';
77

8-
import { useCheckoutContext, useEnvironment } from '../../contexts';
8+
import { useCheckoutContext } from '../../contexts';
99
import { Box, Button, descriptors, Heading, localizationKeys, Span, Text, useAppearance } from '../../customizables';
1010
import { transitionDurationValues, transitionTiming } from '../../foundations/transitions';
1111
import { usePrefersReducedMotion } from '../../hooks';
@@ -161,8 +161,7 @@ export const CheckoutComplete = () => {
161161
const { setIsOpen } = useDrawerContext();
162162
const { newSubscriptionRedirectUrl } = useCheckoutContext();
163163
const { checkout } = useCheckout();
164-
const { totals, paymentMethod, planPeriodStart, freeTrialEndsAt } = checkout;
165-
const environment = useEnvironment();
164+
const { totals, paymentMethod, planPeriodStart, freeTrialEndsAt, needsPaymentMethod } = checkout;
166165
const [mousePosition, setMousePosition] = useState({ x: 256, y: 256 });
167166

168167
const prefersReducedMotion = usePrefersReducedMotion();
@@ -431,16 +430,14 @@ export const CheckoutComplete = () => {
431430
<LineItems.Group variant='secondary'>
432431
<LineItems.Title
433432
title={
434-
totals.totalDueNow.amount > 0 ||
435-
(freeTrialEndsAt !== null && environment.commerceSettings.billing.freeTrialRequiresPaymentMethod)
433+
totals.totalDueNow.amount > 0 || needsPaymentMethod
436434
? localizationKeys('billing.checkout.lineItems.title__paymentMethod')
437435
: localizationKeys('billing.checkout.lineItems.title__subscriptionBegins')
438436
}
439437
/>
440438
<LineItems.Description
441439
text={
442-
totals.totalDueNow.amount > 0 ||
443-
(freeTrialEndsAt !== null && environment.commerceSettings.billing.freeTrialRequiresPaymentMethod)
440+
totals.totalDueNow.amount > 0 || needsPaymentMethod
444441
? paymentMethod
445442
? paymentMethod.paymentType !== 'card'
446443
? `${capitalize(paymentMethod.paymentType)}`

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Tooltip } from '@/ui/elements/Tooltip';
1212
import { handleError } from '@/ui/utils/errorHandler';
1313

1414
import { DevOnly } from '../../common/DevOnly';
15-
import { useCheckoutContext, useEnvironment, usePaymentMethods } from '../../contexts';
15+
import { useCheckoutContext, usePaymentMethods } from '../../contexts';
1616
import { Box, Button, Col, descriptors, Flex, Form, localizationKeys, Spinner, Text } from '../../customizables';
1717
import { ChevronUpDown, InformationCircle } from '../../icons';
1818
import type { PropsOfComponent, ThemableCssProp } from '../../styledSystem';
@@ -219,17 +219,15 @@ const CheckoutFormElements = () => {
219219

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

226225
const [paymentMethodSource, setPaymentMethodSource] = useState<PaymentMethodSource>(() =>
227226
paymentMethods.length > 0 || __BUILD_DISABLE_RHC__ ? 'existing' : 'new',
228227
);
229228

230229
const isFreeTrial = Boolean(freeTrialEndsAt);
231230
const showTabs = isImmediatePlanChange && (totals.totalDueNow.amount > 0 || isFreeTrial);
232-
const needsPaymentMethod = !(isFreeTrial && !environment.commerceSettings.billing.freeTrialRequiresPaymentMethod);
233231

234232
if (!id) {
235233
return null;
@@ -427,8 +425,7 @@ const ExistingPaymentMethodForm = withCardStateProvider(
427425
paymentMethods: BillingPaymentMethodResource[];
428426
}) => {
429427
const { checkout } = useCheckout();
430-
const { paymentMethod, isImmediatePlanChange, freeTrialEndsAt } = checkout;
431-
const environment = useEnvironment();
428+
const { paymentMethod, isImmediatePlanChange, needsPaymentMethod } = checkout;
432429

433430
const { payWithExistingPaymentMethod } = useCheckoutMutations();
434431
const card = useCardState();
@@ -450,10 +447,7 @@ const ExistingPaymentMethodForm = withCardStateProvider(
450447
});
451448
}, [paymentMethods]);
452449

453-
const showPaymentMethods =
454-
isImmediatePlanChange &&
455-
(totalDueNow.amount > 0 ||
456-
(!!freeTrialEndsAt && environment.commerceSettings.billing.freeTrialRequiresPaymentMethod));
450+
const showPaymentMethods = isImmediatePlanChange && (totalDueNow.amount > 0 || needsPaymentMethod);
457451

458452
return (
459453
<Form

packages/types/src/billing.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,10 @@ export interface BillingCheckoutResource extends ClerkResource {
779779
* Unix timestamp (milliseconds) of when the free trial ends.
780780
*/
781781
freeTrialEndsAt: Date | null;
782+
/**
783+
* Whether a payment method is required for this checkout session.
784+
*/
785+
needsPaymentMethod: boolean;
782786
/**
783787
* The payer associated with the checkout.
784788
*/

packages/types/src/json.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ export interface BillingCheckoutJSON extends ClerkResourceJSON {
815815
is_immediate_plan_change: boolean;
816816
// TODO(@COMMERCE): Remove optional after GA.
817817
free_trial_ends_at: number | null;
818+
needs_payment_method: boolean;
818819
payer: BillingPayerJSON;
819820
}
820821

0 commit comments

Comments
 (0)