コード例 #1
0
        public async Task <IActionResult> Index(CheckoutModel model)
        {
            //Here is how it works: Javascript sends the information TO Braintree and Braintree sends the "nonce" back.
            if (ModelState.IsValid)
            {
                VacationOrder order = new VacationOrder
                {
                    email = model.email
                };

                VacationCart cart = null;

                if (User.Identity.IsAuthenticated)
                {
                    var currentUser = _userManager.GetUserAsync(User).Result;
                    cart = _context.VacationCarts.Include(x => x.VacationModelVacationCarts).ThenInclude(x => x.vacationModel).Single(x => x.Applicationuserid == currentUser.Id);
                }

                else if (Request.Cookies.ContainsKey("cart_id"))
                {
                    int existingCartID = int.Parse(Request.Cookies["cart_id"]);
                    cart = _context.VacationCarts.Include(x => x.VacationModelVacationCarts).ThenInclude(x => x.vacationModel).FirstOrDefault(x => x.id == existingCartID); //So, if there is a cookie attached to the cart, then set cart to = the cart, the product AND the existing ID?
                }

                foreach (var item in cart.VacationModelVacationCarts)
                {
                    order.VacationOrderDestinationDetails.Add(new VacationOrderDestinationDetails
                    {
                        DateCreated            = DateTime.Now,
                        DateLastModified       = DateTime.Now,
                        Quantity               = item.Quantity,
                        destinationdescription = item.vacationModel.DestinationDescription,
                        destination            = item.vacationModel.DreamDestination
                    });
                }

                _context.VacationModelCarts.RemoveRange(cart.VacationModelVacationCarts);

                _context.VacationCarts.Remove(cart); //So, after we listed the items in the foreach, we don't need the cart anymore. We empty they products and disengage the model from the cart (???).

                var result = await _braintreeGateway.Transaction.SaleAsync(new TransactionRequest
                {
                    Amount = order.VacationOrderDestinationDetails.Sum(x => x.Quantity *x.Budget),

                    /* ERROR -- FIX.
                     * PaymentMethodNonce = model.Nonce,
                     * LineItems = order.VacationOrderDestinationDetails.Select(x => new TransactionLineItemRequest
                     * {
                     *  Description = x.destinationdescription,
                     *  Name = x.destination,
                     *  Quantity = x.Quantity,
                     *  UnitAmount = x.Budget,
                     *  TotalAmount = x.Budget * x.Quantity
                     * })
                     *
                     * await _emailSender.SendEmailAsync(model.email, " your order " + order.id + " Thank you for ordering!");*/
                });
            }
            return(View());
        }
コード例 #2
0
        public IActionResult Index()
        {
            VacationCart cart = new VacationCart
            {
                id = 1,
            };

            return(View());

            /*
             *   BeachCart model = new BeachCart //NOTE: BeachCart was generated as a new class.
             *   {
             *       id = 1;
             *       products = new BeachCart[]{
             *       new BeachProduct(
             *       id=1;
             *       nameof = "red Umbrella",
             *       Price: 12.99,
             *       Description:
             *       ImagePath:
             *       );
             *
             *       new BeachProduct(
             *       );
             *       }
             *
             *   }
             *
             *   To remove a product:
             *   Public IActionResult Remove(int id) {}
             *
             *   NEXT: Below the table, do a checkout button but it won't work until you build a CHECKOUT CONTROLLER and a VIEW.
             */

            //How to render out your composite model:
            //Under the index.cshtml: @if(model.Products.Count == 0) {"You have nothing in your cart."}. Else {
            //<table>@foreach(var product in Model.Products) {<tr><td>@(product.Name)</td></tr>, @(product.Price).toString...</table>
        }
コード例 #3
0
        public async Task <IActionResult> Details(int?id, int quantity, string color)
        {
            VacationCart cart = null;

            if (User.Identity.IsAuthenticated)
            {
                var currentUser = await _userManager.GetUserAsync(User);

                //cart = await _context.VacationCarts.Include(x => x.VacationModelVacationCarts).FirstOrDefault(x => x.Applicationuserid == currentUser.Id);

                if (cart == null)
                {
                    cart = new VacationCart();
                    cart.Applicationuserid = currentUser.Id;
                    cart.DateCreated       = DateTime.Now;
                    cart.DateLastModified  = DateTime.Now;
                    _context.VacationCarts.Add(cart);
                }
            }

            else
            {
                if (Request.Cookies.ContainsKey("cart_id")) //All future cookie references MUST have this!!!
                {
                    int existingCartID = int.Parse(Request.Cookies["cart_id"]);
                    cart = _context.VacationCarts.Include(x => x.VacationModelVacationCarts).FirstOrDefault(x => x.id == existingCartID);
                    cart.DateLastModified = DateTime.Now;
                }

                if (cart == null)
                {
                    cart = new VacationCart
                    {
                        DateCreated      = DateTime.Now,
                        DateLastModified = DateTime.Now,
                    };

                    _context.VacationCarts.Add(cart);
                }
            }

            VacationModelVacationCart product = cart.VacationModelVacationCarts.FirstOrDefault(x => x.vacationModelId == id);

            if (product == null)
            {
                product = new VacationModelVacationCart
                {
                    DateCreated      = DateTime.Now,
                    DateLastModified = DateTime.Now,
                    vacationModelId  = id ?? 0,
                    Quantity         = 0
                };

                cart.VacationModelVacationCarts.Add(product);
            }

            product.Quantity        += quantity;
            product.DateLastModified = DateTime.Now;

            _context.SaveChanges();

            if (!User.Identity.IsAuthenticated)
            {
                Response.Cookies.Append("cart_id", cart.id.ToString(), new Microsoft.AspNetCore.Http.CookieOptions
                {
                    Expires = DateTime.Now.AddMonths(9)
                });
            }

            return(RedirectToAction("index", "CartController"));
        }