Example #1
0
        public JsonResult AddressDelete(DeleteAddressInputModelItem model)
        {
            try
            {
                Assert.ArgumentNotNull(model, nameof(model));

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

                var addresses = new List <IParty>();
                var response  = AccountManager.RemovePartiesFromUser(Context.User.Name, model.ExternalId);
                var result    = new AddressListItemApiModel(response.ServiceProviderResult);
                if (response.ServiceProviderResult.Success)
                {
                    addresses = AllAddresses(result);
                }

                result.Initialize(addresses, null);
                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new ErrorApiModel("AddressDelete", e), JsonRequestBehavior.AllowGet));
            }
        }
Example #2
0
        private List <IParty> AllAddresses(AddressListItemApiModel result)
        {
            var addresses = new List <IParty>();
            var response  = AccountManager.GetCustomerParties(CommerceUserContext.Current.UserName);

            if (response.ServiceProviderResult.Success && response.Result != null)
            {
                addresses = response.Result.ToList();
            }

            result.SetErrors(response.ServiceProviderResult);
            return(addresses);
        }
Example #3
0
        private Dictionary <string, string> GetAvailableCountries(AddressListItemApiModel result)
        {
            var countries = new Dictionary <string, string>();
            var response  = CountryManager.GetAvailableCountries();

            if (response.ServiceProviderResult.Success && response.Result != null)
            {
                countries = response.Result;
            }

            result.SetErrors(response.ServiceProviderResult);
            return(countries);
        }
Example #4
0
 public JsonResult AddressList()
 {
     try
     {
         var result    = new AddressListItemApiModel();
         var addresses = AllAddresses(result);
         var countries = GetAvailableCountries(result);
         result.Initialize(addresses, countries);
         return(Json(result, JsonRequestBehavior.AllowGet));
     }
     catch (Exception e)
     {
         return(Json(new ErrorApiModel("AddressList", e), JsonRequestBehavior.AllowGet));
     }
 }
        public JsonResult AddressList()
        {
            var result = new AddressListItemApiModel();

            try
            {
                var addresses = this.AllAddresses(result);
                var countries = this.GetAvailableCountries(result);
                result.Initialize(addresses, countries);
                return(this.Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Sitecore.Commerce.OpenIDConnectionClosedUnexpectedlyException)
            {
                this.CleanNotAuthorizedSession();
                return(this.Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(this.Json(new ErrorApiModel("AddressList", e), JsonRequestBehavior.AllowGet));
            }
        }
Example #6
0
        public JsonResult AddressModify(PartyInputModelItem model)
        {
            try
            {
                Assert.ArgumentNotNull(model, nameof(model));

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

                var addresses = new List <IParty>();
                var result    = new AddressListItemApiModel();
                if (CommerceUserContext.Current != null)
                {
                    var user = CommerceUserContext.Current;
                    if (string.IsNullOrEmpty(model.ExternalId))
                    {
                        // Verify we have not reached the maximum number of addresses supported.
                        var numberOfAddresses = AllAddresses(result).Count;
                        if (numberOfAddresses >= MaxNumberOfAddresses)
                        {
                            var message = DictionaryPhraseRepository.Current.Get("/Accounts/Max Address Limit Reached", "The maximum number of addresses ({0}) has been reached.");
                            result.Errors.Add(string.Format(message, numberOfAddresses));
                            result.Success = false;
                        }
                        else
                        {
                            model.ExternalId = Guid.NewGuid().ToString("B");

                            var response = AccountManager.AddParties(user.UserName, new List <IParty> {
                                model
                            });
                            result.SetErrors(response.ServiceProviderResult);
                            if (response.ServiceProviderResult.Success)
                            {
                                addresses = AllAddresses(result);
                            }

                            result.Initialize(addresses, null);
                        }
                    }
                    else
                    {
                        var response = AccountManager.UpdateParties(user.UserName, new List <IParty> {
                            model
                        });
                        result.SetErrors(response.ServiceProviderResult);
                        if (response.ServiceProviderResult.Success)
                        {
                            addresses = AllAddresses(result);
                        }

                        result.Initialize(addresses, null);
                    }
                }

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

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

                var addresses = new List <IParty>();
                var user      = this.CommerceUserContext.Current;
                var result    = new AddressListItemApiModel();

                var customer = new CommerceCustomer {
                    ExternalId = user.UserId
                };
                var party = new Party
                {
                    ExternalId    = model.ExternalId,
                    FirstName     = model.Name,
                    LastName      = string.Empty,
                    Address1      = model.Address1,
                    City          = model.City,
                    Country       = model.Country,
                    State         = model.State,
                    ZipPostalCode = model.ZipPostalCode,
                    PartyId       = model.PartyId,
                    IsPrimary     = model.IsPrimary
                };

                if (string.IsNullOrEmpty(party.ExternalId))
                {
                    // Verify we have not reached the maximum number of addresses supported.
                    int numberOfAddresses = this.AllAddresses(result).Count;
                    if (numberOfAddresses >= this.MaxNumberOfAddresses)
                    {
                        var message = "Address limit reached";
                        result.Errors.Add(string.Format(CultureInfo.InvariantCulture, message, numberOfAddresses));
                        result.Success = false;
                    }
                    else
                    {
                        var response = this.AccountManager.AddParties(user.UserName, new List <IParty> {
                            model
                        });
                        result.SetErrors(response.ServiceProviderResult);
                        if (response.ServiceProviderResult.Success)
                        {
                            addresses = this.AllAddresses(result);
                        }

                        result.Initialize(addresses, null);
                    }
                }
                else
                {
                    var response = this.AccountManager.UpdateParties(user.UserName, new List <IParty> {
                        model
                    });
                    result.SetErrors(response.ServiceProviderResult);
                    if (response.ServiceProviderResult.Success)
                    {
                        addresses = this.AllAddresses(result);
                    }

                    result.Initialize(addresses, null);
                }

                return(this.Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (OpenIDConnectionClosedUnexpectedlyException e)
            {
                this.CleanNotAuthorizedSession();
                return(this.Json(new ErrorApiModel("Login", e), JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(this.Json(new ErrorApiModel("AddressModify", e), JsonRequestBehavior.AllowGet));
            }
        }