Shopify / shopify-app-template-remix

374 stars 151 forks source link

Declarative webhook not being triggered #818

Closed vincaslt closed 3 months ago

vincaslt commented 3 months ago

Issue summary

Before opening this issue, I have:

I'm trying to register a new webhook for orders/paid topic, and I'm using this Remix template. I have this in my shopify.app.toml file:

[webhooks]
api_version = "2024-07"

  [[webhooks.subscriptions]]
  topics = [ "app/uninstalled", "orders/paid" ]
  uri = "/webhooks"
  compliance_topics = [ "customers/data_request", "customers/redact", "shop/redact" ]

I deployed this config shopify app deploy, I uninstalled/reinstalled the app. In development npm run dev -- --reset the orders/paid webhook is not firing at all, but uninstall does fire. I receive an empty list of registered webhooks when I check registered webhooks: https://.myshopify.com/admin/api/2024-07/webhooks.json

The old way of adding webhooks to config when initializing shopify client still works fine and the webhooks.json contains my registered webhook:

webhooks: {
    [WebhookSubscriptionTopic.OrdersPaid]: {
      deliveryMethod: DeliveryMethod.Http,
      callbackUrl: "/webhooks",
    },
  },
  hooks: {
    afterAuth: async ({ session }) => {
      await shopify.registerWebhooks({ session });
    },
  },

Is there something that I do wrong?

Expected behavior

The webhook fires.

Actual behavior

Webhook is not fired.

Debug logs

// Paste any relevant logs here
FynnMenkIT commented 3 months ago

Hey maybe its the way you register your webhook in combination with the problem I had here: https://github.com/Shopify/shopify-app-template-remix/issues/796#issuecomment-2252418725

you should reinstall your App, when registering a new webhook, as you mentioned. For me only the npm run dev -- reset did not work. So I reinstalled it and used the command shopify app dev --reset to get the new webhooks being registered. This did it for me, hope it helps.

and also in addition orders/paid need an extra authorization in the partners shopify site because its sensitive date. See also the documentation https://shopify.dev/docs/api/webhooks?reference=graphql#list-of-topics-orders_paid Make sure you also have the right access scopes in your toml for order/paid.

Maybe first do a simple access scope and topic like PRODUCTS_UPDATE

paulomarg commented 3 months ago

Thanks for jumping in @FynnMenkIT! I think that's a good point about the scope being restricted. If these suggestions don't work, I'll raise this with the team so they can investigate what might be going wrong, but I think trying with a non-restricted topic and reinstalling the app first might be helpful to know if there's nothing else going wrong.

About the subscriptions not showing up when you query the API, the declarative webhooks are not exposed via the API, so it's expected that they won't be there.

vincaslt commented 3 months ago

No, it doesn't work for me at all, only the uninstalled webhook is consistently being delivered. All others aren't (product update, order fulfilled), even by following the steps you mentioned exactly (also added any required scopes). My settings:

[webhooks]
api_version = "2024-07"

  [[webhooks.subscriptions]]
  topics = [
  "app/uninstalled",
  "discounts/create",
  "orders/fulfilled",
  "products/update"
]
  uri = "/webhooks"

I notice that when I deploy settings get pushed with hardcoded URLs (e.g. application_url) and when I then start the app with they change, I wonder if it has something to do with it. I'm assuming shopify doesn't know about the changed URLs, unless maybe there's some handling for dev environment.

I can see in the dashboard that all webhooks are failing to deliver, and the last calls are always to the endpoint URL before I call npm run dev -- --reset

image

vincaslt commented 3 months ago

OK, so riding on the back of my observation, I got it to work like this:

  1. npm run dev -- --reset
  2. uninstall the app if installed before
  3. install the app
  4. shopify app deploy in a new console window
  5. hooks are firing (until next start, then repeat the steps again if adding webhooks, or 1 & 4 if not).

Is there a better way that doesn't require juggling URLs?

paulomarg commented 3 months ago

I don't believe you should have to deploy on every restart if your topics haven't changed. I'll raise this with the team to see if there is anything we can do here.

paulomarg commented 3 months ago

UPDATE: for now, you'll need to deploy before dev when developing webhooks, so the URLs are updated. I've forwarded this feedback to the team, and we're looking for ways to improve this.

Since there isn't a way for us to change that behaviour from this repo, I'm going to close the issue, but the team is aware of it!