sreekanthkura7
sreekanthkura7

Reputation: 115

TypeError: encoding without a string argument for razorpay webhook secret verification error

I am trying verify if the webhook came from Razorpay but getting following error.

TypeError: encoding without a string argument

Here is the code:

webhook_secret = MY_WEBHOOK_SECRET
signature = request.headers['X-Razorpay-Signature']
jsondata = json.loads(request.body)
client = razorpay.Client(auth=(MY_KEY, MY_SIGNATURE))
verify = client.utility.verify_webhook_signature(jsondata, signature, webhook_secret)

I'm getting error in the last line. Can someone help me with this? Thanks!

Traceback (most recent call last): File "C:\Users\Sreekanth\Anaconda3\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "C:\Users\Sreekanth\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Users\Sreekanth\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\Sreekanth\Anaconda3\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "C:\Users\Sreekanth\Documents\BoosterKick_software\BoosterKick\pages\views.py", line 168, in razorpay_webhook verify = client.utility.verify_webhook_signature(jsondata, signature, webhook_secret) File "C:\Users\Sreekanth\Anaconda3\lib\site-packages\razorpay\utility\utility.py", line 25, in verify_webhook_signature self.verify_signature(body, signature, secret) File "C:\Users\Sreekanth\Anaconda3\lib\site-packages\razorpay\utility\utility.py", line 30, in verify_signature body = bytes(body, 'utf-8') TypeError: encoding without a string argument

Upvotes: 0

Views: 723

Answers (2)

not-a-bot
not-a-bot

Reputation: 80

For Django it's not really required to convert the request body to json and back to string. This process kept giving me signature verification error. Just decoding the body will do the trick. So the code is as follows:

webhook_secret = MY_WEBHOOK_SECRET
signature = request.headers['X-Razorpay-Signature']
body = request.body.decode()
client = razorpay.Client(auth=(MY_KEY, MY_SIGNATURE))
client.utility.verify_webhook_signature(body, signature, webhook_secret)

This worked for me. Please check this answer and this issue for reference.

Upvotes: 0

sreekanthkura7
sreekanthkura7

Reputation: 115

webhook_secret = MY_WEBHOOK_SECRET
signature = request.headers['X-Razorpay-Signature']
jsondata = json.loads(request.body)
client = razorpay.Client(auth=(MY_KEY, MY_SIGNATURE))
client.utility.verify_webhook_signature(json.dumps(jsondata, separators=(',', ':')), signature, webhook_secret)

This is working for me.

Upvotes: 2

Related Questions