public ActionResult ProcessOrder(MVCWEF.Models.Order order) { User user = new User(); user = (User)@Session["User"]; List <Cart> lsCart = (List <Cart>)Session[strCart]; double TotalPrices = 0.0; double newbalance = 0.0; double balance = 0.0; foreach (Cart cart in lsCart) { TotalPrices += (double)cart.Product.Price * (double)cart.Quantity; } balance = user.Balance.Value; if (user.Balance >= TotalPrices) { newbalance = balance - TotalPrices; user.Balance = newbalance; db.Users.Attach(user); db.Entry(user).Property(x => x.Balance).IsModified = true; db.SaveChanges(); Session["User"] = user; using (MvcCrudDBEntities1 db = new MvcCrudDBEntities1()) { order.UserID = user.UserID; order.OrderDate = DateTime.Now; order.PaymentType = "Cash"; order.StatusID = 5; db.Orders.Add(order); db.SaveChanges(); } foreach (Cart cart in lsCart) { OrderDetail orderdetail = new OrderDetail() { OrderId = order.OrderId, ProductId = cart.Product.ProuductID, Quantity = cart.Quantity, TotalPrice = (double)cart.Product.Price * (double)cart.Quantity }; db.OrderDetails.Add(orderdetail); db.SaveChanges(); } Session.Remove(strCart); return(View("OrderSuccess")); } else { return(View("Failure")); } }
public ActionResult PaymentWithPaypal() { APIContext apiContext = PaypalConfiguration.GetAPIContext(); try { string payerId = Request.Params["PayerID"]; if (string.IsNullOrEmpty(payerId)) { //Creating Payment string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "ShoppingCart/PaymentWithPaypal?"; var guid = Convert.ToString((new Random()).Next(100000)); var createdPayment = CreatePayment(apiContext, baseURI + "guid=" + guid); //Get Links returned from paypal response to create call function 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; } } MVCWEF.Models.Order order = new MVCWEF.Models.Order(); User user = new User(); user = (User)@Session["User"]; List <Cart> lsCart = (List <Cart>)Session[strCart]; using (MvcCrudDBEntities1 db = new MvcCrudDBEntities1()) { order.UserID = user.UserID; order.CustomerAddress = user.Address; order.CustomerPhone = user.Phone; order.OrderDate = DateTime.Now; order.PaymentType = "Paypal"; order.StatusID = 6; db.Orders.Add(order); db.SaveChanges(); } foreach (Cart cart in lsCart) { OrderDetail orderdetail = new OrderDetail() { OrderId = order.OrderId, ProductId = cart.Product.ProuductID, Quantity = cart.Quantity, TotalPrice = ((double)cart.Product.Price * (double)cart.Quantity) + 3 }; db.OrderDetails.Add(orderdetail); db.SaveChanges(); } Session.Remove(strCart); Session.Add(guid, createdPayment.id); return(Redirect(paypalRedirectUrl)); } else { var guid = Request.Params["guid"]; var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string); if (executedPayment.state.ToLower() != "approval") { return(View("Failure")); } } }catch (Exception ex) { PaypalLogger.Log("Error :" + ex.Message); return(View("Failure")); } return(View("Success")); }