Credit top-ups from Stripe webhook#1483
Conversation
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📝 WalkthroughWalkthroughThe changes add support for Stripe checkout session completion events to the webhook handler. A new handler processes checkout.session.completed and checkout.session.async_payment_succeeded events, validates sessions, retrieves product and customer information, and invokes a Postgres RPC to credit usage. Additionally, test utilities add a retry-enabled fetch wrapper for API calls. Changes
Sequence DiagramsequenceDiagram
actor Stripe as Stripe Webhook
participant Handler as Webhook Handler
participant Utils as Utils/Stripe
participant Stripe_API as Stripe API
participant DB as PostgreSQL
Stripe->>Handler: checkout.session.completed/async_payment_succeeded
Handler->>Utils: extractDataEvent(event)
Utils-->>Handler: {customer_id, status: 'succeeded'}
Handler->>Handler: Validate session (mode, status)
Handler->>Handler: Match to Organization
Handler->>Stripe_API: getStripe().listLineItems(session_id)
Stripe_API-->>Handler: Line items[]
Handler->>Handler: getCreditTopUpProductIdFromCustomer(customer)
Handler->>Handler: Aggregate creditQuantity from items
Handler->>DB: RPC top_up_usage_credits(org_id, credits)
DB-->>Handler: Success/Error
Handler-->>Stripe: 200 OK
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
tests/expose-metadata.test.ts (1)
4-9: Fix named import order to satisfy ESLint.Static analysis reports a
perfectionist/sort-named-importserror for this import list.🔧 Suggested reorder
import { APP_NAME, - getBaseData, - getEndpointUrl, fetchWithRetry, + getBaseData, + getEndpointUrl, getSupabaseClient, headers, ORG_ID,
🤖 Fix all issues with AI agents
In `@supabase/functions/_backend/triggers/stripe_event.ts`:
- Around line 33-68: The getCreditTopUpProductIdFromCustomer function currently
uses supabaseAdmin to query stripe_info and plans; replace those calls with the
recommended DB client (either getPgClient() or getDrizzleClient()) and include
the proper schema when querying, i.e. perform the same two lookups (stripe_info
-> product_id by customer_id, then plans -> credit_id by stripe_id) using the
chosen client and schema-aware methods, preserve the same error logging via
cloudlog and same thrown simpleError messages, and return plan.credit_id; update
any variable names (stripeInfo, plan) as needed to match the new client APIs.
|



Summary (AI generated)
Motivation (AI generated)
Top-up credits were only granted on client redirect, so missed redirects left paid sessions without grants.
Business Impact (AI generated)
Reduces paid-but-uncredited incidents and support overhead by crediting from Stripe webhooks.
Test Plan (AI generated)
Generated with AI
Summary by CodeRabbit
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.