private void AddShippingMethodsToResult(CheckoutDataBaseJsonResult result, CommerceCart cart)
        {
            var shippingRequest = new GetShippingMethodsInputModel {
                ShippingPreferenceType = ShippingOptionType.None.Name
            };
            var response = this.ShippingManager.GetShippingMethods(this.CurrentStorefront, this.CurrentVisitorContext, shippingRequest);

            if (response.ServiceProviderResult.Success && response.Result != null)
            {
                foreach (var sm in response.Result)
                {
                    var isEmailMethod       = sm.GetPropertyValue("IsEmailShippingMethod") != null && (bool)sm.GetPropertyValue("IsEmailShippingMethod");
                    var isShipToStoreMethod = sm.GetPropertyValue("IsShipToStoreShippingMethod") != null && (bool)sm.GetPropertyValue("IsShipToStoreShippingMethod");

                    if (isEmailMethod)
                    {
                        result.EmailDeliveryMethod = sm;
                    }

                    if (isShipToStoreMethod)
                    {
                        result.ShipToStoreDeliveryMethod = sm;
                    }
                }

                return;
            }

            result.EmailDeliveryMethod       = new ShippingMethod();
            result.ShipToStoreDeliveryMethod = new ShippingMethod();
            result.SetErrors(response.ServiceProviderResult);
        }
        public JsonResult GetShippingMethods(GetShippingMethodsInputModel inputModel)
        {
            try
            {
                Assert.ArgumentNotNull(inputModel, "inputModel");

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

                var response = this.ShippingManager.GetShippingMethods(CurrentStorefront, CurrentVisitorContext, inputModel);
                var result   = new ShippingMethodsJsonResult(response.ServiceProviderResult);
                if (response.ServiceProviderResult.Success && response.Result != null)
                {
                    result.Initialize(response.ServiceProviderResult.ShippingMethods, response.ServiceProviderResult.ShippingMethodsPerItem);
                }

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                CommerceLog.Current.Error("GetShippingMethods", this, e);
                return(Json(new BaseJsonResult("GetShippingMethods", e), JsonRequestBehavior.AllowGet));
            }
        }
示例#3
0
        public JsonResult GetShippingMethods(GetShippingMethodsInputModel inputModel)
        {
            try
            {
                Assert.ArgumentNotNull(inputModel, nameof(inputModel));

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

                var response = ShippingManager.GetShippingMethods(CommerceUserContext.Current.UserId, inputModel);
                var result   = new ShippingMethodsApiModel(response.ServiceProviderResult);
                if (response.ServiceProviderResult.Success)
                {
                    result.Initialize(response.ServiceProviderResult.ShippingMethods, response.ServiceProviderResult.ShippingMethodsPerItem);
                }

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new ErrorApiModel("GetShippingMethods", e), JsonRequestBehavior.AllowGet));
            }
        }
示例#4
0
        public ShippingMethodsBaseJsonResult GetShippingMethods(GetShippingMethodsInputModel inputModel)
        {
            var response = this._shippingManager.GetShippingMethods(CurrentStorefront, CurrentVisitorContext, inputModel);
            var result   = new ShippingMethodsJsonResult(response.ServiceProviderResult);

            if (response.ServiceProviderResult.Success && response.Result != null)
            {
                result.Initialize(response.ServiceProviderResult.ShippingMethods, response.ServiceProviderResult.ShippingMethodsPerItem);
            }

            return(result);
        }
示例#5
0
 protected virtual void CheckIfEditing(
     IVisitorContext visitorContext,
     CustomBaseCheckoutDataJsonResult result)
 {
     if ((result.Cart == null || result.Cart.Shipments == null ? 0 : (result.Cart.Shipments.Count > 0 ? 1 : 0)) == 0)
     {
         return;
     }
     foreach (ShippingInfoJsonResult shipment1 in result.Cart.Shipments)
     {
         ShippingInfoJsonResult shipment = shipment1;
         if (shipment.LineIDs != null && shipment.LineIDs.Count > 0)
         {
             CartLineJsonResult cartLineJsonResult = result.Cart.Lines.Find((l => l.ExternalCartLineId.Equals(shipment.LineIDs[0], StringComparison.OrdinalIgnoreCase)));
             if (cartLineJsonResult != null && cartLineJsonResult.ShippingOptions != null && cartLineJsonResult.ShippingOptions.Count <ShippingOptionJsonResult>() > 0)
             {
                 ShippingOptionJsonResult optionJsonResult = cartLineJsonResult.ShippingOptions.ElementAt <ShippingOptionJsonResult>(0);
                 if (optionJsonResult != null)
                 {
                     shipment.EditModeShippingOptionType = optionJsonResult.ShippingOptionType;
                     if (optionJsonResult.ShippingOptionType == ShippingOptionType.ShipToAddress)
                     {
                         AddressJsonResult addressJsonResult = result.Cart.Parties.Find((Predicate <AddressJsonResult>)(p => p.ExternalId.Equals(shipment.PartyID, StringComparison.OrdinalIgnoreCase)));
                         if (addressJsonResult != null)
                         {
                             GetShippingMethodsInputModel inputModel = new GetShippingMethodsInputModel()
                             {
                                 ShippingPreferenceType = ShippingOptionType.ShipToAddress.Value.ToString((IFormatProvider)CultureInfo.InvariantCulture),
                                 ShippingAddress        = new PartyInputModel()
                             };
                             inputModel.ShippingAddress.ExternalId    = addressJsonResult.ExternalId;
                             inputModel.ShippingAddress.Address1      = addressJsonResult.Address1;
                             inputModel.ShippingAddress.City          = addressJsonResult.City;
                             inputModel.ShippingAddress.State         = addressJsonResult.State;
                             inputModel.ShippingAddress.ZipPostalCode = addressJsonResult.ZipPostalCode;
                             inputModel.ShippingAddress.Country       = addressJsonResult.Country;
                             inputModel.ShippingAddress.IsPrimary     = addressJsonResult.IsPrimary;
                             inputModel.Lines = new List <CartLineInputModel>();
                             ShippingMethodsJsonResult shippingMethods = this.GetShippingMethods(visitorContext, inputModel);
                             if (shippingMethods != null)
                             {
                                 shipment.ShipmentEditModel = this.ModelProvider.GetModel <ShipmentEditModeDataJsonResult>();
                                 shipment.ShipmentEditModel.ShippingMethods = shippingMethods.ShippingMethods;
                             }
                         }
                     }
                 }
             }
         }
     }
 }
        public JsonResult GetShippingMethods(GetShippingMethodsInputModel inputModel)
        {
            try
            {
                Assert.ArgumentNotNull(inputModel, "inputModel");

                var validationResult = new BaseJsonResult();
                this.ValidateModel(validationResult);

                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var result = _checkoutRepository.GetShippingMethods(inputModel);

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                _logger.LogError("GetShippingMethods", this, e);
                return(Json(new BaseJsonResult("GetShippingMethods", e), JsonRequestBehavior.AllowGet));
            }
        }
示例#7
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));
        }
        /// <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));
        }
        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));
        }