djangowebhooksrazorpay

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


Solution

  • 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.