11using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
34using System . Threading . Tasks ;
45using FluentValidation ;
@@ -28,39 +29,23 @@ public virtual async Task<PostProcessPaymentRequestResult> PostProcessPaymentAsy
2829 {
2930 ArgumentNullException . ThrowIfNull ( paymentParameters ) ;
3031
31- if ( string . IsNullOrEmpty ( paymentParameters . OrderId ) )
32- {
33- throw new InvalidOperationException ( "The 'orderid' parameter must be passed" ) ;
34- }
35-
36- //some payment method require customer number to be passed and returned. First search customer order by number
37- var searchCriteria = AbstractTypeFactory < CustomerOrderSearchCriteria > . TryCreateInstance ( ) ;
38- searchCriteria . Number = paymentParameters . OrderId ;
39- searchCriteria . ResponseGroup = CustomerOrderResponseGroup . Full . ToString ( ) ;
40-
41- //if order not found by order number search by order id
42- var orders = await customerOrderSearchService . SearchAsync ( searchCriteria ) ;
43-
44- var customerOrder = orders . Results . FirstOrDefault ( ) ?? await customerOrderService . GetByIdAsync ( paymentParameters . OrderId , CustomerOrderResponseGroup . Full . ToString ( ) ) ;
45-
32+ var customerOrder = await GetCustomerOrder ( paymentParameters ) ;
4633 if ( customerOrder == null )
4734 {
4835 throw new InvalidOperationException ( $ "Cannot find order with ID { paymentParameters . OrderId } ") ;
4936 }
5037
5138 var store = await storeService . GetByIdAsync ( customerOrder . StoreId , StoreResponseGroup . StoreInfo . ToString ( ) ) ;
52-
5339 if ( store == null )
5440 {
5541 throw new InvalidOperationException ( $ "Cannot find store with ID { customerOrder . StoreId } ") ;
5642 }
5743
58- //Need to use concrete payment method if it code passed otherwise use all order payment methods
59- foreach ( var inPayment in customerOrder . InPayments . Where ( x => x . PaymentMethod != null && ( string . IsNullOrEmpty ( paymentParameters . PaymentMethodCode ) || x . GatewayCode . EqualsIgnoreCase ( paymentParameters . PaymentMethodCode ) ) ) )
44+ var inPayments = GetInPayments ( customerOrder , paymentParameters ) ;
45+ foreach ( var inPayment in inPayments )
6046 {
6147 //Each payment method must check that these parameters are addressed to it
6248 var paymentMethodValidationResult = inPayment . PaymentMethod . ValidatePostProcessRequest ( paymentParameters . Parameters ) ;
63-
6449 if ( ! paymentMethodValidationResult . IsSuccess )
6550 {
6651 continue ;
@@ -79,14 +64,12 @@ public virtual async Task<PostProcessPaymentRequestResult> PostProcessPaymentAsy
7964 } ;
8065
8166 var paymentMethodPostProcessResult = inPayment . PaymentMethod . PostProcessPayment ( paymentMethodRequest ) ;
82-
8367 if ( paymentMethodPostProcessResult == null )
8468 {
8569 continue ;
8670 }
8771
8872 var customerOrderValidationResult = await ValidateAsync ( customerOrder ) ;
89-
9073 if ( ! customerOrderValidationResult . IsValid )
9174 {
9275 return new PostProcessPaymentRequestNotValidResult ( )
@@ -107,6 +90,32 @@ public virtual async Task<PostProcessPaymentRequestResult> PostProcessPaymentAsy
10790 return new PostProcessPaymentRequestResult { ErrorMessage = "Payment method not found" } ;
10891 }
10992
93+ protected virtual async Task < CustomerOrder > GetCustomerOrder ( PaymentParameters paymentParameters )
94+ {
95+ if ( string . IsNullOrEmpty ( paymentParameters . OrderId ) )
96+ {
97+ throw new InvalidOperationException ( "The 'orderid' parameter must be passed" ) ;
98+ }
99+
100+ //some payment method require customer number to be passed and returned. First search customer order by number
101+ var searchCriteria = AbstractTypeFactory < CustomerOrderSearchCriteria > . TryCreateInstance ( ) ;
102+ searchCriteria . Number = paymentParameters . OrderId ;
103+ searchCriteria . ResponseGroup = CustomerOrderResponseGroup . Full . ToString ( ) ;
104+
105+ //if order not found by order number search by order id
106+ var orders = await customerOrderSearchService . SearchAsync ( searchCriteria ) ;
107+
108+ return orders . Results . FirstOrDefault ( ) ?? await customerOrderService . GetByIdAsync ( paymentParameters . OrderId , CustomerOrderResponseGroup . Full . ToString ( ) ) ;
109+ }
110+
111+ protected virtual IList < PaymentIn > GetInPayments ( CustomerOrder customerOrder , PaymentParameters paymentParameters )
112+ {
113+ //Need to use concrete payment method if it code passed otherwise use all order payment methods
114+ return customerOrder . InPayments
115+ . Where ( x => x . PaymentMethod != null && ( string . IsNullOrEmpty ( paymentParameters . PaymentMethodCode ) || x . GatewayCode . EqualsIgnoreCase ( paymentParameters . PaymentMethodCode ) ) )
116+ . ToList ( ) ;
117+ }
118+
110119 protected virtual async Task < ValidationResult > ValidateAsync ( CustomerOrder customerOrder )
111120 {
112121 if ( await settingsManager . GetValueAsync < bool > ( ModuleConstants . Settings . General . CustomerOrderValidation ) )
0 commit comments