Topics covering webhook creation & management, event handling, Pub/Sub, and Eventbridge, in Shopify apps.
Hello, I have new using webhooks. I need to do very simple thing: execute a script in rails when a order will be created.
I have an ROR App to make something, this thing execute correctly if I put http://www.mydomain/myapp/myscript but I don't know how can do this automátically when an order will be created in my Shopify store.
I have created a webhook in my shopify admin like this:
- event: order creation
- callback url: http://www.mydomain/myapp/webhooks
- format: JSON
In my app I have created a webhooks controller with:
def order_create data = ActiveSupport::JSON.decode(request.body.read) ...my code... head :ok end
And in my routes.rb I have:
root to: 'home#index' resources :codes, only: [:index, :create] post 'webhooks/orders/create' => 'webhook#order_create'
When I send test notificaion from my shopify admin or make a new order I get in my apache.log:
204.93.213.120 - - [25/Nov/2014:17:11:29 +0100] "POST /myapp/webhooks HTTP/1.1" 404 1014 "-" "Ruby" 3836
I am forgetting something, sure, but I don't know what
Thank you so much.
Hello Unai,
Perhaps some folks from the community can shed some light on this method. In my case I chose a different solution to the same problem.
Webhooks can sometimes time out due to the regular state of the internet and I was hosting on heroku which especially has a tendancy to time out a bit. Rather than deal with the wasted time for a webhook to be retried I directly hook into the Shopify API.
By running a regular job and getting Orders.all I am able to then call the first order (if it exists) and process it, then archive/close the order. Of course it always runs, which probably isn't ideal.
Just taking a quick look at the code, it seems as if you are hitting the wrong endpoint. The code ""POST /myapp/webhooks" isn't set up in a route that I see. Perhaps try a post to webhooks/orders/create unless I'm interpreting the logs incorrectly
Best of luck,
Alex
To learn more visit the Shopify Help Center or the Community Blog.
Thanks for your answer Massaad. I understand you say but I would like to know how to get order/create action first, and then I will work to make more robust system.
I am sure that this question is very easy for a shopify developer. I only need a small example of rails code to get the order/create action with a webhook created through Shopify admin. Or a minimal code to make this throgh API webhook using a private app secret shared.
My new code is:
Routes.rb
Rails.application.routes.draw do root to: 'home#index' resources :codes, only: [:index, :create] post 'codes/create' => 'codes#create' end
codes_controller.rb
class CodesController < ApplicationController def index create end def create data = ActiveSupport::JSON.decode(request.body.read) @code = Code.new range = [*'0'..'9',*'A'..'Z',*'a'..'z'] begin string_codigo_venta = (Array.new(4){ range.sample }.join + ' - ' + Array.new(4){ range.sample }.join + ' - ' + Array.new(4){ range.sample }.join).downcase end while Code.exists?(:codigo_venta => string_codigo_venta) @code.codigo_venta = string_codigo_venta @code.codigo_seguridad = '-' @code.order_id = '356897352' @code.product_id = '2' @code.notified = true @code.variant_title = 'a mano' if @code.save flash[:success] = @code.codigo_venta.to_s else flash[:danger] = 'cagada' end head :ok #render 'index' end end
The Callback URL of admin shopify webhook: http://mydomain/cupones/codes/create
Thanks.