public async Task <IActionResult> Create(Product product, Microsoft.AspNetCore.Http.IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                string newFile = System.IO.Path.Combine(_env.WebRootPath, "images", imageFile.FileName);

                System.IO.FileInfo newFileInfo = new System.IO.FileInfo(newFile);
                using (System.IO.FileStream fs = newFileInfo.Create())
                {
                    imageFile.CopyTo(fs);
                    fs.Close();
                }

                product.Image = "/images/" + imageFile.FileName;

                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(product));
        }
예제 #2
0
        public async Task <IActionResult> Index(CheckoutViewModel model)
        {
            await GetCurrentCart(model);

            if (ModelState.IsValid)
            {
                Order newOrder = new Order
                {
                    TrackingNumber = Guid.NewGuid().ToString(),
                    OrderDate      = DateTime.Now,
                    OrderItems     = model.Cart.CartItems.Select(x => new OrderItem
                    {
                        ProductID    = x.Product.ID,
                        ProductName  = x.Product.Name,
                        ProductPrice = (x.Product.Price ?? 0),
                        Quantity     = x.Quantity
                    }).ToArray(),
                    ShippingAddress    = model.ShippingAddress,
                    ShippingCountry    = model.ShippingCountry,
                    ContactEmail       = model.ContactEmail,
                    ContactPhoneNumber = model.ContactPhoneNumber,
                    ShippingLocale     = model.ShippingLocale,
                    ShippingPostalCode = model.ShippingPostalCode,
                    ShippingRegion     = model.ShippingRegion,
                };


                //this is our braintree transaction section

                TransactionRequest transaction = new TransactionRequest
                {
                    //Amount = 1,
                    Amount = model.Cart.CartItems.Sum(x => x.Quantity * (x.Product.Price ?? 0)),

                    CreditCard = new TransactionCreditCardRequest
                    {
                        Number          = model.BillingCardNumber,
                        CardholderName  = model.NameOnCard,
                        CVV             = model.BillingCardVerificationValue,
                        ExpirationMonth = model.BillingCardExpirationMonth.ToString().PadLeft(2, '0'),
                        ExpirationYear  = model.BillingCardExpirationYear.ToString()
                    }
                };

                var transactionResult = await _braintreeGateway.Transaction.SaleAsync(transaction);

                if (transactionResult.IsSuccess())
                {
                    _burgerStoreDbContext.Orders.Add(newOrder);
                    _burgerStoreDbContext.CartItems.RemoveRange(model.Cart.CartItems);
                    _burgerStoreDbContext.Carts.Remove(model.Cart);
                    await _burgerStoreDbContext.SaveChangesAsync();

                    //Try to checkout
                    Response.Cookies.Delete("cartId");
                    return(RedirectToAction("Index", "Receipt", new { id = newOrder.TrackingNumber }));
                }
                for (int i = 0; i < transactionResult.Errors.Count; i++)
                {
                    ModelState.AddModelError("BillingCardNumber" + i, transactionResult.Errors.All()[i].Message);
                }
            }
            return(View(model));
        }