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.
Ruby 2.6.6 Rails 6.0.2
shopify_app.rb
ShopifyApp.configure do |config|
...
config.scope = "read_products, read_orders"
]
...
end
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
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.
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.
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.
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