public async Task <ActionResult> AddOrEditMerchantDialog(string id = null)
        {
            var merchant = new MerchantModel();

            bool isNewMerchant = id == null;

            if (!isNewMerchant)
            {
                merchant = await _payMerchantClient.Api.GetByIdAsync(id);
            }

            var viewModel = new AddOrEditMerchantDialogViewModel
            {
                Caption       = isNewMerchant ? "Add merchant" : "Edit merchant",
                IsNewMerchant = id == null,
                ApiKey        = merchant.ApiKey,
                Id            = id,
                LwId          = merchant.LwId,
                Name          = merchant.Name,
                DisplayName   = merchant.DisplayName,
                Email         = merchant.Email
            };

            return(View(viewModel));
        }
        public async Task <ActionResult> AddOrEditMerchant(AddOrEditMerchantDialogViewModel vm)
        {
            var merchants = await _payMerchantClient.Api.GetAllAsync();

            if (string.IsNullOrEmpty(vm.Name))
            {
                return(this.JsonFailResult("MerchantId required", ErrorMessageAnchor));
            }

            if (string.IsNullOrEmpty(vm.DisplayName))
            {
                return(this.JsonFailResult("DisplayName required", ErrorMessageAnchor));
            }

            if (string.IsNullOrEmpty(vm.ApiKey))
            {
                vm.ApiKey = StringUtils.GenerateId().Replace("-", string.Empty);
            }

            if (string.IsNullOrEmpty(vm.Email) && vm.IsNewMerchant)
            {
                return(this.JsonFailResult("Email required", ErrorMessageAnchor));
            }

            if (vm.IsNewMerchant)
            {
                if (merchants != null && merchants.Select(x => x.Name).Contains(vm.Name))
                {
                    return(this.JsonFailResult(Phrases.AlreadyExists, "#name"));
                }

                try
                {
                    var merchant = await _payMerchantClient.Api.CreateAsync(new CreateMerchantRequest
                    {
                        Name        = vm.Name,
                        ApiKey      = vm.ApiKey,
                        LwId        = vm.LwId,
                        DisplayName = vm.DisplayName,
                        Email       = vm.Email
                    });

                    await _payAuthClient.RegisterAsync(new Lykke.Service.PayAuth.Client.Models.RegisterRequest
                    {
                        ApiKey   = vm.ApiKey,
                        ClientId = merchant.Id
                    });
                }
                catch (Exception ex)
                {
                    return(this.JsonFailResult(ex.Message, ErrorMessageAnchor));
                }
            }
            else
            {
                MerchantModel existingMerchant = await _payMerchantClient.Api.GetByIdAsync(vm.Id);

                try
                {
                    var updateRequest = new UpdateMerchantRequest
                    {
                        Id          = vm.Id,
                        ApiKey      = vm.ApiKey,
                        LwId        = vm.LwId,
                        Name        = vm.Name,
                        DisplayName = vm.DisplayName,
                    };

                    if (string.IsNullOrEmpty(existingMerchant.Email))
                    {
                        if (!string.IsNullOrEmpty(vm.Email))
                        {
                            updateRequest.Email = vm.Email;
                        }
                    }

                    await _payMerchantClient.Api.UpdateAsync(updateRequest);

                    await _payAuthClient.UpdateApiKeyAsync(new Lykke.Service.PayAuth.Client.Models.UpdateApiKeyRequest
                    {
                        ApiKey   = vm.ApiKey,
                        ClientId = vm.Id
                    });
                }
                catch (Exception ex)
                {
                    return(this.JsonFailResult(ex.Message, ErrorMessageAnchor));
                }
            }

            return(this.JsonRequestResult("#merchantsList", Url.Action("MerchantsList")));
        }