Ejemplo n.º 1
0
        public ActionResult checkout(string shipName, string shipEmail, string shipMobile, string shipAddress)
        {
            var order = new Order();

            order.CreateDate  = DateTime.Now;
            order.ShipAddress = shipAddress;
            order.ShipEmail   = shipEmail;
            order.ShipName    = shipName;
            order.ShipMoblie  = shipMobile;
            try
            {
                var     id        = new OderDao().Insert(order);
                var     cart      = (List <CartItem>)Session[CartSession];
                var     detailDao = new OderDetailDao();
                decimal total     = 0;
                foreach (var item in cart)
                {
                    var orderDetail = new OderDetail();
                    orderDetail.ProductID = item.Pro.ID;
                    orderDetail.OrderID   = id;
                    orderDetail.Price     = item.Pro.Price;
                    orderDetail.Quantity  = item.Quantity;
                    detailDao.Insert(orderDetail);
                    total += (item.Pro.Price.GetValueOrDefault(0) * item.Quantity);
                }
            }
            catch (Exception)
            {
                return(Redirect("/loi-thanh-toan"));
            }
            return(Redirect("/hoan-thanh"));
        }
        public async Task <IActionResult> Edit(int id, [Bind("OderID,Quantity,Gia,MaHH")] OderDetail oderDetail)
        {
            if (id != oderDetail.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(oderDetail);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!OderDetailExists(oderDetail.OderID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(oderDetail));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Quantity,price,OderId,ProductId")] OderDetail oderDetail)
        {
            if (id != oderDetail.OderId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(oderDetail);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!OderDetailExists(oderDetail.OderId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["OderId"]    = new SelectList(_context.oders, "Id", "Id", oderDetail.OderId);
            ViewData["ProductId"] = new SelectList(_context.products, "ID", "Color", oderDetail.ProductId);
            return(View(oderDetail));
        }
Ejemplo n.º 4
0
        public ActionResult DeleteConfirmed(int id)
        {
            OderDetail oderdetail = db.OderDetail.Find(id);

            db.OderDetail.Remove(oderdetail);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 5
0
        //
        // GET: /OrderDetail/Details/5

        public ActionResult Details(int id = 0)
        {
            OderDetail oderdetail = db.OderDetail.Find(id);

            if (oderdetail == null)
            {
                return(HttpNotFound());
            }
            return(View(oderdetail));
        }
        public async Task <IActionResult> Create([Bind("OderID,Quantity,Gia,MaHH")] OderDetail oderDetail)
        {
            if (ModelState.IsValid)
            {
                _context.Add(oderDetail);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(oderDetail));
        }
Ejemplo n.º 7
0
 public bool Insert1(OderDetail detail)
 {
     try {
         _context.OderDetails.Add(detail);
         _context.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Ejemplo n.º 8
0
        //
        // GET: /OrderDetail/Edit/5

        public ActionResult Edit(int id = 0)
        {
            OderDetail oderdetail = db.OderDetail.Find(id);

            if (oderdetail == null)
            {
                return(HttpNotFound());
            }
            ViewBag.orderId   = new SelectList(db.Order, "id", "id", oderdetail.orderID);
            ViewBag.productId = new SelectList(db.Product, "id", "name", oderdetail.productID);
            return(View(oderdetail));
        }
Ejemplo n.º 9
0
 public ActionResult Edit(OderDetail oderdetail)
 {
     if (ModelState.IsValid)
     {
         db.Entry(oderdetail).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.orderId   = new SelectList(db.Order, "id", "id", oderdetail.orderID);
     ViewBag.productId = new SelectList(db.Product, "id", "name", oderdetail.productID);
     return(View(oderdetail));
 }
Ejemplo n.º 10
0
 public bool Insert(OderDetail detail)
 {
     try
     {
         db.OderDetail.Add(detail);
         db.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Ejemplo n.º 11
0
        public async Task <IActionResult> Create([Bind("Id,Quantity,price,OderId,ProductId")] OderDetail oderDetail)
        {
            if (ModelState.IsValid)
            {
                _context.Add(oderDetail);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["OderId"]    = new SelectList(_context.oders, "Id", "Id", oderDetail.OderId);
            ViewData["ProductId"] = new SelectList(_context.products, "ID", "Color", oderDetail.ProductId);
            return(View(oderDetail));
        }
Ejemplo n.º 12
0
        public ActionResult Create(OderDetail oderdetail)
        {
            if (ModelState.IsValid)
            {
                db.OderDetail.Add(oderdetail);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.orderId   = new SelectList(db.Order, "id", "id", oderdetail.orderID);
            ViewBag.productId = new SelectList(db.Product, "id", "name", oderdetail.productID);
            return(View(oderdetail));
        }
Ejemplo n.º 13
0
 public bool Insert(OderDetail details)
 {
     try
     {
         db.OderDetail.Add(details);
         db.SaveChanges();
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Ejemplo n.º 14
0
 public bool Insert1(OderDetail detail)
 {
     try
     {
         _context.OderDetails.Add(detail);
         _context.SaveChanges();
         return(true);
     }
     catch (Exception e)
     {
         Console.Write(e);
         return(false);
     }
 }
Ejemplo n.º 15
0
        public ActionResult Payment(string name, string mobile, string address, string email)
        {
            //  tạo đối tượng Oder
            var oder = new Order();

            oder.ShipName    = name;
            oder.ShipMobie   = mobile;
            oder.ShipAddress = address;
            oder.ShipEmail   = email;
            oder.CreateDate  = DateTime.Now;
            try
            {
                var     oderid     = new OderDao().InsertOder(oder);
                var     cart       = (List <CarItem>)Session[CommonConstants.CartSession];
                var     oderDetail = new OderDetail();
                decimal total      = 0;
                foreach (var item in cart)
                {
                    var product = item.Product;
                    oderDetail.ProductID = product.ID;
                    oderDetail.OderID    = oderid;
                    oderDetail.Price     = product.Price;
                    oderDetail.Quantity  = item.Quantity;
                    var result = new OderDetailDao().Insert(oderDetail);
                    total += (item.Product.Price.GetValueOrDefault(0) * item.Quantity);
                }
                string content = System.IO.File.ReadAllText(Server.MapPath("~/assets/client/template/newOrder.html"));
                content = content.Replace("{{CustomerName}}", name);
                content = content.Replace("{{Phone}}", mobile);
                content = content.Replace("{{Email}}", email);
                content = content.Replace("{{Address}}", address);
                content = content.Replace("{{Total}}", total.ToString("N0"));
                var toEmail = ConfigurationManager.AppSettings["ToEmailAddress"].ToString();
                // Gữi  email xác  nhận  đã đặt mua  đến khách hàng
                new MailHelper().SendMail(email, "Đơn  đặt hàng của bạn trên OnlineShop", content);
                // Gữi email  thông báo  tới  quản trị  có đơn hàng
                new MailHelper().SendMail(toEmail, "Đơn  đặt hàng mới của khách hàng", content);
            }
            catch (Exception)
            {
                // ghi log
                return(Redirect("/loi-thanh-toan"));
            }

            return(Redirect("/hoan-thanh"));
        }
Ejemplo n.º 16
0
        public ActionResult Payment(string shipName, string mobile, string email, string address)
        {
            var order = new Order();

            order.CreateDate  = DateTime.Now;
            order.ShipAddress = address;
            order.ShipMoblie  = mobile;
            order.ShipName    = shipName;

            try
            {
                var     id        = new OrderDao().Insert(order);
                var     cart      = (List <CartItem>)Session[CartSection];
                var     detailDao = new Model.Dao.OrderDetailDao();
                decimal total     = 0;
                foreach (var item in cart)
                {
                    var orderDetail = new OderDetail();
                    orderDetail.ProductID = item.Product.ID;
                    orderDetail.OrderID   = id;
                    orderDetail.Price     = item.Product.Price;
                    orderDetail.Quanlity  = item.Quantity;
                    detailDao.Insert(orderDetail);


                    total += (item.Product.Price.GetValueOrDefault(0) * item.Quantity);
                }
                string content = System.IO.File.ReadAllText(Server.MapPath("~/assets/client/template/neworder.html"));

                content = content.Replace("{{CustomerName}}", shipName);
                content = content.Replace("{{Phone}}", mobile);
                content = content.Replace("{{Email}}", email);
                content = content.Replace("{{Address}}", address);
                content = content.Replace("{{Total}}", total.ToString("N0"));
                var toEmail = ConfigurationManager.AppSettings["ToEmailAddress"].ToString();

                new MailHelper().SendMail(email, "Đơn hàng mới từ CoffeeMaster", content);
                new MailHelper().SendMail(toEmail, "Đơn hàng mới từ CoffeeMaster", content);
            }
            catch (Exception ex)
            {
                //ghi log
                return(Redirect("/loi-thanh-toan"));
            }
            return(Redirect("/hoan-thanh"));
        }
Ejemplo n.º 17
0
        public IActionResult ThanhToan(string shipName, int mobile, string address, string email)
        {
            var model = _context.loais.ToList();

            ViewBag.model = model;

            var oder = new Oder();

            oder.CreatedDate = DateTime.Now;
            oder.ShipName    = shipName;
            oder.ShipAddress = address;
            oder.ShipMobile  = mobile;
            oder.ShipEmail   = email;

            try
            {
                var id   = Insert(oder);
                var cart = SessionHelper.Get <List <Item> >(HttpContext.Session, "cart");
                foreach (var item in cart)
                {
                    var oderDetail = new OderDetail();
                    oderDetail.MaHH     = item.Product.MaHH;
                    oderDetail.OderID   = id;
                    oderDetail.Gia      = item.Product.DonGia;
                    oderDetail.Quantity = item.Quantity;
                    Insert1(oderDetail);

                    var hanghoas = _context.HangHoas.Where(x => x.MaHH == item.Product.MaHH).First();


                    hanghoas.DaMua += item.Quantity;
                    _context.Update(hanghoas);
                    _context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
            }
            SessionHelper.Set(HttpContext.Session, "cart", "");
            return(View("HoanThanh", ViewBag.model));
        }
Ejemplo n.º 18
0
        public async Task <IActionResult> Mua(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var product = await _context.products
                          .Include(p => p.Brand)
                          .Include(p => p.Category)
                          .FirstOrDefaultAsync(m => m.ID == id);

            if (product == null)
            {
                return(NotFound());
            }
            Oder       oder       = new Oder();
            OderDetail oderDetail = new OderDetail();

            oder.OderDate   = DateTime.Now;
            oder.Status     = true;
            oder.TotalPrice = product.Price;
            oder.UserId     = HttpContext.Session.GetInt32("userId");
            _context.Add(oder);
            _context.SaveChanges();
            oderDetail.Quantity  = 1;
            oderDetail.price     = product.Price;
            oderDetail.OderId    = oder.Id;
            oderDetail.ProductId = product.ID;
            _context.Add(oderDetail);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));

            //return View(product);
        }
Ejemplo n.º 19
0
        public static bool addOderDt(ModelView.oderDetailView item)
        {
            db = new ShopOnlineEntities();
            try
            {
                OderDetail odt = new OderDetail
                {
                    IDO       = item.IDO,
                    IDP       = item.IDP,
                    quantity  = item.quantity,
                    total     = item.total,
                    StatusPay = false
                };
                db.OderDetails.Add(odt);
                db.SaveChanges();
                return(true);
            }
            catch (Exception)
            {
                return(false);

                throw;
            }
        }
Ejemplo n.º 20
0
        public async Task <IActionResult> ThanhToan(string shipName, string address)
        {
            var model = _context.loais.ToList();

            ViewBag.model = model;

            var user = await _userManager.FindByNameAsync(User.Identity.Name);

            if (user.PhoneNumber == null)
            {
                ViewBag.NoPhoneNumber = "You dont have Phone Number. Pleave add your Phone Number in Manage Account.";
                return(View());
            }

            var cart = SessionHelper.Get <List <Item> >(HttpContext.Session, "cart");

            var oder = new Oder();

            oder.ID           = DateTime.Now.Ticks;
            oder.CreatedDate  = DateTime.Now;
            oder.CustomerID   = user.Id;
            oder.ShipName     = shipName;
            oder.ShipAddress  = address;
            oder.ShipMobile   = user.PhoneNumber;
            oder.ShipEmail    = user.Email;
            oder.CheckOutType = "Normal";

            var subTotal = cart.Sum(item => (item.Product.DonGia - item.Product.DonGia * item.Product.GiamGia / 100) * item.Quantity);

            oder.Total = Math.Round(subTotal, 0);
            _orderId   = oder.ID;


            try
            {
                var id = Insert(oder);
                foreach (var item in cart)
                {
                    var oderDetail = new OderDetail();
                    oderDetail.MaHH     = item.Product.MaHH;
                    oderDetail.OderID   = id;
                    oderDetail.Gia      = item.Product.DonGia;
                    oderDetail.Quantity = item.Quantity;
                    Insert1(oderDetail);

                    var hanghoas = _context.HangHoas.Where(x => x.MaHH == item.Product.MaHH).First();


                    hanghoas.DaMua += item.Quantity;
                    _context.Update(hanghoas);
                    _context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
                return(View("ThatBai"));
            }

            oder.Status = true;
            _context.Update(oder);
            _context.SaveChanges();

            return(View("HoanThanh"));
        }
Ejemplo n.º 21
0
        public async Task <IActionResult> ThanhToanPaypal()
        {
            var model = _context.loais.ToList();

            ViewBag.model = model;

            var user = await _userManager.FindByNameAsync(User.Identity.Name);

            var environment = new SandboxEnvironment(_clientId, _secretKey);
            var client      = new PayPalHttpClient(environment);

            #region Create Paypal Order
            var cart     = SessionHelper.Get <List <Item> >(HttpContext.Session, "cart");
            var itemList = new ItemList()
            {
                Items = new List <item>()
            };
            //var total = Math.Round(cart.Sum(item => (item.Product.DonGia * item.Quantity) - ((item.Product.GiamGia) * (item.Product.DonGia)) / 100) / tyGiaUSD, 2);
            var total = cart.Sum(item => Math.Round(Math.Round(item.Product.DonGia / tyGiaUSD, 2) - Math.Round(Math.Round(item.Product.DonGia / tyGiaUSD, 2) * item.Product.GiamGia / 100, 2), 2) * item.Quantity);
            var giam  = Math.Round(cart.Sum(item => Math.Round(Math.Round(item.Product.DonGia / tyGiaUSD, 2) * item.Quantity * item.Product.GiamGia / 100, 2)), 2);
            //var total = Math.Round(sub - giam, 2);
            foreach (var item in cart)
            {
                itemList.Items.Add(new item()
                {
                    Name     = item.Product.TenHH,
                    Currency = "USD",
                    Price    = Math.Round(Math.Round(item.Product.DonGia / tyGiaUSD, 2) - Math.Round(Math.Round(item.Product.DonGia / tyGiaUSD, 2) * item.Product.GiamGia / 100, 2), 2).ToString(),
                    Quantity = item.Quantity.ToString(),
                    Sku      = "sku",
                    Tax      = "0"
                });
            }
            #endregion


            var paypalOrderId = DateTime.Now.Ticks;
            var hostname      = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}";
            var payment       = new Payment()
            {
                Intent       = "sale",
                Transactions = new List <Transaction>()
                {
                    new Transaction()
                    {
                        Amount = new Amount()
                        {
                            Total    = total.ToString(),
                            Currency = "USD",
                            Details  = new AmountDetails
                            {
                                Tax      = "0",
                                Shipping = "0",
                                Subtotal = total.ToString()
                            }
                        },
                        ItemList      = itemList,
                        Description   = $"Invoice #{paypalOrderId}",
                        InvoiceNumber = paypalOrderId.ToString(),
                        Payee         = new Payee()
                        {
                            Email      = _email,
                            MerchantId = _merchantId
                        }
                    }
                },
                RedirectUrls = new RedirectUrls()
                {
                    CancelUrl = $"{hostname}/cart/that-bai",
                    ReturnUrl = $"{hostname}/cart/paypal-hoan-thanh"
                },
                Payer = new Payer()
                {
                    PaymentMethod = "paypal"
                }
            };

            #region Insert Order to Database
            var oder = new Oder();
            oder.ID           = paypalOrderId;
            oder.ShipAddress  = "Paypal User";
            oder.ShipName     = "Paypal User";
            oder.ShipMobile   = "Paypal User";
            oder.ShipEmail    = "Paypal User";
            oder.CheckOutType = "Paypal";
            oder.CustomerID   = user.Id;
            oder.CreatedDate  = DateTime.Now;

            var subTotal = cart.Sum(item => (item.Product.DonGia - item.Product.DonGia * item.Product.GiamGia / 100) * item.Quantity);

            oder.Total = Math.Round(subTotal, 0);

            _orderId = oder.ID;
            SessionHelper.Set(HttpContext.Session, "orderId", _orderId);

            try
            {
                var id = Insert(oder);
                foreach (var item in cart)
                {
                    var oderDetail = new OderDetail();
                    oderDetail.MaHH     = item.Product.MaHH;
                    oderDetail.OderID   = id;
                    oderDetail.Gia      = item.Product.DonGia;
                    oderDetail.Quantity = item.Quantity;
                    Insert1(oderDetail);

                    var hanghoas = _context.HangHoas.Where(x => x.MaHH == item.Product.MaHH).First();

                    hanghoas.DaMua += item.Quantity;
                    _context.Update(hanghoas);
                    _context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
                return(View("ThatBai"));
            }
            #endregion
            PaymentCreateRequest request = new PaymentCreateRequest();
            request.RequestBody(payment);

            try
            {
                var response = await client.Execute(request);

                var     statusCode = response.StatusCode;
                Payment result     = response.Result <Payment>();

                var    links             = result.Links.GetEnumerator();
                string paypalRedirectUrl = null;
                while (links.MoveNext())
                {
                    LinkDescriptionObject lnk = links.Current;
                    if (lnk.Rel.ToLower().Trim().Equals("approval_url"))
                    {
                        paypalRedirectUrl = lnk.Href;
                    }
                }

                return(Redirect(paypalRedirectUrl));
            }
            catch (HttpException httpException)
            {
                var statusCode = httpException.StatusCode;
                var debugId    = httpException.Headers.GetValues("PayPal-Debug-Id").FirstOrDefault();

                return(Redirect("/cart/that-bai"));
            }
        }
Ejemplo n.º 22
0
        public ActionResult Edit(Order ord, string submit)
        {
            List <Product> completedProduct;
            Order          order;


            TimeSpan noon = new TimeSpan(24, 0, 0);

            //int hourdiff = span.Hours;
            if (ord.deliveryDate == null)
            {
                ModelState.AddModelError("deliveryDate", "Please specify delivery date.");
                return(Edit(0, 0));
            }

            bool isNew = true;

            if (ord != null)
            {
                order = db.Order.FirstOrDefault(x => x.orderID == ord.orderID);;
                isNew = false;

                if (order == null)
                {
                    order = new Order();
                    isNew = true;
                }
            }
            else
            {
                order = new Order();
            }

            completedProduct = (List <Product>)Session["cart"];
            Member profile = null;
            Member user    = null;

            DateTime dnow    = DateTime.Now.Date;
            TimeSpan span    = (TimeSpan)(ord.deliveryDate - dnow);
            int      daydiff = span.Days;

            switch (submit)
            {
            case "Submit":


                if (daydiff <= 0)
                {
                    ModelState.AddModelError("", "Invalid Code verified.");
                    return(View());
                }
                if (DateTime.Now.TimeOfDay >= noon)
                {
                    ModelState.AddModelError("", "Invalid 2 Code verified.");
                    return(View());
                }

                if (Session["cart"] != null && completedProduct.Count > 0)
                {
                    List <int> detailIds = order.OderDetail.Select(x => x.detailID).ToList();
                    foreach (int id in detailIds)
                    {
                        var detail = db.OderDetail.FirstOrDefault(x => x.detailID == id);

                        order.OderDetail.Remove(detail);
                        db.OderDetail.Remove(detail);
                    }

                    if (!isNew)
                    {
                        db.SaveChanges();
                    }

                    order.OderDetail  = new List <OderDetail>();
                    profile           = db.Member.FirstOrDefault(x => x.UserName == this.User.Identity.Name);
                    order.Member      = profile;
                    order.orderStatus = 1;     // save
                    foreach (Product product in completedProduct)
                    {
                        if (product.quantity > 0)
                        {
                            OderDetail orderDetail = new OderDetail();
                            orderDetail.productID       = product.productID;
                            orderDetail.orderID         = order.orderID;
                            orderDetail.productQuantity = product.quantity;
                            order.OderDetail.Add(orderDetail);
                        }
                    }
                    order.orderDate    = DateTime.Now;
                    order.deliveryDate = ord.deliveryDate;

                    if (isNew)
                    {
                        db.Order.Add(order);
                    }
                    db.SaveChanges();
                }
                else
                {
                    return(View("Cart", "Home"));
                }

                Session.Remove("cart");
                user = db.Member.FirstOrDefault(x => x.UserName == this.User.Identity.Name);
                return(View("History", user.Order));


            case "Save":
                if (Session["cart"] != null && completedProduct.Count > 0)
                {
                    //order.OderDetail.Clear();
                    List <int> detailIds = order.OderDetail.Select(x => x.detailID).ToList();
                    foreach (int id in detailIds)
                    {
                        var detail = db.OderDetail.FirstOrDefault(x => x.detailID == id);

                        order.OderDetail.Remove(detail);
                        db.OderDetail.Remove(detail);
                    }

                    if (!isNew)
                    {
                        db.SaveChanges();
                    }
                    //order.OderDetail.Clear();
                    //if (!isNew)
                    //        db.SaveChanges();
                    //order.OderDetail = new List<OderDetail>();
                    profile           = db.Member.FirstOrDefault(x => x.UserName == this.User.Identity.Name);
                    order.Member      = profile;
                    order.orderStatus = 2;     // Draft
                    foreach (Product product in completedProduct)
                    {
                        if (product.quantity > 0)
                        {
                            OderDetail orderDetail = new OderDetail();
                            orderDetail.productID = product.productID;
                            orderDetail.orderID   = order.orderID;

                            orderDetail.productQuantity = product.quantity;
                            order.OderDetail.Add(orderDetail);
                        }
                    }
                    order.orderDate    = DateTime.Now;
                    order.deliveryDate = ord.deliveryDate;

                    if (isNew)
                    {
                        db.Order.Add(order);
                    }

                    db.SaveChanges();
                }
                else
                {
                    return(View("Product", "Home"));
                }

                Session.Remove("cart");
                user = db.Member.FirstOrDefault(x => x.UserName == this.User.Identity.Name);
                return(View("History", user.Order));

            default:
                return(null);
            }
        }
Ejemplo n.º 23
0
        public async Task <IActionResult> ThanhToanVnPay()
        {
            var user = await _userManager.FindByNameAsync(User.Identity.Name);

            var hostname = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}";

            var oder = new Oder();

            oder.ID           = DateTime.Now.Ticks;
            oder.ShipName     = "VNPay User";
            oder.ShipMobile   = "VNPay User";
            oder.ShipAddress  = "VNPay User";
            oder.ShipEmail    = "VNPay User";
            oder.CustomerID   = user.Id;
            oder.CreatedDate  = DateTime.Now;
            oder.CheckOutType = "VNPay";

            var cart     = SessionHelper.Get <List <Item> >(HttpContext.Session, "cart");
            var subTotal = cart.Sum(item => (item.Product.DonGia - item.Product.DonGia * item.Product.GiamGia / 100) * item.Quantity);

            oder.Total = Math.Round(subTotal);

            try
            {
                var id = Insert(oder);
                foreach (var item in cart)
                {
                    var oderDetail = new OderDetail();
                    oderDetail.MaHH     = item.Product.MaHH;
                    oderDetail.OderID   = id;
                    oderDetail.Gia      = item.Product.DonGia;
                    oderDetail.Quantity = item.Quantity;
                    Insert1(oderDetail);

                    var hanghoas = _context.HangHoas.Where(x => x.MaHH == item.Product.MaHH).First();

                    hanghoas.DaMua += item.Quantity;
                    _context.Update(hanghoas);
                    _context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
                return(View("ThatBai"));
            }

            var oderInfo = "";

            foreach (var item in cart)
            {
                var itemGiamGia = item.Product.DonGia * item.Product.GiamGia / 100;
                var itemGia     = Math.Round((item.Product.DonGia - itemGiamGia) * item.Quantity, 0);
                oderInfo = oderInfo + item.Quantity.ToString() + " " + item.Product.TenHH + " " + itemGia.ToString() + "VND; ";
            }

            if (oderInfo.Length > 0)
            {
                oderInfo = oderInfo.Remove(oderInfo.Length - 2, 2);
            }


            _vnPayLibrary.AddRequestData("vnp_Version", "2.0.0");
            _vnPayLibrary.AddRequestData("vnp_Command", "pay");
            _vnPayLibrary.AddRequestData("vnp_TmnCode", _tmnCode);
            _vnPayLibrary.AddRequestData("vnp_Amount", (oder.Total * 100).ToString());
            _vnPayLibrary.AddRequestData("vnp_BankCode", "");
            _vnPayLibrary.AddRequestData("vnp_CreateDate", oder.CreatedDate.ToString("yyyyMMddHHmmss"));
            _vnPayLibrary.AddRequestData("vnp_CurrCode", "VND");
            _vnPayLibrary.AddRequestData("vnp_IpAddr", _utils.GetIpAddress());
            _vnPayLibrary.AddRequestData("vnp_Locale", "vn");
            _vnPayLibrary.AddRequestData("vnp_OrderInfo", oderInfo);
            _vnPayLibrary.AddRequestData("vnp_OrderType", "130001"); //default value: other
            _vnPayLibrary.AddRequestData("vnp_ReturnUrl", $"{hostname}/cart/ket-qua-vnpay");
            _vnPayLibrary.AddRequestData("vnp_TxnRef", oder.ID.ToString());

            string paymentUrl = _vnPayLibrary.CreateRequestUrl(_url, _hashSecret);

            return(Redirect(paymentUrl));
        }