public async Task <IActionResult> Create([Bind("ID,Rating,Body,IsApproved")] Review review)
        {
            if (ModelState.IsValid)
            {
                _context.Add(review);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(review));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Index(ShippingsViewModel model) // this Model info needs to go to braintree.
        {
            if (Request.Cookies.Keys.Contains("cartId") && Guid.TryParse(Request.Cookies["cartId"], out Guid cartId))
            {
                model.Cart = await _context.Cart.Include(c => c.Product).Include(c => c.User).SingleAsync(x => x.CartId == cartId);
            }
            if (ModelState.IsValid)
            {
                Orders newOrder = new Orders();
                newOrder.User    = model.Cart.User;
                newOrder.Product = model.Cart.Product;
                newOrder.LineItems.Add(new LineItem
                {
                    Product  = model.Cart.Product,
                    Quantity = 1
                });
                newOrder.Shipping.Add(new Shipping
                {
                    Address = new Address
                    {
                        Street = model.address.Street,
                        City   = model.address.City,
                        State  = model.address.State,
                        Zip    = model.address.Zip
                    }
                });
                if (ModelState.IsValid)
                {
                    // this is to interact with Braintree. Getting all the user card information and sending it to BrainTree.
                    Braintree.TransactionRequest saleRequest = new Braintree.TransactionRequest();
                    saleRequest.Amount     = model.Cart.Product.Price.Value;
                    saleRequest.CreditCard = new Braintree.TransactionCreditCardRequest
                    {
                        CardholderName  = model.CreditCartName,
                        CVV             = model.CreditCardVerificationValue,
                        ExpirationMonth = model.ExpirationMonth,
                        ExpirationYear  = model.ExpirationYear,
                        Number          = model.CreditCardNumber
                    };

                    // awaiting the result of card validation
                    var result = await _braintreeGateway.Transaction.SaleAsync(saleRequest);

                    if (result.IsSuccess())
                    {
                        _context.Orders.Add(newOrder);     // => this adds the newly created order.
                        _context.Cart.Remove(model.Cart);  // => once it's added, Remove() will clear out the cart.
                        Response.Cookies.Delete("cartId"); // => Delete() will delete the cookie with the cart info.
                        await _context.SaveChangesAsync();

                        return(RedirectToAction("Index", "OrderComplete", new { ID = newOrder.Id }));
                    }
                    foreach (var error in result.Errors.All())
                    {
                        ModelState.AddModelError(error.Code.ToString(), error.Message);
                    }
                }


                //return RedirectToAction("Index", "OrderComplete");
            }
            await _context.SaveChangesAsync();

            return(View(model));
        }