如何从API Webhook(Shopify Webhook)保存JSON?

时间:2019-05-17 07:40:50

标签: ruby-on-rails json ruby shopify shopify-app

我在保存创建订单时收到的Webhook请求时遇到麻烦。

我希望能够将此信息保存到我的数据库中

我有这个控制器:

class WebhooksController < ShopifyApp::WebhooksController
  include ShopifyApp::WebhookVerification
  skip_before_action :verify_authenticity_token, raise: false

  def orders_create
    params.permit!
    OrdersCreateJob.perform_later(shop_domain: shop_domain, webhook: webhook_params.to_h)
    head :no_content
  end

  def orders_create_save
    @order = Order.new(shopify_order_id: webhook_params[:webhook][:id])
    @order.save

  end

  private

  def webhook_params
    params.except(:controller, :action, :type)
  end
end

这项工作:

class OrdersCreateJob < ActiveJob::Base
  def perform(shop_domain:, webhook:)
    shop = Shop.find_by(shopify_domain: shop_domain)
    shop.with_shopify_session do
      line_items = webhook[:line_items]
      line_items.each do |item|
        variant_id = item[:variant_id]
        product_id = item[:product_id]
      end
    end
  end
end

路线:

post '/webhooks/orders_create', to: 'webhooks#orders_create'
get '/webhooks/orders_create', to: 'webhooks#orders_create_save'
resources :orders
mount ShopifyApp::Engine, at: '/'
...

我的配置正确设置:

config.webhooks = [
    {topic: 'orders/create', address: 'https://i-shelf-74894.herokuapp.com/webhooks/orders_create', format: 'json'},
    # {topic: 'orders/update', address: 'https://example.herokuapp.com/webhooks/orders_update', format: 'json'}
  ]

然后我在创建订单时得到此信息:

2019-05-17T07:06:31.036912+00:00 app[web.1]: I, [2019-05-17T07:06:31.036742 #4]  INFO -- : [213ed49f-d14c-4118-bb07-021a34614db7] Started POST "/webhooks/orders_create" for 35.231.147.210 at 2019-05-17 07:06:31 +0000

2019-05-17T07:06:31.049488+00:00 app[web.1]: I, [2019-05-17T07:06:31.049364 #4]  INFO -- : [213ed49f-d14c-4118-bb07-021a34614db7] Processing by WebhooksController#orders_create as */*

2019-05-17T07:06:31.050418+00:00 app[web.1]: I, [2019-05-17T07:06:31.050279 #4]  INFO -- : [213ed49f-d14c-4118-bb07-021a34614db7]   Parameters: {"id"=>922193756209, "email"=>"", "closed_at"=>nil, "created_at"=>"2019-05-17T02:58:25-04:00", "updated_at"=>"2019-05-17T02:58:26-04:00", "number"=>38, "note"=>"", "token"=>"f506480412c5128d64301346c42a0734", "gateway"=>"manual", "test"=>false, "total_price"=>"15.99", "subtotal_price"=>"15.00", "total_weight"=>198, "total_tax"=>"0.99", "taxes_included"=>false,
.........

下一行:

2019-05-17T07:27:59.308685+00:00 app[web.1]: I, [2019-05-17T07:27:59.308487 #4]  INFO -- : [e65ceb94-bf08-4875-89a9-b1353e03b8d8] [ActiveJob] Enqueued OrdersCreateJob (Job ID: 1a8a3f9b-87d5-47fb-8e02-6d32df51234f861) to Async(default) with arguments: {:shop_domain=>"shopifey-store-2123445345.myshopify.com", :webhook=>{"id"=>922

下一步:

    2019-05-17T07:27:59.329687+00:00 app[web.1]: I, [2019-05-17T07:27:59.329531 #4]  INFO -- : [e65ceb94-bf08-4875-89a9-b1353e03b8d8] Completed 204 No Content in 40ms

2019-05-17T07:27:59.330964+00:00 app[web.1]: I, [2019-05-17T07:27:59.329116 #4]  INFO -- : [ActiveJob] [OrdersCreateJob] [1a8a3f9b-87d5-47fb-8e02-6d32df5ff861] Performing OrdersCreateJob (Job ID: 1a8a3f9b-87d5-47fb-8e02-6d32df5ff861) from Async(default) with arguments: {:shop_domain=>"shopifey-store-
....

结束:

2019-05-17T07:27:59.370131+00:00 app[web.1]: D, [2019-05-17T07:27:59.369968 #4] DEBUG -- : [ActiveJob] [OrdersCreateJob] [1a8a3f9b-87d5-47fb-8e02-6d32df5ff861]   [1m[36mShop Load (2.0ms)[0m  [1m[34mSELECT  "shops".* FROM "shops" WHERE "shops"."shopify_domain" = $1 LIMIT $2[0m  [["shopify_domain", "s1234re-2111234.myshopify.com"], ["LIMIT", 1]]

2019-05-17T07:27:59.371321+00:00 app[web.1]: I, [2019-05-17T07:27:59.371240 #4]  INFO -- : [ActiveJob] [OrdersCreateJob] [1a81234f5ff861] Performed OrdersCreateJob (Job ID: 17d5-47fb-8e02-6d3222234ff861) from Async(default) in 39.89ms

我尝试使用:

@order = Order.new(shopify_order_id:webhook_params [:webhook] [:id])     @ order.save

直接在orders_create方法和OrdersCreateJob作业代码中。

尝试使用“ webhook”代替“ webhook_params”。

我在这里做什么错了?

订单模型与shopify_order_id一起存在

0 个答案:

没有答案