[RUBY] Set up a webhook in Shopify's custom app

Introduction

This article uses certain events that occur in the Shopify store to work with custom apps created with rails. I will talk about the settings of the webhook that I did. In order to receive the notification of the webhook, there is also a method to set manually from Settings → Notification on the management screen, but it is not possible to put it in the management screen of all the stores that made the application, for those who want to operate it as a public application In addition, this time we will not set from the management screen.

environment

Ruby 2.6.6 Rails 6.0.2

1. Configure the required access scope

shopify_app.rb

ShopifyApp.configure do |config|
  ...
  config.scope = "read_products, read_orders" 
  ]
  ...
end

2. Specify the webhook you want to be notified of and the URL of the request destination

Specify the following on the command line assuming you have gem'shopify_app' installed

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

After this, the following will be added to shopify_app.rb

ShopifyApp.configure do |config|
  ...
  config.webhooks = [
    {topic: 'orders/create', address: 'https://example.com/webhooks/orders_create'},
  ]
  ...
end

3. Job settings

Create a job on each webhook app/jobs/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
      #Add the function you want to implement here
    end
  end
end

If you are using a library that performs asynchronous processing such as sidekiq, the above job will be processed in the background.

4. Create a custom controller for webhook

There is no problem even if this part is not included, but it will be described because it is necessary when functions beyond what is done by job through webhook are required. routes.rb

...
post 'webhooks/orders_create', :to => 'custom_webhooks#orders_create'
...

app/controllers/custom_webhooks_controller.rb

class CustomWebhooksController < ApplicationController
  
  def orders_create
  end
  ...

  private

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

end
class CustomWebhooksController < ApplicationController
  include ShopifyApp::WebhookVerification
  ...
end

--Create the required method

def orders_create
  params.permit!
  OrdersCreateJob.perform_later(shop_domain: shop_domain, webhook: webhook_params.to_h)
  head :no_content
end

After making a POST call through a webhook, you need to return 200 series status to Shopify. (According to the official documentation, if you get a response other than 200 OK, it will make 19 calls within 48 hours) By setting head: no_content, the response is "204 no content" = no data is responding = it is judged that the webhook data has been received.

important point

Even if the webhook notification was set on the management screen first, it didn't seem to even make an http request unless the scope access required on the config was allowed. If your app is already installed in the store, please make sure that scope permission is granted on the screen below when the app is reinstalled with this scope addition. image.png

If you want to check if the webhook is set to the store

header
Content-Type: application/json
X-Shopify-Access-Token:Applicable shop.shopify_token
GET https://Store name.myshopify.com/admin/api/2020-07/webhooks.json

You can also check with.

Recommended Posts

Set up a webhook in Shopify's custom app
How to set up a proxy with authentication in Feign
Set up pre-login routing in Devise
Create a new app in Rails
Create a TODO app in Java 7 Create Header
Try making a calculator app in Java
Set up a Java GUI in a separate thread to keep the main
Let's create a custom tab view in SwiftUI 2.0
Draw a too beloved Mandelbrot set in Processing
Setting up Docker Engine in a non-internet connection
Set up a CentOS virtual server with Vagrant
[Android / Java] Set up a button to return to Fragment
[Ruby / Rails] Set a unique (unique) value in the class
Steps to set up a VNC server on CentOS 8.3
[Rails 6] How to set a background image in Rails [CSS]
Set up a Db2 DB container and insert a little data