lineofflight / peddler

Amazon Selling Partner API (SP-API) in Ruby
MIT License
307 stars 130 forks source link

ItemList sub array not being built by StructuredList as expected #87

Closed jayshepherd closed 7 years ago

jayshepherd commented 7 years ago

I am trying to use MWS::MerchantFulfillment::Client.get_eligible_shipping_services but getting an error due to improper formatting for the ItemList value.

I have a Hash that contains and Array of objects containing OrderItemId and Quantity but it doesn't seem that StructuredList parses the array properly. I get the error "Unexpected list element termination" when submitting the following:

My hash

{
  "AmazonOrderId": "112-AAABBBC-3725822",
  "ItemList": [
    {
      "OrderItemId": "08898918454321",
      "Quantity": 1
    },
    {
      "OrderItemId": "08898918412345",
      "Quantity": 2
    }
  ],
  "ShipFromAddress": {
    "Name": "My Company",
    "AddressLine1": "1234 Main Street",
    "AddressLine2": "Suite A",
    "City": "Anywhere",
    "PostalCode": "40000",
    "CountryCode": "US",
    "Email": "orders@mycompany.com",
    "Phone": "800-800-8000"
  },
  "PackageDimensions": {
    "Length": "12.0",
    "Width": "9.0",
    "Height": "2.0",
    "Unit": "inches"
  },
  "Weight": {
    "Value": 115.25,
    "Unit": "oz"
  },
  "ShippingServiceOptions": {
    "DeliveryExperience": "DeliveryConfirmationWithoutSignature",
    "CarrierWillPickup": true
  }
}

client.operation shows the following conversion of the hash:

{
  "Action": "GetEligibleShippingServices",
  "ShipmentRequestDetails.AmazonOrderId": "112-AAABBBC-3725822",
  "ShipmentRequestDetails.ItemList": [
    {
      "OrderItemId": "08898918454321",
      "Quantity": 1
    },
    {
      "OrderItemId": "08898918412345",
      "Quantity": 2
    }
  ],
  "ShipmentRequestDetails.ShipFromAddress.Name": "My Company",
  "ShipmentRequestDetails.ShipFromAddress.AddressLine1": "1234 Main Street",
  "ShipmentRequestDetails.ShipFromAddress.AddressLine2": "Suite A",
  "ShipmentRequestDetails.ShipFromAddress.City": "Anywhere",
  "ShipmentRequestDetails.ShipFromAddress.PostalCode": "40000",
  "ShipmentRequestDetails.ShipFromAddress.CountryCode": "US",
  "ShipmentRequestDetails.ShipFromAddress.Email": "orders@mycompany.com",
  "ShipmentRequestDetails.ShipFromAddress.Phone": "800-800-8000",
  "ShipmentRequestDetails.PackageDimensions.Length": "12.0",
  "ShipmentRequestDetails.PackageDimensions.Width": "9.0",
  "ShipmentRequestDetails.PackageDimensions.Height": "2.0",
  "ShipmentRequestDetails.PackageDimensions.Unit": "inches",
  "ShipmentRequestDetails.Weight.Value": 115.25,
  "ShipmentRequestDetails.Weight.Unit": "oz",
  "ShipmentRequestDetails.ShippingServiceOptions.DeliveryExperience": "DeliveryConfirmationWithoutSignature",
  "ShipmentRequestDetails.ShippingServiceOptions.CarrierWillPickup": true
}

According to the docs, I would expect the ItemList section to look like:

"ShipmentRequestDetails.ItemList.Item.1.OrderItemId": "08898918454321",
"ShipmentRequestDetails.ItemList.Item.1.Quantity": 1,
"ShipmentRequestDetails.ItemList.Item.2.OrderItemId": "08898918412345",
"ShipmentRequestDetails.ItemList.Item.2.Quantity": 2,

How to I prepare my hash for being built to break out the ItemList array properly?

jayshepherd commented 7 years ago

So, unless there is something I'm missing in Peddler, I just had to crunch a workable hash based on the order items.

Crude, but works for now.

order.items.each_with_index.inject({}) {|h, (item, i)| h["Item.#{i+1}"] = {OrderItemId: item.order_item_id, Quantity: item.quantity}; h}
hakanensari commented 7 years ago

@jayshepherd thanks for reporting this. The above commit should fix. I'll release a new version in a moment.

jayshepherd commented 7 years ago

The updated gem worked perfectly. Thanks!