public async Task <IActionResult> Edit(int id, [Bind("MaCTHD,MaHd,MaHh,DonGia,SoLuong,GiamGia")] ChiTietHd chiTietHd)
        {
            if (id != chiTietHd.MaCTHD)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(chiTietHd);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!HoaDonExists(chiTietHd.MaHd))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["MaHh"] = new SelectList(_context.HangHoas, "MaHh", "MaHh", chiTietHd.MaHh);
            ViewData["MaHd"] = new SelectList(_context.HoaDons, "MaHd", "MaHd", chiTietHd.MaHd);

            return(View(chiTietHd));
        }
        // GET: ChiTietHds/Edit/5
        public ActionResult Edit(int id)
        {
            ChiTietHd ct = _context.ChiTietHds.Where(p => p.MaCTHD == id).First();

            ViewData["MaHh"] = new SelectList(_context.HangHoas, "MaHh", "MaHh", ct.MaHh);
            ViewData["MaHd"] = new SelectList(_context.HoaDons, "MaHd", "MaHd", ct.MaHd);

            return(View(ct));
        }
Esempio n. 3
0
        public IActionResult DatHang(string makh, string hotenkh, string diachikh, string sdt, string tennhan, string sdtnhan, string diachinhan, string ghichunhan)
        {
            HoaDon hd = new HoaDon
            {
                MaKh         = makh,
                HoTen        = hotenkh,
                DiaChi       = diachikh,
                DienThoai    = sdt,
                TenNgNhan    = tennhan,
                DtngNhan     = sdtnhan,
                DiaChiNgNhan = diachinhan,
                GhiChu       = ghichunhan,
                NgayDat      = DateTime.Now,
                MaTrangThai  = 0,
                PhiVanChuyen = 0
            };

            db.HoaDon.Add(hd);

            foreach (var item in Carts)
            {
                SanPham hh = db.SanPham.SingleOrDefault(p => p.MaSp == item.MaHh);
                //Lưu chi tiết hóa đơn
                ChiTietHd cthd = new ChiTietHd
                {
                    MaHd    = hd.MaHd,
                    MaSp    = item.MaHh,
                    DonGia  = hh.DonGia,
                    SoLuong = item.SoLuong,
                };
                db.ChiTietHd.Add(cthd);
                db.SaveChanges();

                //Kiểm tra hàng tồn kho
                KhoHang kho = db.KhoHang.SingleOrDefault(p => p.MaSp == cthd.MaSp);
                if (kho.SoLuong >= cthd.SoLuong)
                {
                    kho.SoLuong = kho.SoLuong - cthd.SoLuong;
                    db.SaveChanges();
                }
                else
                {
                    SanPham sp = db.SanPham.SingleOrDefault(p => p.MaSp == cthd.MaSp);

                    db.ChiTietHd.Remove(cthd);
                    db.HoaDon.Remove(hd);
                    db.SaveChanges();
                    return(RedirectToAction("ThanhToan1"));
                }
            }

            HttpContext.Session.Remove("GioHang");
            HttpContext.Session.Remove("ThongTin");
            return(RedirectToAction("ThanhToan1"));
        }
        public IActionResult XoaCTHD(int mact)
        {
            ChiTietHd  ct  = db.ChiTietHd.Find(mact);
            SanPhamKho spk = db.SanPhamKho.SingleOrDefault(p => p.MaHh == ct.MaHh && p.KichCo == ct.KichCo);

            if (spk != null)
            {
                spk.SoLuong += ct.SoLuong;
                db.ChiTietHd.Remove(ct);
            }
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public async Task <IActionResult> Create([Bind("ChiTietHdID,HoaDonID,MonAnID,DonGia,SoLuong,GiamGia")] ChiTietHd chiTietHd)
        {
            if (ModelState.IsValid)
            {
                _context.Add(chiTietHd);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["HoaDonID"] = new SelectList(_context.HoaDons, "HoaDonID", "HoaDonID", chiTietHd.HoaDonID);
            ViewData["MonAnID"]  = new SelectList(_context.MonAns, "MonAnID", "MonAnID", chiTietHd.MonAnID);
            return(View(chiTietHd));
        }
        public async Task <IActionResult> Create([Bind("MaHd,MaSp,SoLuong,IdchitietHd")] ChiTietHd chiTietHd)
        {
            if (ModelState.IsValid)
            {
                _context.Add(chiTietHd);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["MaHd"] = new SelectList(_context.HoaDon, "MaHd", "MaKh", chiTietHd.MaHd);
            ViewData["MaSp"] = new SelectList(_context.SanPham, "MaSp", "MaSp", chiTietHd.MaSp);
            return(View(chiTietHd));
        }
        public async Task <IActionResult> Create([Bind("MaCTHD,MaHd,MaHh,DonGia,SoLuong,GiamGia")] ChiTietHd chiTietHd)
        {
            if (ModelState.IsValid)
            {
                _context.Add(chiTietHd);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["MaHh"] = new SelectList(_context.HangHoas, "MaHh", "MaHh", chiTietHd.MaHh);
            ViewData["MaHd"] = new SelectList(_context.HoaDons, "MaHd", "MaHd", chiTietHd.MaHd);

            return(View(chiTietHd));
        }
Esempio n. 8
0
        public IActionResult CheckoutSuccess()
        {
            double tong = 0;
            //Tạo đơn hàng trong database với trạng thái thanh toán là "Paypal" và thành công
            //Xóa session

            List <CartItem> giohang = Carts;

            foreach (var item in giohang)
            {
                tong = tong + item.ThanhTien;
            }
            HttpContext.Session.Set("GioHang", giohang);
            KhachHang kh = HttpContext.Session.Get <KhachHang>("TaiKhoan");
            HoaDon    hd = new HoaDon
            {
                MaKh          = kh.MaKh,
                HoTen         = kh.HoTen,
                DiaChi        = "TPHCM",
                MaOnline      = "IM" + Math.Round(tong * 3, 5).ToString(),
                DienThoai     = kh.DienThoai,
                TenNgNhan     = kh.HoTen,
                DtngNhan      = kh.DienThoai,
                DiaChiNgNhan  = kh.DiaChi,
                NgayDat       = DateTime.Now,
                CachThanhToan = "Online",
                MaTrangThai   = 1,
                PhiVanChuyen  = 0,
                TongTien      = tong
            };

            db.HoaDon.Add(hd);
            //  db.SaveChanges();
            foreach (var item in Carts)
            {
                SanPham hh = db.SanPham.SingleOrDefault(p => p.MaSp == item.MaHh);
                //Lưu chi tiết hóa đơn
                ChiTietHd cthd = new ChiTietHd
                {
                    MaHd    = hd.MaHd,
                    MaSp    = item.MaHh,
                    DonGia  = hh.DonGia,
                    SoLuong = item.SoLuong,
                };
                db.ChiTietHd.Add(cthd);
                db.SaveChanges();
            }
            //HttpContext.Session.Remove("GioHang");
            return(View());
        }
Esempio n. 9
0
        public IActionResult PostHoaDon([FromBody] Checkoutmodel hoaDon)
        {
            int    sl = 0;
            HoaDon Hd = new HoaDon
            {
                IdHd         = GetHDID.getIDforHD(),
                IdKh         = hoaDon.IDkh,
                DiaChi       = hoaDon.DiaChi,
                NgayLap      = hoaDon.NgayLap,
                TenNguoiNhan = hoaDon.TenNguoiNhan,
                Sodienthoai  = hoaDon.SDT,
                TinhTrang    = "Dang Xu Ly",
                TongGiaTri   = decimal.Parse(hoaDon.TongGiaTri),
                GhiChu       = hoaDon.MessfromClient
            };
            var cartItems = _context.CartItems.Where(
                c => c.CartId == Hd.IdKh.ToString());

            foreach (CartItems items in cartItems)
            {
                sl = sl + 1;
                ChiTietHd HDinfo = new ChiTietHd()
                {
                    IdChitiet    = Guid.NewGuid().ToString(),
                    IdHd         = Hd.IdHd,
                    IdSp         = items.Idsanpham,
                    SoLuongDaMua = items.Quantity,
                    GiaSp        = _context.SanPham.FirstOrDefault(n => n.IdSp.Equals(items.Idsanpham)).GiaSp,
                    UnitPrice    = (float.Parse(_context.SanPham.FirstOrDefault(n => n.IdSp.Equals(items.Idsanpham)).GiaSp) * items.Quantity.Value).ToString(),
                };
                _context.ChiTietHd.Add(HDinfo);
            }
            Hd.TongSlsp = sl;
            _context.HoaDon.Add(Hd);

            var Citems = _context.CartItems.Where(n => n.CartId == Hd.IdKh.ToString());

            if (Citems != null)
            {
                _context.CartItems.RemoveRange(Citems);
            }
            _context.SaveChanges();
            return(Ok(Hd.IdHd));
        }
Esempio n. 10
0
        public async Task <IActionResult> Create([Bind("MaCt,MaHd,MaSp,DonGia,SoLuong,GiamGia")] ChiTietHd chiTietHd)
        {
            if (HttpContext.Session.Get <NhanVien>("MaNv") == null)
            {
                return(Redirect("/Admin/Login"));
            }
            else
            {
                if (ModelState.IsValid)
                {
                    _context.Add(chiTietHd);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                ViewData["MaHd"] = new SelectList(_context.HoaDon, "MaHd", "DiaChi", chiTietHd.MaHd);
                ViewData["MaSp"] = new SelectList(_context.SanPham, "MaSp", "MaSp", chiTietHd.MaSp);
                return(View(chiTietHd));
            }
        }
Esempio n. 11
0
        public async Task <IActionResult> Edit(int id, [Bind("MaCt,MaHd,MaSp,DonGia,SoLuong,GiamGia")] ChiTietHd chiTietHd)
        {
            if (HttpContext.Session.Get <NhanVien>("MaNv") == null)
            {
                return(Redirect("/Admin/Login"));
            }
            else
            {
                if (id != chiTietHd.MaCt)
                {
                    return(NotFound());
                }

                if (ModelState.IsValid)
                {
                    try
                    {
                        _context.Update(chiTietHd);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!ChiTietHdExists(chiTietHd.MaCt))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            throw;
                        }
                    }
                    return(RedirectToAction(nameof(Index)));
                }
                ViewData["MaHd"] = new SelectList(_context.HoaDon, "MaHd", "DiaChi", chiTietHd.MaHd);
                ViewData["MaSp"] = new SelectList(_context.SanPham, "MaSp", "MaSp", chiTietHd.MaSp);
                return(View(chiTietHd));
            }
        }
Esempio n. 12
0
        public IActionResult mua(int mahh, int soluong, int id)
        {
            if (mahh != 0)
            {
                HangHoa hh = new HangHoa();
                hh = _context.HangHoas.Where(p => p.HanghoaID == mahh).First();
                if (HttpContext.Session.Get <int>("xacnhanmuaxong") != 1)
                {
                    if (HttpContext.Session.Get <NguoiDung>("MaKH") == null)
                    {
                        HttpContext.Session.Set <int>("a", 3);
                        return(RedirectToAction("index", "Home"));
                    }
                    else
                    {
                        NguoiDung ngd = HttpContext.Session.Get <NguoiDung>("MaKH");
                        HoaDon    hd  = new HoaDon();
                        hd.NguoiDungID = ngd.NguoiDungID;
                        DateTime d = DateTime.Now;

                        hd.NgayDat  = d;
                        hd.NgayNhan = d;

                        hd.HoTen        = ngd.HoTen;
                        hd.DiaChi       = ngd.DiaChi;
                        hd.ThanhToanID  = 1;
                        hd.VanChuyenID  = 1;
                        hd.PhiVanChuyen = 0;
                        hd.TrangThaiID  = 1;
                        hd.GhiChu       = ngd.DiaChi;
                        _context.hoaDons.Add(hd);
                        _context.SaveChanges();
                        HttpContext.Session.Set("hoadonid", hd.HoaDonID);
                        ChiTietHd cthd = new ChiTietHd();

                        cthd.HoaDonID = hd.HoaDonID;

                        cthd.HangHoaID = mahh;
                        cthd.DonGia    = hh.DonGia;
                        cthd.SoLuong   = soluong;

                        cthd.GiamGia = hh.GiamGia;

                        _context.chiTietHds.Add(cthd);
                        _context.SaveChanges();
                        HttpContext.Session.Set("xacnhanmuaxong", 1);
                    }
                }
                else
                {
                    ChiTietHd cthd = new ChiTietHd();

                    cthd.HoaDonID = HttpContext.Session.Get <int>("hoadonid");

                    cthd.HangHoaID = mahh;
                    cthd.DonGia    = hh.DonGia;
                    cthd.SoLuong   = soluong;

                    cthd.GiamGia = hh.GiamGia;

                    _context.chiTietHds.Add(cthd);
                    _context.SaveChanges();
                    HttpContext.Session.Set("xacnhanmuaxong", 1);
                }
            }
            else
            {
                var ct = _context.chiTietHds.Find(id);
                _context.chiTietHds.Remove(ct);
                _context.SaveChanges();
            }


            List <ChiTietHd> dscts = new List <ChiTietHd>();

            dscts = _context.chiTietHds.Include(x => x.HangHoa).Where(p => p.HoaDonID == HttpContext.Session.Get <int>("hoadonid")).ToList();
            double tongtien = 0;

            foreach (var item in dscts)
            {
                tongtien += item.ThanhTien;
            }
            ViewBag.TongTien = tongtien;
            return(View(dscts));
        }
Esempio n. 13
0
        public IActionResult TaoHoaDonBT(string email, string hoten_ngnhan, string dc_nguoinhan, string ghichu, string sdt, string magiamgia)
        {
            KhachHang kh = new KhachHang();

            kh.HoTen     = hoten_ngnhan;
            kh.DiaChi    = dc_nguoinhan;
            kh.DienThoai = sdt;

            kh.Email = email;
            db.KhachHang.Add(kh);
            db.SaveChanges();
            // tạo hóa đơn
            var getKH = db.KhachHang.Where(p => p.Email == email).OrderByDescending(p => p.MaKh).Take(1);

            foreach (var titem in getKH)
            {
                HoaDon hd = new HoaDon
                {
                    MaKh         = titem.MaKh,
                    HoTen        = hoten_ngnhan,
                    DiaChi       = dc_nguoinhan,
                    NgayDat      = DateTime.Now,
                    GhiChu       = ghichu,
                    SdtNguoinhan = sdt,
                    MaTrangThai  = 0,
                    PhiVanChuyen = 35000,
                    MaVoucher    = magiamgia
                };
                db.HoaDon.Add(hd);
                // tạo chi tiết hóa đơn
                //  double tt = 0;
                double tongtienhang = 0;
                double tongthucthu  = 0;

                foreach (var item in Carts)
                {
                    tongtienhang += item.ThanhTien;
                    HangHoa hh = db.HangHoa.SingleOrDefault(p => p.MaHh == item.MaHh);
                    //   tt = item.SoLuong * hh.DonGia * (1 - hh.GiamGia);
                    ChiTietHd cthd = new ChiTietHd
                    {
                        MaHd    = hd.MaHd,
                        MaHh    = item.MaHh,
                        DonGia  = hh.DonGia,
                        GiamGia = hh.GiamGia,
                        SoLuong = item.SoLuong,
                        KichCo  = item.KichCo
                    };

                    db.ChiTietHd.Add(cthd);
                    db.SaveChanges();
                    // trừ sản phẩm từ kho
                    SanPhamKho spk = db.SanPhamKho.SingleOrDefault(p => p.MaHh == cthd.MaHh && p.KichCo == cthd.KichCo);
                    if (spk.SoLuong >= cthd.SoLuong)
                    {
                        if (HttpContext.Session.Get <string>("ErrorGH") != null)
                        {
                            HttpContext.Session.Remove("ErrorGH");
                        }
                        spk.SoLuong = spk.SoLuong - cthd.SoLuong;
                    }
                    else
                    {
                        HangHoa hangHoa = db.HangHoa.SingleOrDefault(p => p.MaHh == cthd.MaHh);
                        string  loi     = "Hàng hóa có mã " + hangHoa.TenHh + " chỉ còn : " + spk.SoLuong + " sản phẩm";
                        HttpContext.Session.Set("ErrorGH", loi);
                        db.ChiTietHd.Remove(cthd);
                        db.HoaDon.Remove(hd);
                        db.SaveChanges();
                        return(RedirectToAction("Index"));
                    }
                }
                Voucher v = db.Voucher.Find(magiamgia);
                if (v != null)
                {
                    tongthucthu = tongtienhang + 35000 - Convert.ToDouble(tongtienhang * v.GiamGia);
                }
                else
                {
                    tongthucthu = tongtienhang + 35000;
                }
                hd.TongTienHang = tongtienhang;
                hd.TongThucThu  = tongthucthu;
                db.SaveChanges();
                HttpContext.Session.Set <string>("mess", "Hóa đơn của bạn đã được gửi tới cửa hàng vui lòng chờ kiểm tra mail để biết trạng thái đơn hàng của bạn . ESHOP");
                HttpContext.Session.Remove("GioHang");
            }

            return(RedirectToAction("Index"));
        }
        public IActionResult TaoHoaDon(int makh, string hotenkh, string diachi, string hoten_ngnhan, string dc_nguoinhan, string ghichu, string sdt)
        {
            // tạo hóa đơn
            HoaDon hd = new HoaDon
            {
                MaKh         = makh,
                HoTen        = hoten_ngnhan,
                DiaChi       = dc_nguoinhan,
                NgayDat      = DateTime.Now,
                GhiChu       = ghichu,
                SdtNguoinhan = sdt,
                MaTrangThai  = 0,
                PhiVanChuyen = 35000
            };

            db.HoaDon.Add(hd);
            // tạo chi tiết hóa đơn
            //  double tt = 0;
            KhachHang kh = db.KhachHang.SingleOrDefault(p => p.MaKh == makh);

            foreach (var item in Carts)
            {
                HangHoa hh = db.HangHoa.SingleOrDefault(p => p.MaHh == item.MaHh);
                //   tt = item.SoLuong * hh.DonGia * (1 - hh.GiamGia);
                ChiTietHd cthd = new ChiTietHd
                {
                    MaHd    = hd.MaHd,
                    MaHh    = item.MaHh,
                    DonGia  = hh.DonGia,
                    GiamGia = hh.GiamGia,
                    SoLuong = item.SoLuong,
                    KichCo  = item.KichCo
                };

                db.ChiTietHd.Add(cthd);
                db.SaveChanges();
                // trừ sản phẩm từ kho
                SanPhamKho spk = db.SanPhamKho.SingleOrDefault(p => p.MaHh == cthd.MaHh && p.KichCo == cthd.KichCo);
                if (spk.SoLuong >= cthd.SoLuong)
                {
                    if (HttpContext.Session.Get <string>("ErrorGH") != null)
                    {
                        HttpContext.Session.Remove("ErrorGH");
                    }
                    spk.SoLuong = spk.SoLuong - cthd.SoLuong;
                }
                else
                {
                    HangHoa hangHoa = db.HangHoa.SingleOrDefault(p => p.MaHh == cthd.MaHh);
                    string  loi     = "Hàng hóa có mã " + hangHoa.TenHh + " chỉ còn : " + spk.SoLuong + " sản phẩm";
                    HttpContext.Session.Set("ErrorGH", loi);
                    db.ChiTietHd.Remove(cthd);
                    db.HoaDon.Remove(hd);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }
            HttpContext.Session.Set <string>("mess", "Hóa đơn của bạn đã được gửi tới cửa hàng vui lòng chờ kiểm tra mail để biết trạng thái đơn hàng của bạn . ESHOP");
            HttpContext.Session.Remove("GioHang");
            return(RedirectToAction("Index"));
        }
Esempio n. 15
0
        public IActionResult mua(int mamon, int soluong)
        {
            MonAn ma = new MonAn();

            ma = db.MonAns.Where(p => p.MonAnID == mamon).First();
            // = new KhachHang();
            // kh = db.khachHangs.Where(p => p.TenKh == HttpContext.Session.Get<KhachHang>("MaKH"));

            if (HttpContext.Session.Get <int>("xacnhanmuaxong") != 1)
            {
                HoaDon hd = new HoaDon();
                hd.BanID    = 1;
                hd.ChuThich = null;

                hd.KhachHangID = HttpContext.Session.Get <int>("MaKH");
                hd.TrangThaiID = 8;

                db.HoaDons.Add(hd);
                db.SaveChanges();
                HttpContext.Session.Set("hoadonid", hd.HoaDonID);
                ChiTietHd cthd = new ChiTietHd();

                cthd.HoaDonID = hd.HoaDonID;

                cthd.MonAnID = mamon;
                cthd.DonGia  = ma.DonGia;
                cthd.SoLuong = soluong;

                cthd.GiamGia = ma.GiamGia;

                db.ChiTietHds.Add(cthd);
                db.SaveChanges();
                HttpContext.Session.Set("xacnhanmuaxong", 1);
                if (HttpContext.Session.Get <int>("view") == 4)
                {
                    return(RedirectToAction("Montrangmieng", "KhachHang"));
                }
                else if (HttpContext.Session.Get <int>("view") == 3)
                {
                    return(RedirectToAction("Monchinh", "KhachHang"));
                }
                else if (HttpContext.Session.Get <int>("view") == 2)
                {
                    return(RedirectToAction("Monkhaivi", "KhachHang"));
                }
                else
                {
                    return(RedirectToAction("Mondactrung", "KhachHang"));
                }
            }
            else if (HttpContext.Session.Get <int>("xacnhanmuaxong") == 1)
            {
                ChiTietHd cthd = new ChiTietHd();

                cthd.HoaDonID = HttpContext.Session.Get <int>("hoadonid");
                cthd.MonAnID  = mamon;
                cthd.DonGia   = ma.DonGia;
                cthd.SoLuong  = soluong;

                cthd.GiamGia = ma.GiamGia;

                db.ChiTietHds.Add(cthd);
                db.SaveChanges();

                if (HttpContext.Session.Get <int>("view") == 4)
                {
                    return(RedirectToAction("Montrangmieng", "KhachHang"));
                }
                else if (HttpContext.Session.Get <int>("view") == 3)
                {
                    return(RedirectToAction("Monchinh", "KhachHang"));
                }
                else if (HttpContext.Session.Get <int>("view") == 2)
                {
                    return(RedirectToAction("Monkhaivi", "KhachHang"));
                }
                else
                {
                    return(RedirectToAction("Mondactrung", "KhachHang"));
                }
            }



            return(RedirectToAction("Montrangmieng", "KhachHang"));
        }
Esempio n. 16
0
        public async Task <IActionResult> PaypalPayment(string makh, string hotenkh, string diachikh, string sdt, string tennhan, string sdtnhan, string diachinhan, string ghichunhan)
        {
            List <CartItem> danhsachhang = HttpContext.Session.Get <List <CartItem> >("GioHang");
            List <Item>     items        = new List <Item>();
            double          total        = 0;

            foreach (var x in danhsachhang)
            {
                var gia = Math.Round(x.GiaBan / 23000, 0);
                items.Add(new Item()
                {
                    Name     = x.TenHh,
                    Currency = "USD",
                    Price    = gia.ToString(),
                    Quantity = x.SoLuong.ToString(),
                    Sku      = "sku",
                    Tax      = "0"
                });
                total += gia * x.SoLuong;
            }
            HoaDon hd = new HoaDon
            {
                MaKh         = makh,
                HoTen        = hotenkh,
                DiaChi       = "TPHCM",
                DienThoai    = sdt,
                TenNgNhan    = tennhan,
                DtngNhan     = sdtnhan,
                DiaChiNgNhan = diachinhan,
                GhiChu       = ghichunhan,
                NgayDat      = DateTime.Now,
                MaTrangThai  = 0,
                PhiVanChuyen = 0
            };

            db.HoaDon.Add(hd);
            foreach (var item in Carts)
            {
                SanPham hh = db.SanPham.SingleOrDefault(p => p.MaSp == item.MaHh);
                //Lưu chi tiết hóa đơn
                ChiTietHd cthd = new ChiTietHd
                {
                    //MaHd = hd.MaHd,
                    MaSp    = item.MaHh,
                    DonGia  = hh.DonGia,
                    SoLuong = item.SoLuong,
                };
                db.ChiTietHd.Add(cthd);
                db.SaveChanges();
                KhoHang kho = db.KhoHang.SingleOrDefault(p => p.MaSp == cthd.MaSp);
                if (kho.SoLuong >= cthd.SoLuong)
                {
                    kho.SoLuong = kho.SoLuong - cthd.SoLuong;
                    db.SaveChanges();
                }
                else
                {
                    SanPham sp = db.SanPham.SingleOrDefault(p => p.MaSp == cthd.MaSp);

                    db.ChiTietHd.Remove(cthd);
                    db.HoaDon.Remove(hd);
                    db.SaveChanges();
                }
            }
            Payment payment           = _payPal.CreatePayment(total, @"https://*****:*****@"https://localhost:44345/GioHang/CheckoutFail", "sale", items);
            string  paypalRedirectUrl = await _payPal.ExecutePayment(payment);

            if (paypalRedirectUrl == "fail")

            {
                hd.MaTrangThai = 0;
                db.SaveChanges();
                TempData["status"] = "Thanh toán thất bại";
                return(RedirectToAction("Fail"));
            }
            else
            {
                hd.MaTrangThai = 1;
                db.SaveChanges();
                TempData["status"] = "Thanh toán đơn hàng thanh cong";
                return(Redirect(paypalRedirectUrl));
            }
        }
Esempio n. 17
0
        public async Task <IActionResult> Checkout()
        {
            //SandboxEnvironment(clientId, clientSerect)
            var environment = new SandboxEnvironment("AW0XNlAAqYLVMTbaBJVpAz_DFp2TrHMQgAmZ5WXDPt9Hoyt4Jrrb1ZpdvpI70CIqetpb3yHYV23Z9vsS", "EKqZen_z_GAihIxYgBDxy8mvu2Cola9Pdq09v38SSWn05gaAXgmlPy7H-UK7bhL9tW8CSCrVYM5A7WWv");
            var client      = new PayPalHttpClient(environment);

            //Đọc thông tin đơn hàng từ Session
            var itemList = new ItemList()
            {
                Items = new List <Item>()
            };

            var tongTien = Cart.Sum(p => p.ThanhTien);

            foreach (var item in Cart)
            {
                itemList.Items.Add(new Item()
                {
                    Name     = item.TenHh,
                    Currency = "USD",
                    Price    = item.GiaBan.ToString(),
                    Quantity = item.SoLuong.ToString(),
                    Sku      = "sku",
                    Tax      = "0"
                });
            }

            var payment = new Payment()
            {
                Intent       = "sale",
                Transactions = new List <Transaction>()
                {
                    new Transaction()
                    {
                        Amount = new Amount()
                        {
                            Total    = tongTien.ToString(),
                            Currency = "USD",
                            Details  = new AmountDetails
                            {
                                Tax      = "0",
                                Shipping = "0",
                                Subtotal = tongTien.ToString()
                            }
                        },
                        ItemList      = itemList,
                        Description   = "Shop online technology products",
                        InvoiceNumber = DateTime.Now.Ticks.ToString()
                    }
                },
                RedirectUrls = new RedirectUrls()
                {
                    CancelUrl = "https://techworld.azurewebsites.net/Paypal/Fail",
                    ReturnUrl = "https://techworld.azurewebsites.net/Paypal/Success"
                },
                Payer = new Payer()
                {
                    PaymentMethod = "paypal"
                }
            };

            KhachHang kh = HttpContext.Session.Get <KhachHang>("Username");
            HoaDon    hd = new HoaDon
            {
                MaKh    = kh.MaKh,
                HoTen   = kh.HoTen,
                DiaChi  = kh.DiaChi,
                NgayDat = DateTime.Now,
                // GhiChu = ghichu,
                SdtNguoinhan        = kh.DienThoai,
                MaTrangThai         = 0,
                PhuongThucThanhToan = 1,
                TinhTrangThanhToan  = 1,
                PhiVanChuyen        = 0
            };

            db.HoaDon.Add(hd);
            double tongtien   = 0;
            double tonghoadon = 0;

            foreach (var i in Cart)
            {
                tongtien = tongtien + i.ThanhTien;
                HangHoa hh = db.HangHoa.SingleOrDefault(p => p.MaHh == i.MaHh);

                ChiTietHd cthd = new ChiTietHd
                {
                    MaHd    = hd.MaHd,
                    MaHh    = i.MaHh,
                    DonGia  = hh.DonGia,
                    GiamGia = hh.GiamGia,
                    SoLuong = i.SoLuong
                };
                db.ChiTietHd.Add(cthd);
                db.SaveChanges();
            }
            tonghoadon      = tongtien + 0;
            hd.TongTienHang = tongtien;
            hd.TongThucThu  = tonghoadon;
            db.SaveChanges();
            HttpContext.Session.Remove("GioHang");

            PaymentCreateRequest request = new PaymentCreateRequest();

            request.RequestBody(payment);

            try
            {
                HttpResponse 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"))
                    {
                        //saving the payapalredirect URL to which user will be redirected for payment
                        paypalRedirectUrl = lnk.Href;
                    }
                }

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

                return(RedirectToAction("Fail"));
            }

            //return View();
        }
Esempio n. 18
0
        public IActionResult HoaDonTV(string ghichu)
        {
            KhachHang kh = HttpContext.Session.Get <KhachHang>("Username");
            HoaDon    hd = new HoaDon
            {
                MaKh                = kh.MaKh,
                HoTen               = kh.HoTen,
                DiaChi              = kh.DiaChi,
                GhiChu              = ghichu,
                NgayDat             = DateTime.Now,
                MaTrangThai         = 0,
                PhuongThucThanhToan = 0,
                TinhTrangThanhToan  = 0,
                PhiVanChuyen        = 0
            };

            db.HoaDon.Add(hd);
            double tongtien   = 0;
            double tonghoadon = 0;

            foreach (var i in Carts)
            {
                tongtien = tongtien + i.ThanhTien;
                HangHoa hh = db.HangHoa.SingleOrDefault(p => p.MaHh == i.MaHh);

                ChiTietHd cthd = new ChiTietHd
                {
                    MaHd    = hd.MaHd,
                    MaHh    = i.MaHh,
                    DonGia  = hh.DonGia,
                    GiamGia = hh.GiamGia,
                    SoLuong = i.SoLuong
                };
                db.ChiTietHd.Add(cthd);
                db.SaveChanges();


                if (hh.SoLuong >= cthd.SoLuong)
                {
                    hh.SoLuong = hh.SoLuong - cthd.SoLuong;
                }
                else
                {
                    HangHoa hanghoa = db.HangHoa.SingleOrDefault(p => p.MaHh == cthd.MaHh);
                    string  hethang = "Mặt hàng " + hanghoa.TenHh + " Chỉ còn : " + hh.SoLuong + " sản phẩm";
                    HttpContext.Session.Set("HetHang", hethang);
                    db.ChiTietHd.Remove(cthd);
                    db.HoaDon.Remove(hd);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }
            tonghoadon      = tongtien + 0;
            hd.TongTienHang = tongtien;
            hd.TongThucThu  = tonghoadon;
            db.SaveChanges();
            HttpContext.Session.Set <string>("OrderSuccess", "Đơn hàng của bạn đã được đặt thành công.");
            //ViewBag.OrderSuccess = true;
            HttpContext.Session.Remove("GioHang");


            return(RedirectToAction("Index"));
        }