예제 #1
0
        public Money GetBasketTotalForUser(string userToken)
        {
            if (!UserTokenIsValid(userToken))
            {
                return(Money.From(0));
            }

            var userSessionData = _userSessions[Guid.Parse(userToken)];
            var finalPrice      = userSessionData.Basket.Total;

            userSessionData.Basket.AdjustedPrice = false;

            if (userSessionData.SelectedDeal == null)
            {
                return(finalPrice);
            }

            var verifyVoucherResponse = _voucherService.Verify(userSessionData.Basket,
                                                               userSessionData.SelectedDeal.AllowedDeliveryTypes, userSessionData.SelectedDeal.Voucher.Code);

            if (verifyVoucherResponse.HasError)
            {
                return(finalPrice);
            }

            var dealPrice = verifyVoucherResponse.Total;

            userSessionData.Basket.AdjustedPrice = false;

            if (finalPrice == dealPrice)
            {
                return(finalPrice);
            }

            userSessionData.Basket.AdjustedPrice = true;
            finalPrice = dealPrice;

            return(finalPrice);
        }
예제 #2
0
        public BasketCheckoutResponse Checkout(DeliveryType delivery, string voucher, string userId, OrderStatus orderStatus)
        {
            var response = new BasketCheckoutResponse();

            if (!_userSessionService.IsLoggedIn(userId))
            {
                response.AddError(new Error
                {
                    Code        = ErrorCodes.UserNotLoggedIn,
                    UserMessage = "You have to be logged in to complete your order"
                });
                return(response);
            }

            if (delivery == DeliveryType.Unknown)
            {
                response.AddError(new Error
                {
                    Code        = ErrorCodes.DeliveryTypeUnknown,
                    UserMessage = "Delivery type not specified."
                });
                return(response);
            }

            if (orderStatus == OrderStatus.Unknown)
            {
                response.AddError(new Error
                {
                    Code        = ErrorCodes.OrderStatusUnkown,
                    UserMessage = "Order status not specified."
                });
                return(response);
            }

            var userBasket = _userSessionService.GetBasketForUser(userId);

            if (!voucher.IsEmpty())
            {
                var verifyVoucherResponse = _voucherService.Verify(userBasket, new List <DeliveryType> {
                    delivery
                }, voucher);

                if (!verifyVoucherResponse.HasError)
                {
                    userBasket.Total = verifyVoucherResponse.Total;
                }
            }

            var orderRequest = new SaveOrderRequest
            {
                UserId       = _userSessionService.GetUserByUserToken(userId),
                DeliveryType = delivery.ToString(),
                Voucher      = voucher,
                GrandTotal   = userBasket.Total.InPence,
                Status       = orderStatus.ToString(),
                Orders       = userBasket.Items.ConvertAll(x => new Order
                {
                    PizzaId         = x.Pizza.Id,
                    SizeId          = x.Size.Id,
                    ExtraToppingIds = x.ExtraToppings.ConvertAll(y => y.Id),
                    SubTotal        = x.Total.InPence
                })
            };
            var saveOrderResponse = _orderRepository.SaveOrder(orderRequest);

            if (saveOrderResponse.HasError)
            {
                response.AddError(saveOrderResponse.Error);
            }

            _userSessionService.ClearBasketForUser(userId);

            return(response);
        }