I am trying to establish a webhook with stripe. (first timer)
However, it seems the path to the webhook is not found.
Hoping someone might be able to bring a pair of fresh eyes and tell me what I am doing wrong.
In my urls.py (project level)
urlpatterns = [
path('my_app/', include('my_app.urls')),
...
]
In urls.py app level:
urlpatterns = [
..
path('stripe_webhook/', views.stripe_webhook, name='stripe_webhook')
]
In my views.py (my_app level):
@csrf_exempt
def stripe_webhook(request):
print("enter webhook")
stripe.api_key = settings.STRIPE_SECRET_KEY_TEST
payload = request.body
signature_header = request.META.get('HTTP_STRIPE_SIGNATURE')
webhook_secret = settings.STRIPE_WEBHOOK_SECRET_TEST
...
In stripe, in local listenners I have regiestered:
localhost:8000/stripe_webhook/
If I run stripe trigger customer.created
. I get the following returned to me:
A newer version of the Stripe CLI is available, please update to: v1.22.0
Setting up fixture for: customer
Running fixture for: customer
Trigger succeeded! Check dashboard for event details.
However, running simultaneously stripe listen --forward-to localhost:8000/stripe_webhook/
, I also get the following logs:
> Ready! You are using Stripe API Version [2024-09-30.acacia]. Your webhook signing secret is XXXXX (^C to quit)
2024-12-05 22:27:54 --> customer.created [xxx]
2024-12-05 22:27:54 <-- [404] POST http://localhost:8000/stripe_webhook/ [xx]
and my server logs will also return (whether in production or local): Not Found: /stripe_webhook/
.
This makes me think the path to my webhook is not properly configured, but I cannot see anything I could have missed. Is there soemthing on the Stripe platform that I forgot?
Note: all my keys (STRIPE_PUBLIC_KEY_TEST
, STRIPE_SECRET_KEY_TEST
and STRIPE_WEBHOOK_SECRET_TEST
) in my .env file are matching the keys provided on Stripe's platform.
Any thoughts?
It looks to me like you are prefacing your app URLs with my_app/
so I would expect that to be in the final URL. I have a long-standing (3+ years) Django integration with Stripe and, when I check my root urls.py
file I see that I have my main app URLs specified like so
path('', include('payments.urls')),
This avoids needing to include an app prefix so localhost:8000/webhooks/
works for me.
Since you specify my_app/
, you should try localhost:8000/my_app/stripe_webhook
. You can use the CLI to trigger webhook events so you can check if that works.
Another approach I like to use is to add an API method check in my webhook function and respond to a GET request with something like <h1>Hello Webhook!</h1>
. Something like this:
@csrf_exempt
def stripe_webhook(request):
if request.method == "GET":
# Respond with webpage indicating success
else:
# Do webhook-y stuff here
That way you can easily test if the route is working by putting the URL in your web browser.