Created
July 16, 2025 21:32
-
-
Save kjlape/63393a8a49e5d0a717c17401eaf5174c to your computer and use it in GitHub Desktop.
Automatically setup Stripe webhooks with signing secrets in dev
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # … | |
| Dotenv::Rails.files.unshift(".env.stripe-webhooks.local") if Rails.env.development? # Dotenv technically optional… You'd just need to parse this yourself without it. | |
| # … |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| # Help flag check | |
| if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then | |
| echo "Usage: $0 [host] [--skip-memory] [--skip-restart]" | |
| echo | |
| echo "Options:" | |
| echo " host The development host (default: localhost:3000)" | |
| echo " --skip-memory Don't prompt to remember the host" | |
| echo " --skip-restart Skip restarting Rails server" | |
| echo " --help, -h Show this help message" | |
| exit 0 | |
| fi | |
| WEBHOOK_PATH="/pay/webhooks/stripe" | |
| MEMORY_FILE="tmp/.stripe-webhook-host" | |
| ENV_FILE=".env.stripe-webhooks.local" | |
| SKIP_MEMORY=false | |
| SKIP_RESTART=false | |
| # Check for flags | |
| if [[ "${@:-}" =~ "--skip-memory" ]]; then | |
| SKIP_MEMORY=true | |
| # Remove --skip-memory from arguments | |
| set -- "${@/--skip-memory/}" | |
| fi | |
| if [[ "${@:-}" =~ "--skip-restart" ]]; then | |
| SKIP_RESTART=true | |
| # Remove --skip-restart from arguments | |
| set -- "${@/--skip-restart/}" | |
| fi | |
| # Try to load remembered host | |
| if [[ -f "$MEMORY_FILE" && -z "${1:-}" ]]; then | |
| REMEMBERED_HOST=$(cat "$MEMORY_FILE") | |
| DEV_HOST="${REMEMBERED_HOST}" | |
| else | |
| DEV_HOST="${1:-localhost:3000}" | |
| # Ask to remember the host if it was provided and we're not skipping memory | |
| if [[ -n "${1:-}" && "$SKIP_MEMORY" == false ]]; then | |
| read -p "Remember this host for next time? (y/N) " -n 1 -r | |
| echo | |
| if [[ $REPLY =~ ^[Yy]$ ]]; then | |
| mkdir -p tmp | |
| echo "$DEV_HOST" > "$MEMORY_FILE" | |
| echo "Saved host preference to $MEMORY_FILE" | |
| fi | |
| fi | |
| fi | |
| WEBHOOK_TARGET="http://${DEV_HOST}${WEBHOOK_PATH}" | |
| # Check if stripe CLI is installed | |
| if ! command -v stripe &> /dev/null; then | |
| echo "stripe CLI is not installed. Please install it first:" | |
| echo "brew install stripe/stripe-cli/stripe" | |
| exit 1 | |
| fi | |
| # Check if Rails server is running | |
| if ! curl -s "http://${DEV_HOST}" > /dev/null; then | |
| echo "Rails server not running at ${DEV_HOST}" | |
| echo "Please start the Rails server first with: bin/rails server" | |
| exit 1 | |
| fi | |
| # Get the webhook signing secret from stripe CLI | |
| echo "Starting stripe listener to get webhook signing secret..." | |
| SIGNING_SECRET=$(stripe listen --print-secret) | |
| if [ -z "$SIGNING_SECRET" ]; then | |
| echo "Failed to get webhook signing secret" | |
| exit 1 | |
| fi | |
| # Update the env file | |
| echo "STRIPE_SIGNING_SECRET=$SIGNING_SECRET" > "$ENV_FILE" | |
| echo "Updated ${ENV_FILE} with new signing secret" | |
| # Restart Rails to pick up the new environment variables | |
| if [[ "$SKIP_RESTART" == false ]]; then | |
| echo "Restarting Rails server..." | |
| bin/rails restart | |
| fi | |
| # Start the stripe listener | |
| echo "Starting stripe webhook listener..." | |
| echo "Forwarding webhooks to: $WEBHOOK_TARGET" | |
| stripe listen --forward-to "$WEBHOOK_TARGET" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment