[英]Using django-stripe's built-in webhook support
I'm using Django 3.0, dj-stripe 2.0, and the Stripe CLI.我正在使用 Django 3.0、dj-stripe 2.0 和 Stripe CLI。 dj-stripe provides native support for Stripe webhooks, and their documentation says to include the following in my django project's main urls.py
file to expose the webhook endpoint: dj-stripe为 Stripe webhooks 提供本机支持,他们的文档说在我的 django 项目的主urls.py
文件中包含以下内容以公开 webhook 端点:
url(r"^stripe/", include("djstripe.urls", namespace="djstripe")),
I have done this, but how do I now leverage the endpoint?我已经这样做了,但我现在如何利用端点?
As a separate experiment, I created a payments
app, setup the URL conf, and successfully called the view when triggering the non dj-stripe webhook endpoint via the Stripe CLI.作为一个单独的实验,我创建了一个payments
应用程序,设置了 URL conf,并在通过 Stripe CLI 触发非 dj-stripe webhook 端点时成功调用了视图。 But it doesn't use any dj-stripe
functionality:但它不使用任何dj-stripe
功能:
# project urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('stripe/', include("djstripe.urls", namespace="djstripe")),
path('payments/', include("payments.urls", namespace="payments")),
]
# payments/urls.py
from django.urls import path
from . import views
app_name="payments"
urlpatterns = [
path("", views.my_handler, name="my-handler"),
]
# payments/views.py
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def my_handler(request, **kwargs):
print(request.body)
return HttpResponse(status=200)
Running stripe listen --forward-to localhost:8000/payments/
and, in a separate window, stripe trigger product.created
returns a 200
response.运行stripe listen --forward-to localhost:8000/payments/
并在单独的窗口中, stripe trigger product.created
返回200
响应。
But stripe listen --forward-to localhost:8000/stripe/webhook/
and stripe trigger product.created
returns a 500
.但是stripe listen --forward-to localhost:8000/stripe/webhook/
和stripe trigger product.created
返回500
。
Thank you in advance for your help.预先感谢您的帮助。
[UPDATE] [更新]
I have not modified the default DJSTRIPE_WEBHOOK_URL
or DJSTRIPE_WEBHOOK_VALIDATION
settings.我没有修改默认的DJSTRIPE_WEBHOOK_URL
或DJSTRIPE_WEBHOOK_VALIDATION
设置。 In settings.py
I have:在settings.py
我有:
STRIPE_LIVE_PUBLIC_KEY = os.environ.get("STRIPE_LIVE_PUBLIC_KEY")
STRIPE_LIVE_SECRET_KEY = os.environ.get("STRIPE_LIVE_SECRET_KEY")
STRIPE_TEST_PUBLIC_KEY = os.environ.get("STRIPE_TEST_PUBLIC_KEY")
STRIPE_TEST_SECRET_KEY = os.environ.get("STRIPE_TEST_SECRET_KEY")
STRIPE_LIVE_MODE = os.environ.get("STRIPE_LIVE_MODE")
DJSTRIPE_WEBHOOK_SECRET = "*************************************"
The test keys are pulled from env/bin/activate
:测试密钥从env/bin/activate
中提取:
export STRIPE_LIVE_PUBLIC_KEY="pk_live_****...."
export STRIPE_LIVE_SECRET_KEY="sk_live_****...."
export STRIPE_TEST_PUBLIC_KEY="pk_test_****...."
export STRIPE_TEST_SECRET_KEY="sk_test_****...."
export STRIPE_LIVE_MODE="False"
When I run stripe listen --forward-to localhost:8000/stripe/webhook/
and trigger stripe trigger customer.created
I get the following 500 error:当我运行stripe listen --forward-to localhost:8000/stripe/webhook/
并触发stripe trigger customer.created
我收到以下 500 错误:
stripe.error.InvalidRequestError: Request req_NTtzsTFS8uVdfL: No such customer; a similar object exists in test mode, but a live mode key was used to make this request.
But I don't understand how my keys could be getting mixed up, because the webhooks work fine when I trigger the same event and listen via the /payments/
endpoint.但我不明白我的密钥是如何混淆的,因为当我触发相同的事件并通过/payments/
端点侦听时,网络钩子工作正常。
Thank you again for your time.再次感谢您的时间。
In summary, you need to decorate a handler function using the @webhooks.handler
decorator.总之,您需要使用@webhooks.handler
装饰器来装饰处理程序函数。
You have some examples in the webhooks section of the dj-stripe documentation.您在 dj-stripe 文档的webhooks 部分有一些示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.