示例#1
0
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            User user = (User)Session["User"];
            if (user == null)
                Response.Redirect("Register.aspx");
            List<ShoppingCart> lst = (List<ShoppingCart>)Session["ShoppingCart"];

            if(ddlTypePayment.Value == ONLINE)
            {
                int indexRow = 0;
                int? totalPrice = 0;
                CartOnline cartOnl = new CartOnline();
                cartOnl.lstSP = new List<ShoppingCart>();
                foreach (GridViewRow r in gvProducts.Rows)
                {
                    DropDownList ddl = r.FindControl("ddlColor") as DropDownList;
                    ShoppingCart sp = new ShoppingCart();
                    sp.ProductID = lst[indexRow].ProductID;
                    sp.color = ddl.SelectedItem.Text;
                    sp.Price = lst[indexRow].Price;
                    sp.ProductName = lst[indexRow].ProductName;
                    sp.timeInsurance = lst[indexRow].timeInsurance;
                    cartOnl.lstSP.Add(sp);
                    totalPrice += lst[indexRow].Price;
                    indexRow++;
                }
                cartOnl.DateTransfer = DateTime.Now;
                Session["CartOnl"] = cartOnl;
                string redirectUrl = "";
                totalPrice = totalPrice / 20000;
                //Mention URL to redirect content to paypal site
                redirectUrl += "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_xclick&business=" + ConfigurationManager.AppSettings["paypalemail"].ToString();

                //First name I assign static based on login details assign this value
                redirectUrl += "&first_name="+ user.FirstName;
                redirectUrl += "&last_name=" + user.LastName;

                //Product Name
                redirectUrl += "&item_name=" + "Mobile";

                //Business contact paypal EmailID
                redirectUrl += "&[email protected]";
                redirectUrl += "&amount=" + totalPrice.ToString();

                //Quantiy of product, Here statically added quantity 1
                redirectUrl += "&quantity=1";

                //If transactioin has been successfully performed, redirect SuccessURL page- this page will be designed by developer
                redirectUrl += "&return=" + "http://localhost:3475/ThankYou";

                //If transactioin has been failed, redirect FailedURL page- this page will be designed by developer
                redirectUrl += "&cancel_return=" + "http://localhost:3475/Home";

                Response.Redirect(redirectUrl);
            }

            Cart cart = new Cart();
            cart.CreatedDate = DateTime.UtcNow;
            cart.CustomerID = user.UserID;
            cart.Quantity = lst.Count;
            cart.DateTranfer = DateTime.Parse(txtGiaoHang.Value);
            cart.TypePayment = ddlTypePayment.Value;
            cart.Status = "Processing";

            db.Carts.Add(cart);
            db.SaveChanges();

            string s = Server.MapPath("xml");
            string fileName = user.LastName + user.FirstName + "_" + cart.CartID;
            string path_file = string.Format("{0}/{1}.xml", s, fileName);
            XmlTextWriter xtw = new XmlTextWriter(path_file, System.Text.Encoding.UTF8);
            xtw.Formatting = Formatting.Indented;
            xtw.WriteStartDocument();
            xtw.WriteStartElement("Cart");
            xtw.WriteStartElement("CustomerInformation");
            xtw.WriteStartElement("CustomerName");
            xtw.WriteString(user.LastName + " " + user.FirstName);
            xtw.WriteEndElement();
            xtw.WriteStartElement("CustomerEmail");
            xtw.WriteString(user.Email);
            xtw.WriteEndElement();
            xtw.WriteStartElement("CustomerPhone");
            xtw.WriteString(user.Phone);
            xtw.WriteEndElement();
            xtw.WriteStartElement("CustomerAddress");
            xtw.WriteString(user.Address);
            xtw.WriteEndElement();
            xtw.WriteStartElement("DateTransfer");
            xtw.WriteString(txtGiaoHang.Value);
            xtw.WriteEndElement();
            xtw.WriteStartElement("DateCreated");
            xtw.WriteString(DateTime.Now.ToString());
            xtw.WriteEndElement();

            xtw.WriteEndElement();
            int index = 0;
            int? total = 0;
            foreach (GridViewRow r in gvProducts.Rows)
            {
                DropDownList ddl = r.FindControl("ddlColor") as DropDownList;
                string color = ddl.SelectedItem.Text;
                createNode(lst[index].ProductID.ToString(), lst[index].ProductName, lst[index].Price.ToString(), color, lst[index].timeInsurance.ToString(), xtw);
                total += lst[index].Price;
                CartItem cartItem = new CartItem();
                cartItem.MobileID = lst[index].ProductID;
                cartItem.CartID = cart.CartID;
                cartItem.Color = color;
                db.CartItems.Add(cartItem);
                int id = int.Parse(lst[index].ProductID.ToString());
                MobilePhone mobile = db.MobilePhones.Where(n => n.MobileID == id).SingleOrDefault();
                mobile.Quantity--;
                db.SaveChanges();
                index++;
            }
            cart.TotalPrice = total;
            db.SaveChanges();
            xtw.WriteEndElement();
            xtw.WriteEndDocument();
            xtw.Flush();
            xtw.Close();
            Session["ShoppingCart"] = null;
            Response.Redirect("Home.aspx");

            //CODE XML
        }
 protected void linkBtnAddToCart_Click(object sender, EventArgs e)
 {
     int nProductBought = int.Parse(nPhoneBought.Value.ToString());
     int id = int.Parse(Request.QueryString["id"]);
     MobilePhone mobile = db.MobilePhones.Where(n => n.MobileID == id).SingleOrDefault();
     List<ShoppingCart> lstCart;
     if (Session["ShoppingCart"] != null)
     {
         lstCart = (List<ShoppingCart>)Session["ShoppingCart"];
     }
     else lstCart = new List<ShoppingCart>();
     for (int i = 1; i <= nProductBought; i++)
     {
         ShoppingCart sp = new ShoppingCart();
         sp.ProductID = mobile.MobileID;
         sp.ProductName = mobile.MobileName;
         sp.Price = mobile.Price;
         sp.timeInsurance = mobile.TimeInsurance + " months";
         lstCart.Add(sp);
     }
     Session["ShoppingCart"] = lstCart;
     Response.Redirect("Product.aspx");
 }