protected bool SaveConfigurationModel <TSetting>(ApiConfigurationModel model, FormCollection form, Action <TSetting> map = null) where TSetting : PayPalApiSettingsBase, ISettings, new() { var storeDependingSettingHelper = new StoreDependingSettingHelper(ViewData); var storeScope = this.GetActiveStoreScopeConfiguration(Services.StoreService, Services.WorkContext); var settings = Services.Settings.LoadSetting <TSetting>(storeScope); var oldClientId = settings.ClientId; var oldSecret = settings.Secret; var oldProfileId = settings.ExperienceProfileId; var validator = new PayPalApiConfigValidator(T, x => { return(storeScope == 0 || storeDependingSettingHelper.IsOverrideChecked(settings, x, form)); }); validator.Validate(model, ModelState); if (!ModelState.IsValid) { return(false); } ModelState.Clear(); model.TransactMode = TransactMode.AuthorizeAndCapture; MiniMapper.Map(model, settings); settings.ApiAccountName = model.ApiAccountName.TrimSafe(); settings.ApiAccountPassword = model.ApiAccountPassword.TrimSafe(); settings.ClientId = model.ClientId.TrimSafe(); settings.ExperienceProfileId = model.ExperienceProfileId.TrimSafe(); settings.Secret = model.Secret.TrimSafe(); settings.Signature = model.Signature.TrimSafe(); settings.WebhookId = model.WebhookId.TrimSafe(); // Additional mapping. map?.Invoke(settings); // Credentials changed: reset profile and webhook id to avoid errors. if (!oldClientId.IsCaseInsensitiveEqual(settings.ClientId) || !oldSecret.IsCaseInsensitiveEqual(settings.Secret)) { if (oldProfileId.IsCaseInsensitiveEqual(settings.ExperienceProfileId)) { settings.ExperienceProfileId = null; } settings.WebhookId = null; } using (Services.Settings.BeginScope()) { storeDependingSettingHelper.UpdateSettings(settings, form, storeScope, Services.Settings); } using (Services.Settings.BeginScope()) { // Multistore context not possible, see IPN handling. Services.Settings.SaveSetting(settings, x => x.UseSandbox, 0, false); } NotifySuccess(T("Admin.Common.DataSuccessfullySaved")); return(true); }
protected bool SaveConfigurationModel( ApiConfigurationModel model, FormCollection form, Action <TSetting> map = null) { var storeDependingSettingHelper = new StoreDependingSettingHelper(ViewData); var storeScope = this.GetActiveStoreScopeConfiguration(Services.StoreService, Services.WorkContext); var settings = Services.Settings.LoadSetting <TSetting>(storeScope); var oldClientId = settings.ClientId; var oldSecret = settings.Secret; var oldProfileId = settings.ExperienceProfileId; var validator = new PayPalApiConfigValidator(T, x => { return(storeScope == 0 || storeDependingSettingHelper.IsOverrideChecked(settings, x, form)); }); validator.Validate(model, ModelState); // Additional validation. if (ModelState.IsValid) { // PayPal review: check if credentials are valid. var credentialChanged = model.ClientId.HasValue() && model.Secret.HasValue() && (!model.ClientId.IsCaseInsensitiveEqual(settings.ClientId) || !model.Secret.IsCaseInsensitiveEqual(settings.Secret)); if (credentialChanged) { var session = new PayPalSessionData { ProviderSystemName = ProviderSystemName }; var tmpSettings = new PayPalApiSettingsBase { UseSandbox = model.UseSandbox, ClientId = model.ClientId, Secret = model.Secret }; var result = PayPalService.EnsureAccessToken(session, tmpSettings); if (!result.Success) { ModelState.AddModelError("", T("Plugins.SmartStore.PayPal.InvalidCredentials")); ModelState.AddModelError("", result.ErrorMessage); } } } if (!ModelState.IsValid) { return(false); } ModelState.Clear(); model.TransactMode = TransactMode.AuthorizeAndCapture; MiniMapper.Map(model, settings); settings.ApiAccountName = model.ApiAccountName.TrimSafe(); settings.ApiAccountPassword = model.ApiAccountPassword.TrimSafe(); settings.ClientId = model.ClientId.TrimSafe(); settings.ExperienceProfileId = model.ExperienceProfileId.TrimSafe(); settings.Secret = model.Secret.TrimSafe(); settings.Signature = model.Signature.TrimSafe(); settings.WebhookId = model.WebhookId.TrimSafe(); // Additional mapping. map?.Invoke(settings); // Credentials changed: reset profile and webhook id to avoid errors. if (!oldClientId.IsCaseInsensitiveEqual(settings.ClientId) || !oldSecret.IsCaseInsensitiveEqual(settings.Secret)) { if (oldProfileId.IsCaseInsensitiveEqual(settings.ExperienceProfileId)) { settings.ExperienceProfileId = null; } settings.WebhookId = null; } using (Services.Settings.BeginScope()) { storeDependingSettingHelper.UpdateSettings(settings, form, storeScope, Services.Settings); } using (Services.Settings.BeginScope()) { // Multistore context not possible, see IPN handling. Services.Settings.SaveSetting(settings, x => x.UseSandbox, 0, false); } NotifySuccess(T("Admin.Common.DataSuccessfullySaved")); return(true); }