// **************************************
        // URL: /Account/LogOff
        // **************************************

        public ActionResult LogOff()
        {
            var cart = RentalCart.GetCart(this.HttpContext);

            cart.EmptyCart();

            FormsService.SignOut();
            FacebookWebContext.Current.DeleteAuthCookie();

            Session["RentalCartItems"] = 0;
            return(RedirectToAction("Index", "Home"));
        }
Exemple #2
0
        //
        // GET: /RentalCart/
        public ActionResult Index()
        {
            var cart = RentalCart.GetCart(this.HttpContext);

            // Set up our ViewModel
            var viewModel = new RentalCartViewModel
            {
                CartItems = cart.GetCartItems(),
                CartTotal = cart.GetTotal()
            };

            // Return the view
            return(View(viewModel));
        }
Exemple #3
0
        public ActionResult PayPalCheckOut()
        {
            var    cart   = RentalCart.GetCart(this.HttpContext);
            string retMsg = "";
            string token  = "";

            if (cart.GetCartItems().Count == 0)
            {
                return(RedirectToAction("ErrorMessage", "Checkout", new { ErrorCode = ErrorCode.EMPTY_CART }));
            }

            foreach (CartItemModel c in cart.GetCartItems())
            {
                if (c.Item.Owner.Email == User.Identity.Name)
                {
                    return(RedirectToAction("ErrorMessage", "Checkout", new { ErrorCode = ErrorCode.OWNER_ITEM_IN_CART }));
                }
            }

            NVPAPICaller checkout = new NVPAPICaller(complete: Url.Action("Complete", "CheckOut", null, Request.Url.Scheme, Request.Url.Host), cancel: Url.Action("Cancel", "CheckOut", null, Request.Url.Scheme, Request.Url.Host));

            var order = new Order()
            {
                OrderId   = System.Guid.NewGuid().ToString(),
                OrderDate = DateTime.Now,
                User      = market.Users.Find(User.Identity.Name),
                Confirmed = false
            };

            market.Orders.Add(order);
            market.SaveChanges();
            cart.CreateOrder(order);

            string amt = order.Total.ToString();
            bool   ret = checkout.ShortcutExpressCheckout(amt, checkout.GetNVPFromOrder(order), ref token, ref retMsg);

            if (ret)
            {
                order.PayPalToken         = token;
                market.Entry(order).State = System.Data.EntityState.Modified;
                market.SaveChanges();
                return(Redirect(retMsg));
            }
            else
            {
                return(Redirect(Url.Action("ErrorMessage", "Checkout") + retMsg + "&Order=" + order.OrderId));
            }
        }
Exemple #4
0
        public ActionResult RemoveFromCart(int id)
        {
            string msg;
            // Remove the item from the cart
            RentalCart cart     = null;
            string     itemName = "";

            try
            {
                cart = RentalCart.GetCart(this.HttpContext);

                // Get the name of the Item to display confirmation
                itemName = market.CartItems
                           .Single(item => item.id == id).Item.Name;

                // Remove from cart
                cart.RemoveFromCart(id);
            }
            catch (Exception ex)
            {
                ErrorMessage.ErrorCode = ErrorCode.UNKNOWN;
                return(View("ErrorMessage", ErrorMessage));
            }

            if (cart.GetCartItems().Count == 0)
            {
                msg = "Your Rental Cart is Empty.";
            }
            else
            {
                msg = Server.HtmlEncode(itemName) +
                      " has been removed from your shopping cart.";
            }
            // Display the confirmation message
            var results = new RentalCartRemoveViewModel
            {
                Message   = msg,
                CartTotal = cart.GetTotal(),
                DeleteId  = id,
                CartCount = cart.GetCartItems().Count
            };

            Session["RentalCartItems"] = results.CartCount;
            return(Json(results));
        }
Exemple #5
0
        public ActionResult Complete(string token, string PayerID)
        {
            if (string.IsNullOrEmpty(token) && string.IsNullOrEmpty(PayerID))
            {
                //Need to specify error handling
                return(RedirectToAction("ErrorMessage", "Checkout", new { ErrorCode = ErrorCode.PAYPAL_ERROR }));
            }

            var    order   = market.Orders.SingleOrDefault(o => o.PayPalToken == token);
            var    decoder = new NVPCodec();
            string retMsg  = "";

            // This is where we call DoExpressCheckoutPayment
            NVPAPICaller completeCheckout = new NVPAPICaller(token: token, PayerID: PayerID, total: order.Total.ToString());
            bool         ret = completeCheckout.ConfirmPayment(order.Total.ToString(), ref decoder, ref retMsg);

            if (ret)
            {
                var cart = RentalCart.GetCart(this.HttpContext);
                cart.EmptyCart();
                Session["RentalCartItems"] = 0;


                order.Confirmed           = true;
                market.Entry(order).State = System.Data.EntityState.Modified;
                market.SaveChanges();

                var orderDetails = market.OrderDetails.Where(o => o.OrderId == order.OrderId).ToList();

                if (order != null)
                {
                    foreach (OrderDetailModel o in orderDetails)
                    {
                        if (EmailSecurityCode(User.Identity.Name, o.OrderDetailId, false))
                        {
                        }
                        //Need to specify error handling
                        else
                        {
                            return(RedirectToAction("ErrorMessage", "Checkout", new { ErrorCode = ErrorCode.UNKNOWN }));
                        }
                        if (EmailSecurityCode(market.OrderDetails.Find(o.OrderDetailId).Item.Owner.Email, o.OrderDetailId, true))
                        {
                        }
                        //Need to specify error handling
                        else
                        {
                            return(RedirectToAction("ErrorMessage", "Checkout", new { ErrorCode = ErrorCode.UNKNOWN }));
                        }
                        o.Status = (int)OrderStatus.ORDER_TENTATIVE;
                        market.Entry(o).State = System.Data.EntityState.Modified;
                        market.SaveChanges();
                    }
                    return(View(order.OrderDetails));
                }
                return(RedirectToAction("ErrorMessage", "Checkout", new { ErrorCode = ErrorCode.UNKNOWN }));
            }
            else
            {
                //PayPal payment didn't go through
                return(Redirect(Url.Content("~/CheckOut/ErrorMessage") + retMsg));
            }
            //Need to specify error handling
            return(RedirectToAction("ErrorMessage", "Checkout", new { ErrorCode = ErrorCode.UNKNOWN }));
        }
Exemple #6
0
        public ActionResult AddToCart(FormCollection collection)
        {
            string   id      = collection.Get("itemId");
            string   pickup  = collection.Get("pickupdate");
            string   dropoff = collection.Get("dropoffdate");
            DateTime pickupDate;
            DateTime dropoffDate;

            if (String.IsNullOrEmpty(id) || String.IsNullOrEmpty(pickup) || String.IsNullOrEmpty(dropoff))
            {
                string error = "Plase select accurate dates and try again.";
                return(RedirectToAction("Details", "Item", new { id = collection.Get("itemid"), errMsg = error }));
            }

            try
            {
                pickupDate  = DateTime.Parse(pickup);
                dropoffDate = DateTime.Parse(dropoff);
            }
            catch (Exception ex)
            {
                string error = "Please select accurate dates and try again.";
                return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
            }
            //Invalid Dates
            if (pickupDate.Date < DateTime.Today.Date || dropoffDate.Date < DateTime.Today.Date)
            {
                string error = "Please select accurate dates and try again.";
                return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
            }

            int rentalPeriod = (dropoffDate - pickupDate).Days;

            if (rentalPeriod <= 0)
            {
                string error = "Dropoff date cannot be earlier than or the same as the pickup date.";
                return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
            }

            // Retrieve the album from the database
            var addedItem = market.Items.SingleOrDefault(m => m.Id == id);

            if (addedItem == null)
            {
                string error = "The item you specified doesn't exist on Rambla.";
                return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
            }

            if (User.Identity.IsAuthenticated)
            {
                if (addedItem.Owner.Email == User.Identity.Name)
                {
                    string error = "You cannot add your own item to your Rental cart.";
                    return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
                }
            }

            foreach (DateTime d in GetBlockedDates(id))
            {
                if (pickupDate.Date <= d.Date && dropoffDate.Date >= d.Date)
                {
                    string error = "Your proposed rental period includes blocked dates. Please select different dates and try again.";
                    return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
                }

                if (pickupDate.AddDays(1).Date == d.Date)
                {
                    string error = "Your pickup date has to be atlest two days before a blocked date.";
                    return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
                }
            }

            // Add it to the shopping cart
            RentalCart cart = null;

            try
            {
                cart = RentalCart.GetCart(this.HttpContext);
            }
            catch (Exception ex)
            {
                ErrorMessage.ErrorCode = ErrorCode.UNKNOWN;
                return(View("ErrorMessage", ErrorMessage));
            }

            if (cart.AddToCart(addedItem, DateTime.Parse(collection.Get("pickupdate")),
                               DateTime.Parse(collection.Get("dropoffdate"))))
            {
                Session["RentalCartItems"] = cart.GetCartItems().Count;
                // Go back to the main store page for more shopping
                return(RedirectToAction("Index"));
            }
            else
            {
                //ModelState.AddModelError("", "This item is already in your cart. Select a different item");
                string error = "This item is already in your cart. Select a different item";
                return(RedirectToAction("Details", "Item", new { id = id, errMsg = error }));
            }
        }
Exemple #7
0
        public ActionResult EditCartItem(RentalCart CartModel, int id, string pickupdate, string dropoffdate)
        {
            try
            {
                DateTime pickupDate  = DateTime.Parse(pickupdate);
                DateTime dropoffDate = DateTime.Parse(dropoffdate);

                // TODO: Add update logic here
                RentalCart cart = null;
                try
                {
                    cart = RentalCart.GetCart(this.HttpContext);
                }
                catch
                {
                    ErrorMessage.ErrorCode = ErrorCode.UNKNOWN;
                    return(View("ErrorMessage", ErrorMessage));
                }

                if (pickupDate < DateTime.Today || dropoffDate < DateTime.Today)
                {
                    var results = new RentalCartUpdateViewModel
                    {
                        Error   = true,
                        Message = "Please enter valid dates"
                    };

                    return(Json(results));
                }

                int rentalPeriod = (dropoffDate - pickupDate).Days;
                if (rentalPeriod <= 0)
                {
                    var results = new RentalCartUpdateViewModel
                    {
                        Error   = true,
                        Message = "Your dropoff date cannot be earlier than or the same as your pickup date"
                    };

                    return(Json(results));
                }

                var cartItem = market.CartItems.Single(i => i.id == id);

                foreach (DateTime d in GetBlockedDates(cartItem.ItemId))
                {
                    if (pickupDate.Date <= d.Date && dropoffDate.Date >= d.Date)
                    {
                        var results = new RentalCartUpdateViewModel
                        {
                            Error   = true,
                            Message = "Your proposed rental period includes blocked dates. Please select different dates and try again."
                        };

                        return(Json(results));
                    }

                    if (pickupDate.AddDays(1).Date == d.Date)
                    {
                        var results = new RentalCartUpdateViewModel
                        {
                            Error   = true,
                            Message = "Your pickup date has to be atlest two days before a blocked date."
                        };

                        return(Json(results));
                    }
                }

                // Get the name of the Item to display confirmation
                string  itemName = cartItem.Item.Name;
                decimal itemCost = cartItem.Item.CostPerDay;

                if (cart.EditInCart(id, DateTime.Parse(pickupdate),
                                    DateTime.Parse(dropoffdate)))
                {
                    var result = new RentalCartUpdateViewModel
                    {
                        Error            = false,
                        CartTotal        = cart.GetTotal(),
                        UpdatedItemTotal = (rentalPeriod * itemCost) + cartItem.Item.SecurityDeposit,
                        UpdatedId        = id,
                        Message          = "",
                        NumberOfDays     = rentalPeriod
                    };

                    return(Json(result));
                }
                else
                {
                    var results = new RentalCartUpdateViewModel
                    {
                        Error   = true,
                        Message = "There already exists an item in your rental cart during the same period"
                    };
                    ModelState.AddModelError("", "There already exists an item in your rental cart during the same period");
                    return(Json(results));
                }
            }
            catch (Exception ex)
            {
                /*
                 * ErrorMessage.ErrorCode = ErrorCode.UNKNOWN;
                 * return View("ErrorMessage", ErrorMessage);
                 */
                var results = new RentalCartUpdateViewModel
                {
                    Error   = true,
                    Message = "Something went wrong on our side."
                };
                ModelState.AddModelError("", "There already exists an item in your rental cart during the same period");
                return(Json(results));
            }
        }