Ad

Consuming Webhooks Shopify-api

- 1 answer

I'm in the process of building my first RoR webapp and I'm currently trying to set up an integration with Shopify. I want to create an order in my web application whenever one is created in Shopify. For this I want to use a Shopify webhook. Step two will be to set it up so that after processing the order I'll use the Shopify API to update some records. For now my main concern is receiving the webhooks. I've been looking at the documentation for the Shopify gem but with my limited RoR skills I can't seem to figure it out. I've been searching the web for a few hours but can't find any clear examples / explanation.

I'm confused about this gem; shopify-api (https://docs.shopify.com/api/authentication/using-api-gem-with-private-app-credentials). I've created a private app in shopify, but after that I'm lost. Where am I supposed to place this code and how is this invoked after receiving a webhook?

It seems to me that this gem is mainly used to access the Shopify API instead of consuming webhooks. Am I going at this all wrong?

Ad

Answer

For receiving the changes (especially Order Creation event) from Shopify, you don't need to use any gem or third party to integrate!

Here are the steps that I did before in my app:

1. Provide an api in your app to receive a webhook event

Here is an example:

Your route:

post '/shopify/create_order', to: 'shopify#create_order'

Your controller:

class ShopifyController < ApplicationController
  def create_order
    # process shopify order here: all info in `params`
  end
end

2. Config to call webhook from your Shopify Settings. The configured url will be:

http://your_production_url/shopify/create_order

If you want to test from localhost, go to step 3

3. (Optional) Test from your localhost

Download this free tool: ngrok and extract it to your machine. Run ngrok in your terminal to generate a forwarding url to your localhost by this command:

ngrok http 3000

So you will get the output from console like this:

enter image description here

Copy the errer">ngrok and extract it to your machine. Run ngrok in your terminal to generate a forwarding url to your localhost by this command:

ngrok http 3000

So you will get the output from console like this:

enter image description here

Copy the generated url (http://fbc5cf88.ngrok.io for example) above and add to your Shopify settings in step 2

4. (Optional but critical) Set privacy in your app, to make sure only Shopify can call your api. Follow this documentation

Shopify also provides api to integrate with Shopify resources: create/update product, collection, collect, metafield,... You can directly use it. All was described at Shopify API documentation. But the easiest way to work with Shopify is shopify_api gem which provides an interface to work with Shopify (via ActiceResource)

Ad
source: stackoverflow.com
Ad