Shopify Webhook Setup With Rails - ruby-on-rails

I'm trying to setup a basic shopify webhook app with order/create in Rails.
I have followed the instructions in the support api for webhooks and deployed the Sync_App_Demo example provided there but cannot get to authorise log in authorize step. It brings up the 'We're sorry something went wrong' page - myapp/login/authenticate
I'm new to webhooks and have looked all over SO and googled but had no joy working out the basic set up for the webhook connection. I placed the shopify app api key and secret in the based on the shopify_app gem setup which is created but not sure if this is correct.
Also once I do get connnected via a webhook controller where abouts do point (eg. what url /order.xml in the order notifcations panel of my test shop) the webhook order/create?
Any help would be much appreciated.
Here is the link to the shopify sync app demo:
https://github.com/Shopify/sync_app_demo
http://wiki.shopify.com/WebHook#Rails

Stumbled upon your question when searching for how to do this myself. Given that it's been more than a few years since you posted this here, hopefully you've already found the answer. If you haven't, here's what we did:
In both cases, you'll have to know your Shopify API key, password, and store name.
1) Using Shopify's shopify_api gem:
new_webhook = ShopifyAPI::Webhook.new({
topic: "orders/create",
address: "http://www.example.com/webhook", # substitute url with your endpoint
format: "json"
})
new_webhook.save
2) Using terminal:
curl -X POST -H 'Content-Type: application/json' 'https://shopify_api_key:shopify_api_password#store_name.myshopify.com/admin/webhooks.json' -d '{"webhooks": {"topic": "orders\/create", "address": "http:\/\/www.example.com\/webhook", "format": "json"}}'
You so you'll need to add in your own shopify_api_key, shopify_api_password, store_name, and address endpoint that you want to receive the published events.
Other useful things:
Ultrahook - for receiving webhooks on locally run server (eg. when using rails s)
RequestBin - for seeing webhook responses raw body and headers

For new people getting started with Shopify App development with Ruby on Rails, I strongly suggest using the shopify_app gem that includes the shopify_api gem.
To register a webhook, you can run the add_webhook generator or add a line manually in the shopify_app.rb initializer:
config.webhooks = [
{topic: 'app/uninstalled', address: 'https://myapp.url/webhooks/app_uninstalled', format: 'json'},
{topic: 'orders/create', address: 'https://myapp.url/webhooks/orders_create', format: 'json'},
]
With this setup, your app will register the webhooks on application install.

Related

How to Parse Json with Bearer Token in Ruby

I have recently graduated a Bootcamp and trying my luck in some projects and applying for jobs.
Got a test, for this potential job, that I don't know how to tackle exactly. At this stage, kind of just wondering how to solve the test.
I need to create a website and display a list of all of the buildings, from this real estate company via their API. They have provided me with the token for the access already.
However, I'm not entirely sure how to connect with their API via Ruby. They gave me an example of how to connect but I haven't seen this before.
curl -H "Authorization: Bearer token_they_provided" https://theirsite/api/buildings.
Any assistance to point me in the right direction will be appreciated.
Thank you all!
What you are trying to achieve is pretty much simple. You can just add HTTP gem (there are other options, like Faraday gem) to your project, which turns everything even simpler.
Using HTTP Gem
Add it to your Gemfile
Install using bundle
Fetches API as below:
response = HTTP[Authorization: "Bearer #{token_they_provided}"].get("https://theirsite/api/buildings")
Parse JSON body:
JSON.parse(response.body)
You can read the gem docs for more information. Also, feel free to connect me via LinkedIn

How to call api using savon gem

I am using savon gem in my rails application for calling service_now api. I want to call the api for update description. I have url for updating
https://servicenow.com/xyzlist?JSONv2&sysparm_query=number='xyz123'&sysparm_action=update&displayvalue=true
body "comments":"updated description"
Above is working perfectly in postmen. But how can i pass this params into my local rails application for updating
I am using below code for creating tickets.
client = Savon.client(wsdl: "https://servicenow.com/xyzlist.do?WSDL",basic_auth: ['xyz', 'abc'], log: true, log_level: :debug, pretty_print_xml: true)
response = client.call(:insert, message: params)
Can you please anyone help me for this issue.
You are referring to two different APIs in your example code: JSONv2 and SOAP.
Two questions: 1) do you need to use SOAP, or can you use REST instead? (Rails should support this without issue) and 2) what version of ServiceNow are you using? If you're running Geneva or later and you're able to use REST, you should be using the REST Table API for new integrations, not JSONv2.
See the documentation here for examples:
https://developer.servicenow.com/app.do#!/rest_api_doc?v=istanbul&id=r_TableAPI-PUT

Consuming webhooks shopify-api

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?
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:
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)
Probably reading up on the documentation on Webhooks would help you figure out a lot of what you need to do
In essence, here are the overview of what you need to start consuming web hooks:
1. Create your web hooks
You can do it using the Shopify API (POST /admin/webhooks.json)
Or you can do it through store admin
The params that you need to take note of:
topic: the type of hook you want (for e.g orders/create)
address: the URL of the endpoint that you would be consuming the hook at (for e.g: https://myapp.domain.com/hooks/order_create)
2. Create your endpoint that will respond to hooks
You then need to setup your ROR app to expose an endpoint that will respond to a POST request.
This endpoint is the same URL that you previously specified when creating the hook
More info at https://docs.shopify.com/api/webhooks/using-webhooks#respond-to-webhook
Notes: You also need to implement a way to verify that requests that you receive through the endpoints came from Shopify, detailed here.

Where in the Shopify Rails App should I create uninstall webhooks?

I'm trying to create an 'app/uninstall' webhook in my Shopify app. The app boilerplate was generated using the Shopify API gem(https://github.com/Shopify/shopify_app), and I've followed the instructions in their readme to the letter.
To create these webhooks, I'm assuming they'd have to be somewhere when the client shop first connects with the app - but I'm not sure where exactly that takes place in the whole thing. I've pushed the boilerplate code here: https://github.com/shabbirun/shopify-help-app
I first guessed that the code should be in the Shop model, so I tried implementing it using :after_create , but I'm getting an error.
Any ideas as to where I can place the code?
Thank you!
You will need to post/put a new webhook through the API.
Example code is in node: (ruby will be different)
shopify.post('/admin/webhooks.json', {
webhook: {
topic: 'app/uninstalled',
address: 'https://yoururl.com/shopify/uninstall',
format: 'json'
}
}, done);
Once the admin webhook has been added, anytime a user uninstalls the app it will hit your api route for shopify uninstalls.
In terms of placing the code we recommend doing it when a user first approves your app, whichever route that may be.
https://docs.shopify.com/api/webhook
To register a webhook, you can run the add_webhook generator or add a line manually in the shopify_app.rb initializer:
config.webhooks = [
{topic: 'app/uninstalled', address: 'https://myapp.url/webhooks/app_uninstalled', format: 'json'},
{topic: 'orders/create', address: 'https://myapp.url/webhooks/orders_create', format: 'json'},
]
With this setup, your app will register the webhooks automatically when your app gets installed in a shop.

Sendgrid receive email locally

I am trying to implement sendgrid parse API using the 'griddler' gem in my rails application. The problem is how can I test it locally? And how to receive email in my local machine.
The are a couple ways to test SendGrid's Parse Webhook locally.
The easiest is to just simulate the Webhook by POSTing data to your endpoint yourself (via cURL or some other mechanism, like Postman [example]). The SendGrid Parse Webhook Docs has an example of the payload that will be posted to your server, so you may mimic that.
The second option, which allows for end-to-end testing rather than just simulation is by creating a tunnel your local machine and provide SendGrid with the URL of the tunnel. There are a number of free services that allow you to do just that very easily:
https://ngrok.com/
http://progrium.com/localtunnel/
If I may self-promote, I created a Ruby gem that does this:
https://github.com/ccallebs/pokey-sendgrid
It runs alongside your server and generates SendGrid webhook requests at regular intervals.

Resources