-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstripe-webhook-forward.sh
More file actions
executable file
·82 lines (69 loc) · 2.64 KB
/
Copy pathstripe-webhook-forward.sh
File metadata and controls
executable file
·82 lines (69 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
# Stripe webhook forwarding for local development
# This forwards Stripe webhooks to your local Docker container
echo "Starting Stripe webhook forwarding..."
echo ""
echo "This will:"
echo "1. Forward webhooks from Stripe to http://localhost:8000/api/stripe/webhook/"
echo "2. Automatically update STRIPE_WEBHOOK_SECRET in .env"
echo "3. Restart the web container to apply the new secret"
echo ""
# Login if not already logged in
stripe login 2>/dev/null || echo "Already logged in to Stripe"
echo ""
echo "Getting webhook signing secret..."
# Start stripe listen in background, capture output, extract secret, then bring to foreground
stripe listen --forward-to http://localhost:8000/api/stripe/webhook/ --print-secret > /tmp/stripe_secret.txt 2>&1 &
STRIPE_PID=$!
# Wait for secret to appear (max 10 seconds)
for i in {1..10}; do
if grep -q "whsec_" /tmp/stripe_secret.txt 2>/dev/null; then
break
fi
sleep 1
done
# Extract the webhook secret
WEBHOOK_SECRET=$(grep -o "whsec_[a-zA-Z0-9]*" /tmp/stripe_secret.txt | head -1)
if [ -n "$WEBHOOK_SECRET" ]; then
echo "✓ Webhook secret obtained: ${WEBHOOK_SECRET:0:20}..."
# Update .env file
if [ -f .env ]; then
if grep -q "STRIPE_WEBHOOK_SECRET=" .env; then
# Replace existing secret
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
sed -i '' "s/STRIPE_WEBHOOK_SECRET=.*/STRIPE_WEBHOOK_SECRET=$WEBHOOK_SECRET/" .env
else
# Linux
sed -i "s/STRIPE_WEBHOOK_SECRET=.*/STRIPE_WEBHOOK_SECRET=$WEBHOOK_SECRET/" .env
fi
echo "✓ Updated STRIPE_WEBHOOK_SECRET in .env"
else
# Add new secret
echo "STRIPE_WEBHOOK_SECRET=$WEBHOOK_SECRET" >> .env
echo "✓ Added STRIPE_WEBHOOK_SECRET to .env"
fi
# Restart web container
echo ""
echo "Restarting web container..."
docker compose restart web
echo "✓ Web container restarted"
else
echo "⚠ Warning: .env file not found, please create it and add:"
echo "STRIPE_WEBHOOK_SECRET=$WEBHOOK_SECRET"
fi
else
echo "⚠ Warning: Could not extract webhook secret automatically"
echo "Please copy it from the output below and add to .env manually"
fi
echo ""
echo "✓ Webhook forwarding active"
echo "Press Ctrl+C to stop"
echo ""
# Kill the background process and start a new one in foreground
kill $STRIPE_PID 2>/dev/null
wait $STRIPE_PID 2>/dev/null
# Clean up temp file
rm -f /tmp/stripe_secret.txt
# Now run stripe listen in foreground
exec stripe listen --forward-to http://localhost:8000/api/stripe/webhook/