Skip to content

Commit 07b6845

Browse files
committed
Add some identifiers to stripe billing requests
1 parent aa7780c commit 07b6845

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

packages/billing/src/balance-calculator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ export async function consumeCredits(params: {
475475
stripeCustomerId: params.stripeCustomerId,
476476
purchasedCredits: result.fromPurchased,
477477
logger,
478+
eventId: crypto.randomUUID(),
478479
extraPayload: {
479480
source: 'consumeCredits',
480481
},

packages/billing/src/stripe-metering.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export async function reportPurchasedCreditsToStripe(params: {
5050
if (userId === TEST_USER_ID) return
5151
if (!shouldAttemptStripeMetering()) return
5252

53-
const logContext = { userId, purchasedCredits, eventId }
53+
const identifier = eventId ?? crypto.randomUUID()
54+
const logContext = { userId, purchasedCredits, eventId, identifier }
5455

5556
let stripeCustomerId = providedStripeCustomerId
5657
if (stripeCustomerId === undefined) {
@@ -76,7 +77,7 @@ export async function reportPurchasedCreditsToStripe(params: {
7677
}
7778

7879
const stripeTimestamp = Math.floor(timestamp.getTime() / 1000)
79-
const idempotencyKey = eventId ? `meter-${eventId}` : undefined
80+
const idempotencyKey = `meter-${identifier}`
8081

8182
try {
8283
await withTimeout(
@@ -85,15 +86,15 @@ export async function reportPurchasedCreditsToStripe(params: {
8586
stripeServer.billing.meterEvents.create(
8687
{
8788
event_name: STRIPE_METER_EVENT_NAME,
89+
identifier,
8890
timestamp: stripeTimestamp,
8991
payload: {
9092
stripe_customer_id: stripeCustomerId,
9193
value: purchasedCredits.toString(),
92-
...(eventId ? { event_id: eventId } : {}),
9394
...(extraPayload ?? {}),
9495
},
9596
},
96-
idempotencyKey ? { idempotencyKey } : undefined,
97+
{ idempotencyKey },
9798
),
9899
{
99100
maxRetries: 3,

0 commit comments

Comments
 (0)