Shopify / shopify-api-ruby

ShopifyAPI is a lightweight gem for accessing the Shopify admin REST and GraphQL web services.
MIT License
1.06k stars 471 forks source link

Register webhook manually #1259

Closed remy727 closed 10 months ago

remy727 commented 10 months ago

Issue summary

I want to register orders/fulfilled webhook for one of my public app customers.

shop = Shop.find_by(shopify_domain: "client-store.myshopify.com")
shop.with_shopify_session do
  ShopifyAPI::Webhooks::Registry.add_registration(
    topic: "orders/fulfilled",
    delivery_method: :http,
    handler: OrdersFulfilledJob,
    path: "https://myapp.com/api/custom_webhooks/orders_fulfilled"
  )
end
ShopifyAPI::Webhooks::Registry.register(topic: "orders/fulfilled", session: shop.session)

I was able to register the webhook and confirmed it was added. But I am getting ShopifyAPI::Errors::NoWebhookHandler (No webhook handler found for topic: orders/fulfilled error when running the below code.

ShopifyAPI::Webhooks::Registry.process(ShopifyAPI::Webhooks::Request.new(raw_body: request.raw_post,
      headers: headers))
ShopifyAPI::Errors::NoWebhookHandler (No webhook handler found for topic: orders/fulfilled 

Expected behavior

Handles orders/fulfilled webhook

Actual behavior

No webhook handler found for topic: orders/fulfilled

Steps to reproduce the problem

  1. Register webhook manually with the above code
  2. Fulfill the order in the Shopify Admin
lizkenyon commented 10 months ago

Hi there 👋

Just a couple of things to double check here

remy727 commented 10 months ago

@lizkenyon, thank you for your reply.

lizkenyon commented 10 months ago

How can I reload ShopifyAPI::Webhooks::Registry?

In your apps boot up code you will need to have code that adds your handlers to your registry.

You can see an example in the ruby template that uses a helper from shopify_app gem.

remy727 commented 10 months ago

Aha got it. I am already using that ruby template.

lizkenyon commented 10 months ago

👍 Hope that resolves that for you.

If you have any suggestions/a PR on how we could clarify this in the documentation I would love to hear it. Thanks! 😄

weizheheng commented 9 months ago

Hi @remy727, did you manage to figure it out? I am having the same issue:

  1. I have a base webhooks for every users and it's set in the config.webhooks in the shopify_app initializer
  2. Then I have a few webhooks I want to add for certain shops only, but after registering the specific webhook for a shop, the Registry is not "reloaded" I will assume:
  # Here the @registry is only the one I set in the `config.webhooks` not including the new registration 
  handler = @registry[request.topic]&.handler

  # This error is then raised
  unless handler
    raise Errors::NoWebhookHandler, "No webhook handler found for topic: #{request.topic}."
  end