Ejemplo n.º 1
0
        public ActionResult PayBasket(string idcount, bool?isFast)
        {
            var customer = new CustomerAction(Session.GetUserId());

            Session.UpdateBasket();
            var basket = Session.GetUser().basket;

            if (isFast != true && !string.IsNullOrWhiteSpace(idcount))
            {
                var splitBasket = idcount.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                if (basket.Count != splitBasket.Count())
                {
                    var message = "Количество товара в корзине не соответствует ожидаемому";
                    return(RedirectToAction("Info", "Home", new InfoModel {
                        message = message, header = "Ошибка!", status = StatusMessage.Error
                    }));
                }
                foreach (var item in splitBasket)
                {
                    var bask     = item.Trim().Split(':');
                    var basketId = Int64.Parse(bask[0]);
                    var count    = decimal.Parse(bask[1]);
                    if (!basket.Any(s => s.id == basketId))
                    {
                        var message = "Идентификатор корзины был испорчен!!";
                        return(RedirectToAction("Info", "Home", new InfoModel {
                            message = message, header = "Ошибка!", status = StatusMessage.Error
                        }));
                    }
                    var basketElement = basket.Single(s => s.id == basketId);
                    if (basketElement.count != count && count > 0)
                    {
                        customer.UpdateCount(basketElement.id, count);
                    }
                }
                Session.UpdateBasket();
                basket = Session.GetUser().basket;
            }
            var payModel = new PayModel
            {
                countInBasket     = basket.Count(),
                payDate           = DateTime.Now,
                totalSumm         = basket.Sum(s => s.summTotal) ?? 0.00M,
                transactionNumber = Guid.NewGuid().ToString(),
                packId            = basket.Select(s => s.packId).Distinct().Single() ?? 1
            };

            customer.Pay(payModel);
            Session.UpdateBasket();
            var mess = $"Заказ на сумму {payModel.totalSumm} руб. успешно создан. Идентификатор заказа: {payModel.transactionNumber}";

            return(RedirectToAction("Info", "Home", new InfoModel {
                message = mess, header = "Оплачено!", status = StatusMessage.Accept
            }));
        }