Search code examples
ruby-on-railsruby-on-rails-4shopifywebhooksshopify-app

How can I access the webhook data in jobs? (Shopify Rails)


I'm making a Shopify App with rails and I have this:

webhooks_controller.rb

  module ShopifyApp
    class WebhooksController < ActionController::Base
      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

orders_create_job.rb

class OrdersCreateJob < ActiveJob::Base

  def perform(shop_domain:, webhook:)
    shop = Shop.find_by(shopify_domain: shop_domain)
    shop.with_shopify_session do
      #DATA GOES HERE
    end
  end
end

Is it possible to get the webhook's variables in "#DATA GOES HERE" such as the "title" of "shipping_lines"?

How can I do that?


Solution

  • The data is included in the webhook argument. Note that an Order has no title, so webhook[:title] will be non-existent, but an order has shipping lines so you get at them with webhook[:shipping_lines].