【问题标题】:Stripe's webhook with django says: stripe.error.SignatureVerificationErrorStripe 与 django 的 webhook 说:stripe.error.SignatureVerificationError
【发布时间】:2020-09-08 05:47:21
【问题描述】:

我使用与stripe tutorial中相同的代码:

def webhook(request):
    payload = request.body
    sig_header = request.META['HTTP_STRIPE_SIGNATURE']
    event = None

    try:
        event = stripe.Webhook.construct_event(
            payload, sig_header, endpoint_secret
        )
    except ValueError as e:
        raise(e)
        return HttpResponse(status=400)
    except stripe.error.SignatureVerificationError as e:
        raise(e)
        return HttpResponse(status=400)

    # ...

但是当我尝试使用条带 CLI (stripe trigger payment_intent.created) 测试 webhook 时,我遇到了这个错误:

Internal Server Error: /payment/webhook/
Traceback (most recent call last):
  File "/home/rouizi/django-ecommerce/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/rouizi/django-ecommerce/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/rouizi/django-ecommerce/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/rouizi/django-ecommerce/venv/lib/python3.6/site-packages/django/views/decorators/http.py", line 40, in inner
    return func(request, *args, **kwargs)
  File "/home/rouizi/django-ecommerce/venv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/rouizi/django-ecommerce/payment/views.py", line 99, in webhook
    raise(e)
  File "/home/rouizi/django-ecommerce/payment/views.py", line 88, in webhook
    payload, sig_header, endpoint_secret
  File "/home/rouizi/django-ecommerce/venv/lib/python3.6/site-packages/stripe/webhook.py", line 23, in construct_event
    WebhookSignature.verify_header(payload, sig_header, secret, tolerance)
  File "/home/rouizi/django-ecommerce/venv/lib/python3.6/site-packages/stripe/webhook.py", line 78, in verify_header
    payload,
stripe.error.SignatureVerificationError: No signatures found matching the expected signature for payload

我尝试像这样解码有效载荷:

payload = request.body.decode('utf-8')

但我仍然有同样的错误。

错误可能来自哪里?

【问题讨论】:

    标签: django stripe-payments webhooks


    【解决方案1】:

    对我来说,通过解码request.data 解决了这个问题。

    payload = request.body.decode('utf-8')
    

    我刚刚决定将它作为一个单独的答案记录下来,也许有人在问题描述中遗漏了这一行。

    【讨论】:

      【解决方案2】:

      您将 raw 请求正文传递到 construct_event 并且之前在您的堆栈中没有任何东西对其进行解析或修改,这一点很重要。

      与您引用的示例的一个重要区别是如何访问标题和正文。您也没有显示您的endpoint_secret 来自哪里。您是否检查过所有输入是否符合您的预期?

      如果有,请尝试使用 steps documented 手动重新创建签名。

      【讨论】:

      • 我将原始请求正文传递给construct_event,如果我这样做了print(type(payload)),我的回应是:<class 'bytes'>,我的endpoint_secret 来自设置:endpoint_secret = settings.ENDPOINT_SECRET,以及重新创建签名我不知道如何手动验证签名。我还是卡住了
      • 如果您确定 request.body 是原始请求正文,请仔细检查您的密码是否正在加载(即,记录要确认的值)并且它是端点的正确密码.请特别注意,即使您为实时模式和测试模式设置了相同的 URL,它们也会被视为具有不同签名秘密的不同端点。我的答案的最后一个链接中描述了复制签名的步骤。
      • 我发现问题出在哪里。为了测试我的 webhook,我在使用条带 CLI 时使用了 Developer Dashboard 中 webhook 设置中的端点密码,而不是使用运行 stripe listen 时打印的端点密码。我花了几个小时试图找出问题出在哪里。我不知道它是否只发生在我身上,但我在文档中表示他们应该注意这一点,而不是发表评论。感谢您的帮助
      【解决方案3】:

      就我而言,在我重新创建端点密码后问题解决了。

      stripe.error.SignatureVerificationError
      

      异常,不再引发。事实证明,端点密码被设置为过期。因此,您可以仔细检查端点密码。

      【讨论】:

        【解决方案4】:

        在测试模式和实时模式下——所有三个键都不同:

        # web_app/settings.py
        ## STRIPE LIVE
        STRIPE_ENDPOINT_SECRET = 'whsec_******'
        STRIPE_PUBLISHABLE_KEY =  'pk_live_*******'
        STRIPE_SECRET_KEY = 'sk_live_******'
        

        因此,我建议您检查是否使用了正确的 ENDPOINT 密码,该密码对于 Live 模式也不同,即使 webhook url 与 Test 模式相同:

        【讨论】:

          猜你喜欢
          • 2013-03-06
          • 2021-02-04
          • 2015-04-18
          • 1970-01-01
          • 2017-10-03
          • 2021-01-14
          • 2018-09-18
          • 2016-05-04
          • 2021-11-28
          相关资源
          最近更新 更多