gpt4 book ai didi

python - Razorpay Webhook 签名验证错误

转载 作者:太空宇宙 更新时间:2023-11-03 21:28:11 27 4
gpt4 key购买 nike

在尝试验证从 Razorpay 收到的 Webhook 请求时,我收到 TypeError:不带字符串参数的编码

我使用 Flask 在 Python3.6 中编写了代码。

我知道它与我作为参数传递的request.data有关,但无法解决问题。

下面是调用 webhook 时执行的代码:

@app.route('/razorpay', methods=['POST'])
def razorpay_webhook():
webhook_secret: str = MY_WEBHOOK_SECRET
signature = request.headers['X-Razorpay-Signature']
client = razorpay.Client(auth=(MY_KEY, MY_SIGNATURE))
verify = client.utility.verify_webhook_signature(request.data, signature, webhook_secret)

我收到的错误如下:

2018-12-10T13:32:16.177198+00:00 app[web.1]: [2018-12-10 13:32:16,174] ERROR in app: Exception on /razorpay [POST]
2018-12-10T13:32:16.177227+00:00 app[web.1]: Traceback (most recent call last):
2018-12-10T13:32:16.177229+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
2018-12-10T13:32:16.177231+00:00 app[web.1]: response = self.full_dispatch_request()
2018-12-10T13:32:16.177233+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
2018-12-10T13:32:16.177235+00:00 app[web.1]: rv = self.handle_user_exception(e)
2018-12-10T13:32:16.177237+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
2018-12-10T13:32:16.177238+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2018-12-10T13:32:16.177240+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
2018-12-10T13:32:16.177243+00:00 app[web.1]: raise value
2018-12-10T13:32:16.177244+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
2018-12-10T13:32:16.177246+00:00 app[web.1]: rv = self.dispatch_request()
2018-12-10T13:32:16.177247+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
2018-12-10T13:32:16.177250+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2018-12-10T13:32:16.177251+00:00 app[web.1]: File "/app/app.py", line 81, in razorpay_webook
2018-12-10T13:32:16.177253+00:00 app[web.1]: verify = client.utility.verify_webhook_signature(request.data, signature, webhook_secret)
2018-12-10T13:32:16.177255+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/razorpay/utility/utility.py", line 25, in verify_webhook_signature
2018-12-10T13:32:16.177256+00:00 app[web.1]: self.verify_signature(body, signature, secret)
2018-12-10T13:32:16.177258+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/razorpay/utility/utility.py", line 30, in verify_signature
2018-12-10T13:32:16.177260+00:00 app[web.1]: body = bytes(body, 'utf-8')
2018-12-10T13:32:16.177269+00:00 app[web.1]: TypeError: encoding without a string argument

编辑

将代码中的 request.data 行更改为 str(request.data) 解决了上述错误。但现在我收到如下新错误:

2018-12-11T04:24:16.973496+00:00 app[web.1]: Traceback (most recent call last):
2018-12-11T04:24:16.973500+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
2018-12-11T04:24:16.973502+00:00 app[web.1]: response = self.full_dispatch_request()
2018-12-11T04:24:16.973503+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
2018-12-11T04:24:16.973505+00:00 app[web.1]: rv = self.handle_user_exception(e)
2018-12-11T04:24:16.973507+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
2018-12-11T04:24:16.973508+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2018-12-11T04:24:16.973510+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
2018-12-11T04:24:16.973512+00:00 app[web.1]: raise value
2018-12-11T04:24:16.973514+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
2018-12-11T04:24:16.973515+00:00 app[web.1]: rv = self.dispatch_request()
2018-12-11T04:24:16.973517+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
2018-12-11T04:24:16.973519+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2018-12-11T04:24:16.973520+00:00 app[web.1]: File "/app/app.py", line 81, in razorpay_webook
2018-12-11T04:24:16.973522+00:00 app[web.1]: verify = client.utility.verify_webhook_signature(str(request.data), signature, webhook_secret)
2018-12-11T04:24:16.973524+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/razorpay/utility/utility.py", line 25, in verify_webhook_signature
2018-12-11T04:24:16.973526+00:00 app[web.1]: self.verify_signature(body, signature, secret)
2018-12-11T04:24:16.973527+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/razorpay/utility/utility.py", line 45, in verify_signature
2018-12-11T04:24:16.973529+00:00 app[web.1]: 'Razorpay Signature Verification Failed')
2018-12-11T04:24:16.973536+00:00 app[web.1]: razorpay.errors.SignatureVerificationError: Razorpay Signature Verification Failed

我已经仔细检查过,我传递的所有值都是正确的。那么可能是什么问题呢?

最佳答案

stringify json请求数据后,它在django中工作,试试这个,它会100%工作

payload_body = json.dumps(request.data, separators=(',', ':'))
verify = client.utility.verify_webhook_signature(payload_body, signature, webhook_secret)

如果 flask 出现问题,则使用

payload_body = json.dumps(request.json, separators=(',', ':'))

关于python - Razorpay Webhook 签名验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53706973/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com