如何访问作业中的 webhook 数据? (购物 Rails)
How can I access the webhook data in jobs? (Shopify Rails)
我正在用 rails 制作一个 Shopify 应用,我有这个:
webhooks_controller.rb
module ShopifyApp
class WebhooksController < ActionController::Base
include ShopifyApp::WebhookVerification
class ShopifyApp::MissingWebhookJobError < StandardError; end
def receive
params.try(:permit!)
job_args = {shop_domain: shop_domain, webhook: webhook_params.to_h}
webhook_job_klass.perform_later(job_args)
head :no_content
end
private
def webhook_params
params.except(:controller, :action, :type)
end
def webhook_job_klass
"#{webhook_type.classify}Job".safe_constantize or raise ShopifyApp::MissingWebhookJobError
end
def webhook_type
params[:type]
end
end
end
orders_create_job.rb
class OrdersCreateJob < ActiveJob::Base
def perform(shop_domain:, webhook:)
shop = Shop.find_by(shopify_domain: shop_domain)
shop.with_shopify_session do
#DATA GOES HERE
end
end
end
是否可以在“#DATA GOES HERE”中获取 webhook 的变量,例如 "shipping_lines" 的 "title"?
我该怎么做?
数据包含在 webhook 参数中。请注意,Order 没有标题,因此 webhook[:title] 将是 non-existent,但订单有运输线,因此您使用 webhook[:shipping_lines].
获取它们
我正在用 rails 制作一个 Shopify 应用,我有这个:
webhooks_controller.rb
module ShopifyApp
class WebhooksController < ActionController::Base
include ShopifyApp::WebhookVerification
class ShopifyApp::MissingWebhookJobError < StandardError; end
def receive
params.try(:permit!)
job_args = {shop_domain: shop_domain, webhook: webhook_params.to_h}
webhook_job_klass.perform_later(job_args)
head :no_content
end
private
def webhook_params
params.except(:controller, :action, :type)
end
def webhook_job_klass
"#{webhook_type.classify}Job".safe_constantize or raise ShopifyApp::MissingWebhookJobError
end
def webhook_type
params[:type]
end
end
end
orders_create_job.rb
class OrdersCreateJob < ActiveJob::Base
def perform(shop_domain:, webhook:)
shop = Shop.find_by(shopify_domain: shop_domain)
shop.with_shopify_session do
#DATA GOES HERE
end
end
end
是否可以在“#DATA GOES HERE”中获取 webhook 的变量,例如 "shipping_lines" 的 "title"?
我该怎么做?
数据包含在 webhook 参数中。请注意,Order 没有标题,因此 webhook[:title] 将是 non-existent,但订单有运输线,因此您使用 webhook[:shipping_lines].
获取它们