public ActionResult Configure(ConfigurationModel model)
        {
            if (!ModelState.IsValid)
                return Configure();

            //load settings for a chosen store scope
            var storeScope = this.GetActiveStoreScopeConfiguration(_storeService, _workContext);
            var PayPalAdvancedPaymentSettings = _settingService.LoadSetting<PayPalAdvancedPaymentSettings>(storeScope);

            //save settings
            PayPalAdvancedPaymentSettings.UseSandbox = model.UseSandbox;
            PayPalAdvancedPaymentSettings.TransactMode = (TransactMode)model.TransactModeId;
            PayPalAdvancedPaymentSettings.PFPartner = model.PFPartner;
            PayPalAdvancedPaymentSettings.PFMerchantLogin = model.PFMerchantLogin;
            PayPalAdvancedPaymentSettings.PFUser = model.PFUser;
            ModelState.Remove("PFPassword"); //ensure value is updated to view
            ModelState.Remove("IsEncryptPassword");
            if (string.IsNullOrEmpty(model.PFPassword))
            {
                PayPalAdvancedPaymentSettings.PFPassword = model.PFPassword;
                model.IsEncryptPassword = false;
                PayPalAdvancedPaymentSettings.IsEncryptPassword = model.IsEncryptPassword;
            }
            else if (model.IsEncryptPassword)
            {
                try
                {
                    // try decrypt first
                    string tempPwd = _encryptionService.DecryptText(model.PFPassword, SecurityHelper.GetPrivateKey());
                    // now encrypt it again
                    PayPalAdvancedPaymentSettings.PFPassword = _encryptionService.EncryptText(tempPwd, SecurityHelper.GetPrivateKey());
                }
                catch
                {
                    // if failed, now encrypt it
                    try
                    {
                        PayPalAdvancedPaymentSettings.PFPassword = _encryptionService.EncryptText(model.PFPassword, SecurityHelper.GetPrivateKey());
                    }
                    catch
                    {
                        // if failed again, set it as is
                        PayPalAdvancedPaymentSettings.PFPassword = model.PFPassword;
                    }
                }
                PayPalAdvancedPaymentSettings.IsEncryptPassword = model.IsEncryptPassword;
            }
            else
            {
                // decrypt it
                try
                {
                    PayPalAdvancedPaymentSettings.PFPassword = _encryptionService.DecryptText(model.PFPassword, SecurityHelper.GetPrivateKey());
                }
                catch
                {
                    // if failed again, set it as is
                    PayPalAdvancedPaymentSettings.PFPassword = model.PFPassword;
                }
                PayPalAdvancedPaymentSettings.IsEncryptPassword = model.IsEncryptPassword;
            }
            
            PayPalAdvancedPaymentSettings.AdditionalFee = model.AdditionalFee;
            PayPalAdvancedPaymentSettings.AdditionalFeePercentage = model.AdditionalFeePercentage;
            PayPalAdvancedPaymentSettings.SkipPaymentInfo = model.SkipPaymentInfo;
            PayPalAdvancedPaymentSettings.EnableMobileOptimizedLayout = model.EnableMobileOptimizedLayout;

            /* We do not clear cache after each setting update.
             * This behavior can increase performance because cached settings will not be cleared 
             * and loaded from database after each update */
            if (model.UseSandbox_OverrideForStore || storeScope == 0)
                _settingService.SaveSetting(PayPalAdvancedPaymentSettings, x => x.UseSandbox, storeScope, false);
            else if (storeScope > 0)
                _settingService.DeleteSetting(PayPalAdvancedPaymentSettings, x => x.UseSandbox, storeScope);

            if (model.TransactModeId_OverrideForStore || storeScope == 0)
                _settingService.SaveSetting(PayPalAdvancedPaymentSettings, x => x.TransactMode, storeScope, false);
            else if (storeScope > 0)
                _settingService.DeleteSetting(PayPalAdvancedPaymentSettings, x => x.TransactMode, storeScope);

            if (model.PFPartner_OverrideForStore || storeScope == 0)
                _settingService.SaveSetting(PayPalAdvancedPaymentSettings, x => x.PFPartner, storeScope, false);
            else if (storeScope > 0)
                _settingService.DeleteSetting(PayPalAdvancedPaymentSettings, x => x.PFPartner, storeScope);

            if (model.PFMerchantLogin_OverrideForStore || storeScope == 0)
                _settingService.SaveSetting(PayPalAdvancedPaymentSettings, x => x.PFMerchantLogin, storeScope, false);
            else if (storeScope > 0)
                _settingService.DeleteSetting(PayPalAdvancedPaymentSettings, x => x.PFMerchantLogin, storeScope);

            if (model.PFUser_OverrideForStore || storeScope == 0)
                _settingService.SaveSetting(PayPalAdvancedPaymentSettings, x => x.PFUser, storeScope, false);
            else if (storeScope > 0)
                _settingService.DeleteSetting(PayPalAdvancedPaymentSettings, x => x.PFUser, storeScope);

            if (model.PFPassword_OverrideForStore || storeScope == 0)
                _settingService.SaveSetting(PayPalAdvancedPaymentSettings, x => x.PFPassword, storeScope, false);
            else if (storeScope > 0)
                _settingService.DeleteSetting(PayPalAdvancedPaymentSettings, x => x.PFPassword, storeScope);

            if (model.IsEncryptPassword_OverrideForStore || storeScope == 0)
                _settingService.SaveSetting(PayPalAdvancedPaymentSettings, x => x.IsEncryptPassword, storeScope, false);
            else if (storeScope > 0)
                _settingService.DeleteSetting(PayPalAdvancedPaymentSettings, x => x.IsEncryptPassword, storeScope);

            if (model.AdditionalFee_OverrideForStore || storeScope == 0)
                _settingService.SaveSetting(PayPalAdvancedPaymentSettings, x => x.AdditionalFee, storeScope, false);
            else if (storeScope > 0)
                _settingService.DeleteSetting(PayPalAdvancedPaymentSettings, x => x.AdditionalFee, storeScope);

            if (model.AdditionalFeePercentage_OverrideForStore || storeScope == 0)
                _settingService.SaveSetting(PayPalAdvancedPaymentSettings, x => x.AdditionalFeePercentage, storeScope, false);
            else if (storeScope > 0)
                _settingService.DeleteSetting(PayPalAdvancedPaymentSettings, x => x.AdditionalFeePercentage, storeScope);

            if (model.SkipPaymentInfo_OverrideForStore || storeScope == 0)
                _settingService.SaveSetting(PayPalAdvancedPaymentSettings, x => x.SkipPaymentInfo, storeScope, false);
            else if (storeScope > 0)
                _settingService.DeleteSetting(PayPalAdvancedPaymentSettings, x => x.SkipPaymentInfo, storeScope);

            if (model.EnableMobileOptimizedLayout_OverrideForStore || storeScope == 0)
                _settingService.SaveSetting(PayPalAdvancedPaymentSettings, x => x.EnableMobileOptimizedLayout, storeScope, false);
            else if (storeScope > 0)
                _settingService.DeleteSetting(PayPalAdvancedPaymentSettings, x => x.EnableMobileOptimizedLayout, storeScope);

            //now clear settings cache
            _settingService.ClearCache();

            SuccessNotification(_localizationService.GetResource("Admin.Plugins.Saved"));

            return Configure();
        }                      
        public ActionResult Configure()
        {
            //load settings for a chosen store scope
            var storeScope = this.GetActiveStoreScopeConfiguration(_storeService, _workContext);
            var payPalAdvancedPaymentSettings = _settingService.LoadSetting<PayPalAdvancedPaymentSettings>(storeScope);

            var model = new ConfigurationModel();
            model.UseSandbox = payPalAdvancedPaymentSettings.UseSandbox;
            model.TransactModeId = Convert.ToInt32(payPalAdvancedPaymentSettings.TransactMode);
            model.PFPartner = payPalAdvancedPaymentSettings.PFPartner;
            model.PFMerchantLogin = payPalAdvancedPaymentSettings.PFMerchantLogin;
            model.PFUser = payPalAdvancedPaymentSettings.PFUser;
            model.PFPassword = payPalAdvancedPaymentSettings.PFPassword;
            model.IsEncryptPassword = payPalAdvancedPaymentSettings.IsEncryptPassword;
            model.AdditionalFee = payPalAdvancedPaymentSettings.AdditionalFee;
            model.AdditionalFeePercentage = payPalAdvancedPaymentSettings.AdditionalFeePercentage;
            model.TransactModeValues = payPalAdvancedPaymentSettings.TransactMode.ToSelectList();
            model.SkipPaymentInfo = payPalAdvancedPaymentSettings.SkipPaymentInfo;
            model.EnableMobileOptimizedLayout = payPalAdvancedPaymentSettings.EnableMobileOptimizedLayout;
            model.LicenseKeys = (from k in this._licenseService.GetAll() select new LicenseModel { Id = k.Id, LicenseKey = k.LicenseKey, Host = this._licenseService.GetLicenseHost(k.LicenseKey), Type = this._licenseService.GetLicenseType(k.LicenseKey) }).ToList<LicenseModel>();
            
            model.ActiveStoreScopeConfiguration = storeScope;
            if (storeScope > 0)
            {
                model.UseSandbox_OverrideForStore = _settingService.SettingExists(payPalAdvancedPaymentSettings, x => x.UseSandbox, storeScope);
                model.TransactModeId_OverrideForStore = _settingService.SettingExists(payPalAdvancedPaymentSettings, x => x.TransactMode, storeScope);
                model.PFPartner_OverrideForStore = _settingService.SettingExists(payPalAdvancedPaymentSettings, x => x.PFPartner, storeScope);
                model.PFMerchantLogin_OverrideForStore = _settingService.SettingExists(payPalAdvancedPaymentSettings, x => x.PFMerchantLogin, storeScope);
                model.PFUser_OverrideForStore = _settingService.SettingExists(payPalAdvancedPaymentSettings, x => x.PFUser, storeScope);
                model.PFPassword_OverrideForStore = _settingService.SettingExists(payPalAdvancedPaymentSettings, x => x.PFPassword, storeScope);
                model.IsEncryptPassword_OverrideForStore = _settingService.SettingExists(payPalAdvancedPaymentSettings, x => x.IsEncryptPassword, storeScope);
                model.AdditionalFee_OverrideForStore = _settingService.SettingExists(payPalAdvancedPaymentSettings, x => x.AdditionalFee, storeScope);
                model.AdditionalFeePercentage_OverrideForStore = _settingService.SettingExists(payPalAdvancedPaymentSettings, x => x.AdditionalFeePercentage, storeScope);
                model.SkipPaymentInfo_OverrideForStore = _settingService.SettingExists(payPalAdvancedPaymentSettings, x => x.SkipPaymentInfo, storeScope);
                model.EnableMobileOptimizedLayout_OverrideForStore = _settingService.SettingExists(payPalAdvancedPaymentSettings, x => x.EnableMobileOptimizedLayout, storeScope);                
            }

            return View("~/Plugins/Payments.PayPalAdvanced/Views/PaymentPayPalAdvanced/Configure.cshtml", model);
        }