Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(billing): include prepaid credits in credit balance (#45177)

Kanishk Dudeja9d2807e19b77
diff --git a/apps/studio/components/interfaces/Organization/BillingSettings/CreditBalance.tsx b/apps/studio/components/interfaces/Organization/BillingSettings/CreditBalance.tsx
index ca1d7e25209..72a964c1988 100644
--- a/apps/studio/components/interfaces/Organization/BillingSettings/CreditBalance.tsx
+++ b/apps/studio/components/interfaces/Organization/BillingSettings/CreditBalance.tsx
@@ -3,6 +3,7 @@ import { useParams } from 'common'
import { CreditCodeRedemption } from './CreditCodeRedemption'
import { CreditTopUp } from './CreditTopUp'
+import { getTotalCreditBalanceCents } from './helpers'
import {
ScaffoldSection,
ScaffoldSectionContent,
@@ -31,13 +32,14 @@ const CreditBalance = () => {
isSuccess,
} = useOrgSubscriptionQuery({ orgSlug: slug }, { enabled: canReadSubscriptions })
- const customerBalance = (subscription?.customer_balance ?? 0) / 100
- const isCredit = customerBalance < 0
- const isDebt = customerBalance > 0
- const balance =
- isCredit && customerBalance !== 0
- ? customerBalance.toFixed(2).toString().replace('-', '')
- : customerBalance.toFixed(2)
+ const combinedCreditBalanceCents = getTotalCreditBalanceCents({
+ customerBalance: subscription?.customer_balance,
+ prepaidCreditsBalance: subscription?.prepaid_credits_balance,
+ })
+ const combinedCreditBalance = combinedCreditBalanceCents / 100
+ const hasCredits = combinedCreditBalanceCents > 0
+ const hasDebt = combinedCreditBalanceCents < 0
+ const balance = Math.abs(combinedCreditBalance).toFixed(2)
return (
<ScaffoldSection>
@@ -47,8 +49,11 @@ const CreditBalance = () => {
<p className="text-foreground text-base m-0">Credit Balance</p>
</div>
<p className="text-sm text-foreground-light m-0">
- Credits will be applied to future invoices, before charging your payment method. If your
- credit balance runs out, your default payment method will be charged.
+ Credits will be applied to future invoices, before charging your payment method. This
+ balance includes purchased credits and any prorated credits from plan changes.
+ </p>
+ <p className="text-sm text-foreground-light m-0">
+ If your credits run out, your default payment method will be charged.
</p>
</div>
</ScaffoldSectionDetail>
@@ -79,10 +84,10 @@ const CreditBalance = () => {
<div className="flex w-full justify-between items-center">
<span>Balance</span>
<div className="flex items-center space-x-1">
- {isDebt && <h4 className="opacity-50">-</h4>}
+ {hasDebt && <h4 className="opacity-50">-</h4>}
<h4 className="opacity-50">$</h4>
<h1 className="relative">{balance}</h1>
- {isCredit && <h4 className="opacity-50">/credits</h4>}
+ {hasCredits && <h4 className="opacity-50">/credits</h4>}
</div>
</div>
)}
diff --git a/apps/studio/components/interfaces/Organization/BillingSettings/CreditCodeRedemption.tsx b/apps/studio/components/interfaces/Organization/BillingSettings/CreditCodeRedemption.tsx
index ffdf1c77051..ff1479f94a8 100644
--- a/apps/studio/components/interfaces/Organization/BillingSettings/CreditCodeRedemption.tsx
+++ b/apps/studio/components/interfaces/Organization/BillingSettings/CreditCodeRedemption.tsx
@@ -26,6 +26,7 @@ import { Admonition, ShimmeringLoader, TimestampInfo } from 'ui-patterns'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { z } from 'zod'
+import { getTotalCreditBalanceCents } from './helpers'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { UpgradePlanButton } from '@/components/ui/UpgradePlanButton'
import { useOrganizationCreditCodeRedemptionMutation } from '@/data/organizations/organization-credit-code-redemption-mutation'
@@ -59,6 +60,12 @@ export const CreditCodeRedemption = ({
const { data: org, isLoading: isOrgLoading } = useOrganizationQuery({ slug })
const { data: customerProfile, isLoading: isCustomerProfileLoading } =
useOrganizationCustomerProfileQuery({ slug })
+ const combinedCreditBalanceCents = customerProfile
+ ? getTotalCreditBalanceCents({
+ customerBalance: customerProfile.balance,
+ prepaidCreditsBalance: customerProfile.prepaid_credits_balance,
+ })
+ : undefined
const { can: canRedeemCode, isSuccess: isPermissionsLoaded } = useAsyncCheckPermissions(
PermissionAction.BILLING_WRITE,
@@ -282,12 +289,12 @@ export const CreditCodeRedemption = ({
)}
/>
- {customerProfile && customerProfile.balance < 0 && (
+ {combinedCreditBalanceCents !== undefined && combinedCreditBalanceCents > 0 && (
<div className="flex w-full justify-between items-center">
<span className="text-sm">Current Balance</span>
<div className="flex items-center gap-x-1">
<p className="opacity-50 text-sm">$</p>
- <p className="text-2xl">{customerProfile.balance / -100}</p>
+ <p className="text-2xl">{combinedCreditBalanceCents / 100}</p>
<p className="opacity-50 text-sm">/credits</p>
</div>
</div>
diff --git a/apps/studio/components/interfaces/Organization/BillingSettings/helpers.ts b/apps/studio/components/interfaces/Organization/BillingSettings/helpers.ts
index fb80aa7b2a6..afa26fd4318 100644
--- a/apps/studio/components/interfaces/Organization/BillingSettings/helpers.ts
+++ b/apps/studio/components/interfaces/Organization/BillingSettings/helpers.ts
@@ -94,3 +94,13 @@ export const generateUpgradeReasons = (originalPlan?: string, upgradedPlan?: str
return reasons
}
+
+// For `customerBalance`, negative sign means credit.
+// Negate it first so both sources contribute as positive credit amounts before combining.
+export const getTotalCreditBalanceCents = ({
+ customerBalance = 0,
+ prepaidCreditsBalance = 0,
+}: {
+ customerBalance?: number
+ prepaidCreditsBalance?: number
+}) => -customerBalance + prepaidCreditsBalance
diff --git a/packages/api-types/types/platform.d.ts b/packages/api-types/types/platform.d.ts
index 30cb5676d1b..d78a970759c 100644
--- a/packages/api-types/types/platform.d.ts
+++ b/packages/api-types/types/platform.d.ts
@@ -6071,6 +6071,7 @@ export interface components {
billing_name?: string
billing_via_partner: boolean
email: string
+ prepaid_credits_balance?: number
tax_id: {
country: string
type: string
@@ -6767,6 +6768,7 @@ export interface components {
id: 'free' | 'pro' | 'team' | 'enterprise' | 'platform'
name: string
}
+ prepaid_credits_balance?: number
project_addons: {
addons: {
/** @enum {string} */