Monday, November 11, 2019

Not able to get data on webhook receiver.

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



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEhPkLFs6CxAyzniNuyRvqw%3DLXLCN%3Dj7AHnRMS-njZsFYxSfRA%40mail.gmail.com.

No comments:

Post a Comment