Razorpay Webhook签名验证错误

时间:2018-12-10 13:43:34

标签: python flask razorpay

尝试验证从Razorpay收到的Webhook请求时,我得到TypeError: encoding without a string argument

我已经使用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

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

3 个答案:

答案 0 :(得分:0)

将json请求数据字符串化后,它就可以在django中运行了,尝试一下,它将100%工作

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

如果烧瓶出现问题,请使用

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

答案 1 :(得分:0)

将webhook请求主体转换为字符串并删除所有尾随空格并固定缩进级别会有所帮助。

import razorpay
client = razorpay.Client(auth=("<YOUR_KEY_ID>", "<YOUR_KEY_SECRET>"))
client.utility.verify_webhook_signature(json.dumps(body, separators=(',', ':')), signature, secret)

答案 2 :(得分:0)

使用 Java 的 Razorpay webhook 签名验证(发布它可能有助于其他人在类似集成方面苦苦挣扎)

PersistentVolumeClaims