Ejemplo n.º 1
0
  public ActionResult EditOrderPaymentInfo(PaymentInfo revisedPaymentInfo)
  {
 
      Order order = dbContext.Orders.FirstOrDefault(o => o.Id == revisedPaymentInfo.OrderId);
      if (order == null)
      {
          TempData["Message"] = "Invalid Order Id!";
          return RedirectToAction("Index");
      }
      if (ModelState.IsValid)
      {
          dbContext.PaymentInfoes.Attach(revisedPaymentInfo);
          dbContext.Entry(revisedPaymentInfo).State = EntityState.Modified;
          dbContext.SaveChanges();
          TempData["Message"] = "Payment Information Updated";
          return RedirectToAction("OrderDetails", new { controller = "Manage", area = "", id = revisedPaymentInfo.OrderId });
      }
      return View(revisedPaymentInfo);
  }
Ejemplo n.º 2
0
        public ActionResult PlaceOrder()
        {
            CheckoutModel checkoutModel = GetCheckoutModel();
            ShoppingCart  cart = ShoppingCart.GetCart(this.HttpContext);
            Order order = new Order();
            if (checkoutModel.Step1 == null)
            {
                return RedirectToAction("CheckoutStep1");
            }
            if (checkoutModel.Step2 == null)
            {
                return RedirectToAction("CheckoutStep2");
            }
            if (checkoutModel.Step3 == null)
            {
                return RedirectToAction("CheckoutStep3");
            }
            order.UserName = User.Identity.Name;
            order.OrderDate = DateTime.Now.Date;
            order.OrderNumber = CreateOrderNumber();
            order.OrderStatus = "Received";
            order.CustomerBillingAddress = dbContext.CustomerAddresses.FirstOrDefault(a => a.Id == checkoutModel.Step1.BillingAddressId);
            order.CustomerShippingAddress = dbContext.CustomerAddresses.FirstOrDefault(a => a.Id == checkoutModel.Step2.ShippingAddressId);
            dbContext.Orders.Add(order);
            foreach (var cartItem in cart.GetCartItems())
            {
                if (!CheckStock(cartItem.ProductId, cartItem.Count))
                {
                    TempData["message"] = "Requested quantity not available for Item: " + cartItem.Product.ProductName;
                    return RedirectToAction("Index");
                }
            }
            try
            {
                dbContext.SaveChanges();
            }
            catch
            {
                 return View("error", new string[] { "Database Error!" });
            }
            Order orderCreated = dbContext.Orders.FirstOrDefault(o => o.OrderNumber == order.OrderNumber);

            PaymentInfo paymentInfo = new PaymentInfo() {
                Order=orderCreated,

                Name=checkoutModel.Step3.Name,
                CreditCardNumber=checkoutModel.Step3.CreditCardNumber,
                ExpiredDate=checkoutModel.Step3.ExpiredDate,
                SecurityCode=checkoutModel.Step3.SecurityCode
            };
            dbContext.PaymentInfoes.Add(paymentInfo);
            var cartItems = cart.GetCartItems();

            foreach (var cartItem in cartItems)
            {

                OrderItem orderItem = new OrderItem()
                {
                    ProductId = cartItem.ProductId,
                    OrderId = orderCreated.Id,
                    Quantities = cartItem.Count
                };

                Product product = dbContext.Products.FirstOrDefault(p => p.Id == cartItem.ProductId);
                product.Stock -= cartItem.Count;
                dbContext.Entry(product).State = EntityState.Modified;
                dbContext.OrderItems.Add(orderItem);

            }
            try
            {
                dbContext.SaveChanges();
            }

               catch
            {

                dbContext.DeleteOrder(orderCreated.Id);
                return View("error", new string[] { "Database Error!" });
            }
            cart.EmptyCart();

            Session.Remove("Checkout");
            try
            {
                SendConfirmationEmail(orderCreated);
            }
            catch
            {
                return View("error", new string[] { "Sorry, we have problem to send confirmation email to you, please log into My Account to Track your order." });
            }
            return RedirectToAction("OrderConfirmation", new { id = orderCreated.Id });
        }