Ejemplo n.º 1
0
        public bool ConfirmOrdersShipment()
        {
            // init the Amazon order API
            var config = new MarketplaceWebServiceConfig {
                ServiceURL = "https://mws.amazonservices.com"
            };

            config.SetUserAgentHeader(_ApplicationName, _Version, "C#");
            var serviceClient        = new MarketplaceWebServiceClient(_credential.AccessKeyId, _credential.SecretKey, config);
            var orderFulfillmentList = new List <OrderFulfillment>();

            // get the unshipped orders with tracking number for confirming its shipment
            var unshippedOrderFeeds = _orderRepository.GetUnshippedOrdersForShipment(ChannelName);

            if (!unshippedOrderFeeds.Any())
            {
                Console.WriteLine("No unshipped orders found from {0} for shipment confirmation.", ChannelName);
                return(true);
            }

            try
            {
                Console.WriteLine("Sending {0} orders for shipment confirmation...", unshippedOrderFeeds.Count);

                // create the order fulfillment
                unshippedOrderFeeds.ForEach(order =>
                {
                    // create fulfillment item list from the order items
                    var fulfillmentItems = new List <OrderFulfillmentItem>();
                    foreach (var item in order.OrderItems)
                    {
                        fulfillmentItems.Add(new OrderFulfillmentItem
                        {
                            Item     = item.OrderItemId,
                            Quantity = item.Quantity.ToString()
                        });
                    }

                    // then, the order fulfillment information
                    orderFulfillmentList.Add(new OrderFulfillment
                    {
                        Item            = order.OrderId,
                        FulfillmentDate = order.FulfillmentDate,
                        FulfillmentData = new OrderFulfillmentFulfillmentData
                        {
                            Item                  = order.Carrier.Code,
                            ShippingMethod        = "Ground", // order.ShippingMethod,
                            ShipperTrackingNumber = order.ShipperTrackingNumber
                        },
                        Item1 = fulfillmentItems.ToArray()
                    });
                });

                // iterate to the order fulfillment and add it into envelope message
                var envelopeMessages = new List <AmazonEnvelopeMessage>();
                for (var i = 0; i < orderFulfillmentList.Count; i++)
                {
                    var message = new AmazonEnvelopeMessage
                    {
                        MessageID = string.Format("{0}", i + 1),
                        Item      = orderFulfillmentList[i]
                    };
                    envelopeMessages.Add(message);
                }

                // create Amazon envelope object
                var amazonEnvelope = new AmazonEnvelope
                {
                    Header = new Header {
                        DocumentVersion = "1.01", MerchantIdentifier = _credential.MerchantId
                    },
                    MessageType = AmazonEnvelopeMessageType.OrderFulfillment,
                    Message     = envelopeMessages.ToArray()
                };

                // let's add these orders to the shipment history
                addOrderShipmentHistoryAsync(unshippedOrderFeeds);

                // parse the envelope into file
                var xmlFullName = XmlParser.WriteXmlToFile(amazonEnvelope, "AmazonOrderFulfillment");

                // create feed controller and send the confirmation shipment feed
                var submitController = new SubmitFeedController(serviceClient, _logger, _credential.MarketplaceId, _credential.MerchantId, _ApplicationName);
                var streamResponse   = submitController.SubmitFeedAndGetResponse(xmlFullName, AmazonFeedType._POST_ORDER_FULFILLMENT_DATA_);
                parsedResultStreamAndLogReport(streamResponse, AmazonEnvelopeMessageType.OrderFulfillment, _ApplicationName);

                _logger.LogInfo(LogEntryType.AmazonOrdersProvider,
                                string.Format("{1}:{2} - Successfully sent confirming order shipment for Order IDs: \"{0}\"", string.Join("\", \"", unshippedOrderFeeds.Select(x => x.OrderId)), ChannelName, _ApplicationName, Constants.APP_NAME));
                Console.WriteLine("Successfully sent confirming order shipment for Order IDs: \"{0}\"", string.Join("\", \"", unshippedOrderFeeds.Select(x => x.OrderId)));

                return(true);
            }
            catch (Exception ex)
            {
                _logger.LogError(LogEntryType.AmazonOrdersProvider,
                                 string.Format("{0}:{4} Error in confirming order shipment for for Order IDs: \"{3}\". <br/>Error Message: {1} <br/> Access Key: {2}", ChannelName,
                                               (ex.InnerException != null ? string.Format("{0} <br/>Inner Message: {1}",
                                                                                          ex.Message, ex.InnerException.Message) : ex.Message),
                                               _credential.AccessKeyId,
                                               string.Join("\", \"", unshippedOrderFeeds.Select(x => x.OrderId)),
                                               _ApplicationName),
                                 ex.StackTrace);
                Console.WriteLine("Error in sending shipment confirmation! Error Message: {0} \nStackTrace: {1} ", ex.Message, ex.StackTrace);

                // let's delete the order confirmation
                deleteOrderShipmentHistoryAsync(unshippedOrderFeeds);

                return(false);
            }
        }