public async Task <IActionResult> Confirm(CheckoutViewModel cvm)
        {
            var user = await _userManager.FindByEmailAsync(User.Identity.Name);

            Basket datBasket = _basketRepo.GetUserBasketByEmail(user.Email).Result;

            if (datBasket.BasketItems.Count == 0)
            {
                return(RedirectToAction(nameof(Index)));
            }

            cvm.Basket = datBasket;

            if (!ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Shipping)));
            }
            // add address to database
            await _orderRepo.CreateAddress(cvm.Address);

            // create an new order object and load the order items onto it
            Order datOrder = new Order
            {
                UserID          = user.Id,
                AddressID       = cvm.Address.ID,
                Address         = cvm.Address,
                OrderDate       = DateTime.Now.ToString("MMM d, yyyy (ddd) @ HH:mm tt"),
                Shipping        = cvm.Shipping,
                DiscountName    = cvm.DiscountName,
                DiscountPercent = cvm.DiscountPercent,
                DiscountAmt     = cvm.DiscountAmt,
                TotalItemQty    = cvm.TotalItemQty,
                Subtotal        = cvm.Subtotal,
                Total           = cvm.Total,
            };

            // add order to the database table
            // I'm doing this first in hopes that the order generates an ID that
            // I can add to the order items. Here's hoping...
            await _orderRepo.AddOrderAsync(datOrder);

            // turn basket items into order items
            List <OrderItem> demOrderItems = new List <OrderItem>();

            foreach (var item in datBasket.BasketItems)
            {
                OrderItem tempOrderItem = new OrderItem
                {
                    ProductID   = item.ProductID,
                    OrderID     = datOrder.ID,
                    UserID      = user.Id,
                    ProductName = item.ProductName,
                    Quantity    = item.Quantity,
                    ImgUrl      = item.ImgUrl,
                    UnitPrice   = item.UnitPrice
                };

                // add order item to the database table
                await _orderRepo.AddOrderItemToOrderAsync(tempOrderItem);

                demOrderItems.Add(tempOrderItem);
            }

            // attach orderitems to order
            datOrder.OrderItems = demOrderItems;

            //sends a receipt of the order information
            string htmlMessage = "Thank you for shopping with us!  You ordered: </br>";

            foreach (var item in datOrder.OrderItems)
            {
                htmlMessage += $"Item: {item.ProductName}, Quantity: {item.Quantity}</br>";
            }
            ;

            //CHARGE CARD
            Payment payment = new Payment(Configuration);

            payment.RunPayment(cvm.Total, datOrder, user);

            await _emailSender.SendEmailAsync(user.Email, "Order Information",
                                              htmlMessage);

            // empty out basket
            await _basketRepo.ClearOutBasket(cvm.Basket.BasketItems);

            return(View("Confirmed", datOrder));
        }
示例#2
0
 public async Task AddOrder(OrderViewModel model)
 {
     var order = _mapper.Map <OrderViewModel, Order>(model);
     await _orderRepo.AddOrderAsync(order);
 }