using Nop.Services.Directory; using Nop.Services.Localization; using Nop.Services.Payments; using Nop.Web.Areas.Admin.Infrastructure.Mapper.Extensions; using Nop.Web.Areas.Admin.Models.Directory; using Nop.Web.Areas.Admin.Models.Payments; using Nop.Web.Framework.Models.Extensions; namespace Nop.Web.Areas.Admin.Factories; /// /// Represents the payment method model factory implementation /// public partial class PaymentModelFactory : IPaymentModelFactory { #region Fields protected readonly ICountryService _countryService; protected readonly ILocalizationService _localizationService; protected readonly IPaymentPluginManager _paymentPluginManager; protected readonly IStateProvinceService _stateProvinceService; #endregion #region Ctor public PaymentModelFactory(ICountryService countryService, ILocalizationService localizationService, IPaymentPluginManager paymentPluginManager, IStateProvinceService stateProvinceService) { _countryService = countryService; _localizationService = localizationService; _paymentPluginManager = paymentPluginManager; _stateProvinceService = stateProvinceService; } #endregion #region Methods /// /// Prepare payment methods model /// /// Payment methods model /// /// A task that represents the asynchronous operation /// The task result contains the payment methods model /// public virtual async Task PreparePaymentMethodsModelAsync(PaymentMethodsModel methodsModel) { ArgumentNullException.ThrowIfNull(methodsModel); //prepare nested search models await PreparePaymentMethodSearchModelAsync(methodsModel.PaymentsMethod); await PreparePaymentMethodRestrictionModelAsync(methodsModel.PaymentMethodRestriction); return methodsModel; } /// /// Prepare paged payment method list model /// /// Payment method search model /// /// A task that represents the asynchronous operation /// The task result contains the payment method list model /// public virtual async Task PreparePaymentMethodListModelAsync(PaymentMethodSearchModel searchModel) { ArgumentNullException.ThrowIfNull(searchModel); //get payment methods var paymentMethods = (await _paymentPluginManager.LoadAllPluginsAsync()).ToPagedList(searchModel); //prepare grid model var model = await new PaymentMethodListModel().PrepareToGridAsync(searchModel, paymentMethods, () => { return paymentMethods.SelectAwait(async method => { //fill in model values from the entity var paymentMethodModel = method.ToPluginModel(); //fill in additional values (not existing in the entity) paymentMethodModel.IsActive = _paymentPluginManager.IsPluginActive(method); paymentMethodModel.ConfigurationUrl = method.GetConfigurationPageUrl(); paymentMethodModel.LogoUrl = await _paymentPluginManager.GetPluginLogoUrlAsync(method); paymentMethodModel.RecurringPaymentType = await _localizationService.GetLocalizedEnumAsync(method.RecurringPaymentType); return paymentMethodModel; }); }); return model; } /// /// Prepare payment method search model /// /// Payment method search model /// /// A task that represents the asynchronous operation /// The task result contains the payment method search model /// public virtual Task PreparePaymentMethodSearchModelAsync(PaymentMethodSearchModel searchModel) { ArgumentNullException.ThrowIfNull(searchModel); //prepare page parameters searchModel.SetGridPageSize(); return Task.FromResult(searchModel); } /// /// Prepare payment method restriction model /// /// Payment method restriction model /// /// A task that represents the asynchronous operation /// The task result contains the payment method restriction model /// public virtual async Task PreparePaymentMethodRestrictionModelAsync(PaymentMethodRestrictionModel model) { ArgumentNullException.ThrowIfNull(model); var countries = await _countryService.GetAllCountriesAsync(showHidden: true); model.AvailableCountries = await countries.SelectAwait(async country => { var countryModel = country.ToModel(); countryModel.NumberOfStates = (await _stateProvinceService.GetStateProvincesByCountryIdAsync(country.Id))?.Count ?? 0; return countryModel; }).ToListAsync(); foreach (var method in await _paymentPluginManager.LoadAllPluginsAsync()) { var paymentMethodModel = method.ToPluginModel(); paymentMethodModel.RecurringPaymentType = await _localizationService.GetLocalizedEnumAsync(method.RecurringPaymentType); model.AvailablePaymentMethods.Add(paymentMethodModel); var restrictedCountries = await _paymentPluginManager.GetRestrictedCountryIdsAsync(method); foreach (var country in countries) { if (!model.Restricted.ContainsKey(method.PluginDescriptor.SystemName)) model.Restricted[method.PluginDescriptor.SystemName] = new Dictionary(); model.Restricted[method.PluginDescriptor.SystemName][country.Id] = restrictedCountries.Contains(country.Id); } } return model; } #endregion }