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.
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;
}