Skip to main content

How to add Shopify webhooks in POWR

Prerequisites

  1. What is Shopify?
  2. Knowledge Installing POWR App in Shopify
  3. Shopify in POWR codebase /interactors/shopify folder would be helpful, better to read at least once.
  4. Be familiar with webhooks
  5. Ngrok knowledge

Most important files are:

  • authenticate_interactor.rb
  • webhooks_interactor.rb
  • webhooks_controller.rb

Adding webhooks

In authenticate_interactor.rb you can find the method: create_shopify_webhooks! it is the place where are we going to put our new webhooks.

Pay attention to the %w(shop/update app/uninstalled ... code. This code splits the string by space and creates an array of strings, and after splitting we traverse through Webhooks and create them via ShopifyAPI::Webhook.create() API function.

If you are getting no values in terminal puts like: [Shopify::AuthenticateInteractor] - registered webhook for ID: , Shopify Install ID 22, Topic - {name_of_topic}

As you see here, the webhook for ID: has no number, it means something is broken, so check the logs or messages.

If you want to see the webhook errors, add puts after webhook create function, something like:
puts "CURRENT WEBHOOK: #{webhook.inspect}
It will put the webhook response, and you may find error messages in it.

Continue adding new webhooks into the array of other webhooks

In webhooks_controller.rb you can find an array called: SUBSCRIBED_EVENTS, in this array we save our webhooks that we are already subscribed to get any response that they send us. Add your webhook names to this list.

When do webhooks activate?

In the webhooks_interactor you may find a method called call and you can see the other webhook names. Add your webhook names to the case (switch) clause and put some puts or binding.pry to check when the webhook is activated when some event happens.

Errors that can appear

  1. There is a possibility that your event/webhook may not work, because of the scopes.
  2. TODO if other errors appear

NOTES:

Scopes actually permissions. If you don't have permissions you can't do anything what are you trying to do. {#scope-def} Check the webhook in the debugger or puts to check the response error, and if it says:

@messages={:topic=>["Invalid topic specified: subscription_billing_attempts/success. Does it exist? Is there a missing access scope?