我将我的应用设置为接收 Shopify webhook。我按照这里的指南
我制作了自己的控制器
include ShopifyApp::WebhookVerification
对 webhook 和我的应用进行身份验证
我设置了我的 Shopify_app.rb 文件以将 webhook 发送到正确的路径,如下所示
config.webhooks = [
{topic: 'customers/create', address: 'https://*******.ngrok.io/webhooks/new_contact'},
{topic: 'checkouts/update', address: 'https://*******.ngrok.io/webhooks/checkout_update'},
{topic: 'orders/create', address: 'https://*******.ngrok.io/webhooks/orders_create'}
]
我收到了 Webhooks,但我不断收到消息
Filter chain halted as :verify_request rendered or redirected
这是我的控制器:
class WebhooksController < ApplicationController
include ShopifyApp::WebhookVerification
#webhook that handles new contacts in the shopify application
def new_contact
shop = ShopifyShop.getShop(shop_domain)
if !shop
render json: {success: true} and return
end
# begin
raw_post = request.raw_post.force_encoding("UTF-8")
contact.newShopifyContact(shop.default_tags, shop.organization_id,raw_post)
# rescue Exception => e
# ExceptionLog.track(e.class.name, e.message, e.backtrace)
# end
render json: {success: true}
end
end
接收请求时的终端输出:
我已经尝试添加
skip_before_action :verify_authenticity_token
因为我认为也许我的应用程序身份验证导致了 401
当我删除include ShopifyApp::WebhookVerification
它进入我的方法new_contact
所以我认为问题出在include ShopifyApp::WebhookVerification
我尝试使用以下代码自己验证请求中的 HMAC:
header_hmac = request.headers["HTTP_X_SHOPIFY_HMAC_SHA256"]
digest = OpenSSL::Digest.new("sha256")
request.body.rewind
calculated_hmac = Base64.encode64(OpenSSL::HMAC.digest(digest, ENV['SHOPIFY_SECRET_KEY'], request.body.read)).strip
puts "header hmac: #{header_hmac}"
puts "calculated hmac: #{calculated_hmac}"
puts "Verified:#{ActiveSupport::SecurityUtils.secure_compare(calculated_hmac, header_hmac)}"
并且验证返回错误,我正在为我的应用程序使用正确的 API 私钥我不确定是否可能需要第三个密钥?
更新1:似乎问题在于
config.webhooks = [
{topic: 'customers/create', address: 'https://{url}.ngrok.io/shopify/app/customers_create'},
{topic: 'checkouts/update', address: 'https://{url}.ngrok.io/shopify/app/checkouts_update'},
{topic: 'orders/create', address: 'https://{url}.ngrok.io/shopify/app/orders_create'}
]
没有在安装应用程序的商店上生成 webhook .. 不知道为什么