/// <summary> /// Converts a list of InventoryUpdateItems to a memory stream. /// </summary> /// <param name="items">The list of items that are to have their quantities updated</param> /// <returns>An open memeory stream. This must be closed or handled properly by callers</returns> private MemoryStream ConvertToStream(IEnumerable <Contracts.MarketplaceInventoryUpdateItem> items) { var stream = new MemoryStream(); XmlWriter xmlWriter = XmlWriter.Create(stream); XmlDocument doc = AmazonXmlHelper.CreateXmlDocForRequest(Settings.Default.AWSMerchantToken, "Inventory"); for (var i = 0; i < items.Count(); ++i) { // Each item will have a message envelope, with the id, op type and then the actual data in the inventory element. // see page 43 for an example from Amazon and the XSD. https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/XML_Documentation_Intl._V144967332_.pdf var msg = doc.LastChild.AppendChild(doc.CreateElement("Message")); msg.AppendChild(doc.CreateElement("MessageID")).InnerText = (i + 1).ToString(); //msg.AppendChild(doc.CreateElement("OperationType")).InnerText = "Update"; var inventory = msg.AppendChild(doc.CreateElement("Inventory")); inventory.AppendChild(doc.CreateElement("SKU")).InnerText = items.ElementAt(i).SKU; inventory.AppendChild(doc.CreateElement("Quantity")).InnerText = items.ElementAt(i).Quantity.ToString(); } doc.WriteTo(xmlWriter); xmlWriter.Close(); // Reset the position to 0 for reading. stream.Position = 0; // If the size of the stream exceeds 1 GB, then throw an exception. if (stream.Length > 1073741824) { var length = stream.Length; stream.Close(); throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "The xml to be sent exceeds 1 GB. ({0}). Please reduce the size of this call or re-work the code to create a file and return a FileStream.", length)); } return(stream); }
/// <summary> /// Converts a list of MarketplaceOrderFulfillments to a memory stream. /// </summary> /// <param name="orders">The list of orders that are to be filled</param> /// <returns>An open memeory stream. This must be closed or handled properly by callers</returns> private MemoryStream ConvertToStream(IEnumerable <Contracts.MarketplaceOrderFulfillment> orders) { var stream = new MemoryStream(); XmlWriter xmlWriter = XmlWriter.Create(stream); XmlDocument doc = AmazonXmlHelper.CreateXmlDocForRequest(Settings.Default.AWSMerchantToken, "OrderFulfillment"); for (var i = 0; i < orders.Count(); ++i) { // Each item will have a message envelope, with the id, op type and then the actual data in the inventory element. // see page 43 for an example from Amazon and the XSD. https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/XML_Documentation_Intl._V144967332_.pdf var msg = doc.LastChild.AppendChild(doc.CreateElement("Message")); msg.AppendChild(doc.CreateElement("MessageID")).InnerText = (i + 1).ToString(); //msg.AppendChild(doc.CreateElement("OperationType")).InnerText = "Update"; var orderFulfillment = msg.AppendChild(doc.CreateElement("OrderFulfillment")); orderFulfillment.AppendChild(doc.CreateElement("AmazonOrderID")).InnerText = orders.ElementAt(i).MarketplaceOrderId; //orderFulfillment.AppendChild(doc.CreateElement("MerchantFulfillmentID")).InnerText = Seller supplied value, not used right now // http://msdn.microsoft.com/en-us/library/az4se3k1.aspx#Sortable // and https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/XML_Documentation_Intl.pdf on page 65. It'll use local time zones. orderFulfillment.AppendChild(doc.CreateElement("FulfillmentDate")).InnerText = orders.ElementAt(i).FulfillmentDate.ToString("s"); var fulfillmentData = orderFulfillment.AppendChild(doc.CreateElement("FulfillmentData")); fulfillmentData.AppendChild(doc.CreateElement("CarrierCode")).InnerText = orders.ElementAt(i).Carrier.CarrierCode; fulfillmentData.AppendChild(doc.CreateElement("ShippingMethod")).InnerText = orders.ElementAt(i).ShippingMethod; fulfillmentData.AppendChild(doc.CreateElement("ShipperTrackingNumber")).InnerText = orders.ElementAt(i).ShipperTrackingNumber; foreach (var orderItem in orders.ElementAt(i).Items) { var item = orderFulfillment.AppendChild(doc.CreateElement("Item")); item.AppendChild(doc.CreateElement("AmazonOrderItemCode")).InnerText = orderItem.MarketplaceOrderItemId; item.AppendChild(doc.CreateElement("Quantity")).InnerText = orderItem.Quantity.ToString(); //item.AppendChild(doc.CreateElement("MerchantFulfillmentItemID")).InnerText = orderItem.SKU; The SKU is not for this. It's looking for some ID. } } doc.WriteTo(xmlWriter); xmlWriter.Close(); // Reset the position to 0 for reading. stream.Position = 0; // If the size of the stream exceeds 1 GB, then throw an exception. if (stream.Length > 1073741824) { var length = stream.Length; stream.Close(); throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "The xml to be sent exceeds 1 GB. ({0}). Please reduce the size of this call or re-work the code to create a file and return a FileStream.", length)); } return(stream); }
/// <summary> /// Converts a list of MarketplaceOrderFulfillments to a memory stream. /// </summary> /// <param name="orders">The list of orders that are to be filled</param> /// <returns>An open memeory stream. This must be closed or handled properly by callers</returns> private Stream ConvertToStream(IEnumerable <Contracts.MarketplaceOrderRefund> orders) { var stream = new MemoryStream(); XmlWriter xmlWriter = XmlWriter.Create(stream); XmlDocument doc = AmazonXmlHelper.CreateXmlDocForRequest(Settings.Default.AWSMerchantToken, "OrderAdjustment"); for (var i = 0; i < orders.Count(); ++i) { // Each item will have a message envelope, with the id, op type and then the actual data in the inventory element. // see page 43 for an example from Amazon and the XSD. https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/XML_Documentation_Intl._V144967332_.pdf var msg = doc.LastChild.AppendChild(doc.CreateElement("Message")); msg.AppendChild(doc.CreateElement("MessageID")).InnerText = (i + 1).ToString(); //msg.AppendChild(doc.CreateElement("OperationType")).InnerText = "Update"; var orderAdjustment = msg.AppendChild(doc.CreateElement("OrderAdjustment")); orderAdjustment.AppendChild(doc.CreateElement("AmazonOrderID")).InnerText = orders.ElementAt(i).MarketplaceOrderId; foreach (var orderItem in orders.ElementAt(i).Items) { var item = orderAdjustment.AppendChild(doc.CreateElement("AdjustedItem")); item.AppendChild(doc.CreateElement("AmazonOrderItemCode")).InnerText = orderItem.MarketplaceOrderItemId; item.AppendChild(doc.CreateElement("AdjustmentReason")).InnerText = AmazonXmlHelper.ConvertToString(orderItem.RefundReason); var priceAdjustment = item.AppendChild(doc.CreateElement("ItemPriceAdjustments")); CreateItemPriceAdjustmentXml(doc, "Principal", orderItem.RefundPrice, priceAdjustment); CreateItemPriceAdjustmentXml(doc, "Shipping", orderItem.RefundShipping, priceAdjustment); CreateItemPriceAdjustmentXml(doc, "Tax", orderItem.RefundTax, priceAdjustment); CreateItemPriceAdjustmentXml(doc, "ShippingTax", orderItem.RefundShippingTax, priceAdjustment); CreateItemPriceAdjustmentXml(doc, "ReturnShipping", orderItem.ReturnShipping, priceAdjustment); // Put the quantity cancelled after the prices item.AppendChild(doc.CreateElement("QuantityCancelled")).InnerText = orderItem.QuantityCancelled.ToString(); } } doc.WriteTo(xmlWriter); xmlWriter.Close(); // Reset the position to 0 for reading. stream.Position = 0; // If the size of the stream exceeds 1 GB, then throw an exception. if (stream.Length > 1073741824) { var length = stream.Length; stream.Close(); throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "The xml to be sent exceeds 1 GB. ({0}). Please reduce the size of this call or re-work the code to create a file and return a FileStream.", length)); } return(stream); }