Django 在连接 stripe webhook 时抛出错误 404

Django throwing error 404 while connecting with stripe webhook

大家好,我在我的 Django 应用程序中使用 Stripe webhook 时总是收到 404 错误。非常感谢您的帮助

我的urls.py

urlpatterns = [path('my_webhook/', views.my_webhook, name='my_webhook')]

views.py

@csrf_exempt
def my_webhook(request):
    payload = request.body
    event = None

    try:
        event = stripe.Event.construct_from(
            json.loads(payload), stripe.api_key
        )
    
    except:
        return HttpResponse(status=400)
    
    if event.type == 'payment_intent.succeeded':
        payment_intent = event.data.object
    elif event.type == 'payment_method.attached':
        payment_method = event.data.object
    else:
        print('Unhandled event type {}'.format(event.type))
    
    return HttpResponse(status=200)

错误:

[20/Jul/2021 14:28:36] "POST /my_webhook HTTP/1.1" 404 3816
Not Found: /my_webhook

本地主机详细信息:

stripe listen --forward-to localhost:8000/stripe/my_webhook/

感谢大家的评论。

我找到了解决办法。在调用 stripe listen 时,我还需要提及我的应用程序名称。

之前我在 stripe CLI 中使用

stripe listen --forward-to localhost:8000/my_webhook/

然后我在其中添加了我的应用程序名称,它开始工作了

stripe listen --forward-to localhost:8000/pred/my_webhook/

其中“pred”是我的应用程序的名称