Orders Create Webhook not firing (or is it?)

Graeme_Knight
Tourist
9 0 2

Hi folks,

 

I'm new to the technology stack recommended by Shopify. However following several examples I registered a webhook with read_orders and write_orders scope available after authentication:

 

// register webhook
const registration = await registerWebhook({
address: `${HOST}/webhooks/orders/create`,
topic: "ORDERS_CREATE",
accessToken,
shop,
apiVersion: '2020-01'
});

The registration comes back with success.

I have then done the following to make sure I catch the firing webhook:

const webhook = receiveWebhook({ secret: SHOPIFY_API_SECRET_KEY });

router.post('/webhooks/orders/create', webhook, (ctx) => {
console.log('received webhook: ', ctx.state.webhook);
});

 Koa router is then set up thusly:

router.get('*', verifyRequest(), async (ctx) => {
ctx.respond = false;
ctx.res.statusCode = 200;
});
server.use(router.allowedMethods());
server.use(router.routes());

 I'm using paid ngrok so the host URL is stable. I have the app reinstalled in my test store. And I created a test webhook call in notifications. I also set up the koa router log to show incoming traffic.

 

When I test the webhook through notifications I do not see any traffic coming into the app and no webhook is caught.

 

Seems to me that either the webhook is not firing or the above code is wrong or I have missed a step. Aside from the fact I'm not using next.js I believe I'm doing everything the example app suggests (I may have missed something).

 

I hoped someone may have had the same problem or had some hints to help. Next I'll fire some data via postman to see if it's received.

 

Best regards, G.

0 Likes
Visely-Team
Shopify Partner
1837 210 420

How do you test the webhook? Are you creating an order through Shopify Admin?

Sergiu Svinarciuc | CTO @ visely.io
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!
- To learn more about the awesome stuff we do head over to visely.io or our blog
0 Likes
Graeme_Knight
Tourist
9 0 2

Hi Sergiu,

 

Thank you for your interest.

 

I created an order through the store itself as a customer with the bogus payment gateway.

 

Also through admin->notification->test webhook.

 

Best regards, G.

0 Likes
tolgapaksoy
Shopify Partner
67 5 32
This here is wrong:

address: `${HOST}/webhooks/orders/create`,

It should just be:

address: `/webhooks/orders/create`,

The host is prepended automatically
0 Likes
GMKnight
Shopify Partner
72 7 30

Thanks for your advice. Appreciated.

 

Without the host I receive the message: "Invalid url '/webhooks/orders/create', missing host" as an error on the registerWebhook.

 

G.

Store owner and app developer. Canada.
0 Likes
tolgapaksoy
Shopify Partner
67 5 32

Hmm this is my exact code for registering webhooks. As you can see, I don't prefix the host, otherwise the host is appended twice. What I do know, is that "registerWebhook" fails if you ever called it before. If you messed up your webhook, and want to reregister it, you have to delete it. Otherwise the "registerWebhook" call will fail with an exception.

 

      async afterAuth(ctx) {
        const { shop, accessToken } = ctx.session;

        try {
          await registerWebhooks(
            shop,
            accessToken,
            "APP_SUBSCRIPTIONS_UPDATE",
            "/webhooks/app_subscriptions/update",
            "2020-01"
          );
        } catch (e) {
          console.error(e);
        }

        ctx.redirect("/");
      }

And this my route:

 

  const webhook = receiveWebhook({ secret: SHOPIFY_API_SECRET });
  router.post("/webhooks/app_subscriptions/update", webhook, async ctx => {
    const {
      payload: { app_subscription }
    } = ctx.state.webhook;

    console.log(app_subscription);

    ctx.respond = false;
    ctx.res.statusCode = 200;
  });
0 Likes
GMKnight
Shopify Partner
72 7 30

@tolgapaksoy thank you so much for your help and code. I tried this and a few other things and still no cigar.

 

I'm starting to wonder if it's a ngrok / ssl issue that I am completely misunderstanding.

 

Routing with GETs is working fine (tested with Curl) but a random POST might not be getting through. 

 

I'll post an update if I find any useful information. 

 

G.

Store owner and app developer. Canada.
0 Likes
GMKnight
Shopify Partner
72 7 30

Hi folks,

 

I wanted to give a quick update on my issue. I didn't find any help on other forums or internet searches.

 

- Webhook on order / create is still not being received through ngrok: I tried several ngrok settings and kept it simple with ngrok http -subdomain=xxxx.com.ngrok.io 3000.

 

- I created a whole new development store and installed the app. No luck.

 

- I registered the webhook in Settings->Notifications->Webhooks, just to see if I could fire it from there. Nothing received.

 

- Oddly, if I execute curl --insecure -d 'random data' https://xxxx.com.ngrok.io/webhooks/order/create then the ngrok shows  receipt of the POST and my server routing also logs receipt of the post (302 to auth which I was expecting).

 

So I'm wondering what the difference is between executing the webhook from Shopify and curl. Could it be a SSL cert issue or something similar? I understand that ngrok should take care of this - so it seems unlikely. 

 

Pretty stumped for something that should be so easy. Any thoughts appreciated. I'd love to help out others who might have had the same problem, even given up.

 

G.

Store owner and app developer. Canada.
0 Likes

Hi All, 

I have also an issue with Order Create webhooks (Order Webhooks at ALL).

First of all, when testing Webhooks from Store Admin, at the endpoint the webhook request can not be validated normally as the shared secret is different than when that store is subscribed for notifications through Shopify Application through the API. But that webhooks ( Settings -> Notifications -> Create Webhook) all the time working perfectly.

What I am trying and what I get up to now...

1. I got a multi-channel lister and 2 stores connected inside (Authorized through the Shopify API and subscribed for Carts, Checkouts, and Orders Webhooks)

2. Endpoint to validate and process the Webhooks

Up to now, there was not any issue regarding Orders Webhooks. My APP catches them all and processes it normally.

But before an hour, only the Orders Webhooks stops, I tried a lot of things

- unsubscribe and subscribe Stores

create a new development Store. Subscribed that new store for Carts, Checkouts, and Orders Webhooks

- double-checked that that new store is subscribed 

shopify-response.png

 

one of the answers I think is most accurate in our case is that  that happen after I try to complete the checkout choosing Bogus Gateway card number 2 and 3 - after that everything stuck

 

 

0 Likes
chenster
Pathfinder
86 3 7

The same issue here, ORDERS_CREATE doesn't trigger webhook to ngrok domains. It's impossible to test locally without it.

Try the free Sales Popup Maker
0 Likes