Пример #1
0
        protected void btnPlaceOrder_Click(object sender, EventArgs e)
        {
            var order = new Order();

            //Get method of shipping and freightCost
            order.ShipVia        = Convert.ToInt32(ddlShipVia.SelectedValue);
            order.ShipName       = txtShipName.Text;
            order.ShipAddress    = txtShipAddress.Text;
            order.ShipCity       = txtShipCity.Text;
            order.ShipRegion     = txtShipRegion.Text;
            order.ShipPostalCode = txtShipPostalCode.Text;
            order.ShipCountry    = txtShipCountry.Text;
            order.OrderDetails   = _cart.OrderDetails;
            order.CustomerId     = _customer.CustomerId;
            order.OrderDate      = DateTime.Now;
            order.RequiredDate   = DateTime.Now.AddDays(7);
            order.Freight        = _shipperRepository.GetShipperByShipperId(Convert.ToInt32(ddlShipVia.SelectedValue)).GetShippingCost(_cart.SubTotal);
            //TODO: Throws an error if we don't set the date. Try to set it to null or something.
            order.ShippedDate = DateTime.Now.AddDays(3);
            order.EmployeeId  = 1;

            //Get form of payment
            //If old card is null or if the number, month or year were changed then take what was on the form.
            if (_creditCard.Number.Length <= 4)
            {
                _creditCard.Number = txtCreditCardNumber.Text;
                _creditCard.Expiry = new DateTime(Convert.ToInt32(ddlExpiryYear.SelectedValue),
                                                  Convert.ToInt32(ddlExpiryMonth.SelectedValue), 1);
            }
            else
            {
                if (txtCreditCardNumber.Text.Substring(txtCreditCardNumber.Text.Length - 4) !=
                    _creditCard.Number.Substring(_creditCard.Number.Length - 4))
                {
                    _creditCard.Number = txtCreditCardNumber.Text;
                }
                if (ddlExpiryMonth.SelectedValue != _creditCard.ExpiryMonth.ToString("00") ||
                    ddlExpiryYear.SelectedValue != _creditCard.ExpiryYear.ToString("0000"))
                {
                    _creditCard.Expiry = new DateTime(Convert.ToInt32(ddlExpiryYear.SelectedValue),
                                                      Convert.ToInt32(ddlExpiryMonth.SelectedValue), 1);
                }
            }
            //Authorize payment through our bank or Authorize.net or someone.
            if (!_creditCard.IsValid())
            {
                lblErrorMessage.Text = "That card is not valid.  Please enter a valid card.";
                return;
            }
            var approvalCode = _creditCard.ChargeCard(order.Total);

            if (chkStoreCCNumber.Checked)
            {
                _creditCard.SaveCardForUser();
            }

            var shipment = new Shipment()
            {
                ShipmentDate   = DateTime.Today.AddDays(1),
                ShipperId      = order.ShipVia,
                TrackingNumber = _shipperRepository.GetNextTrackingNumber(_shipperRepository.GetShipperByShipperId(order.ShipVia)),
            };
            //TODO: Uncommenting this line causes EF to throw exception when creating the order.
            //order.Shipment = shipment;

            //Create the order itself.
            int orderId = _orderRepository.CreateOrder(order);

            Session["OrderId"] = orderId;
            Session["Cart"]    = null;

            //Create the payment record.
            _orderRepository.CreateOrderPayment(orderId, order.Total, _creditCard.Number, _creditCard.Expiry, approvalCode);

            //Show success page
            Response.Redirect("Receipt.aspx");
        }
        public IActionResult Checkout(CheckoutViewModel model)
        {
            model.Cart = HttpContext.Session.Get <Cart>("Cart") !;

            var customer = GetCustomerOrAddError();

            if (customer == null)
            {
                return(View(model));
            }

            var creditCard = GetCreditCardForUser();

            try
            {
                creditCard.GetCardForUser();
            }
            catch (NullReferenceException)
            {
            }

            //Get form of payment
            //If old card is null or if the number, month or year were changed then take what was on the form.
            if (creditCard.Number.Length <= 4)
            {
                creditCard.Number = model.CreditCard;
                creditCard.Expiry = new DateTime(model.ExpirationYear, model.ExpirationMonth, 1);
            }
            else
            {
                if (model.CreditCard.Substring(model.CreditCard.Length - 4) !=
                    creditCard.Number.Substring(creditCard.Number.Length - 4))
                {
                    creditCard.Number = model.CreditCard;
                }

                if (model.ExpirationMonth != creditCard.ExpiryMonth || model.ExpirationYear != creditCard.ExpiryYear)
                {
                    creditCard.Expiry = new DateTime(model.ExpirationYear, model.ExpirationMonth, 1);
                }
            }

            //Authorize payment through our bank or Authorize.net or someone.
            if (!creditCard.IsValid())
            {
                ModelState.AddModelError(string.Empty, "That card is not valid. Please enter a valid card.");
                _model = model;
                return(View(_model));
            }

            if (model.RememberCreditCard)
            {
                creditCard.SaveCardForUser();
            }

            var order = new Order
            {
                ShipVia        = model.ShippingMethod,
                ShipName       = model.ShipTarget,
                ShipAddress    = model.Address,
                ShipCity       = model.City,
                ShipRegion     = model.Region,
                ShipPostalCode = model.PostalCode,
                ShipCountry    = model.Country,
                OrderDetails   = model.Cart.OrderDetails,
                CustomerId     = customer.CustomerId,
                OrderDate      = DateTime.Now,
                RequiredDate   = DateTime.Now.AddDays(7),
                Freight        = Math.Round(_shipperRepository.GetShipperByShipperId(model.ShippingMethod).GetShippingCost(model.Cart.SubTotal), 2),
                EmployeeId     = 1,
            };

            var approvalCode = creditCard.ChargeCard(order.Total);

            order.Shipment = new Shipment()
            {
                ShipmentDate   = DateTime.Today.AddDays(1),
                ShipperId      = order.ShipVia,
                TrackingNumber = _shipperRepository.GetNextTrackingNumber(_shipperRepository.GetShipperByShipperId(order.ShipVia)),
            };

            //Create the order itself.
            var orderId = _orderRepository.CreateOrder(order);

            //Create the payment record.
            _orderRepository.CreateOrderPayment(orderId, order.Total, creditCard.Number, creditCard.Expiry, approvalCode);

            HttpContext.Session.SetInt32("OrderId", orderId);
            HttpContext.Session.Remove("Cart");
            return(RedirectToAction("Receipt"));
        }