Skip to content

Instantly share code, notes, and snippets.

@ginjo
Last active July 19, 2023 23:26
Show Gist options
  • Save ginjo/7573ec0dffd2a265fcb4bc20688d48bd to your computer and use it in GitHub Desktop.
Save ginjo/7573ec0dffd2a265fcb4bc20688d48bd to your computer and use it in GitHub Desktop.
Wrapper script for stripe-cli trigger function, opens sender and receiver in split-pane screen sessions.
#!/bin/bash
# Launches two stripe-cli docker containers, each in its own 'screen' session,
# to test stripe webhook events against a local http server.
#
# * Launches one container for the listener/forwarder.
# * Launches one container to trigger the test webhook event.
#
# NOTE: The stripe-cli 'trigger' function will create a lot of data in your
# stripe account's test environment. The only way to remove that data is manual one-by-one
# or by using the 'delete all data' function in your stripe dashboard's test mode.
#
# To start, first get your stripe-api-key into the env var STRIPE_API_KEY.
# . file-with-api-key-assignment
#
# Then make sure the variable assignments below suit your setup.
#
# Then run this script.
# ./<this-script> [local-endpoint-host-with-path] [additional docker options ...]
#
# NOTE: The local-endpoint-host is from the POV of the stripe-cli function running within
# a docker container.
#
# Kill with:
# exit
# C-a
# tab
# C-c
# exit
#
# Example:
# ./this_script.sh stack_app/stripe/webhook --network stack_default
#
# See https://unix.stackexchange.com/questions/89588/opening-multiple-split-windows-with-gnu-screen-with-single-bash-script
# REQUIRED: Change the name of the api-key env var to suit your setup.
# or
# Set this default env var in your app.
STRIPE_API_KEY="${MY_STRIPE_SECRET_KEY}"
#
# REQUIRED: Change this url to point to your endpoint
# webhook route that we are testing against.
LOCAL_ENDPOINT="${1:-stack_web/stripe/webhook}"
shift
#
# OPTIONAL: If your endpoint is reachable via attachable docker network,
# set this accordingly. Otherwise comment-out this line.
DOCKER_OPTIONS="${@:---network stack_web_default}"
#
#
# Creates a temp file to hold screenrc config.
# See https://savannah.gnu.org/bugs/?57629
tmpfile=`mktemp`
#
#
# Builds the screenrc config
cat - >"$tmpfile" <<-EEOOFF
setenv STRIPE_API_KEY "$STRIPE_API_KEY"
setenv LOCAL_ENDPOINT "$LOCAL_ENDPOINT"
screen bash -c "docker run --rm -it $DOCKER_OPTIONS stripe/stripe-cli listen --api-key '\$STRIPE_API_KEY' --forward-to '$LOCAL_ENDPOINT'; exec bash"
split
focus down
screen bash -c "docker run --rm -it $DOCKER_OPTIONS stripe/stripe-cli trigger --api-key '\$STRIPE_API_KEY' checkout.session.completed; exec bash"
focus bottom
EEOOFF
# Runs screen with the generated screenrc file.
screen -c "$tmpfile"
# Shows config (but only after exiting the screen sessions).
echo "Screenrc config from temp file:"
cat "$tmpfile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment