Shopify webhook request verification for `orders/create` topic returning False in python/django project
I am trying to verify a Shopify webhook request for the topic orders/create, but the verification is returning False, I am not able to figure out the reason.
Here's the code snippet I am using to verify the request.
- views.py
class ShopifyWebhookOrderCreate(APIView):
def post(self, request):
verified = verify_webhook(request.body, request.META.get('HTTP_X_SHOPIFY_HMAC_SHA256'))
if not verified:
return Response(status=status.HTTP_401_UNAUTHORIZED)
...
...
...
and for verifying the request I am doing this:-
def verify_webhook(data, hmac_header):
CLIENT_SECRET = config('SHOPIFY_CLIENT_SECRET')
digest = hmac.new(CLIENT_SECRET.encode('utf-8'), data, digestmod=hashlib.sha256).digest()
computed_hmac = base64.b64encode(digest)
return hmac.compare_digest(computed_hmac, hmac_header.encode('utf-8'))
request.body format is b'{'the': 'data'}'
I verified the SECRET with the Shopify app.
Link to the doc:- https://shopify.dev/apps/webhooks/configuration/https#step-5-verify-the-webhook