예제 #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //If QueryString parameters are available, use them.  If not, get them from the user
                _carrier        = Request.QueryString["Carrier"];
                _trackingNumber = Request.QueryString["TrackingNumber"];

                if (!string.IsNullOrEmpty(_carrier) && !string.IsNullOrEmpty(_trackingNumber))
                {
                    Response.Redirect(Order.GetPackageTrackingUrl(_carrier, _trackingNumber).Value);
                }

                //ddlCarriers.SelectedValue = _carrier;
                txtCarrier.Text        = _carrier;
                txtTrackingNumber.Text = _trackingNumber;

                //Load this user's orders.
                if (User.Identity.IsAuthenticated)
                {
                    var customerId = _customerRepository.GetCustomerByUsername(User.Identity.Name).CustomerId;
                    var orders     = _orderRepository.GetAllOrdersByCustomerId(customerId);
                    if (orders.Count == 0)
                    {
                        lblOrders.Text = "You have no orders.";
                    }
                    else
                    {
                        var sb = new StringBuilder();
                        sb.Append("<table>");
                        sb.AppendFormat("<tr><th>{0}</th><th>{1}</th><th>{2}</th><th>{3}</th><th>{4}</th><th>{5}</th></tr>", "Order", "Order date", "Shipping Cost", "Shipping Method", "Order Total", "Tracking number");
                        foreach (var order in orders)
                        {
                            var trackingNumber = "No tracking number recorded.";
                            try
                            {
                                trackingNumber = _orderRepository.GetShipmentByOrderId(order.OrderId).TrackingNumber;
                            }
                            catch (Exception)
                            {}
                            var shipper = _shipperRepository.GetShipperByShipperId(order.ShipVia);
                            sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2:C}</td><td>{3}</td><td>{4:C}</td><td>{5}</td></tr>", order.OrderId, order.OrderDate, order.Freight, shipper.CompanyName, order.SubTotal, trackingNumber);
                        }
                        sb.Append("</table>");
                        lblOrders.Text = sb.ToString();
                    }
                    yourOrdersPanel.Visible = true;
                }
            }
        }
예제 #2
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"));
        }