Exemplo n.º 1
0
        public void TestConfirmPayForAll(long userId)
        {
            var customer = new CustomerAction(userId);
            var store    = new StoreAction();

            customer.ClearBasket();
            Assert.AreEqual(customer.GetBasket().Count(), 0);
            customer.AddBasket(GenerateBasketElement());
            customer.AddBasket(GenerateBasketElement());

            var fastPayModel = GenerateBasketElement(true);
            var fastPayId    = customer.AddBasket(fastPayModel).packId;
            var packId       = customer.AddBasket(GenerateBasketElement()).packId;
            var price        = store.GetAllGoods().First(s => s.id == fastPayModel.goodId).price;
            var total        = price * fastPayModel.count;
            var payModel     = new PayModel
            {
                packId            = fastPayId,
                countInBasket     = 1,
                payDate           = DateTime.Now,
                transactionNumber = Guid.NewGuid().ToString(),
                totalSumm         = total
            };

            customer.Pay(payModel);
            Assert.AreEqual(customer.GetOrders().Where(s => s.packId == fastPayId).Count(), 1);

            customer.AddBasket(GenerateBasketElement(true));
            customer.AddBasket(GenerateBasketElement());
            customer.AddBasket(GenerateBasketElement(true));
            Assert.AreEqual(customer.GetBasket().Count(), 4);
            Assert.IsTrue(customer.GetOrders().Count() > 0);

            var basket    = customer.GetBasket();
            var payModel2 = new PayModel
            {
                packId            = (long)basket.Select(s => s.packId).Distinct().Single(),
                countInBasket     = 4,
                payDate           = DateTime.Now,
                transactionNumber = Guid.NewGuid().ToString(),
                totalSumm         = basket.Sum(s => s.summTotal).Value
            };

            customer.Pay(payModel2);
            Assert.AreEqual(customer.GetOrders().Where(s => s.packId == packId).Count(), 4);

            customer.AddBasket(GenerateBasketElement());
            customer.AddBasket(GenerateBasketElement(true));
            Assert.IsTrue(customer.GetBasket().Count() == 1);
        }
Exemplo n.º 2
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
            }));
        }