0

What would be the best way to retrieve orders from Amazon MWS?

My current code is as follows...

MarketplaceWebServiceOrdersConfig config = new MarketplaceWebServiceOrdersConfig();
config.ServiceURL = productsURL;

MarketplaceWebServiceOrders.MarketplaceWebServiceOrdersClient service = new MarketplaceWebServiceOrdersClient(appname, version, accesskeyID, secretkey, config);             

ListOrdersRequest request = new ListOrdersRequest();
request.MarketplaceId = new MarketplaceIdList();
request.MarketplaceId.Id = new List<string>(new string[] { marketids[0] });
request.SellerId = merchantID;
request.OrderStatus = new OrderStatusList() { Status = new List<OrderStatusEnum>() { OrderStatusEnum.Unshipped, OrderStatusEnum.PartiallyShipped } };
request.CreatedAfter = Convert.ToDateTime(dc.Settings.SingleOrDefault().lastOrdersRetrieved);

ListOrdersResponse response = service.ListOrders(request);

I am having issues passing the ISO Date across, also if you see any other issues with the code please feel free to let me know.

thatuxguy
  • 2,418
  • 7
  • 30
  • 51

1 Answers1

1

If your looking for something created after the immediate second you make the request, it wont find anything at all as with Amazon you can only grab up to the last 2 minutes of data for orders.

I had an issue with trying to set time from Now - 5 minutes. After speaking to Amazon support they provided the following nugget: "

In Orders API, if you don't specify an end time (CreatedBefore or LastUpdatedBefore), it will assume now (actually, now minus 2 minutes). And in its response, it will tell you exactly what time it used as the cutoff time."

In your case you will want to remove the CreatedAfter request and let Amazon choose for you.

If you are then looking for the created after, you can grab the response time Amazon gave and pass that in to your created after param.

The method I have right now to list orders is as follows, mind you this will just list the orders to console, but the data gets returned all the same:

public List<string> ListOrders(MarketplaceWebServiceOrders.MarketplaceWebServiceOrders    service, string merchantId, List<OrderStatusEnum> orderStatus)
    {
        List<string> salesOrderIds = new List<string>();

        ListOrdersRequest listOrdersRequest = new ListOrdersRequest();

        DateTime createdAfter = DateTime.Now.Add(new TimeSpan(-1, 0, 0));
        DateTime createdbefore = DateTime.Now.Add(new TimeSpan(0, -15, 0));

        listOrdersRequest.CreatedAfter = createdAfter;
        listOrdersRequest.CreatedBefore = createdbefore;
        listOrdersRequest.SellerId = merchantId;
        listOrdersRequest.OrderStatus = new OrderStatusList();

        foreach (OrderStatusEnum status in orderStatus)
        {
            listOrdersRequest.OrderStatus.Status.Add(status);
        }

        listOrdersRequest.FulfillmentChannel = new FulfillmentChannelList();
        listOrdersRequest.FulfillmentChannel.Channel = new List<FulfillmentChannelEnum>();
        listOrdersRequest.FulfillmentChannel.Channel.Add(FulfillmentChannelEnum.MFN);


        listOrdersRequest.MarketplaceId = new MarketplaceIdList();
        listOrdersRequest.MarketplaceId.Id = new List<string>();
        listOrdersRequest.MarketplaceId.Id.Add("yourID");

        ListOrdersResponse listOrdersResponse = service.ListOrders(listOrdersRequest);

        int i = 0;

        foreach (Order order in listOrdersResponse.ListOrdersResult.Orders.Order)
        {
            i++;
            Console.WriteLine("Amazon Order ID:             \t" + order.AmazonOrderId);
            Console.WriteLine("Buyer Name:                  \t" + order.BuyerName);
            Console.WriteLine("Buyer Email:                 \t" + order.BuyerEmail);
            Console.WriteLine("Fulfillment Channel:         \t" + order.FulfillmentChannel);
            Console.WriteLine("Order Status:                \t" + order.OrderStatus);
            Console.WriteLine("Order Total:                 \t" + order.OrderTotal);
            Console.WriteLine("Number of Items Shipped:     \t" + order.NumberOfItemsShipped);
            Console.WriteLine("Number of Items Unshipped:   \t" + order.NumberOfItemsUnshipped);
            Console.WriteLine("Purchase Date:               \t" + order.PurchaseDate);
            Console.WriteLine("===========================================================");

            salesOrderIds.Add(order.AmazonOrderId);


        }

        Console.WriteLine("We returned a total of {0} records. ", i);
        return salesOrderIds;
    }
Robert H
  • 11,520
  • 18
  • 68
  • 110
  • atm the datetime i have set is the datetime of the last update. I was looking to retrieve all orders every 15 mins or so. so using this datetime field to set that. if < 15 mins no update, if 15 or > then update essentially :) – thatuxguy Jul 19 '12 at 14:38
  • I dropped in my code for listing orders so you can see how I grab the data right now - Honestly I had just started on the orders API when a few other priority projects came up, so I haven't had the opportunity to ensure this is the proper way to get the orders in a "realtime" fashion, but hopefully it gets you to where you need to be – Robert H Jul 19 '12 at 14:46
  • nice one many thanks, i am guessing you are making a console app? – thatuxguy Jul 19 '12 at 14:49
  • I am working on a web based app. This API is driving me mad lol wish it was slightly easier to use tbh. – thatuxguy Jul 19 '12 at 14:52
  • unfortunately it seems that the C# api was an after thought. The Java API is alot better documented, so if you run into a road block, take a look at the java examples; most of the time Java and C# are similar enough to give you an idea on where to go. – Robert H Jul 19 '12 at 14:53
  • thats cool thanks for that :D I will have a look at them later too :) – thatuxguy Jul 19 '12 at 15:12
  • do you know if the shipping address returned is also the billing address, if the billing address is the same? – thatuxguy Jul 19 '12 at 15:29
  • They can be two different addresses. I have found that > 90% of the time that they are the same, but code for both cases. – Robert H Jul 19 '12 at 15:32
  • shame they dont have billing address, and shipping adress on the list orders. As they can be different :( mmm i will look into this. – thatuxguy Jul 19 '12 at 15:34
  • Looks like you need to run a report to get the billing and shipping details. However on my test this did not work lol – thatuxguy Jul 20 '12 at 08:42
  • I can post my reports enum and what I do for missing data if you want to post another question. – Robert H Jul 20 '12 at 12:27