public static OrdersOrder ToShipstationOrder(this VirtoCommerce.Domain.Order.Model.CustomerOrder order) { if (order.Shipments != null && order.Shipments.Any()) { var retVal = new OrdersOrder { OrderNumber = order.Number, OrderID = order.Id, OrderStatus = order.Status, OrderDate = String.Format("{0:MM'/'dd'/'yyyy HH:mm}", order.CreatedDate), LastModified = String.Format("{0:MM'/'dd'/'yyyy HH:mm}", order.ModifiedDate), OrderTotal = (float)order.Sum, ShippingAmount = (float)order.Shipments.Sum(sh => sh.Sum), TaxAmount = (float)order.Tax, ShippingMethod = order.Shipments.First().ShipmentMethodCode, ShippingAmountSpecified = true, PaymentMethod = order.InPayments.First().GatewayCode }; var items = new List <OrdersOrderItem>(); order.Shipments.Where(s => s.Items != null && s.Items.Any()).ForEach(sh => { sh.Items.ForEach(shi => { var item = new OrdersOrderItem { SKU = shi.LineItem.ProductId, ImageUrl = shi.LineItem.ImageUrl, LineItemID = shi.LineItemId, Name = shi.LineItem.Name, Quantity = (sbyte)shi.Quantity, UnitPrice = (float)order.Items.Single(i => i.ProductId == shi.LineItem.ProductId).Price, Weight = (float)(order.Items.Single(i => i.ProductId == shi.LineItem.ProductId).Weight ?? 0), WeightSpecified = order.Items.Single(i => i.ProductId == shi.LineItem.ProductId).Weight != null, WeightUnits = order.Items.Single(i => i.ProductId == shi.LineItem.ProductId).WeightUnit, }; items.Add(item); }); retVal.Items = items.ToArray(); }); var customer = new OrdersOrderCustomer { CustomerCode = order.CustomerId }; var billAddress = order.Addresses.FirstOrDefault( a => a.AddressType == AddressType.Billing || a.AddressType == AddressType.BillingAndShipping); if (billAddress != null) { var billTo = new OrdersOrderCustomerBillTo { Company = billAddress.Organization, Name = billAddress.FirstName + " " + billAddress.LastName, Phone = billAddress.Phone }; customer.BillTo = billTo; } var shipAddress = order.Addresses.FirstOrDefault( a => a.AddressType == AddressType.Shipping || a.AddressType == AddressType.BillingAndShipping); if (shipAddress != null) { var shipTo = new OrdersOrderCustomerShipTo { Company = shipAddress.Organization, Name = shipAddress.FirstName + " " + shipAddress.LastName, Phone = shipAddress.Phone, Address1 = shipAddress.Line1, City = shipAddress.City, PostalCode = shipAddress.PostalCode, Country = shipAddress.CountryCode, State = shipAddress.RegionId ?? shipAddress.RegionName }; customer.ShipTo = shipTo; } retVal.Customer = customer; return(retVal); } return(null); }
//[Fact] public void TestSerialization() { const string dateFormat = "{0:MM'/'dd'/'yyyy HH:mm:ss tt}"; var billAddress = new OrdersOrderCustomerBillTo { Company = "Home", Email = "*****@*****.**", Name = "Test Person" }; var shipAddress = new OrdersOrderCustomerShipTo { Address1 = "45 Fremont street, 2", City = "Los Angeles", Company = "Home", Country = "US", Name = "Test Person", PostalCode = "91311", State = "California" }; var customer = new OrdersOrderCustomer { CustomerCode = "testCustomer", BillTo = billAddress, ShipTo = shipAddress }; var order = new OrdersOrder { OrderID = "1234567890", OrderNumber = "CU123456789", OrderDate = String.Format(dateFormat, DateTime.UtcNow), LastModified = String.Format(dateFormat, DateTime.UtcNow), OrderStatus = OrderStatusEnum.AwaitingShipment.ToString(), Customer = customer, OrderTotal = (float)111.2, TaxAmount = (float)11.2, ShippingAmount = (float)3.02, ShippingMethod = "USPS" }; var value = new Orders { Order = new[] { order }, pages = 5, pagesSpecified = true }; var xml = new XmlMediaTypeFormatter { UseXmlSerializer = true };; xml.WriterSettings.OmitXmlDeclaration = true; var str = Serialize(xml, value); //IsValidXml(str); //var json = new JsonMediaTypeFormatter(); //str = Serialize(json, value); // Round trip //var orders = Deserialize<Order>(xml, str); }