public ActionResult CompleteOrder(string tx, decimal amt) { string response = TransactionDataRequest(tx); var profile = HttpContext.Profile; var shoppingCart = profile.GetPropertyValue("ShoppingCart") as ShoppingCart; if (shoppingCart != null && shoppingCart.Total == amt) { var order = new Order { AddedBy = User.Identity.Name, DateAdded = DateTime.Now, CustomerEmail = ExtractFromResponse(response, "payer_email"), Shipping = shoppingCart.ShippingPrice, ShippingCity = ExtractFromResponse(response, "address_city"), ShippingFirstName = ExtractFromResponse(response, "first_name"), ShippingLastName = ExtractFromResponse(response, "last_name"), ShippingMethod = shoppingCart.ShippingMethod.Title, ShippingStreet = ExtractFromResponse(response, "address_street"), ShippingState = ExtractFromResponse(response, "address_state"), ShippingZipCode = ExtractFromResponse(response, "address_zip"), Status = "Order Received", TransactionId = tx, SubTotal = shoppingCart.SubTotal }; order.Items=new List<OrderItem>(); var productIds = shoppingCart.Select(i => i.Item.Id); using (var repository = _repositoryProvider.GetRepository()) { //var items = repository.Get<Product>().Where(p => productIds.Any(pId=>pId==p.Id)).OrderBy(p => p.Title).ToList(); var items = new List<Product>(); productIds.Each(id => items.Add(repository.Get<Product>().Where(p => p.Id == id).FirstOrDefault())); shoppingCart.ForEach( p => { order.Items.Add(new OrderItem { AddedBy = User.Identity.Name, DateAdded = DateTime.Now, Order = order, ProductId = p.Item.Id, Quantity = p.Quantity, UnitPrice = p.Item.UnitPrice, SKU = p.Item.SKU, Title=p.Item.Title }); items.Each(i => { i.UnitsInStock -= p.Quantity; repository.Save(i); }); }); repository.Save(order); } profile.SetPropertyValue("ShoppingCart",new ShoppingCart()); ViewBag.OrderNumber = order.Id; ViewBag.PageTitle = "Order Received"; return View(); } return View("TransactionError"); }
public void Orders_Are_Composed_Of_Order_Items() { var orderItem1 = new OrderItem { DateAdded = DateTime.Now.AddMinutes(-30), AddedBy = "shopper1", ProductId = 4, Title = "Cookbook", SKU = "sku1", UnitPrice = 10.0M, Quantity = 10 }; var orderItem2 = new OrderItem { DateAdded = DateTime.Now.AddMinutes(-30), AddedBy = "shopper1", ProductId = 5, Title = "Apple Pie", SKU = "sku11", UnitPrice = 11.11M, Quantity = 10 }; var order = new Order { DateAdded = DateTime.Now.AddHours(-1), AddedBy = "shopper1", Status = "pending", ShippingMethod = "Overnight", SubTotal = 33.23M, Shipping = 11.33M, ShippingFirstName = "John", ShippingLastName = "Doe", ShippingStreet = "123 Main", ShippingZipCode = "11234", ShippingCity = "Springfield", ShippingState = "OH", CustomerEmail = "*****@*****.**", ShippedDate = null, TransactionId = "trxn007", TrackingId = "ax1234", Items = new List<OrderItem> {orderItem1,orderItem2 } }; orderItem1.Order = order; orderItem2.Order = order; using (var trx=_session.BeginTransaction()) { new PersistenceSpecification<Order>(_session,new PocoComparer()).TransactionalSave(order); var savedOrder = _session.Get<Order>(order.Id); Assert.That(savedOrder.Items.Count,Is.EqualTo(2)); savedOrder.Items.Remove(orderItem2); _session.Save(savedOrder); savedOrder = _session.Get<Order>(order.Id); Assert.That(savedOrder.Items.Count, Is.EqualTo(1)); } }