我正在尝试建立一个带有条纹的 webhook。(第一次)
但是,似乎没有找到 webhook 的路径。
希望有人能用全新的视角告诉我我做错了什么。
在我的 urls.py 中(项目级别)
urlpatterns = [
path('my_app/', include('my_app.urls')),
...
]
在 urls.py 应用程序级别:
urlpatterns = [
..
path('stripe_webhook/', views.stripe_webhook, name='stripe_webhook')
]
在我的views.py(my_app级别)中:
@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
...
在条纹中,在本地听众中我已经注册:
localhost:8000/stripe_webhook/
如果我运行stripe trigger customer.created
. 我会得到以下返回结果:
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.
但是,同时运行时stripe listen --forward-to localhost:8000/stripe_webhook/
,我还收到以下日志:
> 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]
并且我的服务器日志也会返回(无论是在生产中还是本地)Not Found: /stripe_webhook/
:。
这让我认为我的 webhook 路径配置不正确,但我看不到任何可能遗漏的内容。我是否忘记了 Stripe 平台上的某些内容?
注意:我的 .env 文件中的所有键(STRIPE_PUBLIC_KEY_TEST
、STRIPE_SECRET_KEY_TEST
和STRIPE_WEBHOOK_SECRET_TEST
)都与 Stripe 平台上提供的键匹配。
有什么想法吗?
在我看来,您在应用 URL 前面添加了
my_app/
,因此我希望它出现在最终 URL 中。我有一个长期(3 年以上)的 Django 与 Stripe 集成,当我检查我的根urls.py
文件时,我发现我的主要应用 URL 指定如下这避免了需要包含应用程序前缀,因此
localhost:8000/webhooks/
对我来说很有用。既然您指定了
my_app/
,您应该尝试一下localhost:8000/my_app/stripe_webhook
。您可以使用CLI触发 webhook 事件,以便检查它是否有效。我喜欢使用的另一种方法是在我的 webhook 函数中添加 API 方法检查,并使用类似的内容响应 GET 请求
<h1>Hello Webhook!</h1>
。如下所示:这样,您可以通过将 URL 放入 Web 浏览器中轻松测试路由是否正常运行。