-
Notifications
You must be signed in to change notification settings - Fork 462
feat(clerk-js, localizations, ui): Add credits information in the user/org profile #8977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dstaley
merged 20 commits into
main
from
lamone/bill-1613-add-credits-information-in-the-userorg-profile
Jun 30, 2026
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4e6cafc
feat: add credits information in the user/org profile
l-armstrong 3f25810
feat: add the ability for the payer to view their credit history
l-armstrong 36a6370
fix: fix the leftIcon lint error
l-armstrong 47f88d7
chore: remove reason column from table and increase maxsize in bundle
l-armstrong 383a3eb
fix: increase bundlewatch sizes
l-armstrong 936fd7a
fix: fix import sort and non-null assertion
l-armstrong a5080a2
feat: add changeset
l-armstrong bfd5f33
fix added the explicit JSX.Element return type to CreditHistoryPage
l-armstrong 3da2c54
fix: change the description of the changeset
l-armstrong 98e5505
fix: remove the need for the payerid from the client side
l-armstrong eb85c11
fix: use $ formatter from useLocalizations and stop use of Box with as
l-armstrong 747b6d9
fix: fix format issue
l-armstrong 9468645
fix: fix balance hook
l-armstrong ed992f6
fix: clean up using payer_id
l-armstrong ff37c1a
fix make the credit balance internal
l-armstrong cf80e51
fix: use the correct BillingMoneyAmount type instead of a raw amount
l-armstrong 6bdb860
fix: fix linter issue
l-armstrong 19d215b
fix: fix the entry amount.
l-armstrong 4902172
fix: remove arrow
l-armstrong 2878666
fix: fix borders in credit history page.
l-armstrong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| '@clerk/localizations': minor | ||
| '@clerk/clerk-js': minor | ||
| '@clerk/shared': minor | ||
| '@clerk/ui': minor | ||
| --- | ||
|
|
||
| Add account credits section and credit history page to the billing tab for payers with an existing credit balance. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/clerk-js/src/core/resources/BillingCreditBalance.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import type { BillingCreditBalanceJSON, BillingCreditBalanceResource, BillingMoneyAmount } from '@clerk/shared/types'; | ||
|
|
||
| import { billingMoneyAmountFromJSON } from '../../utils'; | ||
|
|
||
| export class BillingCreditBalance implements BillingCreditBalanceResource { | ||
| balance: BillingMoneyAmount | null; | ||
|
|
||
| constructor(data: BillingCreditBalanceJSON) { | ||
| this.balance = data.balance ? billingMoneyAmountFromJSON(data.balance) : null; | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
packages/clerk-js/src/core/resources/BillingCreditLedger.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import type { BillingCreditLedgerJSON, BillingCreditLedgerResource, BillingMoneyAmount } from '@clerk/shared/types'; | ||
|
|
||
| import { billingMoneyAmountFromJSON } from '@/utils/billing'; | ||
| import { unixEpochToDate } from '@/utils/date'; | ||
|
|
||
| import { BaseResource } from './internal'; | ||
|
|
||
| export class BillingCreditLedger extends BaseResource implements BillingCreditLedgerResource { | ||
| id!: string; | ||
| amount!: BillingMoneyAmount; | ||
| sourceType!: string; | ||
| sourceId!: string; | ||
| createdAt!: Date; | ||
|
|
||
| constructor(data: BillingCreditLedgerJSON) { | ||
| super(); | ||
| this.fromJSON(data); | ||
| } | ||
|
|
||
| protected fromJSON(data: BillingCreditLedgerJSON | null): this { | ||
| if (!data) { | ||
| return this; | ||
| } | ||
|
|
||
| this.id = data.id; | ||
| this.amount = billingMoneyAmountFromJSON(data.amount); | ||
| this.sourceType = data.source_type; | ||
| this.sourceId = data.source_id; | ||
| this.createdAt = unixEpochToDate(data.created_at); | ||
| return this; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { useCallback, useEffect, useMemo, useRef } from 'react'; | ||
|
|
||
| import { eventMethodCalled } from '../../telemetry/events'; | ||
| import type { BillingCreditBalanceResource, ForPayerType } from '../../types'; | ||
| import { useAssertWrappedByClerkProvider, useClerkInstanceContext } from '../contexts'; | ||
| import { defineKeepPreviousDataFn } from '../query/keep-previous-data'; | ||
| import { useClerkQueryClient } from '../query/use-clerk-query-client'; | ||
| import { useClerkQuery } from '../query/useQuery'; | ||
| import { STABLE_KEYS } from '../stable-keys'; | ||
| import { useOrganizationBase } from './base/useOrganizationBase'; | ||
| import { useUserBase } from './base/useUserBase'; | ||
| import { createCacheKeys } from './createCacheKeys'; | ||
| import { useBillingIsEnabled } from './useBillingIsEnabled'; | ||
| import { useClearQueriesOnSignOut } from './useClearQueriesOnSignOut'; | ||
|
|
||
| const HOOK_NAME = 'useCreditBalance'; | ||
|
|
||
| export type UseCreditBalanceParams = { | ||
| for?: ForPayerType; | ||
| keepPreviousData?: boolean; | ||
| enabled?: boolean; | ||
|
dstaley marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| export type CreditBalanceResult = { | ||
| data: BillingCreditBalanceResource | undefined | null; | ||
| error: Error | undefined; | ||
| isLoading: boolean; | ||
| isFetching: boolean; | ||
| revalidate: () => Promise<void> | void; | ||
| }; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| export function __internal_useCreditBalanceQuery(params?: UseCreditBalanceParams): CreditBalanceResult { | ||
| useAssertWrappedByClerkProvider(HOOK_NAME); | ||
|
|
||
| const clerk = useClerkInstanceContext(); | ||
| const user = useUserBase(); | ||
| const organization = useOrganizationBase(); | ||
|
|
||
| const billingEnabled = useBillingIsEnabled(params); | ||
|
|
||
| const recordedRef = useRef(false); | ||
| useEffect(() => { | ||
| if (!recordedRef.current && clerk?.telemetry) { | ||
| clerk.telemetry.record(eventMethodCalled(HOOK_NAME)); | ||
| recordedRef.current = true; | ||
| } | ||
| }, [clerk]); | ||
|
|
||
| const keepPreviousData = params?.keepPreviousData ?? false; | ||
|
|
||
| const [queryClient] = useClerkQueryClient(); | ||
|
|
||
| const { queryKey, invalidationKey, stableKey, authenticated } = useMemo(() => { | ||
| const isOrganization = params?.for === 'organization'; | ||
| const safeOrgId = isOrganization ? organization?.id : undefined; | ||
|
|
||
| return createCacheKeys({ | ||
| stablePrefix: STABLE_KEYS.CREDIT_BALANCE_KEY, | ||
| authenticated: true, | ||
| tracked: { | ||
| userId: user?.id, | ||
| orgId: safeOrgId, | ||
| }, | ||
| untracked: { | ||
| args: { orgId: safeOrgId }, | ||
| }, | ||
| }); | ||
| }, [user?.id, organization?.id, params?.for]); | ||
|
|
||
| const queriesEnabled = Boolean(user?.id && billingEnabled && (params?.enabled ?? true)); | ||
| useClearQueriesOnSignOut({ | ||
| isSignedOut: user === null, | ||
| authenticated, | ||
| stableKeys: stableKey, | ||
| }); | ||
|
|
||
| const query = useClerkQuery({ | ||
| queryKey, | ||
| queryFn: ({ queryKey }) => { | ||
| const obj = queryKey[3]; | ||
| return clerk.billing.getCreditBalance(obj.args); | ||
| }, | ||
| staleTime: 1_000 * 60, | ||
| enabled: queriesEnabled, | ||
| placeholderData: defineKeepPreviousDataFn(keepPreviousData && queriesEnabled), | ||
| }); | ||
|
|
||
| const revalidate = useCallback( | ||
| () => queryClient.invalidateQueries({ queryKey: invalidationKey }), | ||
| [queryClient, invalidationKey], | ||
| ); | ||
|
|
||
| return { | ||
| data: query.data, | ||
| error: query.error ?? undefined, | ||
| isLoading: query.isLoading, | ||
| isFetching: query.isFetching, | ||
| revalidate, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { useCallback, useEffect, useMemo, useRef } from 'react'; | ||
|
|
||
| import { eventMethodCalled } from '../../telemetry/events'; | ||
| import type { BillingCreditLedgerResource, ClerkPaginatedResponse, ForPayerType } from '../../types'; | ||
| import { useAssertWrappedByClerkProvider, useClerkInstanceContext } from '../contexts'; | ||
| import { useClerkQueryClient } from '../query/use-clerk-query-client'; | ||
| import { useClerkQuery } from '../query/useQuery'; | ||
| import { INTERNAL_STABLE_KEYS } from '../stable-keys'; | ||
| import { useOrganizationBase } from './base/useOrganizationBase'; | ||
| import { useUserBase } from './base/useUserBase'; | ||
| import { createCacheKeys } from './createCacheKeys'; | ||
| import { useBillingIsEnabled } from './useBillingIsEnabled'; | ||
| import { useClearQueriesOnSignOut } from './useClearQueriesOnSignOut'; | ||
|
|
||
| const HOOK_NAME = 'useCreditHistory'; | ||
|
|
||
| export type UseCreditHistoryParams = { | ||
| for?: ForPayerType; | ||
| enabled?: boolean; | ||
| }; | ||
|
|
||
| export type CreditHistoryResult = { | ||
| data: ClerkPaginatedResponse<BillingCreditLedgerResource> | undefined; | ||
| error: Error | undefined; | ||
| isLoading: boolean; | ||
| isFetching: boolean; | ||
| revalidate: () => Promise<void> | void; | ||
| }; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| export function __internal_useCreditHistoryQuery(params?: UseCreditHistoryParams): CreditHistoryResult { | ||
| useAssertWrappedByClerkProvider(HOOK_NAME); | ||
|
|
||
| const clerk = useClerkInstanceContext(); | ||
| const user = useUserBase(); | ||
| const organization = useOrganizationBase(); | ||
|
|
||
| const billingEnabled = useBillingIsEnabled(params); | ||
|
|
||
| const recordedRef = useRef(false); | ||
| useEffect(() => { | ||
| if (!recordedRef.current && clerk?.telemetry) { | ||
| clerk.telemetry.record(eventMethodCalled(HOOK_NAME)); | ||
| recordedRef.current = true; | ||
| } | ||
| }, [clerk]); | ||
|
|
||
| const [queryClient] = useClerkQueryClient(); | ||
|
|
||
| const { queryKey, invalidationKey, stableKey, authenticated } = useMemo(() => { | ||
| const isOrganization = params?.for === 'organization'; | ||
| const safeOrgId = isOrganization ? organization?.id : undefined; | ||
|
|
||
| return createCacheKeys({ | ||
| stablePrefix: INTERNAL_STABLE_KEYS.CREDIT_HISTORY_KEY, | ||
| authenticated: true, | ||
| tracked: { | ||
| userId: user?.id, | ||
| orgId: safeOrgId, | ||
| }, | ||
| untracked: { | ||
| args: { orgId: safeOrgId }, | ||
| }, | ||
| }); | ||
| }, [user?.id, organization?.id, params?.for]); | ||
|
|
||
| const queriesEnabled = Boolean(user?.id && billingEnabled && (params?.enabled ?? true)); | ||
| useClearQueriesOnSignOut({ | ||
| isSignedOut: user === null, | ||
| authenticated, | ||
| stableKeys: stableKey, | ||
| }); | ||
|
|
||
| const query = useClerkQuery({ | ||
| queryKey, | ||
| queryFn: ({ queryKey }) => { | ||
| const obj = queryKey[3]; | ||
| return clerk.billing.getCreditHistory(obj.args); | ||
| }, | ||
| staleTime: 1_000 * 60, | ||
| enabled: queriesEnabled, | ||
| }); | ||
|
|
||
| const revalidate = useCallback( | ||
| () => queryClient.invalidateQueries({ queryKey: invalidationKey }), | ||
| [queryClient, invalidationKey], | ||
| ); | ||
|
|
||
| return { | ||
| data: query.data, | ||
| error: query.error ?? undefined, | ||
| isLoading: query.isLoading, | ||
| isFetching: query.isFetching, | ||
| revalidate, | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❔ Should it be
BillingAccountCreditBalance?cc @dstaley
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the internal type is
CommerceCreditBalanceResponse, soBillingCreditBalanceJSONwould be the correct version of the JavaScript type.