public ActionResult Thanhcong() { if (Session["cart"] != null) { OrderInfoModel data = new OrderInfoModel(); data.CustomerName = Session["CustomerName"].ToString() == "" ? "" : Session["CustomerName"].ToString(); data.CustomerEmail = Session["CustomerEmail"].ToString() == "" ? "" : Session["CustomerEmail"].ToString(); data.CustomerAddress = Session["CustomerAddress"].ToString() == "" ? "" : Session["CustomerAddress"].ToString(); data.CustomerPhone = Session["CustomerPhone"].ToString() == "" ? "" : Session["CustomerPhone"].ToString(); data.CustomerComment = Session["CustomerComment"].ToString() == "" ? "" : Session["CustomerComment"].ToString(); //data.Giftcode = Session["Giftcode"].ToString() ==null? "": Session["Giftcode"].ToString(); data.PaymentMethod = 3; // nhớ set up 3 trang thai paypal if (String.IsNullOrEmpty(data.CustomerPhone) || String.IsNullOrEmpty(data.CustomerName) || String.IsNullOrEmpty(data.CustomerAddress)) { throw new Exception("Vui lòng điền đầy đủ thông tin trước khi đặt hàng"); } var list = (Session["cart"] as List <PRODUCT>); if (list is null) { throw new Exception("Đơn hàng rỗng không thể thanh toán"); } if (list.Count < 1) { throw new Exception("Đơn hàng rỗng không thể thanh toán"); } using (THUONGMAIDIENTUEntities db = new THUONGMAIDIENTUEntities()) { // tìm và update khách nếu ko thì tạo var customer = db.CUSTOMERs.Where(x => x.CustomerPhone.Equals(data.CustomerPhone)).FirstOrDefault(); if (customer is null) { customer = new CUSTOMER(); } customer.CustomerPhone = data.CustomerPhone; customer.CustomerAddress = data.CustomerAddress; customer.CustomerName = data.CustomerName; customer.CustomerEmail = data.CustomerEmail; if (customer.IdCustomer < 1) { db.CUSTOMERs.Add(customer); } db.SaveChanges(); ORDER o = new ORDER(); o.IdCustomer = customer.IdCustomer; o.DateOrder = DateTime.Now; o.DateDelivery = DateTime.Now; o.OrderComment = data.CustomerComment; o.IdPayment = data.PaymentMethod; // nhớ gán session nhé, để bắt trạng thái thanh toán tùy paypal or ngân lượng vd 3-paypal 4-nganluong o.IdStatus = 1; int total = 0; foreach (var p in list) { total += (int)p.ProductPrice; PRODUCT_ORDER po = new PRODUCT_ORDER(); po.Count = (byte)p.Quantily; po.Discount = 0; po.IdProduct = p.IdProduct; po.Price = p.ProductPrice; o.PRODUCT_ORDER.Add(po); } o.Total = total; db.ORDERs.Add(o); db.SaveChanges(); if (data.PaymentMethod == 2) { VNPay vnpay = new VNPay(); string link = vnpay.GetBankingURL(total, data.BankCode, o.IdOrder.ToString(), DateTime.Now); return(Redirect(link)); } TempData["id"] = o.IdOrder; Session["cart"] = null; return(View()); } } return(Redirect("~/home/index")); }
public ActionResult Index(OrderInfoModel data) { try { if (String.IsNullOrEmpty(data.CustomerPhone) || String.IsNullOrEmpty(data.CustomerName) || String.IsNullOrEmpty(data.CustomerAddress)) { throw new Exception("Vui lòng điền đầy đủ thông tin trước khi đặt hàng"); } var list = (Session["cart"] as List <PRODUCT>); if (list is null) { throw new Exception("Đơn hàng rỗng không thể thanh toán"); } if (list.Count < 1) { throw new Exception("Đơn hàng rỗng không thể thanh toán"); } using (THUONGMAIDIENTUEntities db = new THUONGMAIDIENTUEntities()) { // tìm và update khách nếu ko thì tạo var customer = db.CUSTOMERs.Where(x => x.CustomerPhone.Equals(data.CustomerPhone)).FirstOrDefault(); if (customer is null) { customer = new CUSTOMER(); customer.CustomerPhone = data.CustomerPhone; customer.CustomerAddress = data.CustomerAddress; customer.CustomerName = data.CustomerName; customer.CustomerEmail = data.CustomerEmail; db.CUSTOMERs.Add(customer); } else { customer.CustomerAddress = data.CustomerAddress; customer.CustomerName = data.CustomerName; customer.CustomerEmail = data.CustomerEmail; } db.SaveChanges(); var giftCode = db.GIFTCODEs.Where(g => g.Code == data.Giftcode).SingleOrDefault(); if (giftCode != null) { giftCode.Status = true;//đã dùng rồi db.SaveChanges(); } ORDER o = new ORDER(); o.IdCustomer = customer.IdCustomer; o.DateOrder = DateTime.Now; o.DateDelivery = DateTime.Now; o.OrderComment = data.CustomerComment; o.IdPayment = data.PaymentMethod; o.IdStatus = 1; int total = 0; foreach (var p in list) { total += (int)p.ProductPrice * p.Quantily; PRODUCT_ORDER po = new PRODUCT_ORDER(); po.Count = (byte)p.Quantily; po.Discount = 0; po.IdProduct = p.IdProduct; po.Price = p.ProductPrice; o.PRODUCT_ORDER.Add(po); } //double total = (double)data.Total; o.Total = total; db.ORDERs.Add(o); db.SaveChanges(); if (data.PaymentMethod == 2) { VNPay vnpay = new VNPay(); string link = vnpay.GetBankingURL(total, data.BankCode, o.IdOrder.ToString(), DateTime.Now); return(Redirect(link)); } TempData["id"] = o.IdOrder; return(RedirectToAction("Success")); } } catch (Exception ex) { ViewBag.Error = ex.Message; } return(View()); }
public ActionResult Index(OrderInfoModel data) { try { if (String.IsNullOrEmpty(data.CustomerPhone) || String.IsNullOrEmpty(data.CustomerName) || String.IsNullOrEmpty(data.CustomerAddress)) { throw new Exception("Vui lòng điền đầy đủ thông tin trước khi đặt hàng"); } var list = (Session["cart"] as List <PRODUCT>); if (list is null) { throw new Exception("Đơn hàng rỗng không thể thanh toán"); } if (list.Count < 1) { throw new Exception("Đơn hàng rỗng không thể thanh toán"); } using (THUONGMAIDIENTUEntities db = new THUONGMAIDIENTUEntities()) { // tìm và update khách nếu ko thì tạo var customer = db.CUSTOMERs.Where(x => x.CustomerPhone.Equals(data.CustomerPhone)).FirstOrDefault(); if (customer is null) { customer = new CUSTOMER(); } customer.CustomerPhone = data.CustomerPhone; customer.CustomerAddress = data.CustomerAddress; customer.CustomerName = data.CustomerName; customer.CustomerEmail = data.CustomerEmail; if (customer.IdCustomer < 1) { db.CUSTOMERs.Add(customer); } db.SaveChanges(); ORDER o = new ORDER(); o.IdCustomer = customer.IdCustomer; o.DateOrder = DateTime.Now; o.DateDelivery = DateTime.Now; o.OrderComment = data.CustomerComment; o.IdPayment = data.PaymentMethod; o.IdStatus = 1; int total = 0; foreach (var p in list) { total += (int)p.ProductPrice; PRODUCT_ORDER po = new PRODUCT_ORDER(); po.Count = 1; po.Discount = 0; po.IdProduct = p.IdProduct; po.Price = p.ProductPrice; o.PRODUCT_ORDER.Add(po); } o.Total = total; db.ORDERs.Add(o); db.SaveChanges(); TempData["id"] = o.IdOrder; return(RedirectToAction("Success")); } } catch (Exception ex) { ViewBag.Error = ex.Message; } return(View()); }