简体   繁体   English

使用 django-stripe 的内置 webhook 支持

[英]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_URLDJSTRIPE_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.

相关问题 django-stripe错误:无论输入什么年份,​​测试时“您的卡的失效年份无效” - django-stripe error: “Your card's expiration year is invalid” when testing, no matter year entered Django-stripe 集成在付款后给出 403 Forbidden - Django-stripe Integration give 403 Forbidden after payment 使用Django的内置ssi模板标记,导致500错误 - Using Django's built-in ssi template tag, causing 500 error 如何使用两个Django的内置框架为评论添加确认消息? - How to add a confirmation message for comments using both Django's built-in framework? Django 是否内置了用于管理产品用户的身份验证设计? - Is Django built-in authentication design for managing product's users? 向Django的内置评论应用添加可选字段 - Add optional fields to django's built-in comments app Django模板内置过滤器:在参数中使用变量值 - Django templates built-in filters: Using a variable value in an argument 在不使用Python内置的Sort的情况下对元组进行排序 - Sort Tuples without using Python's built-in Sort 使用“自定义用户模型”(Django)条纹“webhook”错误 - Stripe "webhook" error with "custom user model" (Django) Django 2.2.5,具有 Django 内置身份验证系统的多种用户类型 - Django 2.2.5, Multiple User Types with Django's built-in authentication system
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM