private void InitializeModel()
        {
            _model = new CheckoutViewModel();

            _model.Cart = HttpContext.Session.Get <Cart>("Cart");
            _model.AvailableExpirationYears = Enumerable.Range(1, 5).Select(i => DateTime.Now.Year + i).ToList();
            _model.ShippingOptions          = _shipperRepository.GetShippingOptions(_model.Cart?.SubTotal ?? 0);

            if (_model.Cart == null || _model.Cart.OrderDetails.Count == 0)
            {
                ModelState.AddModelError(string.Empty, "You have no items in your cart.");
            }

            var customer = GetCustomerOrAddError();

            if (customer != null)
            {
                var creditCard = GetCreditCardForUser();

                creditCard.GetCardForUser();
                _model.CreditCard      = creditCard.Number;
                _model.ExpirationMonth = creditCard.Expiry.Month;
                _model.ExpirationYear  = creditCard.Expiry.Year;

                _model.ShipTarget = customer.CompanyName;
                _model.Address    = customer.Address ?? string.Empty;
                _model.City       = customer.City ?? string.Empty;
                _model.Region     = customer.Region ?? string.Empty;
                _model.PostalCode = customer.PostalCode ?? string.Empty;
                _model.Country    = customer.Country ?? string.Empty;
            }
        }
Exemple #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            lblErrorMessage.Text = "";

            //Preload the shipping address if available
            _username = User.Identity.Name;
            _customer = _customerRepository.GetCustomerByUsername(_username);
            if (_customer == null)
            {
                lblErrorMessage.Text += "I can't identify you.  Please log in and try again.";
                return;
            }

            //Preload Credit card if available
            _creditCard = new CreditCard()
            {
                Filename = Server.MapPath("StoredCreditCards.xml"), Username = _username
            };
            try
            {
                _creditCard.GetCardForUser();
                _creditCardNumber = _creditCard.Number;
                _expiryMonth      = _creditCard.Expiry.Month;
                _expiryYear       = _creditCard.Expiry.Year;
            }
            catch (NullReferenceException)
            {
            }

            if (Session["Cart"] == null)
            {
                lblErrorMessage.Text = "You have no items in your cart.";
            }
            else
            {
                if (Session["Cart"] is Cart)
                {
                    _cart        = Session["Cart"] as Cart;
                    lblCart.Text = _cart.ToHtml("cartTable");
                }
                else
                {
                    lblErrorMessage.Text = "A problem has occurred. Your cart doesn't seem to be a valid cart.";
                }
            }

            if (!IsPostBack)
            {
                foreach (var option in _shipperRepository.GetShippingOptions(_cart.SubTotal))
                {
                    ddlShipVia.Items.Add(new ListItem(option.Value, option.Key.ToString()));
                }
                txtShipName.Text       = _customer.CompanyName;
                txtShipAddress.Text    = _customer.Address;
                txtShipCity.Text       = _customer.City;
                txtShipRegion.Text     = _customer.Region;
                txtShipPostalCode.Text = _customer.PostalCode;
                txtShipCountry.Text    = _customer.Country;
                for (int i = 0; i <= 5; i++)
                {
                    ddlExpiryYear.Items.Add((DateTime.Now.Year + i).ToString());
                }
                ddlExpiryMonth.SelectedValue = _expiryMonth.ToString("00");
                ddlExpiryYear.SelectedValue  = _expiryYear.ToString("0000");
                txtCreditCardNumber.Text     = _creditCard.Number;
            }
        }