Skip to content

Commit 40ff41c

Browse files
committed
fix(clerk-js,localizations,shared,ui): Use new credits field
1 parent 136e9f3 commit 40ff41c

7 files changed

Lines changed: 113 additions & 20 deletions

File tree

packages/clerk-js/sandbox/scenarios/checkout-account-credit.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,15 @@ export function CheckoutAccountCredit(): MockScenario {
2222
const subscriptionHandler = http.get('https://*.clerk.accounts.dev/v1/me/billing/subscription', () => {
2323
return HttpResponse.json({
2424
response: {
25-
data: {
26-
account_credit: 100,
27-
},
25+
data: {},
2826
},
2927
});
3028
});
3129

3230
const paymentMethodsHandler = http.get('https://*.clerk.accounts.dev/v1/me/billing/payment_methods', () => {
3331
return HttpResponse.json({
3432
response: {
35-
data: {
36-
account_credit: 100,
37-
},
33+
data: {},
3834
},
3935
});
4036
});
@@ -161,11 +157,38 @@ export function CheckoutAccountCredit(): MockScenario {
161157
currency: 'string',
162158
currency_symbol: '$',
163159
},
164-
account_credit: {
165-
amount: 1,
166-
amount_formatted: '10.00',
167-
currency: 'string',
168-
currency_symbol: '$',
160+
credits: {
161+
proration: {
162+
amount: {
163+
amount: 1,
164+
amount_formatted: '5.00',
165+
currency: 'string',
166+
currency_symbol: '$',
167+
},
168+
cycle_days_remaining: 1,
169+
cycle_days_total: 1,
170+
cycle_remaining_percent: 1,
171+
},
172+
payer_credit: {
173+
remaining_balance: {
174+
amount: 1,
175+
amount_formatted: '100.00',
176+
currency: 'string',
177+
currency_symbol: '$',
178+
},
179+
applied_amount: {
180+
amount: 1,
181+
amount_formatted: '10.00',
182+
currency: 'string',
183+
currency_symbol: '$',
184+
},
185+
},
186+
total: {
187+
amount: 1,
188+
amount_formatted: '15.00',
189+
currency: 'string',
190+
currency_symbol: '$',
191+
},
169192
},
170193
},
171194
subscription_item: {

packages/clerk-js/src/utils/billing.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type {
22
BillingCheckoutTotals,
33
BillingCheckoutTotalsJSON,
4+
BillingCredits,
5+
BillingCreditsJSON,
46
BillingMoneyAmount,
57
BillingMoneyAmountJSON,
68
BillingStatementTotals,
@@ -16,6 +18,26 @@ export const billingMoneyAmountFromJSON = (data: BillingMoneyAmountJSON): Billin
1618
};
1719
};
1820

21+
const billingCreditsFromJSON = (data: BillingCreditsJSON): BillingCredits => {
22+
return {
23+
proration: data.proration
24+
? {
25+
amount: billingMoneyAmountFromJSON(data.proration.amount),
26+
cycleDaysRemaining: data.proration.cycle_days_remaining,
27+
cycleDaysTotal: data.proration.cycle_days_total,
28+
cycleRemainingPercent: data.proration.cycle_remaining_percent,
29+
}
30+
: null,
31+
payerCredit: data.payer_credit
32+
? {
33+
remainingBalance: billingMoneyAmountFromJSON(data.payer_credit.remaining_balance),
34+
appliedAmount: billingMoneyAmountFromJSON(data.payer_credit.applied_amount),
35+
}
36+
: null,
37+
total: billingMoneyAmountFromJSON(data.total),
38+
};
39+
};
40+
1941
export const billingTotalsFromJSON = <T extends BillingStatementTotalsJSON | BillingCheckoutTotalsJSON>(
2042
data: T,
2143
): T extends { total_due_now: BillingMoneyAmountJSON } ? BillingCheckoutTotals : BillingStatementTotals => {
@@ -31,8 +53,8 @@ export const billingTotalsFromJSON = <T extends BillingStatementTotalsJSON | Bil
3153
if ('credit' in data) {
3254
totals.credit = data.credit ? billingMoneyAmountFromJSON(data.credit) : null;
3355
}
34-
if ('account_credit' in data) {
35-
totals.accountCredit = data.account_credit ? billingMoneyAmountFromJSON(data.account_credit) : null;
56+
if ('credits' in data) {
57+
totals.credits = data.credits ? billingCreditsFromJSON(data.credits) : null;
3658
}
3759
if ('total_due_now' in data) {
3860
totals.totalDueNow = billingMoneyAmountFromJSON(data.total_due_now);

packages/localizations/src/en-US.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export const enUS: LocalizationResource = {
110110
},
111111
credit: 'Credit',
112112
creditRemainder: 'Credit for the remainder of your current subscription.',
113-
accountCreditRemainder: 'Credit from account balance.',
113+
payerCreditRemainder: 'Credit from account balance.',
114114
defaultFreePlanActive: "You're currently on the Free plan",
115115
free: 'Free',
116116
getStarted: 'Get started',

packages/shared/src/types/billing.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,24 @@ export interface BillingMoneyAmount {
690690
currencySymbol: string;
691691
}
692692

693+
export interface BillingProrationCreditDetail {
694+
amount: BillingMoneyAmount;
695+
cycleDaysRemaining: number;
696+
cycleDaysTotal: number;
697+
cycleRemainingPercent: number;
698+
}
699+
700+
export interface BillingPayerCredit {
701+
remainingBalance: BillingMoneyAmount;
702+
appliedAmount: BillingMoneyAmount;
703+
}
704+
705+
export interface BillingCredits {
706+
proration: BillingProrationCreditDetail | null;
707+
payerCredit: BillingPayerCredit | null;
708+
total: BillingMoneyAmount;
709+
}
710+
693711
/**
694712
* The `BillingCheckoutTotals` type represents the total costs, taxes, and other pricing details for a checkout session.
695713
*
@@ -716,7 +734,7 @@ export interface BillingCheckoutTotals {
716734
* Any credits (like account balance or promo credits) that are being applied to the checkout.
717735
*/
718736
credit: BillingMoneyAmount | null;
719-
accountCredit: BillingMoneyAmount | null;
737+
credits: BillingCredits | null;
720738
/**
721739
* Any outstanding amount from previous unpaid invoices that is being collected as part of the checkout.
722740
*/

packages/shared/src/types/json.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ export interface BillingSubscriptionItemJSON extends ClerkResourceJSON {
695695
credit?: {
696696
amount: BillingMoneyAmountJSON;
697697
};
698+
credits?: BillingCreditsJSON;
698699
plan: BillingPlanJSON;
699700
plan_period: BillingSubscriptionPlanPeriod;
700701
status: BillingSubscriptionStatus;
@@ -744,6 +745,33 @@ export interface BillingMoneyAmountJSON {
744745
currency_symbol: string;
745746
}
746747

748+
/**
749+
* Contains proration credit details including billing cycle information.
750+
*/
751+
export interface BillingProrationCreditDetailJSON {
752+
amount: BillingMoneyAmountJSON;
753+
cycle_days_remaining: number;
754+
cycle_days_total: number;
755+
cycle_remaining_percent: number;
756+
}
757+
758+
/**
759+
* Contains payer credit details including the available balance and the amount applied to this checkout.
760+
*/
761+
export interface BillingPayerCreditJSON {
762+
remaining_balance: BillingMoneyAmountJSON;
763+
applied_amount: BillingMoneyAmountJSON;
764+
}
765+
766+
/**
767+
* Unified credits breakdown for checkout totals. Can be used instead of `credit` field.
768+
*/
769+
export interface BillingCreditsJSON {
770+
proration: BillingProrationCreditDetailJSON | null;
771+
payer_credit: BillingPayerCreditJSON | null;
772+
total: BillingMoneyAmountJSON;
773+
}
774+
747775
/**
748776
* @experimental This is an experimental API for the Billing feature that is available under a public beta, and the API is subject to change. It is advised to [pin](https://clerk.com/docs/pinning) the SDK version and the clerk-js version to avoid breaking changes.
749777
*/
@@ -753,6 +781,7 @@ export interface BillingCheckoutTotalsJSON {
753781
tax_total: BillingMoneyAmountJSON;
754782
total_due_now: BillingMoneyAmountJSON;
755783
credit: BillingMoneyAmountJSON | null;
784+
credits: BillingCreditsJSON | null;
756785
account_credit: BillingMoneyAmountJSON | null;
757786
past_due: BillingMoneyAmountJSON | null;
758787
total_due_after_free_trial: BillingMoneyAmountJSON | null;

packages/shared/src/types/localization.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export type __internal_LocalizationResource = {
207207
subtotal: LocalizationValue;
208208
credit: LocalizationValue;
209209
creditRemainder: LocalizationValue;
210-
accountCreditRemainder: LocalizationValue;
210+
payerCreditRemainder: LocalizationValue;
211211
totalDue: LocalizationValue;
212212
totalDueToday: LocalizationValue;
213213
pastDue: LocalizationValue;

packages/ui/src/components/Checkout/CheckoutForm.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ export const CheckoutForm = withCardStateProvider(() => {
3535
return null;
3636
}
3737

38-
const showProratedCredit = !!totals.credit?.amount && totals.credit.amount > 0;
39-
const showAccountCredits = !!totals.accountCredit?.amount && totals.accountCredit.amount > 0;
38+
const showProratedCredit = !!totals.credits?.proration?.amount && totals.credits.proration.amount.amount > 0;
39+
const showAccountCredits =
40+
!!totals.credits?.payerCredit?.appliedAmount && totals.credits.payerCredit.appliedAmount.amount > 0;
4041
const showPastDue = !!totals.pastDue?.amount && totals.pastDue.amount > 0;
4142
const showDowngradeInfo = !isImmediatePlanChange;
4243

@@ -89,9 +90,9 @@ export const CheckoutForm = withCardStateProvider(() => {
8990
)}
9091
{showAccountCredits && (
9192
<LineItems.Group variant='tertiary'>
92-
<LineItems.Title title={localizationKeys('billing.accountCreditRemainder')} />
93+
<LineItems.Title title={localizationKeys('billing.payerCreditRemainder')} />
9394
<LineItems.Description
94-
text={`- ${totals.accountCredit?.currencySymbol}${totals.accountCredit?.amountFormatted}`}
95+
text={`- ${totals.credits?.payerCredit?.appliedAmount?.currencySymbol}${totals.credits?.payerCredit?.appliedAmount?.amountFormatted}`}
9596
/>
9697
</LineItems.Group>
9798
)}

0 commit comments

Comments
 (0)