Skip to content

Commit c00c524

Browse files
authored
feat(clerk-js,localizations,shared): Render credits (#7885)
1 parent 73e34c1 commit c00c524

File tree

9 files changed

+57
-13
lines changed

9 files changed

+57
-13
lines changed

.changeset/sharp-monkeys-live.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@clerk/localizations': minor
3+
'@clerk/clerk-js': minor
4+
'@clerk/shared': minor
5+
---
6+
7+
Add support for displaying proration and account credits on payment attempts and statements.

packages/clerk-js/bundlewatch.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"files": [
3-
{ "path": "./dist/clerk.js", "maxSize": "929KB" },
3+
{ "path": "./dist/clerk.js", "maxSize": "930KB" },
44
{ "path": "./dist/clerk.browser.js", "maxSize": "87KB" },
55
{ "path": "./dist/clerk.legacy.browser.js", "maxSize": "129KB" },
66
{ "path": "./dist/clerk.headless*.js", "maxSize": "67KB" },

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
BillingCredits,
23
BillingMoneyAmount,
34
BillingSubscriptionItemJSON,
45
BillingSubscriptionItemResource,
@@ -12,7 +13,7 @@ import type {
1213

1314
import { unixEpochToDate } from '@/utils/date';
1415

15-
import { billingMoneyAmountFromJSON } from '../../utils';
16+
import { billingCreditsFromJSON, billingMoneyAmountFromJSON } from '../../utils';
1617
import { Billing } from '../modules/billing/namespace';
1718
import { BaseResource, BillingPlan, DeletedObject } from './internal';
1819

@@ -75,6 +76,7 @@ export class BillingSubscriptionItem extends BaseResource implements BillingSubs
7576
credit?: {
7677
amount: BillingMoneyAmount;
7778
};
79+
credits?: BillingCredits;
7880
isFreeTrial!: boolean;
7981

8082
constructor(data: BillingSubscriptionItemJSON) {
@@ -103,6 +105,8 @@ export class BillingSubscriptionItem extends BaseResource implements BillingSubs
103105
this.credit =
104106
data.credit && data.credit.amount ? { amount: billingMoneyAmountFromJSON(data.credit.amount) } : undefined;
105107

108+
this.credits = data.credits ? billingCreditsFromJSON(data.credits) : undefined;
109+
106110
this.isFreeTrial = this.withDefault(data.is_free_trial, false);
107111
return this;
108112
}

packages/clerk-js/src/ui/components/PaymentAttempts/PaymentAttemptPage.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,26 @@ function PaymentAttemptBody({ subscriptionItem }: { subscriptionItem: BillingSub
233233
text={`${subscriptionItem.amount?.currencySymbol}${subscriptionItem.amount?.amountFormatted}`}
234234
/>
235235
</LineItems.Group>
236-
{subscriptionItem.credit && subscriptionItem.credit.amount.amount > 0 && (
237-
<LineItems.Group variant='tertiary'>
238-
<LineItems.Title title={localizationKeys('billing.credit')} />
239-
<LineItems.Description
240-
text={`- ${subscriptionItem.credit.amount.currencySymbol}${subscriptionItem.credit.amount.amountFormatted}`}
241-
/>
242-
</LineItems.Group>
243-
)}
236+
{subscriptionItem.credits &&
237+
subscriptionItem.credits.proration &&
238+
subscriptionItem.credits.proration.amount.amount > 0 && (
239+
<LineItems.Group variant='tertiary'>
240+
<LineItems.Title title={localizationKeys('billing.prorationCredit')} />
241+
<LineItems.Description
242+
text={`- ${subscriptionItem.credits.proration.amount.currencySymbol}${subscriptionItem.credits.proration.amount.amountFormatted}`}
243+
/>
244+
</LineItems.Group>
245+
)}
246+
{subscriptionItem.credits &&
247+
subscriptionItem.credits.payer &&
248+
subscriptionItem.credits.payer.appliedAmount.amount > 0 && (
249+
<LineItems.Group variant='tertiary'>
250+
<LineItems.Title title={localizationKeys('billing.accountCredit')} />
251+
<LineItems.Description
252+
text={`- ${subscriptionItem.credits.payer.appliedAmount.currencySymbol}${subscriptionItem.credits.payer.appliedAmount.amountFormatted}`}
253+
/>
254+
</LineItems.Group>
255+
)}
244256
</LineItems.Root>
245257
</Box>
246258
);

packages/clerk-js/src/ui/components/Statements/StatementPage.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,24 @@ export const StatementPage = () => {
137137
</SimpleButton>
138138
}
139139
/>
140-
{item.subscriptionItem.credit && item.subscriptionItem.credit.amount.amount > 0 ? (
140+
{item.subscriptionItem.credits &&
141+
item.subscriptionItem.credits.proration &&
142+
item.subscriptionItem.credits.proration.amount.amount > 0 ? (
141143
<Statement.SectionContentDetailsListItem
142144
label={localizationKeys(
143145
`${localizationRoot}.billingPage.statementsSection.itemCaption__proratedCredit`,
144146
)}
145-
value={`(${item.subscriptionItem.credit.amount.currencySymbol}${item.subscriptionItem.credit.amount.amountFormatted})`}
147+
value={`(${item.subscriptionItem.credits.proration.amount.currencySymbol}${item.subscriptionItem.credits.proration.amount.amountFormatted})`}
148+
/>
149+
) : null}
150+
{item.subscriptionItem.credits &&
151+
item.subscriptionItem.credits.payer &&
152+
item.subscriptionItem.credits.payer.appliedAmount.amount > 0 ? (
153+
<Statement.SectionContentDetailsListItem
154+
label={localizationKeys(
155+
`${localizationRoot}.billingPage.statementsSection.itemCaption__payerCredit`,
156+
)}
157+
value={`(${item.subscriptionItem.credits.payer.appliedAmount.currencySymbol}${item.subscriptionItem.credits.payer.appliedAmount.amountFormatted})`}
146158
/>
147159
) : null}
148160
</Statement.SectionContentDetailsList>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const billingMoneyAmountFromJSON = (data: BillingMoneyAmountJSON): Billin
1818
};
1919
};
2020

21-
const billingCreditsFromJSON = (data: BillingCreditsJSON): BillingCredits => {
21+
export const billingCreditsFromJSON = (data: BillingCreditsJSON): BillingCredits => {
2222
return {
2323
proration: data.proration
2424
? {

packages/localizations/src/en-US.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ export const enUS: LocalizationResource = {
109109
totalDueAfterTrial: 'Total Due after trial ends in {{days}} days',
110110
},
111111
credit: 'Credit',
112+
prorationCredit: 'Prorated credit',
113+
accountCredit: 'Account credit',
112114
creditRemainder: 'Credit for the remainder of your current subscription.',
113115
payerCreditRemainder: 'Credit from account balance.',
114116
defaultFreePlanActive: "You're currently on the Free plan",
@@ -314,6 +316,7 @@ export const enUS: LocalizationResource = {
314316
empty: 'No statements to display',
315317
itemCaption__paidForPlan: 'Paid for {{plan}} {{period}} plan',
316318
itemCaption__proratedCredit: 'Prorated credit for partial usage of previous subscription',
319+
itemCaption__payerCredit: 'Credit from account balance',
317320
itemCaption__subscribedAndPaidForPlan: 'Subscribed and paid for {{plan}} {{period}} plan',
318321
notFound: 'Statement not found',
319322
tableHeader__amount: 'Amount',
@@ -1144,6 +1147,7 @@ export const enUS: LocalizationResource = {
11441147
empty: 'No statements to display',
11451148
itemCaption__paidForPlan: 'Paid for {{plan}} {{period}} plan',
11461149
itemCaption__proratedCredit: 'Prorated credit for partial usage of previous subscription',
1150+
itemCaption__payerCredit: 'Credit from account balance',
11471151
itemCaption__subscribedAndPaidForPlan: 'Subscribed and paid for {{plan}} {{period}} plan',
11481152
notFound: 'Statement not found',
11491153
tableHeader__amount: 'Amount',

packages/shared/src/types/billing.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ export interface BillingSubscriptionItemResource extends ClerkResource {
548548
*/
549549
amount: BillingMoneyAmount;
550550
};
551+
credits?: BillingCredits;
551552
/**
552553
* A function to cancel the subscription item. Accepts the following parameters:
553554
* <ul>

packages/shared/src/types/localization.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ export type __internal_LocalizationResource = {
206206
availableFeatures: LocalizationValue;
207207
subtotal: LocalizationValue;
208208
credit: LocalizationValue;
209+
prorationCredit: LocalizationValue;
210+
accountCredit: LocalizationValue;
209211
creditRemainder: LocalizationValue;
210212
payerCreditRemainder: LocalizationValue;
211213
totalDue: LocalizationValue;
@@ -910,6 +912,7 @@ export type __internal_LocalizationResource = {
910912
empty: LocalizationValue;
911913
itemCaption__paidForPlan: LocalizationValue;
912914
itemCaption__proratedCredit: LocalizationValue;
915+
itemCaption__payerCredit: LocalizationValue;
913916
itemCaption__subscribedAndPaidForPlan: LocalizationValue;
914917
notFound: LocalizationValue;
915918
tableHeader__date: LocalizationValue;
@@ -1148,6 +1151,7 @@ export type __internal_LocalizationResource = {
11481151
empty: LocalizationValue;
11491152
itemCaption__paidForPlan: LocalizationValue<'plan' | 'period'>;
11501153
itemCaption__proratedCredit: LocalizationValue;
1154+
itemCaption__payerCredit: LocalizationValue;
11511155
itemCaption__subscribedAndPaidForPlan: LocalizationValue<'plan' | 'period'>;
11521156
notFound: LocalizationValue;
11531157
tableHeader__date: LocalizationValue;

0 commit comments

Comments
 (0)