Exemplo n.º 1
0
        public ActionResult ProcessOrder()
        {
            List <CartItem> ListCart = (List <CartItem>)Session[strCart];

            // save order
            var       order = new WebApplication.Models.Order();
            UserLogin cus   = (UserLogin)Session[CommonConstants.USER_SESSION];

            order.Order_Date = DateTime.Now;

            order.Customer_ID = cus.Customer_ID;
            order.Status      = "Pending";


            // save od
            foreach (CartItem cart in ListCart)
            {
                OrderDetail orderdetail = new OrderDetail()
                {
                    Quantity         = cart.Quantity,
                    Product_Price    = cart.Product.Product_Price,
                    Product_Discount = cart.Product.Product_Discount,
                    Product_ID       = cart.Product.Product_ID,
                    Order_ID         = order.Order_ID
                };
                var model = db.Products.Single(x => x.Product_ID == cart.Product.Product_ID);

                if (cart.Quantity > model.Quantity)
                {
                    return(RedirectToAction("Fail", "Cart"));
                }
                else
                {
                    db.OrderDetails.Add(orderdetail);
                    db.Orders.Add(order);
                    db.SaveChanges();

                    model.Quantity = model.Quantity - orderdetail.Quantity;
                    db.SaveChanges();
                }
            }


            Session.Remove(strCart);

            return(View("Success"));
        }
Exemplo n.º 2
0
        public ActionResult PaymentWithPaypal()
        {
            var session = Session[CommonConstants.USER_SESSION];

            if (session == null || session == " ")
            {
                return(RedirectToAction("Login", "User"));
            }

            List <CartItem> ListCart = (List <CartItem>)Session[strCart];

            // save order
            var       order = new WebApplication.Models.Order();
            UserLogin cus   = (UserLogin)Session[CommonConstants.USER_SESSION];

            order.Order_Date = DateTime.Now;

            order.Customer_ID = cus.Customer_ID;
            order.Status      = "Pending";



            // save od
            foreach (CartItem cart in ListCart)
            {
                OrderDetail orderdetail = new OrderDetail()
                {
                    Quantity         = cart.Quantity,
                    Product_Price    = cart.Product.Product_Price,
                    Product_Discount = cart.Product.Product_Discount,
                    Product_ID       = cart.Product.Product_ID,
                    Order_ID         = order.Order_ID
                };
                TempData["Quan"] = cart.Quantity;
                TempData["PN"]   = cart.Product.Product_Name;
                TempData["pri"]  = cart.Product.Product_Price - ((cart.Product.Product_Price * cart.Product.Product_Discount) / 100);
                TempData["Tot"]  = (cart.Product.Product_Price - ((cart.Product.Product_Price * cart.Product.Product_Discount) / 100)) * cart.Quantity;

                var model = db.Products.Single(x => x.Product_ID == cart.Product.Product_ID);

                if (cart.Quantity > model.Quantity)
                {
                    return(RedirectToAction("Fail", "Cart"));
                }

                db.OrderDetails.Add(orderdetail);
                model.Quantity = model.Quantity - orderdetail.Quantity;
            }
            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 = ExcutePayment(apiContext, payerId, Session[guid] as string);
                    if (executePayment.state.ToLower() != "approved")
                    {
                        return(View("Fail"));
                    }
                }
            }
            catch (Exception ex)
            {
                PaypalLogger.Log("Error: " + ex.Message);
                return(View("Fail"));
            }
            Session.Remove(strCart);
            // Send mail
            db.Orders.Add(order);
            string content = System.IO.File.ReadAllText(Server.MapPath("~/Theme/client/template/order.html"));

            content = content.Replace("{{ProductName}}", TempData["PN"].ToString());
            content = content.Replace("{{Quantity}}", TempData["Quan"].ToString());
            content = content.Replace("{{Price}}", TempData["pri"].ToString());
            content = content.Replace("{{Total}}", TempData["Tot"].ToString());
            content = content.Replace("{{Date}}", DateTime.Now.ToString());
            content = content.Replace("{{CustomerName}}", cus.Username);
            content = content.Replace("{{FullName}}", cus.Customer_Name);
            var toEmail = cus.Email;


            new MailHelper().SendMail(toEmail, "Jordan Shop Receipt", content);
            db.SaveChanges();



            return(View("Success"));
        }