Ejemplo n.º 1
0
        /// <summary>
        /// Gets the shipping methods.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="inputModel">The input model.</param>
        /// <returns>
        /// The manager response where the shipping methods are returned in the Result.
        /// </returns>
        public ManagerResponse <GetShippingMethodsResult, IReadOnlyCollection <ShippingMethod> > GetShippingMethods([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, [NotNull] GetShippingMethodsInputModel inputModel)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNull(inputModel, "inputModel");

            GetShippingMethodsResult result = new GetShippingMethodsResult {
                Success = false
            };
            var cartResult = this.CartManager.GetCurrentCart(storefront, visitorContext);

            if (!cartResult.ServiceProviderResult.Success || cartResult.Result == null)
            {
                result.SystemMessages.ToList().AddRange(cartResult.ServiceProviderResult.SystemMessages);
                return(new ManagerResponse <GetShippingMethodsResult, IReadOnlyCollection <ShippingMethod> >(result, null));
            }

            var cart           = cartResult.Result;
            var preferenceType = InputModelExtension.GetShippingOptionType(inputModel.ShippingPreferenceType);

            var request = new Sitecore.Commerce.Engine.Connect.Services.Shipping.GetShippingMethodsRequest(
                new ShippingOption {
                ShippingOptionType = preferenceType
            },
                (inputModel.ShippingAddress != null) ? inputModel.ShippingAddress.ToParty() : null,
                cart)
            {
                Lines = (inputModel.Lines != null) ? inputModel.Lines.ToCommerceCartLines() : null
            };

            result = this.ShippingServiceProvider.GetShippingMethods <GetShippingMethodsRequest, GetShippingMethodsResult>(request);
            return(new ManagerResponse <GetShippingMethodsResult, IReadOnlyCollection <ShippingMethod> >(result, result.ShippingMethods));
        }
        public ManagerResponse <GetShippingMethodsResult, IReadOnlyCollection <ShippingMethod> > GetShippingMethods(
            string shopName,
            Cart cart,
            ShippingOptionType shippingOptionType,
            PartyEntity address,
            List <string> cartLineExternalIdList)
        {
            if (cartLineExternalIdList != null && cartLineExternalIdList.Any <string>())
            {
            }

            CommerceParty commerceParty = null;

            if (address != null)
            {
                commerceParty = this.connectEntityMapper.MapToCommerceParty(address);
            }

            var shippingOption = new ShippingOption {
                ShippingOptionType = shippingOptionType
            };
            var request = new Sitecore.Commerce.Engine.Connect.Services.Shipping.GetShippingMethodsRequest(shippingOption, commerceParty, cart as CommerceCart);
            GetShippingMethodsResult shippingMethods = this.shippingServiceProvider.GetShippingMethods <GetShippingMethodsRequest, GetShippingMethodsResult>(request);

            return(new ManagerResponse <GetShippingMethodsResult, IReadOnlyCollection <ShippingMethod> >(shippingMethods, shippingMethods.ShippingMethods));
        }
        public ManagerResponse <GetShippingMethodsResult, IReadOnlyCollection <ShippingMethod> > GetShippingMethods(string userId, GetShippingMethodsInputModel inputModel)
        {
            Assert.ArgumentNotNull(inputModel, nameof(inputModel));

            var result = new GetShippingMethodsResult {
                Success = false
            };
            var cartResult = CartManager.GetCart(userId);

            if (!cartResult.ServiceProviderResult.Success || cartResult.Result == null)
            {
                result.SystemMessages.ToList().AddRange(cartResult.ServiceProviderResult.SystemMessages);
                return(new ManagerResponse <GetShippingMethodsResult, IReadOnlyCollection <ShippingMethod> >(result, null));
            }

            var cart           = cartResult.Result;
            var preferenceType = InputModelExtension.GetShippingOptionType(inputModel.ShippingPreferenceType);

            var request = new GetDeliveryMethodsRequest(
                new ShippingOption {
                ShippingOptionType = preferenceType
            },
                (inputModel.ShippingAddress != null) ? inputModel.ShippingAddress.ToParty() : null)
            {
                Cart  = cart,
                Lines = (inputModel.Lines != null) ? inputModel.Lines.ToCommerceCartLines() : null
            };

            result = this.ShippingServiceProvider.GetShippingMethods <GetShippingMethodsRequest, GetShippingMethodsResult>(request);
            return(new ManagerResponse <GetShippingMethodsResult, IReadOnlyCollection <ShippingMethod> >(result, result.ShippingMethods));
        }
        /// <summary>
        /// Gets the shipping methods.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="inputModel">The input model.</param>
        /// <returns>
        /// The manager response where the shipping methods are returned in the Result.
        /// </returns>
        public ManagerResponse <GetShippingMethodsResult, IReadOnlyCollection <ShippingMethod> > GetShippingMethods([NotNull] CommerceStorefront storefront, [NotNull] IVisitorContext visitorContext, [NotNull] GetShippingMethodsInputModel inputModel)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNull(inputModel, "inputModel");

            var errorResult = new GetShippingMethodsResult {
                Success = false
            };
            var cartResult = this._cartManager.GetCurrentCart(storefront, visitorContext);

            if (!cartResult.ServiceProviderResult.Success || cartResult.Result == null)
            {
                errorResult.SystemMessages.ToList().AddRange(cartResult.ServiceProviderResult.SystemMessages);
                return(new ManagerResponse <GetShippingMethodsResult, IReadOnlyCollection <ShippingMethod> >(errorResult, null));
            }

            var cart = cartResult.Result;

            var preferenceType = InputModelExtension.GetShippingOptionType(inputModel.ShippingPreferenceType);

            if (inputModel.Lines != null && inputModel.Lines.Any())
            {
                // We only support a single line at a time, hence accessing element 0 is ok.
                preferenceType = InputModelExtension.GetShippingOptionType(inputModel.Lines[0].ShippingPreferenceType);
            }

            // TODO: Remove hard coded language - will be fixed in connect.
            var request = new Helpers.GetShippingMethodsRequest(
                "en-us",
                new ShippingOption {
                ShippingOptionType = preferenceType
            },
                (inputModel.ShippingAddress != null) ? inputModel.ShippingAddress.ToParty() : null)
            {
                Cart  = cart,
                Lines = (inputModel.Lines != null) ? inputModel.Lines.ToCommerceCartLines().Cast <CartLine>().ToList() : null
            };

            var result = this._shippingServiceProvider.GetShippingMethods <Helpers.GetShippingMethodsRequest, GetShippingMethodsResult>(request);

            //Helpers.LogSystemMessages(errorResult.SystemMessages, errorResult);
            return(new ManagerResponse <GetShippingMethodsResult, IReadOnlyCollection <ShippingMethod> >(result, result.ShippingMethods));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ShippingMethodsJsonResult"/> class.
 /// </summary>
 /// <param name="result">The service provider result.</param>
 public ShippingMethodsJsonResult(GetShippingMethodsResult result)
     : base(result)
 {
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShippingMethodsJsonResult"/> class.
 /// </summary>
 /// <param name="result">The service provider result.</param>
 public ShippingMethodsJsonResult(GetShippingMethodsResult result)
     : base(result)
 {
 }
 public ShippingMethodsApiModel(GetShippingMethodsResult result)
     : base(result)
 {
 }
        public void ShouldGetShippingMethodsByShippingOption()
        {
            var request =
                new GetShippingMethodsRequest(new ShippingOption()
            {
                ShippingOptionType = Commerce.Entities.Shipping.ShippingOptionType.ShipToAddress,
            }, new Party()
            {
                Country = "USA"
            });

            request.Properties.Add(new PropertyItem("cart", new Cart()
            {
                ExternalId = Guid.NewGuid().ToString()
            }));

            var result = new GetShippingMethodsResult();
            var args   = new ServicePipelineArgs(request, result);

            _client.GetShippingMethods(Arg.Any <ShoppingCartModel>(), Arg.Any <string>(), Arg.Any <AddressModel>()).Returns(
                new ArrayOfShippingMethodModelResponse()
            {
                Success = true,
                Result  = new[]
                {
                    new ShippingMethodModel()
                    {
                        Name        = "By Ground",
                        Description = "Compared to other shipping methods, like by flight or over seas, ground shipping is carried out closer to the earth",
                        SystemName  = "Shipping.FixedRate"
                    },
                    new ShippingMethodModel()
                    {
                        Name        = "By Air",
                        Description = "The one day air shipping",
                        SystemName  = "Shipping.FixedRate"
                    }
                }
            });

            var xml      = new XmlDocument();
            var rootNode = xml.CreateElement("map");

            xml.AppendChild(rootNode);

            var shippingOptionValue = xml.CreateAttribute("shippingOptionValue");
            var systemName          = xml.CreateAttribute("systemName");
            var methodName          = xml.CreateAttribute("methodName");

            shippingOptionValue.Value = "1";
            systemName.Value          = "Shipping.FixedRate";
            methodName.Value          = "By Ground";

            rootNode.Attributes.Append(shippingOptionValue);
            rootNode.Attributes.Append(systemName);
            rootNode.Attributes.Append(methodName);

            _processor.Map(rootNode);

            shippingOptionValue.Value = "1";
            systemName.Value          = "Shipping.FixedRate";
            methodName.Value          = "By Air";

            _processor.Map(rootNode);


            _processor.Process(args);

            result.Should().NotBeNull();
            result.Success.Should().BeTrue();
            result.ShippingMethods.Should().HaveCount(2);
            result.ShippingMethods.ElementAt(0).Name.Should().Be("By Ground");
        }