11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using SmartStore . Core . Caching ;
45using SmartStore . Core . Data ;
6+ using SmartStore . Core . Domain . Customers ;
57using SmartStore . Core . Domain . Orders ;
68using SmartStore . Core . Domain . Payments ;
9+ using SmartStore . Core . Domain . Shipping ;
710using SmartStore . Core . Events ;
811using SmartStore . Core . Plugins ;
12+ using SmartStore . Services . Common ;
913using SmartStore . Services . Configuration ;
1014using SmartStore . Services . Localization ;
1115
@@ -16,6 +20,12 @@ namespace SmartStore.Services.Payments
1620 /// </summary>
1721 public partial class PaymentService : IPaymentService
1822 {
23+ #region Constants
24+
25+ private const string PAYMENTMETHOD_ALL_KEY = "SmartStore.paymentmethod.all" ;
26+
27+ #endregion
28+
1929 #region Fields
2030
2131 private readonly IRepository < PaymentMethod > _paymentMethodRepository ;
@@ -26,6 +36,7 @@ public partial class PaymentService : IPaymentService
2636 private readonly ILocalizationService _localizationService ;
2737 private readonly IProviderManager _providerManager ;
2838 private readonly IEventPublisher _eventPublisher ;
39+ private readonly ICacheManager _cacheManager ;
2940
3041 #endregion
3142
@@ -46,7 +57,8 @@ public PaymentService(
4657 ISettingService settingService ,
4758 ILocalizationService localizationService ,
4859 IProviderManager providerManager ,
49- IEventPublisher eventPublisher )
60+ IEventPublisher eventPublisher ,
61+ ICacheManager cacheManager )
5062 {
5163 this . _paymentMethodRepository = paymentMethodRepository ;
5264 this . _paymentSettings = paymentSettings ;
@@ -56,32 +68,83 @@ public PaymentService(
5668 this . _localizationService = localizationService ;
5769 this . _providerManager = providerManager ;
5870 this . _eventPublisher = eventPublisher ;
71+ this . _cacheManager = cacheManager ;
5972 }
6073
6174 #endregion
6275
63- #region Methods
76+ #region Methods
6477
65- /// <summary>
78+ /// <summary>
6679 /// Load active payment methods
6780 /// </summary>
68- /// <param name="filterByCustomerId ">Filter payment methods by customer; null to load all records</param>
69- /// <param name="storeId">Load records allows only in specified store; pass 0 to load all records</param>
81+ /// <param name="customer ">Filter payment methods by customer and apply payment method restrictions ; null to load all records</param>
82+ /// <param name="storeId">Filter payment methods by store identifier ; pass 0 to load all records</param>
7083 /// <returns>Payment methods</returns>
71- public virtual IEnumerable < Provider < IPaymentMethod > > LoadActivePaymentMethods ( int ? filterByCustomerId = null , int storeId = 0 )
84+ public virtual IEnumerable < Provider < IPaymentMethod > > LoadActivePaymentMethods ( Customer customer = null , int storeId = 0 )
7285 {
73- var allMethods = LoadAllPaymentMethods ( storeId ) ;
74- var activeMethods = allMethods
75- . Where ( p => p . Value . IsActive && _paymentSettings . ActivePaymentMethodSystemNames . Contains ( p . Metadata . SystemName , StringComparer . InvariantCultureIgnoreCase ) ) ;
86+ List < int > customerRoleIds = null ;
87+ int ? selectedShippingMethodId = null ;
7688
77- if ( ! activeMethods . Any ( ) )
89+ var allProviders = LoadAllPaymentMethods ( storeId ) ;
90+
91+ var activeProviders = allProviders
92+ . Where ( p =>
93+ {
94+ if ( ! p . Value . IsActive || ! _paymentSettings . ActivePaymentMethodSystemNames . Contains ( p . Metadata . SystemName , StringComparer . InvariantCultureIgnoreCase ) )
95+ return false ;
96+
97+ if ( customer != null )
98+ {
99+ var method = GetPaymentMethodBySystemName ( p . Metadata . SystemName ) ;
100+ if ( method != null )
101+ {
102+ // method restricted by customer role id?
103+ var excludedRoleIds = method . ExcludedCustomerRoleIds . ToIntArray ( ) ;
104+ if ( excludedRoleIds . Any ( ) )
105+ {
106+ if ( customerRoleIds == null )
107+ customerRoleIds = customer . CustomerRoles . Where ( r => r . Active ) . Select ( r => r . Id ) . ToList ( ) ;
108+
109+ if ( customerRoleIds != null && ! customerRoleIds . Except ( excludedRoleIds ) . Any ( ) )
110+ return false ;
111+ }
112+
113+ // method restricted by selected shipping method?
114+ var excludedShippingMethodIds = method . ExcludedShippingMethodIds . ToIntArray ( ) ;
115+ if ( excludedShippingMethodIds . Any ( ) )
116+ {
117+ if ( ! selectedShippingMethodId . HasValue )
118+ {
119+ var selectedShipping = customer . GetAttribute < ShippingOption > ( SystemCustomerAttributeNames . SelectedShippingOption , storeId ) ;
120+ selectedShippingMethodId = ( selectedShipping == null ? 0 : selectedShipping . ShippingMethodId ) ;
121+ }
122+
123+ if ( ( selectedShippingMethodId ?? 0 ) != 0 && excludedShippingMethodIds . Contains ( selectedShippingMethodId . Value ) )
124+ return false ;
125+ }
126+
127+ // method restricted by country of selected billing address?
128+ var excludedCountryIds = method . ExcludedCountryIds . ToIntArray ( ) ;
129+ if ( excludedCountryIds . Any ( ) && customer . BillingAddress != null && ( customer . BillingAddress . CountryId ?? 0 ) != 0 )
130+ {
131+ if ( excludedCountryIds . Contains ( customer . BillingAddress . CountryId . Value ) )
132+ return false ;
133+ }
134+ }
135+ }
136+ return true ;
137+ } ) ;
138+
139+ if ( ! activeProviders . Any ( ) )
78140 {
79- var fallbackMethod = allMethods . FirstOrDefault ( ) ;
141+ var fallbackMethod = allProviders . FirstOrDefault ( ) ;
80142 if ( fallbackMethod != null )
81143 {
82144 _paymentSettings . ActivePaymentMethodSystemNames . Clear ( ) ;
83145 _paymentSettings . ActivePaymentMethodSystemNames . Add ( fallbackMethod . Metadata . SystemName ) ;
84146 _settingService . SaveSetting ( _paymentSettings ) ;
147+
85148 return new Provider < IPaymentMethod > [ ] { fallbackMethod } ;
86149 }
87150 else
@@ -91,7 +154,7 @@ public virtual IEnumerable<Provider<IPaymentMethod>> LoadActivePaymentMethods(in
91154 }
92155 }
93156
94- return activeMethods ;
157+ return activeProviders ;
95158 }
96159
97160 /// <summary>
@@ -130,21 +193,37 @@ public virtual IEnumerable<Provider<IPaymentMethod>> LoadAllPaymentMethods(int s
130193
131194
132195 /// <summary>
133- /// Gets a payment method by system name
196+ /// Gets all payment method extra data
197+ /// </summary>
198+ /// <returns>List of payment method objects</returns>
199+ public virtual IList < PaymentMethod > GetAllPaymentMethods ( )
200+ {
201+ var paymentMethods = _cacheManager . Get ( PAYMENTMETHOD_ALL_KEY , ( ) =>
202+ {
203+ return _paymentMethodRepository . Table . ToList ( ) ;
204+ } ) ;
205+
206+ return paymentMethods ;
207+ }
208+
209+ /// <summary>
210+ /// Gets payment method extra data by system name
134211 /// </summary>
135212 /// <param name="systemName">Provider system name</param>
136213 /// <returns>Payment method entity</returns>
137214 public virtual PaymentMethod GetPaymentMethodBySystemName ( string systemName )
138215 {
139216 if ( systemName . HasValue ( ) )
140217 {
141- return _paymentMethodRepository . Table . FirstOrDefault ( x => x . PaymentMethodSystemName == systemName ) ;
218+ var allPaymentMethods = GetAllPaymentMethods ( ) ;
219+
220+ return allPaymentMethods . FirstOrDefault ( x => x . PaymentMethodSystemName . IsCaseInsensitiveEqual ( systemName ) ) ;
142221 }
143222 return null ;
144223 }
145224
146225 /// <summary>
147- /// Insert a payment method
226+ /// Insert payment method extra data
148227 /// </summary>
149228 /// <param name="paymentMethod">Payment method</param>
150229 public virtual void InsertPaymentMethod ( PaymentMethod paymentMethod )
@@ -154,11 +233,13 @@ public virtual void InsertPaymentMethod(PaymentMethod paymentMethod)
154233
155234 _paymentMethodRepository . Insert ( paymentMethod ) ;
156235
236+ _cacheManager . RemoveByPattern ( PAYMENTMETHOD_ALL_KEY ) ;
237+
157238 _eventPublisher . EntityInserted ( paymentMethod ) ;
158239 }
159240
160241 /// <summary>
161- /// Updates a payment method
242+ /// Updates payment method extra data
162243 /// </summary>
163244 /// <param name="paymentMethod">Payment method</param>
164245 public virtual void UpdatePaymentMethod ( PaymentMethod paymentMethod )
@@ -168,9 +249,27 @@ public virtual void UpdatePaymentMethod(PaymentMethod paymentMethod)
168249
169250 _paymentMethodRepository . Update ( paymentMethod ) ;
170251
252+ _cacheManager . RemoveByPattern ( PAYMENTMETHOD_ALL_KEY ) ;
253+
171254 _eventPublisher . EntityUpdated ( paymentMethod ) ;
172255 }
173256
257+ /// <summary>
258+ /// Delete payment method extra data
259+ /// </summary>
260+ /// <param name="paymentMethod">Payment method</param>
261+ public virtual void DeletePaymentMethod ( PaymentMethod paymentMethod )
262+ {
263+ if ( paymentMethod == null )
264+ throw new ArgumentNullException ( "paymentMethod" ) ;
265+
266+ _paymentMethodRepository . Delete ( paymentMethod ) ;
267+
268+ _cacheManager . RemoveByPattern ( PAYMENTMETHOD_ALL_KEY ) ;
269+
270+ _eventPublisher . EntityDeleted ( paymentMethod ) ;
271+ }
272+
174273
175274 /// <summary>
176275 /// Pre process a payment
0 commit comments