Exemplo n.º 1
0
        /// <summary>
        /// Updates the user password.
        /// </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.</returns>
        public virtual ManagerResponse <UpdatePasswordResult, bool> UpdateUserPassword([NotNull] CommerceStorefront storefront, [NotNull] IVisitorContext visitorContext, ChangePasswordInputModel inputModel)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNull(inputModel, "inputModel");
            Assert.ArgumentNotNullOrEmpty(inputModel.OldPassword, "inputModel.OldPassword");
            Assert.ArgumentNotNullOrEmpty(inputModel.NewPassword, "inputModel.NewPassword");

            var userName = visitorContext.UserName;

            var request = new UpdatePasswordRequest(userName, inputModel.OldPassword, inputModel.NewPassword);
            var result  = this._customerServiceProvider.UpdatePassword(request);

            if (!result.Success && !result.SystemMessages.Any())
            {
                var message = StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.PasswordCouldNotBeReset);
                result.SystemMessages.Add(new SystemMessage {
                    Message = message
                });
            }

            //            if (!result.Success)
            //            {
            //                Helpers.LogSystemMessages(result.SystemMessages, result);
            //            }

            return(new ManagerResponse <UpdatePasswordResult, bool>(result, result.Success));
        }
        /// <summary>
        /// Submits the visitor order.
        /// </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 new CommerceOrder is returned in the Result.
        /// </returns>
        public ManagerResponse <SubmitVisitorOrderResult, CommerceOrder> SubmitVisitorOrder([NotNull] CommerceStorefront storefront, [NotNull] IVisitorContext visitorContext, [NotNull] SubmitOrderInputModel inputModel)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNull(inputModel, "inputModel");

            SubmitVisitorOrderResult errorResult = new SubmitVisitorOrderResult {
                Success = false
            };

            var response = this._cartManager.GetCurrentCart(storefront, visitorContext, true);

            if (!response.ServiceProviderResult.Success || response.Result == null)
            {
                response.ServiceProviderResult.SystemMessages.ToList().ForEach(m => errorResult.SystemMessages.Add(m));
                return(new ManagerResponse <SubmitVisitorOrderResult, CommerceOrder>(errorResult, null));
            }

            var cart = (CommerceCart)response.ServiceProviderResult.Cart;

            if (cart.Lines.Count == 0)
            {
                errorResult.SystemMessages.Add(new SystemMessage {
                    Message = StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.SubmitOrderHasEmptyCart)
                });
                return(new ManagerResponse <SubmitVisitorOrderResult, CommerceOrder>(errorResult, null));
            }

            var cartChanges = new CommerceCart();

            cartChanges.Properties["Email"] = inputModel.UserEmail;

            var updateCartResult = this._cartManager.UpdateCart(storefront, visitorContext, cart, cartChanges);

            if (!updateCartResult.ServiceProviderResult.Success)
            {
                response.ServiceProviderResult.SystemMessages.ToList().ForEach(m => errorResult.SystemMessages.Add(m));
                return(new ManagerResponse <SubmitVisitorOrderResult, CommerceOrder>(errorResult, null));
            }

            var request = new SubmitVisitorOrderRequest(cart);

            request.RefreshCart(true);
            errorResult = this._orderServiceProvider.SubmitVisitorOrder(request);
            if (errorResult.Success && errorResult.Order != null && errorResult.CartWithErrors == null)
            {
                var cartCache = ContextTypeLoader.CreateInstance <CartCacheHelper>();
                cartCache.InvalidateCartCache(visitorContext.GetCustomerId());
            }

            //Helpers.LogSystemMessages(errorResult.SystemMessages, errorResult);
            return(new ManagerResponse <SubmitVisitorOrderResult, CommerceOrder>(errorResult, errorResult.Order as CommerceOrder));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Updates the user.
        /// </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 user is returned.
        /// </returns>
        public virtual ManagerResponse <UpdateUserResult, CommerceUser> UpdateUser([NotNull] CommerceStorefront storefront, [NotNull] IVisitorContext visitorContext, ProfileModel inputModel)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(visitorContext, "visitorContext");
            Assert.ArgumentNotNull(inputModel, "inputModel");

            UpdateUserResult result;

            var userName     = visitorContext.UserName;
            var commerceUser = this.GetUser(userName).Result;

            if (commerceUser != null)
            {
                commerceUser.FirstName = inputModel.FirstName;
                commerceUser.LastName  = inputModel.LastName;
                commerceUser.Email     = inputModel.Email;
                commerceUser.SetPropertyValue("Phone", inputModel.TelephoneNumber);

                try
                {
                    var request = new UpdateUserRequest(commerceUser);
                    result = this._customerServiceProvider.UpdateUser(request);
                }
                catch (Exception ex)
                {
                    result = new UpdateUserResult {
                        Success = false
                    };
                    result.SystemMessages.Add(new Sitecore.Commerce.Services.SystemMessage()
                    {
                        Message = ex.Message + "/" + ex.StackTrace
                    });
                }
            }
            else
            {
                // user is authenticated, but not in the CommerceUsers domain - probably here because we are in edit or preview mode
                var message = StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.UpdateUserProfileError);
                message = string.Format(CultureInfo.InvariantCulture, message, Context.User.LocalName);
                result  = new UpdateUserResult {
                    Success = false
                };
                result.SystemMessages.Add(new Commerce.Services.SystemMessage {
                    Message = message
                });
            }

            //Helpers.LogSystemMessages(result.SystemMessages, result);
            return(new ManagerResponse <UpdateUserResult, CommerceUser>(result, result.CommerceUser));
        }
        /// <summary>
        /// Activates the loyalty account for the current user cart.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <returns>
        /// The manager response where the loyalty card is returned in the Result.
        /// </returns>
        public virtual ManagerResponse <JoinLoyaltyProgramResult, LoyaltyCard> ActivateAccount([NotNull] CommerceStorefront storefront, [NotNull] IVisitorContext visitorContext)
        {
            var result = new JoinLoyaltyProgramResult {
                Success = false
            };
            var cartResult = this._cartManager.GetCurrentCart(storefront, visitorContext, false);

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

            var errorResult = new JoinLoyaltyProgramResult()
            {
                Success = false
            };

            // Limit the number of loyalty programs a user may join.
            var currentLoyaltyProgramResult = this.GetLoyaltyCards(storefront, visitorContext.UserId);

            if (!currentLoyaltyProgramResult.ServiceProviderResult.Success)
            {
                currentLoyaltyProgramResult.ServiceProviderResult.SystemMessages.ToList().ForEach(m => errorResult.SystemMessages.Add(m));
                return(new ManagerResponse <JoinLoyaltyProgramResult, LoyaltyCard>(errorResult, null));
            }

            int maxLoyaltyProgramsToJoin = 10;//(StorefrontManager.CurrentStorefront).MaxNumberOfLoyaltyProgramsToJoin;

            if (currentLoyaltyProgramResult.Result.Count() >= maxLoyaltyProgramsToJoin)
            {
                var message = StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.MaxLoyaltyProgramsToJoinReached);
                message = string.Format(CultureInfo.InvariantCulture, message, maxLoyaltyProgramsToJoin);
                errorResult.SystemMessages.Add(new Commerce.Services.SystemMessage()
                {
                    Message = message
                });
                return(new ManagerResponse <JoinLoyaltyProgramResult, LoyaltyCard>(errorResult, null));
            }

            var cart = (CommerceCart)cartResult.ServiceProviderResult.Cart;

            //var request = new Sitecore.Commerce.Connect.DynamicsRetail.Services.LoyaltyPrograms.JoinLoyaltyProgramRequest(visitorContext.UserId, storefront.ShopName) { CartId = cart.ExternalId };
            result = new JoinLoyaltyProgramResult();//this.LoyaltyProgramServiceProvider.JoinLoyaltyProgram(request);

            Helpers.LogSystemMessages(result.SystemMessages, result);
            return(new ManagerResponse <JoinLoyaltyProgramResult, LoyaltyCard>(result, result.LoyaltyCard));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the user.
        /// </summary>
        /// <param name="userName">The username.</param>
        /// <returns>
        /// The manager response where the user is returned in the response.
        /// </returns>
        public virtual ManagerResponse <GetUserResult, CommerceUser> GetUser(string userName)
        {
            Assert.ArgumentNotNullOrEmpty(userName, "userName");

            var request = new GetUserRequest(userName);
            var result  = this._customerServiceProvider.GetUser(request);

            if (!result.Success || result.CommerceUser == null)
            {
                var message = StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.UserNotFoundError);
                result.SystemMessages.Add(new SystemMessage {
                    Message = message
                });
            }

            //Helpers.LogSystemMessages(result.SystemMessages, result);
            return(new ManagerResponse <GetUserResult, CommerceUser>(result, result.CommerceUser));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Registers the user.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="inputModel">The input model.</param>
        /// <returns>
        /// The manager result where the user is returned as the Result.
        /// </returns>
        public virtual ManagerResponse <CreateUserResult, CommerceUser> RegisterUser([NotNull] CommerceStorefront storefront, RegisterUserInputModel inputModel)
        {
            Assert.ArgumentNotNull(storefront, "storefront");
            Assert.ArgumentNotNull(inputModel, "inputModel");
            Assert.ArgumentNotNullOrEmpty(inputModel.UserName, "inputModel.UserName");
            Assert.ArgumentNotNullOrEmpty(inputModel.Password, "inputModel.Password");

            CreateUserResult result;

            // Attempt to register the user
            try
            {
                var request = new CreateUserRequest(inputModel.UserName, inputModel.Password, inputModel.UserName, storefront.ShopName);
                result = this._customerServiceProvider.CreateUser(request);
                //                if (!result.Success)
                //                {
                //                    Helpers.LogSystemMessages(result.SystemMessages, result);
                //                }
                //                else

                if (result.Success && result.CommerceUser == null && result.SystemMessages.Count() == 0)
                {
                    // Connect bug:  This is a work around to a Connect bug.  When the user already exists,connect simply aborts the pipeline but
                    // does not set the success flag nor does it return an error message.
                    result.Success = false;
                    result.SystemMessages.Add(new SystemMessage {
                        Message = StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.UserAlreadyExists)
                    });
                }
            }
            catch (MembershipCreateUserException e)
            {
                result = new CreateUserResult {
                    Success = false
                };
                result.SystemMessages.Add(new Commerce.Services.SystemMessage {
                    Message = ErrorCodeToString(e.StatusCode)
                });
            }

            return(new ManagerResponse <CreateUserResult, CommerceUser>(result, result.CommerceUser));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Errors the code to string.
        /// </summary>
        /// <param name="createStatus">The create status.</param>
        /// <returns>The membership error status.</returns>
        protected virtual string ErrorCodeToString(MembershipCreateStatus createStatus)
        {
            // See http://go.microsoft.com/fwlink/?LinkID=177550 for
            // a full list of status codes.
            switch (createStatus)
            {
            case MembershipCreateStatus.DuplicateUserName:
                return(StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.UserAlreadyExists));

            case MembershipCreateStatus.DuplicateEmail:
                return(StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.UserNameForEmailExists));

            case MembershipCreateStatus.InvalidPassword:
                return(StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.InvalidPasswordError));

            case MembershipCreateStatus.InvalidEmail:
                return(StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.InvalidEmailError));

            case MembershipCreateStatus.InvalidAnswer:
                return(StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.PasswordRetrievalAnswerInvalid));

            case MembershipCreateStatus.InvalidQuestion:
                return(StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.PasswordRetrievalQuestionInvalid));

            case MembershipCreateStatus.InvalidUserName:
                return(StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.UserNameInvalid));

            case MembershipCreateStatus.ProviderError:
                return(StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.AuthenticationProviderError));

            case MembershipCreateStatus.UserRejected:
                return(StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.UserRejectedError));

            default:
                return(StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.UnknownMembershipProviderError));
            }
        }