Fileoverview

Stripe Webhook Edge Function

Handles incoming Stripe webhook events to keep subscription state in sync. Processes checkout completions, subscription updates, and cancellations.

Endpoint

POST /stripe-webhook

Auth

Stripe signature verification (not JWT)

Remarks

This function is called by Stripe when subscription events occur. It verifies the webhook signature and updates the database accordingly.

Handled events:

  • checkout.session.completed: Creates/updates subscription after successful checkout
  • customer.subscription.updated: Syncs plan changes and cancellation state
  • customer.subscription.deleted: Resets user to Free plan

Tables touched:

  • sgtm_container_subscriptions (upsert/update)
  • clients (update external_customer_id)
  • billing_plans (read for plan lookup)

Environment variables required:

  • STRIPE_SECRET_KEY: Stripe API secret key
  • STRIPE_WEBHOOK_SECRET: Webhook endpoint signing secret
  • SUPABASE_URL: Supabase project URL
  • SUPABASE_SERVICE_ROLE_KEY: Service role key for admin operations

Returns

200 - { received: true, eventType: string }

Returns

400 - Missing signature or invalid signature

Returns

500 - Missing environment variables or processing error