Not able to get data on webhook receiver.

15 views
Skip to first unread message

Saswat Ray

unread,
Nov 11, 2019, 4:43:04 AM11/11/19
to django...@googlegroups.com
Hi,
I am trying to create webhook for shopify.I have successfully registered the webhook(see the below code).But i am not able to get data in webhook_receiver.

View.py

def webhook_register(request, organization_pk):

    shop = 'gachibowli.myshopify.com'
    headers = {
        "X-Shopify-Access-Token": SOCIAL_AUTH_ACCESS_TOKEN,
        "Accept": "application/json",
        "Content-Type": "application/json",

    }

    payload = {
        "webhook": {
            "topic": "products/create",
            "address": "https://89a15e36.ngrok.io/shopify/webhook_receiver/",
            "format": "json"
        }
    }
    response = requests.post("https://" + shop
                             + "/admin/api/2019-10/webhooks.json",
                             data=json.dumps(payload), headers=headers)

    print (response.content)
    return HttpResponse('success')


def verify_webhook(data, hmac_header):

    digest = hmac.new(SOCIAL_AUTH_SHOPIFY_SECRET, data.encode('utf-8'), hashlib.sha256).digest()
    computed_hmac = base64.b64encode(digest)

    return hmac.compare_digest(computed_hmac, hmac_header.encode('utf-8'))


@require_http_methods(["GET", "POST"])
@csrf_exempt
def webhook_receiver(request, organization_pk):
    print("#####")
    data = request.body
    print(data)
    verified = verify_webhook(data, request.headers.get('X-Shopify-Hmac-SHA256'))

    # build a request object
    req = json.loads(data)
    print(req)
    # get action from json
    action = req.get('queryResult').get('action')
    return HttpResponse('sucessfully received')

 url.py :
    path(
        route="org/<int:organization_pk>/integration/shopify/webhook_register/",
        view=views.webhook_register,
        name="webhook_register",
    ),
    path(
        route="shopify/webhook_receiver/",
        view=views.webhook_receiver,
        name="shopify_webhook_receiver",


Thanks,
Saswat



Reply all
Reply to author
Forward
0 new messages