Skip to content

Commit 6bb1472

Browse files
committed
fix(billing): make stripe metering skip list env-based
1 parent 3144056 commit 6bb1472

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ STRIPE_SECRET_KEY=sk_test_dummy_stripe_secret
1717
STRIPE_WEBHOOK_SECRET_KEY=whsec_dummy_webhook_secret
1818
STRIPE_USAGE_PRICE_ID=price_dummy_usage_id
1919
STRIPE_TEAM_FEE_PRICE_ID=price_dummy_team_fee_id
20+
# Optional: comma-separated user IDs to skip Stripe metering (dev/staging)
21+
CODEBUFF_STRIPE_METERING_SKIP_USER_IDS=
2022

2123
# External Services
2224
LINKUP_API_KEY=dummy_linkup_key

packages/billing/src/stripe-metering.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { TEST_USER_ID } from '@codebuff/common/old-constants'
21
import { withRetry, withTimeout } from '@codebuff/common/util/promise'
32
import db from '@codebuff/internal/db'
43
import * as schema from '@codebuff/internal/db/schema'
@@ -9,6 +8,8 @@ import type { Logger } from '@codebuff/common/types/contracts/logger'
98

109
const STRIPE_METER_EVENT_NAME = 'credits'
1110
const STRIPE_METER_REQUEST_TIMEOUT_MS = 10_000
11+
const STRIPE_METERING_SKIP_USER_IDS_ENV_VAR =
12+
'CODEBUFF_STRIPE_METERING_SKIP_USER_IDS'
1213

1314
function shouldAttemptStripeMetering(): boolean {
1415
// Avoid sending Stripe metering events in CI/tests, and when Stripe isn't configured.
@@ -17,6 +18,16 @@ function shouldAttemptStripeMetering(): boolean {
1718
return Boolean(process.env.STRIPE_SECRET_KEY)
1819
}
1920

21+
function shouldSkipStripeMeteringForUser(userId: string): boolean {
22+
const rawIds = process.env[STRIPE_METERING_SKIP_USER_IDS_ENV_VAR]
23+
if (!rawIds) return false
24+
return rawIds
25+
.split(',')
26+
.map((value) => value.trim())
27+
.filter(Boolean)
28+
.includes(userId)
29+
}
30+
2031
export async function reportPurchasedCreditsToStripe(params: {
2132
userId: string
2233
stripeCustomerId?: string | null
@@ -48,7 +59,7 @@ export async function reportPurchasedCreditsToStripe(params: {
4859
} = params
4960

5061
if (purchasedCredits <= 0) return
51-
if (userId === TEST_USER_ID) return
62+
if (shouldSkipStripeMeteringForUser(userId)) return
5263
if (!shouldAttemptStripeMetering()) return
5364

5465
const logContext = { userId, purchasedCredits, eventId }

0 commit comments

Comments
 (0)