Sha256: df0a168aad021fe5db1f99477f365b87d9afe6f2e5b4a19f134dce165ddb0400

Contents?: true

Size: 1.34 KB

Versions: 13

Compression:

Stored size: 1.34 KB

Contents

module DiscoApp
  class WebhooksController < ActionController::Base

    before_action :verify_webhook

    def process_webhook
      # Get the topic and domain for this webhook.
      topic = request.headers['HTTP_X_SHOPIFY_TOPIC']
      domain = request.headers['HTTP_X_SHOPIFY_SHOP_DOMAIN']

      # Ensure a domain was provided in the headers.
      unless domain
        head :bad_request
      end

      # Try to find a matching background job task for the given topic using class name.
      job_class = DiscoApp::WebhookService.find_job_class(topic)

      # Return bad request if we couldn't match a job class.
      unless job_class.present?
        head :bad_request
      end

      # Decode the body data and enqueue the appropriate job.
      data = ActiveSupport::JSON::decode(request.body.read).with_indifferent_access
      job_class.perform_later(domain, data)

      render nothing: true
    end

    private

      def verify_webhook
        unless webhook_is_valid?
          head :unauthorized
        end
        request.body.rewind
      end

      def webhook_is_valid?
        return true if Rails.env.development? and DiscoApp.configuration.skip_webhook_verification?
        DiscoApp::WebhookService.is_valid_hmac?(request.body.read.to_s, ShopifyApp.configuration.secret, request.headers['HTTP_X_SHOPIFY_HMAC_SHA256'])
      end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
disco_app-0.8.8 app/controllers/disco_app/webhooks_controller.rb
disco_app-0.8.9 app/controllers/disco_app/webhooks_controller.rb
disco_app-0.9.0 app/controllers/disco_app/webhooks_controller.rb
disco_app-0.9.1 app/controllers/disco_app/webhooks_controller.rb
disco_app-0.9.2 app/controllers/disco_app/webhooks_controller.rb
disco_app-0.9.3 app/controllers/disco_app/webhooks_controller.rb
disco_app-0.9.4 app/controllers/disco_app/webhooks_controller.rb
disco_app-0.9.5 app/controllers/disco_app/webhooks_controller.rb
disco_app-0.9.6 app/controllers/disco_app/webhooks_controller.rb
disco_app-0.9.7 app/controllers/disco_app/webhooks_controller.rb
disco_app-0.9.8 app/controllers/disco_app/webhooks_controller.rb
disco_app-0.9.9 app/controllers/disco_app/webhooks_controller.rb
disco_app-0.9.10 app/controllers/disco_app/webhooks_controller.rb