Пример #1
0
        public async Task <IActionResult> Receipt([Bind(Prefix = "ShippingDetails")] ShippingDetails sdvm)
        {
            var result = await _basketManager.FindBasketByUserEager(sdvm.Username);

            if (result != null)
            {
                // Run() method on payment class
                Payment payment  = new Payment(_configuration);
                var     response = payment.Run(result, sdvm);

                if (response != null)
                {
                    // Capture date and time order was placed
                    DateTime orderDateTime = DateTime.Now;

                    // Creates the order from the Basket information and saves it in the database.
                    Order order = new Order
                    {
                        ID                = 0,
                        UserName          = result.UserName,
                        Subtotal          = result.Subtotal,
                        FinalTotal        = result.Subtotal,
                        OrderItems        = new List <OrderItem>(),
                        OrderDateTime     = orderDateTime,
                        FirstName         = sdvm.FirstName,
                        LastName          = sdvm.LastName,
                        Address           = sdvm.Address,
                        City              = sdvm.City,
                        State             = sdvm.State,
                        PostalCode        = sdvm.PostalCode,
                        PhoneNumber       = sdvm.PhoneNumber,
                        TransactionNumber = response.transId
                    };
                    await _orderManager.CreateOrder(order);

                    // Reverse list before iterating over list backwards (to delete basket items starting from back of list; otherwise loop fails because previous index was deleted)
                    result.BasketItems.Reverse();

                    // Loops through the basket items creating new order items and adding them to the order
                    for (int i = result.BasketItems.Count - 1; i > -1; i--)
                    {
                        BasketItem bi        = result.BasketItems[i];
                        OrderItem  orderItem = new OrderItem
                        {
                            OrderID   = order.ID,
                            Order     = order,
                            ID        = 0,
                            ProductID = bi.ProductID,
                            Product   = bi.Product,
                            Quantity  = bi.Quantity
                        };

                        order.OrderItems.Add(orderItem);
                        await _orderItemManager.CreateOrderItem(orderItem);

                        _basketItemManager.DeleteBasketItem(bi.ID);
                    }
                    _basketManager.DeleteBasket(result.ID);

                    // Send receipt email only if not in dev environment (to avoid excessive emailing)
                    if (!_environment.IsDevelopment())
                    {
                        await SendReceiptEmail(order.UserName, order.ID);
                    }

                    return(View(order));
                }
                else
                {
                    OrderConfirmation orderConfirmation = new OrderConfirmation {
                        ShippingDetails = sdvm, Basket = result, TransactionFailure = true
                    };
                    return(View("ShippingDetails", orderConfirmation));
                }
            }

            return(RedirectToAction("Basket", "ViewBasketPage"));
        }