public virtual bool IsPaymentMethodFiltered(PaymentFilterRequest filterRequest) { Guard.NotNull(filterRequest, nameof(filterRequest)); var allFilters = GetAllPaymentMethodFilters(); return(allFilters.Any(x => x.IsExcluded(filterRequest))); }
public virtual IEnumerable <Provider <IPaymentMethod> > LoadActivePaymentMethods( Customer customer = null, IList <OrganizedShoppingCartItem> cart = null, int storeId = 0, PaymentMethodType[] types = null, bool provideFallbackMethod = true) { var filterRequest = new PaymentFilterRequest { Cart = cart, StoreId = storeId, Customer = customer }; var allFilters = GetAllPaymentMethodFilters(); var allProviders = types != null && types.Any() ? LoadAllPaymentMethods(storeId).Where(x => types.Contains(x.Value.PaymentMethodType)) : LoadAllPaymentMethods(storeId); var paymentMethods = GetAllPaymentMethods(storeId); var activeProviders = allProviders .Where(p => { try { // Only active payment methods. if (!p.Value.IsActive || !_paymentSettings.ActivePaymentMethodSystemNames.Contains(p.Metadata.SystemName, StringComparer.InvariantCultureIgnoreCase)) { return(false); } // Rule sets. if (paymentMethods.TryGetValue(p.Metadata.SystemName, out var pm)) { if (!_cartRuleProvider.RuleMatches(pm)) { return(false); } } filterRequest.PaymentMethod = p; // Only payment methods that have not been filtered out. if (allFilters.Any(x => x.IsExcluded(filterRequest))) { return(false); } } catch (Exception ex) { Logger.Error(ex); } return(true); }); if (!activeProviders.Any() && provideFallbackMethod) { var fallbackMethod = allProviders.FirstOrDefault(x => x.IsPaymentMethodActive(_paymentSettings)); if (fallbackMethod == null) { fallbackMethod = allProviders.FirstOrDefault(x => x.Metadata?.PluginDescriptor?.SystemName?.IsCaseInsensitiveEqual("SmartStore.OfflinePayment") ?? false) ?? allProviders.FirstOrDefault(); } if (fallbackMethod != null) { return(new Provider <IPaymentMethod>[] { fallbackMethod }); } if (DataSettings.DatabaseIsInstalled()) { throw new SmartException(T("Payment.OneActiveMethodProviderRequired")); } } return(activeProviders); }
/// <summary> /// Load active payment methods /// </summary> /// <param name="customer">Filter payment methods by customer and apply payment method restrictions; null to load all records</param> /// <param name="cart">Filter payment methods by cart amount; null to load all records</param> /// <param name="storeId">Filter payment methods by store identifier; pass 0 to load all records</param> /// <param name="types">Filter payment methods by payment method types</param> /// <param name="provideFallbackMethod">Provide a fallback payment method if none is active</param> /// <returns>Payment methods</returns> public virtual IEnumerable<Provider<IPaymentMethod>> LoadActivePaymentMethods( Customer customer = null, IList<OrganizedShoppingCartItem> cart = null, int storeId = 0, PaymentMethodType[] types = null, bool provideFallbackMethod = true) { IList<IPaymentMethodFilter> allFilters = null; IEnumerable<Provider<IPaymentMethod>> allProviders = null; var filterRequest = new PaymentFilterRequest { Cart = cart, StoreId = storeId, Customer = customer }; if (types != null && types.Any()) allProviders = LoadAllPaymentMethods(storeId).Where(x => types.Contains(x.Value.PaymentMethodType)); else allProviders = LoadAllPaymentMethods(storeId); var activeProviders = allProviders .Where(p => { if (!p.Value.IsActive || !_paymentSettings.ActivePaymentMethodSystemNames.Contains(p.Metadata.SystemName, StringComparer.InvariantCultureIgnoreCase)) return false; // payment method filtering if (allFilters == null) allFilters = GetAllPaymentMethodFilters(); filterRequest.PaymentMethod = p; if (allFilters.Any(x => x.IsExcluded(filterRequest))) return false; return true; }); if (!activeProviders.Any() && provideFallbackMethod) { var fallbackMethod = allProviders.FirstOrDefault(x => x.IsPaymentMethodActive(_paymentSettings)); if (fallbackMethod == null) fallbackMethod = allProviders.FirstOrDefault(); if (fallbackMethod != null) { return new Provider<IPaymentMethod>[] { fallbackMethod }; } else { if (DataSettings.DatabaseIsInstalled()) throw new SmartException(T("Payment.OneActiveMethodProviderRequired")); } } return activeProviders; }
/// <summary> /// Load active payment methods /// </summary> /// <param name="customer">Filter payment methods by customer and apply payment method restrictions; null to load all records</param> /// <param name="cart">Filter payment methods by cart amount; null to load all records</param> /// <param name="storeId">Filter payment methods by store identifier; pass 0 to load all records</param> /// <param name="types">Filter payment methods by payment method types</param> /// <param name="provideFallbackMethod">Provide a fallback payment method if none is active</param> /// <returns>Payment methods</returns> public virtual IEnumerable <Provider <IPaymentMethod> > LoadActivePaymentMethods( Customer customer = null, IList <OrganizedShoppingCartItem> cart = null, int storeId = 0, PaymentMethodType[] types = null, bool provideFallbackMethod = true) { IList <IPaymentMethodFilter> allFilters = null; IEnumerable <Provider <IPaymentMethod> > allProviders = null; var filterRequest = new PaymentFilterRequest { Cart = cart, StoreId = storeId, Customer = customer }; if (types != null && types.Any()) { allProviders = LoadAllPaymentMethods(storeId).Where(x => types.Contains(x.Value.PaymentMethodType)); } else { allProviders = LoadAllPaymentMethods(storeId); } var activeProviders = allProviders .Where(p => { if (!p.Value.IsActive || !_paymentSettings.ActivePaymentMethodSystemNames.Contains(p.Metadata.SystemName, StringComparer.InvariantCultureIgnoreCase)) { return(false); } // payment method filtering if (allFilters == null) { allFilters = GetAllPaymentMethodFilters(); } filterRequest.PaymentMethod = p; if (allFilters.Any(x => x.IsExcluded(filterRequest))) { return(false); } return(true); }); if (!activeProviders.Any() && provideFallbackMethod) { var fallbackMethod = allProviders.FirstOrDefault(x => x.IsPaymentMethodActive(_paymentSettings)); if (fallbackMethod == null) { fallbackMethod = allProviders.FirstOrDefault(); } if (fallbackMethod != null) { return(new Provider <IPaymentMethod>[] { fallbackMethod }); } else { if (DataSettings.DatabaseIsInstalled()) { throw new SmartException(T("Payment.OneActiveMethodProviderRequired")); } } } return(activeProviders); }