1
votes

I want to perform a job when an order is created in Shopify, but it seems as if nothing is listening to the webhook and I can't figure out why. I followed the documentation for the shopify_app gem but it is not working.

I ran

rails g shopify_app:add_webhook -t orders/create -a https://example.com/webhooks/orders_create

Initializer shopify_app.rb

ShopifyApp.configure do |config|

...

  config.webhooks = [
    {topic: 'orders/create', address: 'https://myappwebaddress_not_showing_on_stackoverflow/webhooks/orders_create', format: 'json'},
  ]

...
end

I am using the WebhooksController

module ShopifyApp
  class WebhooksController < ApplicationController
    include ShopifyApp::WebhookVerification


    class ShopifyApp::MissingWebhookJobError < StandardError; end

    def receive
      params.try(:permit!)
      job_args = {shop_domain: shop_domain, webhook: webhook_params.to_h}
      webhook_job_klass.perform_later(job_args)
      head :no_content
    end

    private

    def webhook_params
      params.except(:controller, :action, :type)
    end

    def webhook_job_klass
      "#{webhook_type.classify}Job".safe_constantize or raise ShopifyApp::MissingWebhookJobError
    end

    def webhook_type
      params[:type]
    end
  end
end

I know the webhook is created because I display it within the app view and it shows.

[#<ShopifyAPI::Webhook:0x007f183012d578 @attributes={"id"=>462377288, "address"=>"https://myappwebaddress_not_showing_on_stackoverflow/webhooks/orders_create", "topic"=>"orders/create", "created_at"=>"2016-11-29T20:29:24-08:00", "updated_at"=>"2016-11-29T20:29:24-08:00", "format"=>"json", "fields"=>[], "metafield_namespaces"=>[]}, @prefix_options={}, @persisted=true>]

When I create an order through Shopify, nothing happens in my app.

I should add, I am using sidekiq to process the job. Sidekiq is working because when I "install" the app in Shopify, it processes the job of creating the webhook.

What am I missing to process the webhook? Do I still need to create something in routes.rb? Nothing in the documentation says I need to. I tried creating a route post 'webhooks/orders_create' => 'webhooks#receive' But, that didn't do anything.

What do I need to do?

1
Have u created the web hook on shopify?Chakreshwar Sharma
My app creates the webhook when the user "installs" my app. So, I create the webhook through an initializer in my rails appDoughtz
Login to the shopify admin panel , click on settings --> notifications -> check that whether the webhook present or not?Chakreshwar Sharma
It is not. My understanding is that it won't show up there unless you created it there. The webhook created through the app is for the owner. Those webhooks you speak of are just at the store levelDoughtz
You need to create the web hook there for every eventChakreshwar Sharma

1 Answers

1
votes

The problem was that the webhook could not access the endpoint because the development server was hosted on cloud9 and was only accessible via my browser.