Overview
Thestripe-webhook edge function processes Stripe webhook events, specifically handling checkout.session.completed events to finalize token purchases. When a payment succeeds, this function updates the transaction status and credits tokens to the organization.
Endpoint
Authentication
Webhook signature verification viastripe-signature header. No Bearer token required.
Webhook Events Handled
checkout.session.completed
Triggered when a Stripe checkout session is successfully completed. This is the primary event that completes token purchases.Request
Stripe sends the webhook request with:- Raw request body (used for signature verification)
stripe-signatureheader for webhook verification- Event data in Stripe’s webhook format
Response
Success Response (200 OK)
Always
true to acknowledge receipt of the webhookError Responses
400 Bad Request
Returned when signature is missing or metadata is incomplete.500 Internal Server Error
Returned when webhook processing fails or configuration is missing.Implementation Details
Webhook Signature Verification
From source/supabase/functions/stripe-webhook/index.ts:26-45:Checkout Session Processing
From source/supabase/functions/stripe-webhook/index.ts:47-80:Audit Logging
From source/supabase/functions/stripe-webhook/index.ts:82-95:Checkout Session Metadata
The checkout session created by create-checkout-session includes metadata:| Field | Type | Description |
|---|---|---|
organization_id | UUID | Organization receiving the tokens |
token_type | string | Either "event" or "attendee" |
quantity | string | Number of tokens purchased |
price_per_unit | string | Price per token in decimal format |
total_amount | string | Total purchase amount in decimal format |
user_id | UUID | User who initiated the purchase |
Token Crediting Logic
When a payment succeeds:- Transaction Update: Sets transaction status to
"completed" - Token Retrieval: Fetches current token balance for the organization
- Token Addition: Adds purchased quantity to existing balance
- Database Update: Updates organization’s token column (
event_tokensorattendee_tokens) - Audit Log: Records the purchase action with full details
Stripe Configuration
Required Environment Variables
STRIPE_SECRET_KEY: Your Stripe secret keySTRIPE_WEBHOOK_SECRET: Webhook signing secret from Stripe dashboard
Stripe Dashboard Setup
- Go to Stripe Dashboard > Developers > Webhooks
- Click “Add endpoint”
- Set endpoint URL to:
https://<project-ref>.supabase.co/functions/v1/stripe-webhook - Select event:
checkout.session.completed - Copy the “Signing secret” and set it as
STRIPE_WEBHOOK_SECRET
Security Features
- Signature Verification: Uses Stripe’s
constructEventto verify webhook authenticity - Metadata Validation: Validates required metadata fields before processing
- Idempotency: Uses
stripe_session_idto prevent duplicate processing - Service Role: Uses service role key for database operations
- Audit Trail: Complete audit log of all token purchases
Testing
Use Stripe CLI to test webhooks locally:Error Handling
The function logs errors to console but returns 200 OK to prevent Stripe from retrying:Flow Diagram
- User completes Stripe checkout session
- Stripe sends
checkout.session.completedwebhook - Function verifies webhook signature
- Function extracts metadata from session
- Function updates transaction status to “completed”
- Function credits tokens to organization
- Function logs audit event
- Function returns 200 OK to Stripe