public ActionResult CheckOut1(FormCollection form) { try { Cart cart = Session["Cart"] as Cart; Order _order = new Order(); _order.OrderDate = DateTime.Now; _order.Email = Session["Email"].ToString(); _order.Address_Cus = form["diachi"]; _order.Descriptions = "Đã Chuyển Khoản"; _order.Amount = int.Parse(form["amount"]); _order.TinhTrangDonHang = false; _order.TinhTrangGiao = false; _order.TinhTrangThanhToan = false; _order.HuyDon = false; _order.TinhTrangDongGoi = false; db.Orders.Add(_order); //**Check order xong, sửa lại giao diện**// foreach (var item in cart.Items) { OrderDetail _order_Detail = new OrderDetail(); _order_Detail.IDOrder = _order.IDOrder; _order_Detail.ngayDat = _order.OrderDate; _order_Detail.namePro = item._shopping_product.NameProduct; _order_Detail.ngayNhan = DateTime.Now.AddDays(5); _order_Detail.IDProduct = item._shopping_product.IDProduct; _order_Detail.UnitPriceSale = item._shopping_product.UnitPrice; _order_Detail.imgPro = item._shopping_product.Images; _order_Detail.QuantitySale = item._shopping_quantity; db.OrderDetails.Add(_order_Detail); } db.SaveChanges(); cart.ClearCart(); return(RedirectToAction("Index", "Home")); } catch (Exception) { return(RedirectToAction("Index", "SanPham")); } }
// Create a paypment using an APIContext private Payment CreatePayment(APIContext apiContext, string redirectUrl) { var listItems = new ItemList() { items = new List <Item>() }; string email = Session["Email"].ToString(); var diemtichluy = db.Users.Where(s => s.Email.StartsWith(email)).ToList(); foreach (var tichluy in diemtichluy) { tichluy.DiemTichLuy += 1; if (tichluy.DiemTichLuy > 5) { tichluy.IDPhanLoai = 2; } else if (tichluy.DiemTichLuy > 20) { tichluy.IDPhanLoai = 3; } } Cart cart = Session["Cart"] as Cart; Order _order = new Order(); _order.OrderDate = DateTime.Now; _order.Email = Session["Email"].ToString(); _order.Address_Cus = "234 Su Van Hanh"; _order.Descriptions = "Đã Thanh Toán Bằng PayPal"; _order.TinhTrangDonHang = false; _order.TinhTrangGiao = false; _order.TinhTrangThanhToan = false; _order.HuyDon = false; _order.TinhTrangDongGoi = false; _order.Amount = /*nt.Parse(form["amount"]);*/ (int)cart.Items.Sum(x => x._shopping_quantity * x._shopping_product.UnitPrice); db.Orders.Add(_order); //**Check order xong, sửa lại giao diện**// foreach (var item in cart.Items) { OrderDetail _order_Detail = new OrderDetail(); _order_Detail.IDOrder = _order.IDOrder; _order_Detail.ngayDat = _order.OrderDate; _order_Detail.namePro = item._shopping_product.NameProduct; _order_Detail.ngayNhan = DateTime.Now.AddDays(5); _order_Detail.IDProduct = item._shopping_product.IDProduct; _order_Detail.UnitPriceSale = item._shopping_product.UnitPrice; _order_Detail.imgPro = item._shopping_product.Images; _order_Detail.QuantitySale = item._shopping_quantity; var check1 = db.Products.Find(item._shopping_product.IDProduct); check1.soLuong -= cart.Total_Quantity(); db.Entry(check1).State = EntityState.Modified; db.OrderDetails.Add(_order_Detail); } db.SaveChanges(); foreach (var item in cart.Items) { listItems.items.Add(new Item() { name = item._shopping_product.NameProduct, currency = "USD", price = item._shopping_product.UnitPrice.ToString(), quantity = item._shopping_quantity.ToString(), sku = "sku" }); } var payer = new Payer() { payment_method = "paypal" }; // Do the configuration RedirectURLs here with redirectURLs object var redirUrls = new RedirectUrls() { cancel_url = redirectUrl, return_url = redirectUrl }; // Create details object var details = new Details() { tax = "1", shipping = "2", subtotal = cart.Items.Sum(x => x._shopping_quantity * x._shopping_product.UnitPrice).ToString() }; // Create amount object var amount = new Amount() { currency = "USD", total = (Convert.ToDouble(details.tax) + Convert.ToDouble(details.shipping) + Convert.ToDouble(details.subtotal)).ToString(),// tax + shipping + subtotal details = details }; // Create transaction var transactionList = new List <Transaction>(); transactionList.Add(new Transaction() { description = "Chien Testing transaction description", invoice_number = Convert.ToString((new Random()).Next(100000)), amount = amount, item_list = listItems }); payment = new Payment() { intent = "sale", payer = payer, transactions = transactionList, redirect_urls = redirUrls }; cart.ClearCart(); return(payment.Create(apiContext)); }
public ActionResult CheckOut(FormCollection form) { string email = Session["Email"].ToString(); var diemtichluy = db.Users.Where(s => s.Email.StartsWith(email)).ToList(); foreach (var tichluy in diemtichluy) { tichluy.DiemTichLuy += 1; if (tichluy.DiemTichLuy >= 5 && tichluy.DiemTichLuy < 20) { tichluy.IDPhanLoai = 2; Session["CheckID"] = 2; } else if (tichluy.DiemTichLuy >= 20) { tichluy.IDPhanLoai = 3; Session["CheckID"] = 3; } else if (tichluy.DiemTichLuy < 5) { tichluy.IDPhanLoai = 1; Session["CheckID"] = 1; } } try { Cart cart = Session["Cart"] as Cart; Order _order = new Order(); string check = form["ship"].ToString(); if ((int)Session["CheckID"] == 1) { if (check == "2") { _order.Descriptions = "Thanh Toán Khi Nhận Hàng , đã bao gòm phí ship $" + form["ship"].ToString(); _order.Amount = int.Parse(form["amount1"]); } else if (check == "3") { _order.Descriptions = " Thanh Toán Khi Nhận Hàng , đã bao gòm phí ship $" + form["ship"].ToString(); _order.Amount = int.Parse(form["amount2"]); } } else if ((int)Session["CheckID"] == 2) { if (check == "2") { _order.Descriptions = "Miễn phí ship (Khách Hàng VIP)" /*+ form["ship"].ToString()*/; _order.Amount = int.Parse(form["amount"]); } else if (check == "3") { _order.Descriptions = "Miễn phí ship (Khách Hàng VIP)" /*+ form["ship"].ToString()*/; _order.Amount = int.Parse(form["amount"]); } } else if ((int)Session["CheckID"] == 3) { if (check == "2") { _order.Descriptions = "Giảm 10% + Miễn phí ship (Khách Hàng SVIP)" /*+ form["ship"].ToString()*/; _order.Amount = double.Parse(form["amount3"]); } else if (check == "3") { _order.Descriptions = "Giảm 10% + Miễn phí ship (Khách Hàng SVIP)" /*+ form["ship"].ToString()*/; _order.Amount = double.Parse(form["amount3"]); } } //foreach (var tichluy in diemtichluy) //{ // if (tichluy.IDPhanLoai == 1) // { // if (check == "2") // { // _order.Descriptions = "Thanh Toán Khi Nhận Hàng, đã bao gòm phí ship $" + form["ship"].ToString(); // _order.Amount = int.Parse(form["amount1"]); // } // else if (check == "3") // { // _order.Descriptions = "Thanh Toán Khi Nhận Hàng , đã bao gòm phí ship $" + form["ship"].ToString(); // _order.Amount = int.Parse(form["amount2"]); // } // } // else if (tichluy.IDPhanLoai == 2) // { // if (check == "2") // { // _order.Descriptions = "Thanh Toán Khi Nhận Hàng, Miễn phí ship (Khách Hàng VIP)" + form["ship"].ToString(); // _order.Amount = int.Parse(form["amount"]); // } // else if (check == "3") // { // _order.Descriptions = "Thanh Toán Khi Nhận Hàng , Miễn phí ship (Khách Hàng VIP)" + form["ship"].ToString(); // _order.Amount = int.Parse(form["amount"]); // } // } // else if (tichluy.IDPhanLoai == 3) // { // if (check == "2") // { // _order.Descriptions = "Thanh Toán Khi Nhận Hàng, Giảm 10% + Miễn phí ship (Khách Hàng SVIP)" + form["ship"].ToString(); // _order.Amount = int.Parse(form["amount3"]); // } // else if (check == "3") // { // _order.Descriptions = "Thanh Toán Khi Nhận Hàng , Giảm 10% + Miễn phí ship (Khách Hàng SVIP)" + form["ship"].ToString(); // _order.Amount = int.Parse(form["amount3"]); // } // } //} _order.OrderDate = DateTime.Now; _order.TinhTrangDonHang = false; _order.TinhTrangGiao = false; _order.TinhTrangThanhToan = false; _order.HuyDon = false; _order.TinhTrangDongGoi = false; _order.Email = Session["Email"].ToString(); _order.Address_Cus = form["diachi"]; //_order.Descriptions = "Thanh Toán Khi Nhận Hàng"; //_order.Amount = int.Parse(form["amount"]); db.Orders.Add(_order); //**Check order xong, sửa lại giao diện**// foreach (var item in cart.Items) { OrderDetail _order_Detail = new OrderDetail(); _order_Detail.IDOrder = _order.IDOrder; int var = (int)_order_Detail.IDOrder; Session["mhd"] = var; _order_Detail.ngayDat = _order.OrderDate; _order_Detail.namePro = item._shopping_product.NameProduct; _order_Detail.ngayNhan = DateTime.Now.AddDays(5); _order_Detail.IDProduct = item._shopping_product.IDProduct; _order_Detail.UnitPriceSale = item._shopping_product.UnitPrice; _order_Detail.imgPro = item._shopping_product.Images; _order_Detail.QuantitySale = item._shopping_quantity; var check1 = db.Products.Find(item._shopping_product.IDProduct); check1.soLuong -= item._shopping_quantity; db.Entry(check1).State = EntityState.Modified; db.OrderDetails.Add(_order_Detail); } db.SaveChanges(); cart.ClearCart(); return(View("SuccessView")); } catch (Exception) { return(RedirectToAction("Index", "SanPham")); } }