Sha256: dc4293563e7c1aad735e3bf7846fa5d3fa10a0e439f04afb00baa85efc3b2aa2

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 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)
      job_class.perform_later(domain, data)

      render nothing: true
    end

    private

      # Verify a webhook request.
      def verify_webhook
        unless DiscoApp::WebhookService.is_valid_hmac?(request.body.read.to_s, ShopifyApp.configuration.secret, request.headers['HTTP_X_SHOPIFY_HMAC_SHA256'])
          head :unauthorized
        end
        request.body.rewind
      end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
disco_app-0.6.0 app/controllers/disco_app/webhooks_controller.rb
disco_app-0.6.1 app/controllers/disco_app/webhooks_controller.rb
disco_app-0.6.2 app/controllers/disco_app/webhooks_controller.rb
disco_app-0.6.3 app/controllers/disco_app/webhooks_controller.rb