@@ -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