mirror of
https://github.com/zoo-apps/computer.git
synced 2026-10-30 14:57:11 +00:00
Extracted from zoo monorepo for independent deployment. Domain: computer.zoo.ngo Deployment: GitHub Pages (GitHub Actions) App Directory: computer
6.2 KiB
6.2 KiB
Stripe Webhooks Setup Guide
Overview
This guide explains how to set up and configure Stripe webhooks for the Hanzo Computer platform. The webhooks handle payment events, subscriptions, invoices, and refunds.
Webhook Events Handled
The following Stripe webhook events are supported:
Payment Events
payment_intent.succeeded- Payment completed successfullypayment_intent.payment_failed- Payment attempt failedcheckout.session.completed- Checkout session completed
Subscription Events
customer.subscription.created- New subscription createdcustomer.subscription.updated- Subscription modifiedcustomer.subscription.deleted- Subscription cancelled
Invoice Events
invoice.paid- Invoice payment successfulinvoice.payment_failed- Invoice payment failed
Refund Events
charge.refunded- Payment refunded
Setup Instructions
1. Environment Variables
Add the following to your .env.local file:
# Stripe API Keys
STRIPE_SECRET_KEY=sk_test_your_stripe_secret_key
STRIPE_WEBHOOK_SECRET=whsec_your_webhook_signing_secret
2. Database Setup
Run the migration to create required tables:
# Apply the webhook tables migration
npx supabase db push
This creates the following tables:
webhook_events- Tracks processed events for idempotencyorders- Stores order informationsubscriptions- Manages subscription datainvoices- Invoice recordsrefunds- Refund trackingpayment_failures- Failed payment logssubscription_changes- Subscription modification historygpu_access- GPU subscription access control
3. Stripe Dashboard Configuration
- Go to Stripe Dashboard Webhooks
- Click "Add endpoint"
- Enter your endpoint URL:
- Development:
https://your-ngrok-url.ngrok.io/api/webhooks/stripe - Production:
https://hanzo.computer/api/webhooks/stripe
- Development:
- Select events to listen for:
payment_intent.succeededpayment_intent.payment_failedcheckout.session.completedcustomer.subscription.createdcustomer.subscription.updatedcustomer.subscription.deletedinvoice.paidinvoice.payment_failedcharge.refunded
- Copy the signing secret (starts with
whsec_) - Add it to your environment variables
4. Local Development with Stripe CLI
For local development, use the Stripe CLI to forward webhooks:
# Install Stripe CLI
brew install stripe/stripe-cli/stripe
# Login to Stripe
stripe login
# Forward webhooks to your local server
stripe listen --forward-to localhost:3000/api/webhooks/stripe
# The CLI will show your webhook signing secret
# Copy it and add to .env.local as STRIPE_WEBHOOK_SECRET
5. Testing Webhooks
Using Stripe CLI
Trigger test events:
# Test payment success
stripe trigger payment_intent.succeeded
# Test subscription creation
stripe trigger customer.subscription.created
# Test invoice payment
stripe trigger invoice.paid
# Test refund
stripe trigger charge.refunded
Manual Testing
Create test payments in your application and monitor:
- Webhook logs in Stripe Dashboard
- Database tables for created records
- Server logs for processing details
Webhook Processing Flow
- Receive Event: Stripe sends POST request to
/api/webhooks/stripe - Verify Signature: Validate webhook signature using signing secret
- Check Idempotency: Verify event hasn't been processed
- Process Event: Execute appropriate handler based on event type
- Update Database: Create/update relevant records
- Mark Processed: Record event as processed
- Return Response: Send 200 OK to Stripe
Error Handling
- 400 Bad Request: Invalid signature or malformed request
- 405 Method Not Allowed: Non-POST requests
- 500 Internal Server Error: Processing error (Stripe will retry)
Stripe Retry Logic
Stripe retries failed webhooks with exponential backoff:
- After 1 hour
- After 2 hours
- After 4 hours
- After 8 hours
- Then every 8 hours for up to 3 days
Security Best Practices
- Always verify webhook signatures - Never process unsigned webhooks
- Use HTTPS in production - Webhooks contain sensitive data
- Implement idempotency - Prevent duplicate processing
- Log all events - Maintain audit trail
- Use environment variables - Never hardcode secrets
- Restrict webhook endpoint - No authentication required, but verify signatures
Database Schema
webhook_events
- id: UUID
- event_id: Text (Stripe event ID)
- event_type: Text
- processed: Boolean
- data: JSONB
- created_at: Timestamp
orders
- id: UUID
- payment_intent_id: Text
- customer_email: Text
- amount: Decimal
- status: Text
- metadata: JSONB
- paid_at: Timestamp
subscriptions
- id: UUID
- subscription_id: Text
- customer_id: Text
- status: Text
- items: JSONB
- current_period_start/end: Timestamp
Monitoring
Key Metrics to Track
- Webhook success rate
- Processing time per event type
- Failed webhook count
- Duplicate event attempts
Logging
All webhook events are logged with:
- Event type and ID
- Processing result
- Error messages (if any)
- Processing duration
Troubleshooting
Common Issues
-
Signature Verification Fails
- Check webhook secret is correct
- Ensure raw request body is used
- Verify Content-Type header
-
Events Not Received
- Check webhook URL is accessible
- Verify event types are selected in Stripe
- Check firewall/security rules
-
Duplicate Processing
- Ensure idempotency check is working
- Check webhook_events table for duplicates
-
Database Errors
- Verify Supabase connection
- Check RLS policies
- Ensure migrations are applied
Admin Dashboard Integration
The webhook events can be viewed in the admin dashboard:
- Navigate to
/admin/webhooks - View event log with filtering options
- Retry failed webhooks
- Export webhook data