As a beginner, I need some very basic js help here I guess. I created a custom shopify app using Shopify CLI out of my terminal. I now want to implement code in the created Shopify App template, to receive webhooks from my Shopify, and I don't know how to do this.
Should I create a "app.rb" file to put the code there, or a "app.jsx" file, and where?
Do I need to "import" or "export" in order to connect my new webhook code to the rest of the App, and how? Just some basic advice, what to read, which tutorials to check, would be already awesome. The code I want to implement looks like this:
require 'rubygems'
require 'base64'
require 'openssl'
require 'sinatra'
require 'shopify_api'
require 'active_support/security_utils'
# The Shopify app's API secret key, viewable from the Partner Dashboard. In a production environment, set the API secret key as an environment variable to prevent exposing it in code.
API_SECRET_KEY = '******************'
helpers do
# Compare the computed HMAC digest based on the API secret key and the request contents to the reported HMAC in the headers
def verify_webhook(data, hmac_header)
calculated_hmac = Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', API_SECRET_KEY, data))
ActiveSupport::SecurityUtils.secure_compare(calculated_hmac, hmac_header)
end
end
# Respond to HTTP POST requests sent to this web service
post '/webook/product_update' do
request.body.rewind
data = request.body.read
verified = verify_webhook(data, env["HTTP_X_SHOPIFY_HMAC_SHA256"])
halt 401 unless verified
# puts "Webhook verified: #{verified}"
# Process webhook payload
# ...
end
Maybe it helps to show my file structure (with open "web" folder):