Razorpay Webhook signature verification error

妖精的绣舞 提交于 2019-12-23 02:25:15

问题


I am getting TypeError: encoding without a string argument when trying to verify webhook request received from Razorpay.

I have written the code in Python3.6 using Flask.

I understand it has something to do with request.data I am passing as argument, but not able to solve the issue.

Below is the code for that is executed when the webhook is called:

@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)

The error I am getting is below:

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

EDIT

Changing the line in code with request.data to str(request.data) solved the above error. But now I am getting new error as below:

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

I've double checked, all the values I am passing are correct. So what could be the problem?


回答1:


After stringify json request data, it is working in django, try this , it will work 100%

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

if get problem in flask, then use

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



回答2:


Converting the webhook request body into a string and removing all trailing spaces and fixing the indent level helps.

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


来源:https://stackoverflow.com/questions/53706973/razorpay-webhook-signature-verification-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!