0

次のようなコードを使用して、特定のショップのすべての注文を取得するように取り組んでいます。

orders = ShopifyAPI::Order.find(:all, :params => {:financial_status => 'paid'})
orders.each do |order|
  order_json = order.to_json
  post_json_to_server(order_json)
end

しかし、何らかの理由で、によって作成されたJSONを調べるとorder.to_json、属性discount_codesclient_details属性は次のようになります。

"client_details":{"":{"accept_language":"en-US,en;q=0.8","browser_ip":"199.185.98.174","session_hash":"6b37d22ebcdf097f5ab4e1c9e596a504c4cdc4c41c4f2b29a3a7aae4ead559c3","user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11"}}

"discount_codes":[{"":{"amount":"20.00","code":"CZV57KSE8VMV"}}]

{"":なぜこれらのリストの両方にリードがあるのですか?テストストアで何かが正しく構成されていませんか?

これが私が送信しているJSONの完全なダンプ(RequestBinでキャプチャされた)です:

{ "billing_address" : { "address1" : "asdf",
      "address2" : "",
      "city" : "asdf",
      "company" : "",
      "country" : "United States",
      "country_code" : "US",
      "first_name" : "asdf",
      "last_name" : "asdf",
      "latitude" : "45.176384",
      "longitude" : "-123.045601",
      "name" : "asdf asdf",
      "phone" : "",
      "province" : "Alaska",
      "province_code" : "AK",
      "zip" : "asdf"
    },
  "browser_ip" : "199.185.98.174",
  "buyer_accepts_marketing" : true,
  "cancel_reason" : null,
  "cancelled_at" : null,
  "cart_token" : "bbf42c99f456f9ccda30554022fec659",
  "client_details" : { "" : { "accept_language" : "en-US,en;q=0.8",
          "browser_ip" : "199.185.98.174",
          "session_hash" : "6b37d22ebcdf097f5ab4e1c9e596a504c4cdc4c41c4f2b29a3a7aae4ead559c3",
          "user_agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11"
        } },
  "closed_at" : null,
  "created_at" : "2012-07-10T17:23:53-04:00",
  "currency" : "CAD",
  "customer" : { "accepts_marketing" : true,
      "created_at" : "2012-07-10T17:23:53-04:00",
      "email" : "asdf@example.com",
      "first_name" : "asdf",
      "id" : 94366870,
      "last_name" : "asdf",
      "last_order_id" : null,
      "last_order_name" : null,
      "note" : null,
      "orders_count" : 0,
      "state" : "disabled",
      "tags" : "",
      "total_spent" : "0.00",
      "updated_at" : "2012-07-10T17:35:23-04:00"
    },
  "discount_codes" : [ { "" : { "amount" : "20.00",
            "code" : "CZV57KSE8VMV"
          } } ],
  "email" : "asdf@example.com",
  "financial_status" : "authorized",
  "fulfillment_status" : null,
  "fulfillments" : [  ],
  "gateway" : "bogus",
  "id" : 134753494,
  "landing_site" : "/",
  "landing_site_ref" : null,
  "line_items" : [ { "fulfillment_service" : "manual",
        "fulfillment_status" : null,
        "grams" : 0,
        "id" : 219421970,
        "name" : "Grass-roots methodical instruction set",
        "price" : "19.00",
        "product_id" : 95843140,
        "quantity" : 2,
        "requires_shipping" : true,
        "sku" : "",
        "title" : "Grass-roots methodical instruction set",
        "variant_id" : 224399478,
        "variant_inventory_management" : null,
        "variant_title" : null,
        "vendor" : "Shopify"
      } ],
  "name" : "#1004",
  "note" : "",
  "note_attributes" : [  ],
  "number" : 4,
  "order_number" : 1004,
  "payment_details" : { "avs_result_code" : null,
      "credit_card_bin" : "1",
      "credit_card_company" : "Bogus",
      "credit_card_number" : "XXXX-XXXX-XXXX-1",
      "cvv_result_code" : null
    },
  "processing_method" : "direct",
  "referring_site" : "",
  "shipping_address" : { "address1" : "asdf",
      "address2" : "",
      "city" : "asdf",
      "company" : "",
      "country" : "United States",
      "country_code" : "US",
      "first_name" : "asdf",
      "last_name" : "asdf",
      "latitude" : "45.176384",
      "longitude" : "-123.045601",
      "name" : "asdf asdf",
      "phone" : "",
      "province" : "Alaska",
      "province_code" : "AK",
      "zip" : "asdf"
    },
  "shipping_lines" : [ { "code" : "International Shipping",
        "price" : "20.00",
        "source" : "shopify",
        "title" : "International Shipping"
      } ],
  "subtotal_price" : "18.00",
  "tax_lines" : [  ],
  "taxes_included" : false,
  "token" : "51116b93d2d774b6c537a3bcc8861506",
  "total_discounts" : "20.00",
  "total_line_items_price" : "38.00",
  "total_price" : "38.00",
  "total_price_usd" : "37.28",
  "total_tax" : "0.00",
  "total_weight" : 0,
  "updated_at" : "2012-07-10T17:35:20-04:00"
}
4

2 に答える 2

1

Webhookによって送信されたJSONと同じ方法でOrderをフォーマットしようとして、私は自分でそれを行うことになりました-コードは次のとおりです。

ActiveResource::Base.include_root_in_json = true

orders = ShopifyAPI::Order.find(:all, :params => {:financial_status => 'paid'})

orders.each do |order|
  order_json = order.as_json
  %w(billing_address customer line_items payment_details shipping_address shipping_lines).each do |attribute|
    order_json[attribute] = order.send(attribute).as_json
  end

  if order_json['discount_codes'].length > 0
    order_json['discount_codes'] = [order_json['discount_codes'].as_json[0][nil]]
  end

  order_json['client_details'] = order_json['client_details'].as_json[nil]

  post_json_to_server(order_json)
end

このようにすることで、JSONをShopifyの有料Webhookの注文に正確にマッピングするものに変えることができました。

于 2012-07-12T15:52:43.170 に答える
-1

ActiveRecordにルートの処理方法を指示せずにto_jsonを使用すると、それを実行できます。

JSONをレンダリングするときにルートを含めるようにActiveRecordに指示できます。

ActiveRecord::Base.include_root_in_json = true

あなたはそれから見るでしょう

{"order":{...}} and not {"":{...}}

多くの場合、構文を使用できます

order_json = order.to_json(:root => true)

必要なキー(ルート)を取得するために。JSONを使用することは、子供向けのカーニバルのびくびくした乗り物の中を歩くようなものです...

于 2012-07-11T18:35:39.310 に答える