public JsonResult RemoveDiscount(DiscountInputModel model)
        {
            try
            {
                Assert.ArgumentNotNull(model, nameof(model));

                var validationResult = this.CreateJsonResult();
                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var response = CartManager.RemovePromoCodeFromCart(CommerceUserContext.Current.UserId, model.PromoCode);
                var result   = new CartApiModel(response.ServiceProviderResult);
                if (response.ServiceProviderResult.Success && response.Result != null)
                {
                    result.Initialize(response.Result);
                }

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new ErrorApiModel("RemoveDiscount", e), JsonRequestBehavior.AllowGet));
            }
        }
        public JsonResult GetCurrentCart()
        {
            try
            {
                var          id   = CommerceUserContext.Current.UserId;
                var          cart = CartCacheHelper.GetCart(id);
                CartApiModel cartResult;

                // The following condition stops the creation of empty carts on startup.
                if (cart == null && CartCookieHelper.DoesCookieExistForCustomer(id))
                {
                    var response = CartManager.GetCart(CommerceUserContext.Current.UserId, true);
                    cartResult = new CartApiModel(response.ServiceProviderResult);
                    if (response.ServiceProviderResult.Success && response.Result != null)
                    {
                        cartResult.Initialize(response.ServiceProviderResult.Cart);
                        if (response.ServiceProviderResult.Cart != null)
                        {
                            CartCacheHelper.AddCartToCache(response.ServiceProviderResult.Cart as CommerceCart);
                        }
                    }
                }
                else
                {
                    cartResult = new CartApiModel();
                    cartResult.Initialize(cart);
                }

                return(Json(cartResult, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new ErrorApiModel("GetCurrentCart", e), JsonRequestBehavior.AllowGet));
            }
        }
        public JsonResult UpdateLineItem(UpdateCartLineInputModel inputModel)
        {
            try
            {
                Assert.ArgumentNotNull(inputModel, nameof(inputModel));

                var validationResult = this.CreateJsonResult();
                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var response = CartManager.ChangeLineQuantity(CommerceUserContext.Current.UserId, inputModel);
                var result   = new CartApiModel(response.ServiceProviderResult);
                if (response.ServiceProviderResult.Success && response.Result != null)
                {
                    result.Initialize(response.Result);

                    if (HasBasketErrors(response.Result))
                    {
                        // We clear the cart from the cache when basket errors are detected.  This stops the message from being displayed over and over as the
                        // cart will be retrieved again from CS and the pipelines will be executed.
                        CartCacheHelper.InvalidateCartCache(CommerceUserContext.Current.UserId);
                    }
                }

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new ErrorApiModel("UpdateLineItem", e), JsonRequestBehavior.AllowGet));
            }
        }
Esempio n. 4
0
        public JsonResult SetPaymentMethods(PaymentInputModel inputModel)
        {
            try
            {
                Assert.ArgumentNotNull(inputModel, nameof(inputModel));

                var validationResult = this.CreateJsonResult();
                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var response = CartManager.SetPaymentMethods(CommerceUserContext.Current.UserId, inputModel);
                var result   = new CartApiModel(response.ServiceProviderResult);
                if (!response.ServiceProviderResult.Success || response.Result == null)
                {
                    return(Json(result, JsonRequestBehavior.AllowGet));
                }

                result.Initialize(response.Result);

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new ErrorApiModel("SetPaymentMethods", e), JsonRequestBehavior.AllowGet));
            }
        }
        public JsonResult SetPaymentMethods(PaymentInputModel inputModel)
        {
            try
            {
                Assert.ArgumentNotNull(inputModel, nameof(inputModel));

                var validationResult = this.CreateJsonResult();
                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var response = this.GetCart();
                var result   = new CartApiModel();
                result.Initialize(response);

                var paymentServiceResponse = this.PaymentManager.GetPaymentServiceActionResult(inputModel.FederatedPayment.CardToken);
                if (paymentServiceResponse.ServiceProviderResult.Success = false || string.IsNullOrEmpty(paymentServiceResponse.Result))
                {
                    result.SetErrors(new List <string> {
                        "CardAuthorizationFailed"
                    });
                    result.SetErrors(paymentServiceResponse.ServiceProviderResult);
                }

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new ErrorApiModel("SetPaymentMethods", e), JsonRequestBehavior.AllowGet));
            }
        }
Esempio n. 6
0
        public void CartApiModelValidation_Null_ShouldValidationError()
        {
            // Arrange
            var arrangement = new CartApiModel()
            {
                Items = null
            };

            // Act
            var validationContext = new ValidationContext(arrangement);

            var result = arrangement.Validate(validationContext);

            // Assert
            result.Count().Should().Be(1);
            result.First().ErrorMessage.Should().Be("The cart cant be empty.");
        }
Esempio n. 7
0
        public async Task <IActionResult> CreateCartAsync([FromBody] CartApiModel cart)
        {
            var cartModel = new CartModel()
            {
                Items = cart.Items
            };

            if (!cartManager.CartValid(cartModel))
            {
                return(BadRequest("Your cart has an invalid item."));
            }

            var id = cartManager.AddCart(cartModel);

            await PublishEvent(cartManager.RetrieveCart(id));

            return(Ok(id));
        }
Esempio n. 8
0
        public async Task <IActionResult> RemoveFromCartAsync(Guid id, [FromBody] CartApiModel cart)
        {
            var cartModel = new CartModel()
            {
                Items = cart.Items
            };

            if (!cartManager.CartValid(cartModel))
            {
                return(BadRequest("Your cart has an invalid item."));
            }

            if (cartManager.RemoveItemsFromCart(id, cartModel))
            {
                await PublishEvent(cartManager.RetrieveCart(id));

                return(Ok());
            }

            return(BadRequest("The cart items specified dont exist."));
        }