Пример #1
0
        /// <summary>
        /// Repsonsible for behavior of Create User button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            string user = System.Web.HttpContext.Current.User.Identity.Name;

            var store = new UserStore <ApplicationUser>(new ApplicationDbContext());

            store.AutoSaveChanges = false;

            var currentUserId = User.Identity.GetUserId();
            var manager       = new UserManager <ApplicationUser>(store);
            var currentUser   = manager.FindById(User.Identity.GetUserId());

            if (currentUser == null)
            {
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                authenticationManager.SignOut();
                HttpContext.Current.Response.Redirect("~/Account/Login.aspx");
            }

            if (Page.IsValid)
            {
                //instantiate MyUserInfo
                currentUser.MyUserInfo = new MyUserInfo();

                currentUser.MyUserInfo.FirstName  = txtFirstName.Text;
                currentUser.MyUserInfo.MiddleName = txtMiddleName.Text;
                currentUser.MyUserInfo.LastName   = txtLastName.Text;
                currentUser.MyUserInfo.Email      = txtEmail.Text;
                currentUser.MyUserInfo.Telephone  = txtTelephone.Text;
                currentUser.MyUserInfo.Cellphone  = txtCellPhone.Text;

                //instantiate BillingAddress
                currentUser.BillingAddress = new MyUserBillingAddress();

                currentUser.BillingAddress.AddressName   = txtBillingAddressName.Text;
                currentUser.BillingAddress.AddressNumber = txtBillingAddressNumber.Text;
                currentUser.BillingAddress.Stair         = txtBillingStair.Text;
                currentUser.BillingAddress.Apartment     = txtBillingApartment.Text;
                currentUser.BillingAddress.City          = txtBillingCity.Text;
                currentUser.BillingAddress.Country       = txtBillingCountry.Text;
                currentUser.BillingAddress.Zipcode       = txtBillingZipcode.Text;

                //instantiate Shipping Address
                currentUser.ShippingAddress = new MyUserShippingAddress();

                currentUser.ShippingAddress.AddressName   = txtShippingAddressName.Text;
                currentUser.ShippingAddress.AddressNumber = txtShippingAddressNumber.Text;
                currentUser.ShippingAddress.Stair         = txtShippingAddressStair.Text;
                currentUser.ShippingAddress.Apartment     = txtShippingAddressApartment.Text;
                currentUser.ShippingAddress.City          = txtShippingAddressCity.Text;
                currentUser.ShippingAddress.Country       = txtShippingAddressCountry.Text;
                currentUser.ShippingAddress.Zipcode       = txtShippingAddressZipcode.Text;

                //instantiate Credit card info
                currentUser.MyUserCCardInfo = new MyUserCCardInfo();

                currentUser.MyUserCCardInfo.CardName         = txtCardName.Text;
                currentUser.MyUserCCardInfo.CardNumber       = txtCardNumber.Text;
                currentUser.MyUserCCardInfo.CardExpiryDate   = txtCardExpiryDate.Text;
                currentUser.MyUserCCardInfo.CardSecurityCode = txtCardSecurityCode.Text;

                ////instantiate Bank info
                //currentUser.MyUserBankInfo = new MyUserBankInfo();
                //currentUser.MyUserBankInfo.BankName = BankName.Text;
                //currentUser.MyUserBankInfo.BankAccountNo = BankAccNumber.Text;
                //currentUser.MyUserBankInfo.BicNo = BankBicNo.Text;
                //currentUser.MyUserBankInfo.BankSwift = BankSwift.Text;


                //Create customer
                Customer customer = new Customer()
                {
                    FirstName  = currentUser.MyUserInfo.FirstName,
                    MiddleName = currentUser.MyUserInfo.MiddleName,
                    LastName   = currentUser.MyUserInfo.LastName,
                    Phone      = currentUser.MyUserInfo.Telephone,
                    CellPhone  = currentUser.MyUserInfo.Cellphone,
                    Email      = currentUser.MyUserInfo.Email
                                 //BillingAddress = billingAddress
                };

                //store all
                store.Context.SaveChanges();

                //Finally create the order
                //Create order
                shoppingCart.CreateOrder(currentUser);

                Response.Redirect("~/Secure/UserPagesSecured/ConfirmOrder.aspx");
            }
        }
Пример #2
0
        /// <summary>
        /// Provide behavior for checkout button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCheckOut_Click(object sender, EventArgs e)
        {
            EntityMappingContext ctx = new EntityMappingContext();

            //instantiate store engine to get cart items
            ShoppingCartEngine cartEngine = new ShoppingCartEngine();


            //create an order status object in order to set it to submitted
            OrderStatus orderstatus = ctx.OrderStatuses.Create();

            //get the cart items
            List <CartItem> cartItemList = cartEngine.GetCartItems();

            // Session["CartItems"] = cartItemList;

            orderstatus.Status = "Created " + DateTime.Now.ToString();

            //Session["Error"] = orderstatus.Status;
            //Response.Redirect("/UserPages/ErrorPage.aspx");

            try
            {
                ctx.OrderStatuses.Add(orderstatus);
                ctx.SaveChanges();
                //validate
                ctx.Configuration.ValidateOnSaveEnabled = true;
            }
            catch (DbEntityValidationException ex)
            {
                var errorMessages = ex.EntityValidationErrors
                                    .SelectMany(x => x.ValidationErrors)
                                    .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                Session["Error"] = fullErrorMessage;

                Response.Redirect("ErrorPage.aspx");

                // Throw a new DbEntityValidationException with the improved exception message.
                throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
            }


            //now deal with registering the user's data. If he / she is not registered
            //He will be prompted to register, otherwise he will proceed to simply checkout

            AdminEngine adminEngine = new AdminEngine();

            //// && System.Web.HttpContext.Current.User.IsInRole("user")
            //&& System.Web.HttpContext.Current.User.IsInRole("Customer")

            string user = System.Web.HttpContext.Current.User.Identity.Name;

            var store = new UserStore <ApplicationUser>(new ApplicationDbContext());

            store.AutoSaveChanges = false;

            var currentUserId = User.Identity.GetUserId();
            var manager       = new UserManager <ApplicationUser>(store);
            var currentUser   = manager.FindById(User.Identity.GetUserId());

            //if the current user is null he is not authenticated
            if (!(currentUser == null))
            {
                //If the object to be checked is null create it
                if (currentUser.MyUserCCardInfo == null)
                {
                    //creating object
                    currentUser.MyUserCCardInfo = new MyUserCCardInfo();
                    //go to enter data
                    Response.Redirect("~/UserPages/EnterUserData.aspx");
                }
                else
                {   //If the credit card number is not null the record exists
                    //and just confirmation is required
                    if (!(currentUser.MyUserCCardInfo.CardNumber == null))
                    {
                        //Create order
                        shoppingCart.CreateOrder(currentUser);

                        //go to confirm
                        Response.Redirect("~/Secure/UserPagesSecured/ConfirmOrder.aspx");
                    }
                    else
                    {
                        //if not then enter user data
                        Response.Redirect("~/UserPages/EnterUserData.aspx");
                    }
                }
            }
            //if the user is not authenticated then make sure he is logged out and send him to login
            else
            {
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                authenticationManager.SignOut();
                HttpContext.Current.Response.Redirect("~/Account/Login.aspx");
            }

            //check if the credit card number is null if not then just confirm
            if (!(currentUser.MyUserCCardInfo.CardNumber == null))
            {
                //Create order
                shoppingCart.CreateOrder(currentUser);

                //go to confirm
                Response.Redirect("~/Secure/UserPagesSecured/ConfirmOrder.aspx");
            }

            //if the user name is null then login
            if (user == null)
            {
                HttpContext.Current.Response.Redirect("~/Account/Login.aspx");
            }
        }