Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

PaymentPluginManager.cs

using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Payments;
using Nop.Services.Configuration;
using Nop.Services.Customers;
using Nop.Services.Plugins;

namespace Nop.Services.Payments;

/// 
/// Represents a payment plugin manager implementation
/// 
public partial class PaymentPluginManager : PluginManager, IPaymentPluginManager
{
    #region Fields

    protected readonly ISettingService _settingService;
    protected readonly PaymentSettings _paymentSettings;

    #endregion

    #region Ctor

    public PaymentPluginManager(ICustomerService customerService,
        IPluginService pluginService,
        ISettingService settingService,
        PaymentSettings paymentSettings) : base(customerService, pluginService)
    {
        _settingService = settingService;
        _paymentSettings = paymentSettings;
    }

    #endregion

    #region Methods

    /// 
    /// Load active payment methods
    /// 
    /// Filter by customer; pass null to load all plugins
    /// Filter by store; pass 0 to load all plugins
    /// Filter by country; pass 0 to load all plugins
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the list of active payment methods
    /// 
    public virtual async Task> LoadActivePluginsAsync(Customer customer = null, int storeId = 0,
        int countryId = 0)
    {
        var paymentMethods = await LoadActivePluginsAsync(_paymentSettings.ActivePaymentMethodSystemNames, customer, storeId);

        //filter by country
        if (countryId > 0)
            paymentMethods = await paymentMethods.WhereAwait(async method => !(await GetRestrictedCountryIdsAsync(method)).Contains(countryId)).ToListAsync();

        return paymentMethods;
    }

    /// 
    /// Check whether the passed payment method is active
    /// 
    /// Payment method to check
    /// Result
    public virtual bool IsPluginActive(IPaymentMethod paymentMethod)
    {
        return IsPluginActive(paymentMethod, _paymentSettings.ActivePaymentMethodSystemNames);
    }

    /// 
    /// Check whether the payment method with the passed system name is active
    /// 
    /// System name of payment method to check
    /// Filter by customer; pass null to load all plugins
    /// Filter by store; pass 0 to load all plugins
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the result
    /// 
    public virtual async Task IsPluginActiveAsync(string systemName, Customer customer = null, int storeId = 0)
    {
        var paymentMethod = await LoadPluginBySystemNameAsync(systemName, customer, storeId);
        return IsPluginActive(paymentMethod);
    }

    /// 
    /// Get countries in which the passed payment method is not allowed
    /// 
    /// Payment method
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the list of country identifiers
    /// 
    public virtual async Task> GetRestrictedCountryIdsAsync(IPaymentMethod paymentMethod)
    {
        ArgumentNullException.ThrowIfNull(paymentMethod);

        var settingKey = string.Format(NopPaymentDefaults.RestrictedCountriesSettingName, paymentMethod.PluginDescriptor.SystemName);

        return await _settingService.GetSettingByKeyAsync>(settingKey) ?? new List();
    }

    /// 
    /// Save countries in which the passed payment method is not allowed
    /// 
    /// Payment method
    /// List of country identifiers
    /// A task that represents the asynchronous operation
    public virtual async Task SaveRestrictedCountriesAsync(IPaymentMethod paymentMethod, IList countryIds)
    {
        ArgumentNullException.ThrowIfNull(paymentMethod);

        var settingKey = string.Format(NopPaymentDefaults.RestrictedCountriesSettingName, paymentMethod.PluginDescriptor.SystemName);

        await _settingService.SetSettingAsync(settingKey, countryIds.ToList());
    }

    #endregion
}