Skip to content

Commit 173837c

Browse files
authored
chore(clerk-js,types): Switch to fees for plan prices (#6490)
1 parent 037f25a commit 173837c

File tree

22 files changed

+621
-454
lines changed

22 files changed

+621
-454
lines changed

.changeset/rich-drinks-ring.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clerk/clerk-js': minor
3+
'@clerk/types': minor
4+
---
5+
6+
[Billing Beta] Replace usage of top level amounts in plan with fees for displaying prices.

.typedoc/__tests__/__snapshots__/file-structure.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ exports[`Typedoc output > should have a deliberate file structure 1`] = `
3030
"types/commerce-feature-resource.mdx",
3131
"types/commerce-initialized-payment-source-json.mdx",
3232
"types/commerce-initialized-payment-source-resource.mdx",
33-
"types/commerce-money-json.mdx",
34-
"types/commerce-money.mdx",
33+
"types/commerce-money-amount-json.mdx",
34+
"types/commerce-money-amount.mdx",
3535
"types/commerce-payer-resource-type.mdx",
3636
"types/commerce-payment-charge-type.mdx",
3737
"types/commerce-payment-json.mdx",

packages/backend/src/api/resources/CommercePlan.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Feature } from './Feature';
22
import type { CommercePlanJSON } from './JSON';
33

4-
type CommerceFee = {
4+
type CommerceMoneyAmount = {
55
amount: number;
66
amountFormatted: string;
77
currency: string;
@@ -53,15 +53,15 @@ export class CommercePlan {
5353
/**
5454
* The monthly fee of the plan.
5555
*/
56-
readonly fee: CommerceFee,
56+
readonly fee: CommerceMoneyAmount,
5757
/**
5858
* The annual fee of the plan.
5959
*/
60-
readonly annualFee: CommerceFee,
60+
readonly annualFee: CommerceMoneyAmount,
6161
/**
6262
* The annual fee of the plan on a monthly basis.
6363
*/
64-
readonly annualMonthlyFee: CommerceFee,
64+
readonly annualMonthlyFee: CommerceMoneyAmount,
6565
/**
6666
* The type of payer for the plan.
6767
*/

packages/backend/src/api/resources/JSON.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -800,17 +800,17 @@ interface CommercePayeeJSON {
800800
gateway_status: 'active' | 'pending' | 'restricted' | 'disconnected';
801801
}
802802

803-
interface CommerceFeeJSON {
803+
interface CommerceMoneyAmountJSON {
804804
amount: number;
805805
amount_formatted: string;
806806
currency: string;
807807
currency_symbol: string;
808808
}
809809

810810
interface CommerceTotalsJSON {
811-
subtotal: CommerceFeeJSON;
812-
tax_total: CommerceFeeJSON;
813-
grand_total: CommerceFeeJSON;
811+
subtotal: CommerceMoneyAmountJSON;
812+
tax_total: CommerceMoneyAmountJSON;
813+
grand_total: CommerceMoneyAmountJSON;
814814
}
815815

816816
export interface FeatureJSON extends ClerkResourceJSON {
@@ -836,9 +836,9 @@ export interface CommercePlanJSON extends ClerkResourceJSON {
836836
is_recurring: boolean;
837837
has_base_fee: boolean;
838838
publicly_visible: boolean;
839-
fee: CommerceFeeJSON;
840-
annual_fee: CommerceFeeJSON;
841-
annual_monthly_fee: CommerceFeeJSON;
839+
fee: CommerceMoneyAmountJSON;
840+
annual_fee: CommerceMoneyAmountJSON;
841+
annual_monthly_fee: CommerceMoneyAmountJSON;
842842
for_payer_type: 'org' | 'user';
843843
features: FeatureJSON[];
844844
}
@@ -847,7 +847,7 @@ export interface CommerceSubscriptionItemJSON extends ClerkResourceJSON {
847847
object: typeof ObjectType.CommerceSubscriptionItem;
848848
status: 'abandoned' | 'active' | 'canceled' | 'ended' | 'expired' | 'incomplete' | 'past_due' | 'upcoming';
849849
credit: {
850-
amount: CommerceFeeJSON;
850+
amount: CommerceMoneyAmountJSON;
851851
cycle_days_remaining: number;
852852
cycle_days_total: number;
853853
cycle_remaining_percent: number;
@@ -861,7 +861,7 @@ export interface CommerceSubscriptionItemJSON extends ClerkResourceJSON {
861861
lifetime_paid: number;
862862
next_payment_amount: number;
863863
next_payment_date: number;
864-
amount: CommerceFeeJSON;
864+
amount: CommerceMoneyAmountJSON;
865865
plan: {
866866
id: string;
867867
instance_id: string;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {
2-
CommerceMoney,
2+
CommerceMoneyAmount,
33
CommercePaymentChargeType,
44
CommercePaymentJSON,
55
CommercePaymentResource,
@@ -8,13 +8,13 @@ import type {
88
CommerceSubscriptionItemResource,
99
} from '@clerk/types';
1010

11-
import { commerceMoneyFromJSON } from '../../utils';
11+
import { commerceMoneyAmountFromJSON } from '../../utils';
1212
import { unixEpochToDate } from '../../utils/date';
1313
import { BaseResource, CommercePaymentSource, CommerceSubscriptionItem } from './internal';
1414

1515
export class CommercePayment extends BaseResource implements CommercePaymentResource {
1616
id!: string;
17-
amount!: CommerceMoney;
17+
amount!: CommerceMoneyAmount;
1818
failedAt?: Date;
1919
paidAt?: Date;
2020
updatedAt!: Date;
@@ -38,7 +38,7 @@ export class CommercePayment extends BaseResource implements CommercePaymentReso
3838
}
3939

4040
this.id = data.id;
41-
this.amount = commerceMoneyFromJSON(data.amount);
41+
this.amount = commerceMoneyAmountFromJSON(data.amount);
4242
this.paidAt = data.paid_at ? unixEpochToDate(data.paid_at) : undefined;
4343
this.failedAt = data.failed_at ? unixEpochToDate(data.failed_at) : undefined;
4444
this.updatedAt = unixEpochToDate(data.updated_at);
Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
import type {
2+
CommerceMoneyAmount,
23
CommercePayerResourceType,
34
CommercePlanJSON,
4-
CommercePlanJSONSnapshot,
55
CommercePlanResource,
66
} from '@clerk/types';
77

8+
import { commerceMoneyAmountFromJSON } from '@/utils/commerce';
9+
810
import { BaseResource, CommerceFeature } from './internal';
911

1012
export class CommercePlan extends BaseResource implements CommercePlanResource {
1113
id!: string;
1214
name!: string;
13-
amount!: number;
14-
amountFormatted!: string;
15-
annualAmount!: number;
16-
annualAmountFormatted!: string;
17-
annualMonthlyAmount!: number;
18-
annualMonthlyAmountFormatted!: string;
19-
currencySymbol!: string;
20-
currency!: string;
15+
fee!: CommerceMoneyAmount;
16+
annualFee!: CommerceMoneyAmount;
17+
annualMonthlyFee!: CommerceMoneyAmount;
2118
description!: string;
2219
isDefault!: boolean;
2320
isRecurring!: boolean;
@@ -42,14 +39,9 @@ export class CommercePlan extends BaseResource implements CommercePlanResource {
4239

4340
this.id = data.id;
4441
this.name = data.name;
45-
this.amount = data.amount;
46-
this.amountFormatted = data.amount_formatted;
47-
this.annualAmount = data.annual_amount;
48-
this.annualAmountFormatted = data.annual_amount_formatted;
49-
this.annualMonthlyAmount = data.annual_monthly_amount;
50-
this.annualMonthlyAmountFormatted = data.annual_monthly_amount_formatted;
51-
this.currencySymbol = data.currency_symbol;
52-
this.currency = data.currency;
42+
this.fee = commerceMoneyAmountFromJSON(data.fee);
43+
this.annualFee = commerceMoneyAmountFromJSON(data.annual_fee);
44+
this.annualMonthlyFee = commerceMoneyAmountFromJSON(data.annual_monthly_fee);
5345
this.description = data.description;
5446
this.isDefault = data.is_default;
5547
this.isRecurring = data.is_recurring;
@@ -64,29 +56,4 @@ export class CommercePlan extends BaseResource implements CommercePlanResource {
6456

6557
return this;
6658
}
67-
68-
public __internal_toSnapshot(): CommercePlanJSONSnapshot {
69-
return {
70-
object: 'commerce_plan',
71-
id: this.id,
72-
name: this.name,
73-
amount: this.amount,
74-
amount_formatted: this.amountFormatted,
75-
annual_amount: this.annualAmount,
76-
annual_amount_formatted: this.annualAmountFormatted,
77-
annual_monthly_amount: this.annualMonthlyAmount,
78-
annual_monthly_amount_formatted: this.annualMonthlyAmountFormatted,
79-
currency: this.currency,
80-
currency_symbol: this.currencySymbol,
81-
description: this.description,
82-
is_default: this.isDefault,
83-
is_recurring: this.isRecurring,
84-
has_base_fee: this.hasBaseFee,
85-
for_payer_type: this.forPayerType,
86-
publicly_visible: this.publiclyVisible,
87-
slug: this.slug,
88-
avatar_url: this.avatarUrl,
89-
features: this.features.map(feature => feature.__internal_toSnapshot()),
90-
};
91-
}
9259
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {
22
CancelSubscriptionParams,
3-
CommerceMoney,
3+
CommerceMoneyAmount,
44
CommerceSubscriptionItemJSON,
55
CommerceSubscriptionItemResource,
66
CommerceSubscriptionJSON,
@@ -12,7 +12,7 @@ import type {
1212

1313
import { unixEpochToDate } from '@/utils/date';
1414

15-
import { commerceMoneyFromJSON } from '../../utils';
15+
import { commerceMoneyAmountFromJSON } from '../../utils';
1616
import { BaseResource, CommercePlan, DeletedObject } from './internal';
1717

1818
export class CommerceSubscription extends BaseResource implements CommerceSubscriptionResource {
@@ -23,7 +23,7 @@ export class CommerceSubscription extends BaseResource implements CommerceSubscr
2323
pastDueAt!: Date | null;
2424
updatedAt!: Date | null;
2525
nextPayment: {
26-
amount: CommerceMoney;
26+
amount: CommerceMoneyAmount;
2727
date: Date;
2828
} | null = null;
2929
subscriptionItems!: CommerceSubscriptionItemResource[];
@@ -47,7 +47,7 @@ export class CommerceSubscription extends BaseResource implements CommerceSubscr
4747
this.pastDueAt = data.past_due_at ? unixEpochToDate(data.past_due_at) : null;
4848
this.nextPayment = data.next_payment
4949
? {
50-
amount: commerceMoneyFromJSON(data.next_payment.amount),
50+
amount: commerceMoneyAmountFromJSON(data.next_payment.amount),
5151
date: unixEpochToDate(data.next_payment.date),
5252
}
5353
: null;
@@ -69,9 +69,9 @@ export class CommerceSubscriptionItem extends BaseResource implements CommerceSu
6969
canceledAt!: Date | null;
7070
pastDueAt!: Date | null;
7171
//TODO(@COMMERCE): Why can this be undefined ?
72-
amount?: CommerceMoney;
72+
amount?: CommerceMoneyAmount;
7373
credit?: {
74-
amount: CommerceMoney;
74+
amount: CommerceMoneyAmount;
7575
};
7676
isFreeTrial!: boolean;
7777

@@ -98,8 +98,9 @@ export class CommerceSubscriptionItem extends BaseResource implements CommerceSu
9898
this.periodEnd = data.period_end ? unixEpochToDate(data.period_end) : null;
9999
this.canceledAt = data.canceled_at ? unixEpochToDate(data.canceled_at) : null;
100100

101-
this.amount = data.amount ? commerceMoneyFromJSON(data.amount) : undefined;
102-
this.credit = data.credit && data.credit.amount ? { amount: commerceMoneyFromJSON(data.credit.amount) } : undefined;
101+
this.amount = data.amount ? commerceMoneyAmountFromJSON(data.amount) : undefined;
102+
this.credit =
103+
data.credit && data.credit.amount ? { amount: commerceMoneyAmountFromJSON(data.credit.amount) } : undefined;
103104

104105
this.isFreeTrial = this.withDefault(data.is_free_trial, false);
105106
return this;

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { __experimental_useCheckout as useCheckout, useOrganization } from '@clerk/shared/react';
2-
import type { CommerceMoney, CommercePaymentSourceResource, ConfirmCheckoutParams } from '@clerk/types';
2+
import type { CommerceMoneyAmount, CommercePaymentSourceResource, ConfirmCheckoutParams } from '@clerk/types';
33
import { useMemo, useState } from 'react';
44

55
import { Card } from '@/ui/elements/Card';
@@ -35,6 +35,8 @@ export const CheckoutForm = withCardStateProvider(() => {
3535
const showPastDue = !!totals.pastDue?.amount && totals.pastDue.amount > 0;
3636
const showDowngradeInfo = !isImmediatePlanChange;
3737

38+
const fee = planPeriod === 'month' ? plan.fee : plan.annualMonthlyFee;
39+
3840
return (
3941
<Drawer.Body>
4042
<Box
@@ -54,7 +56,7 @@ export const CheckoutForm = withCardStateProvider(() => {
5456
/>
5557
<LineItems.Description
5658
prefix={planPeriod === 'annual' ? 'x12' : undefined}
57-
text={`${plan.currencySymbol}${planPeriod === 'month' ? plan.amountFormatted : plan.annualMonthlyAmountFormatted}`}
59+
text={`${fee.currencySymbol}${fee.amountFormatted}`}
5860
suffix={localizationKeys('commerce.checkout.perMonth')}
5961
/>
6062
</LineItems.Group>
@@ -312,7 +314,7 @@ const ExistingPaymentSourceForm = withCardStateProvider(
312314
totalDueNow,
313315
paymentSources,
314316
}: {
315-
totalDueNow: CommerceMoney;
317+
totalDueNow: CommerceMoneyAmount;
316318
paymentSources: CommercePaymentSourceResource[];
317319
}) => {
318320
const { checkout } = useCheckout();

0 commit comments

Comments
 (0)