tags: stripe, webhooks, django, dj-stripe
I realized that Stripe's webhooks don't fire in order, and therefore I was at risk of overwriting valid data with outdated data if I processed them in the order that I received them. I asked Frank for advice and he suggested dj-stripe.
We do have a requirement to use plata if possible, so part of this TIL is seeing if dj-stripe plays well with plata, but a quick glance didn't reveal any obvious red flags.
- Add
dj-stripe
torequirements.in
and usepip-compile
to regenerate my requirements file. Rebuild container. - Add
djstripe
toINSTALLED_APPS
- Add
path("stripe/", include("djstripe.urls", namespace="djstripe")),
tourls.py
- Add these to
settings.py
:
STRIPE_LIVE_MODE = False # Change to True in production
DJSTRIPE_WEBHOOK_SECRET = "whsec_xxx" # Get it from the section in the Stripe dashboard where you added the webhook endpoint
DJSTRIPE_FOREIGN_KEY_TO_FIELD = "id"
- Start my server.
- Run
../manage.py migrate
- Go into the Django Admin and add my test Stripe private API key:

Add my API key and save.

- In local dev, in my Docker
web
container, run./manage.py djstripe_sync_models
. See my models populate!