コード例 #1
0
        public void OrderInsert()
        {
            CoreFaces.Order.Models.Domain.Order order = new Models.Domain.Order {
                StatusId = 2, UserId = Guid.NewGuid()
            };
            List <Models.Domain.OrderItem> orderItems = new List <Models.Domain.OrderItem>();

            _orderDatabaseContext.Database.BeginTransaction();
            try
            {
                Guid result = _orderService.Save(order);

                orderItems.Add(new Models.Domain.OrderItem {
                    ProductId = Guid.NewGuid(), Price = 5, StatusId = 2, Quantity = 1, ProductName = "Ürün Adý", StockCode = "001", Vat = 18, Currency = Helper.Enums.Currency.TL, OrderId = order.Id
                });
                orderItems.Add(new Models.Domain.OrderItem {
                    ProductId = Guid.NewGuid(), Price = 2, StatusId = 2, Quantity = 2, ProductName = "Ürün Adý 2", StockCode = "002", Vat = 18, Currency = Helper.Enums.Currency.TL, OrderId = order.Id
                });
                foreach (var item in orderItems)
                {
                    _orderItemService.Save(item);
                }

                CoreFaces.Order.Models.Domain.OrderAddress orderAddress = new Models.Domain.OrderAddress {
                    StatusId = 2, UserId = Guid.NewGuid(), Name = "Hüseyin", Surname = "altunbaþ", MobilePhone = "5335299862", OrderId = order.Id
                };
                _orderAddressService.Save(orderAddress);
                _orderDatabaseContext.Database.CommitTransaction();
            }
            catch (Exception ex)
            {
                _orderDatabaseContext.Database.RollbackTransaction();
                throw;
            }
        }
コード例 #2
0
        public bool Update(Models.Domain.Order model)
        {
            _orderDatabaseContext.Update(model);
            int result = _orderDatabaseContext.SaveChanges();

            return(Convert.ToBoolean(result));
        }
コード例 #3
0
        public bool Delete(Guid id)
        {
            Models.Domain.Order model = this.GetById(id);
            _orderDatabaseContext.Remove(model);
            int result = _orderDatabaseContext.SaveChanges();

            return(Convert.ToBoolean(result));
        }
コード例 #4
0
        public IActionResult CheckOut(CheckOutModel model)
        {
            // Kiểm tra nếu chưa đăng nhập, trả về trang login
            if (HttpContext.Session.GetInt32("UserID") == null)
            {
                return(RedirectToAction("Login", "Accounts"));
            }

            // Nếu chưa nhập đủ sđt và địa chỉ thì quay lại
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index", model));
            }

            // Tính % còn lại sau khi sale
            double sale = 1;

            if (CheckDiscount(model.DiscountCode) != -1)
            {
                sale = (100 - CheckDiscount(model.DiscountCode)) / 100;
            }

            var cart = SessionHelper.GetObjectFromJson <List <ProductToCart> >(HttpContext.Session, "cart");

            HttpContext.Session.Remove("cart");

            var order = new Models.Domain.Order()
            {
                DateTime   = DateTime.UtcNow,
                UserId     = HttpContext.Session.GetInt32("UserID").GetValueOrDefault(),
                TotalPrice = cart.Sum(item => item.ProductModel.ProductPrice * item.Quantity) * sale,
                Address    = model.Address,
                Phone      = model.PhoneNumber
            };

            // Kiem tra xem phuong thuc thanh toan nao
            order.PaymentMethod = model.PaymentMethod == 0 ? "Ship COD" : "Paypal";

            foreach (var item in cart)
            {
                var orderDetail = new OrderDetail()
                {
                    Quantity  = item.Quantity,
                    Amount    = item.Quantity * item.ProductModel.ProductPrice,
                    ProductId = item.ProductModel.ProductId
                };

                order.OrderDetails.Add(orderDetail);

                var product = _DataContext.Products.FirstOrDefault(x => x.ProductId == item.ProductModel.ProductId);

                if (product != null)
                {
                    if (product.ProducQuantity < item.Quantity)
                    {
                        return(View("OutOfStock.cshtml", product));
                    }

                    product.ProducQuantity -= item.Quantity;

                    _DataContext.Products.Update(product);
                }
            }

            var createdOrder = _DataContext.Orders.Add(order);

            _DataContext.SaveChanges();

            if (model.PaymentMethod != 0)
            {
                model.OrderId = createdOrder.Entity.OrderId;

                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);

                return(RedirectToAction("PaypalPayment", model));
            }

            return(RedirectToAction("PaymentSuccess", new { orderId = createdOrder.Entity.OrderId.ToString() }));
        }
コード例 #5
0
 public Guid Save(Models.Domain.Order model)
 {
     model.OrderNumber = GenerateOrderNumber();
     _orderRepository.Save(model);
     return(model.Id);
 }
コード例 #6
0
 public Models.Domain.Order GetProductId(Guid Id, Guid apiUserId)
 {
     Models.Domain.Order model = _orderRepository.GetById(Id);
     return(model);
 }
コード例 #7
0
 public bool Update(Models.Domain.Order model)
 {
     return(_orderRepository.Update(model));
 }
コード例 #8
0
 public Guid Save(Models.Domain.Order product)
 {
     _orderDatabaseContext.Add(product);
     _orderDatabaseContext.SaveChanges();
     return(product.Id);
 }
コード例 #9
0
 public Models.Domain.Order GetById(Guid id)
 {
     Models.Domain.Order model = _orderDatabaseContext.Set <Models.Domain.Order>().Where(p => p.Id == id).FirstOrDefault();
     return(model);
 }
コード例 #10
0
        public ActionResult PaymentWithPaypal(int?phoneNumber, string email, string address)
        {
            List <Models.Item> cart = (List <Models.Item>)Session["cart"];

            if (phoneNumber != null)
            {
                if (User.Identity.IsAuthenticated)
                {
                    //Orders
                    var acc   = db.account.FirstOrDefault(x => x.UserName == User.Identity.Name);
                    var order = new Models.Domain.Order();
                    order.DateTimeOrder    = DateTime.Now;
                    order.Status           = null;
                    order.UserID           = acc.UserID;
                    order.OrderPhoneNumber = phoneNumber;
                    order.Address          = address;
                    order.Email            = email;
                    order.TypePayment      = 3;
                    db.order.Add(order);
                    db.SaveChanges();

                    //OrderDetail
                    foreach (var item in cart)
                    {
                        OrderDetail orderDetail = new OrderDetail
                        {
                            QuantityBuy = item.QuantityBuy,
                            Price       = item.QuantityBuy * item.product.Price,
                            OrderID     = order.OrderID,
                            ProductID   = item.product.ProductID,
                        };
                        db.orderDetail.Add(orderDetail);
                        db.SaveChanges();

                        //giảm số lượng sp sau khi đặt
                        var product = db.product.FirstOrDefault(x => x.ProductID == item.product.ProductID);
                        product.Quantity        = product.Quantity - item.QuantityBuy;
                        db.Entry(product).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                    }
                }
                else
                {
                    //Orders
                    var order = new Models.Domain.Order();
                    order.DateTimeOrder    = DateTime.Now;
                    order.Status           = null;
                    order.UserID           = null;
                    order.OrderPhoneNumber = phoneNumber;
                    order.Address          = address;
                    order.Email            = email;
                    order.TypePayment      = 3;
                    db.order.Add(order);
                    db.SaveChanges();

                    //OrderDetail
                    foreach (var item in cart)
                    {
                        OrderDetail orderDetail = new OrderDetail
                        {
                            QuantityBuy = item.QuantityBuy,
                            Price       = item.QuantityBuy * item.product.Price,
                            OrderID     = order.OrderID,
                            ProductID   = item.product.ProductID,
                        };
                        db.orderDetail.Add(orderDetail);
                        db.SaveChanges();

                        //giảm số lượng sp sau khi đặt
                        var product = db.product.FirstOrDefault(x => x.ProductID == item.product.ProductID);
                        product.Quantity        = product.Quantity - item.QuantityBuy;
                        db.Entry(product).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                    }
                }
            }
            APIContext apiContext = PaypalConfiguration.GetAPIContext();

            try
            {
                string payerId = Request.Params["PayerID"];
                if (string.IsNullOrEmpty(payerId))
                {
                    string baseURI        = Request.Url.Scheme + "://" + Request.Url.Authority + "/Cart/PaymentWithPaypal?";
                    var    guid           = Convert.ToString((new Random()).Next(100000));
                    var    createdPayment = CreatePayment(apiContext, baseURI + "guid=" + guid);

                    var    links             = createdPayment.links.GetEnumerator();
                    string paypalRedirectUrl = string.Empty;

                    while (links.MoveNext())
                    {
                        Links link = links.Current;
                        if (link.rel.ToLower().Trim().Equals("approval_url"))
                        {
                            paypalRedirectUrl = link.href;
                        }
                    }
                    Session.Add(guid, createdPayment.id);

                    return(Redirect(paypalRedirectUrl));
                }
                else
                {
                    var guid           = Request.Params["guid"];
                    var executePayment = ExecutePayment(apiContext, payerId, Session[guid] as string);
                    if (executePayment.state.ToLower() != "approved")
                    {
                        cart.Clear();
                        return(View("Failure"));
                    }
                }
            }
            catch (Exception e)
            {
                PaypalLogger.Log("Error: " + e.Message);
                cart.Clear();
                return(View("Failure"));
            }
            cart.Clear();
            return(View("Success"));
        }
コード例 #11
0
        //Đặt order sản phẩm
        public ActionResult CheckOut(int?phoneNumber, string email, string address, int typePay, int?codeDiscount)
        {
            List <Models.Item> cart = (List <Models.Item>)Session["cart"];
            var discount            = db.discounts.FirstOrDefault(x => x.EventID == codeDiscount);

            if (discount == null)
            {
            }
            else
            {
                discount.Quantity       -= 1;
                db.Entry(discount).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }
            //Kiểm tra nếu người dùng có tài khoản thêm sp vào giỏ hàng
            if (User.Identity.IsAuthenticated)
            {
                //Orders
                var acc   = db.account.FirstOrDefault(x => x.UserName == User.Identity.Name);
                var order = new Models.Domain.Order();
                order.DateTimeOrder    = DateTime.Now;
                order.Status           = null;
                order.UserID           = acc.UserID;
                order.OrderPhoneNumber = phoneNumber;
                order.Address          = address;
                order.Email            = email;
                order.TypePayment      = typePay;
                db.order.Add(order);
                db.SaveChanges();

                //OrderDetail
                foreach (var item in cart)
                {
                    OrderDetail orderDetail = new OrderDetail
                    {
                        QuantityBuy = item.QuantityBuy,
                        Price       = item.QuantityBuy * item.product.Price,
                        OrderID     = order.OrderID,
                        ProductID   = item.product.ProductID,
                    };
                    db.orderDetail.Add(orderDetail);
                    db.SaveChanges();

                    //giảm số lượng sp sau khi đặt
                    var product = db.product.FirstOrDefault(x => x.ProductID == item.product.ProductID);
                    product.Quantity        = product.Quantity - item.QuantityBuy;
                    db.Entry(product).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
                cart.Clear();
                Session["cart"] = cart;
                if (typePay == 1)
                {
                    return(View("Success"));
                }
                else
                {
                    return(RedirectToAction("Momo", "Cart", new { id = order.OrderID }));
                }
            }
            //Nếu khách hàng không có tài khoản thêm vào giỏ hàng
            else
            {
                //Orders
                var order = new Models.Domain.Order();
                order.UserID           = null;
                order.DateTimeOrder    = DateTime.Now;
                order.Status           = null;
                order.OrderPhoneNumber = phoneNumber;
                order.Address          = address;
                order.Email            = email;
                order.TypePayment      = typePay;
                db.order.Add(order);
                db.SaveChanges();
                //OrderDetail
                foreach (var item1 in cart)
                {
                    OrderDetail orderDetail = new OrderDetail
                    {
                        QuantityBuy = item1.QuantityBuy,
                        Price       = item1.QuantityBuy * item1.product.Price,
                        OrderID     = order.OrderID,
                        ProductID   = item1.product.ProductID,
                    };
                    db.orderDetail.Add(orderDetail);
                    db.SaveChanges();

                    //giảm số lượng sp sau khi đặt
                    var product = db.product.FirstOrDefault(x => x.ProductID == item1.product.ProductID);
                    product.Quantity        = product.Quantity - item1.QuantityBuy;
                    db.Entry(product).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
                cart.Clear();
                Session["cart"] = cart;
                if (typePay == 1)
                {
                    return(View("Success"));
                }
                else
                {
                    return(RedirectToAction("Momo", "Cart", new { id = order.OrderID }));
                }
            }
        }