public void refreshCart() { if (Session["ID"] != null) { int id = Convert.ToInt32(Session["ID"]); using (AABZContext context = new AABZContext()) { Model.Cart cart = (from c in context.Carts where c.user_id == id select c).FirstOrDefault(); rptCart.DataSource = cart.products_cart.ToList(); rptCart.DataBind(); } } }
public void RedirectUser() { if (Session["ID"] != null) { int id = Convert.ToInt32(Session["ID"]); int orderId; double price; using (AABZContext context = new AABZContext()) { //This ensures that order is the most recent order the user has made. Order order = (from o in context.Orders where o.user_id == id orderby o.Id descending select o).FirstOrDefault(); orderId = order.Id; int userId = Convert.ToInt32(Session["ID"].ToString()); Model.Cart cart = (from c in context.Carts where c.user_id == userId select c).FirstOrDefault(); context.ProductsCarts.RemoveRange(context.ProductsCarts.Where(x => x.cart_id == cart.user_id)); context.SaveChanges(); price = getTotalOrderCost(order); } //Assign the values for the properties we need to pass to the service String AppId = System.Configuration.ConfigurationManager.AppSettings["CreditAppId"]; String SharedKey = System.Configuration.ConfigurationManager.AppSettings["CreditAppSharedKey"]; String AppTransId = orderId.ToString(); String AppTransAmount = price.ToString(); // Hash the values so the server can verify the values are original String hash = HttpUtility.UrlEncode(CreditAuthorizationClient.GenerateClientRequestHash(SharedKey, AppId, AppTransId, AppTransAmount)); //Create the URL and concatenate the Query String values String url = "http://ectweb2.cs.depaul.edu/ECTCreditGateway/Authorize.aspx"; url = url + "?AppId=" + AppId; url = url + "&TransId=" + AppTransId; url = url + "&AppTransAmount=" + AppTransAmount; url = url + "&AppHash=" + hash; //Redirect the User to the Service //Response.Redirect(url); Response.Redirect("~/OrderHistory.aspx"); } }
protected void removeItem(object sender, CommandEventArgs e) { if (Session["ID"] != null) { int id = Convert.ToInt32(e.CommandArgument); using (AABZContext context = new AABZContext()) { int userId = Convert.ToInt32(Session["ID"]); Model.Cart cart = (from c in context.Carts where c.user_id == userId select c).FirstOrDefault(); ProductsCart pc = (from p in context.ProductsCarts where p.Id == id select p).FirstOrDefault(); cart.products_cart.Remove(pc); context.ProductsCarts.Remove(pc); context.SaveChanges(); } } }
public double getTotalPrice() { if (Session["ID"] != null) { int id = Convert.ToInt32(Session["ID"]); using (AABZContext context = new AABZContext()) { Model.Cart cart = (from c in context.Carts where c.user_id == id select c).FirstOrDefault(); double cost = 0; foreach (ProductsCart pc in cart.products_cart) { cost += (pc.quantity * pc.Product.price); } return(cost); } } return(0); }
protected Order createOrder() { if (Session["ID"] != null) { try { int userId = Convert.ToInt32(Session["ID"].ToString()); using (AABZContext context = new AABZContext()) { Order order = new Order();//build order //get user cart Model.Cart cart = (from c in context.Carts where c.user_id == userId select c).FirstOrDefault(); //get user User usr = (from u in context.Users where u.Id == userId select u).First(); order.User = usr;//set order user List <UserInfo> ui = (from info in context.UserInfoes where info.user_id == userId select info).ToList(); if (ui.Count == 1 && ui.ElementAt(0).isBilling)//if one address and is billing { UserInfo usrinfo = ui.ElementAt(0); order.BillingAddress = usrinfo; order.billing_address = usrinfo.Id; order.ShippingAddress = usrinfo; order.shipping_address = usrinfo.Id; } else { foreach (UserInfo info in ui)//for each address assign apropriately { if (info.isBilling) { order.BillingAddress = info; order.billing_address = info.Id; } else { order.ShippingAddress = info; order.shipping_address = info.Id; } } } context.Orders.Add(order); context.SaveChanges(); ProductsOrder po = new ProductsOrder(); //for each product in cart create product order and add to order List <ProductsOrder> orders = new List <ProductsOrder>(); foreach (ProductsCart pc in cart.products_cart) { po = new ProductsOrder(); po.order_id = order.Id; po.Product = pc.Product; po.product_id = pc.product_id; po.quantity = pc.quantity; po.price = pc.Product.price * pc.quantity; po.Order = order; context.PoductsOrders.Add(po); orders.Add(po); } order.ProductsOrders = orders; //create payment Payment payment = new Payment(); payment.cc_name = txtCcName.Text; payment.cc_number = txtCcNumber.Text; payment.cc_month = Convert.ToInt32(drpCcMonth.SelectedValue); payment.cc_year = Convert.ToInt32(drpCcYear.SelectedValue); payment.cc_ccv = Convert.ToInt32(txtCcCvv.Text); order.Payments = payment;//set payment payment.Order = order; /* * if (ui.isBilling) * { * order.BillingAddress = ui; * } * else * { * UserInfo ui2 = new UserInfo(); * ui2.User = ui.User; * ui2.address_1 = txtAddress1.Text; * ui2.address_2 = txtAddress2.Text; * ui2.city = txtCity.Text; * ui2.state = txtState.Text; * ui2.zipcode = txtZipCode.Text; * ui2.phone = ui.phone; * order.BillingAddress = ui2; * } */ payment.order_id = order.Id; context.Payments.Add(payment); context.SaveChanges(); return(order); } }catch (DbEntityValidationException e) { return(null); } } else { return(null); } }