|
15 | 15 | using VirtoCommerce.StoreModule.Core.Model; |
16 | 16 | using VirtoCommerce.StoreModule.Core.Services; |
17 | 17 |
|
18 | | -namespace VirtoCommerce.OrdersModule.Data.Services |
| 18 | +namespace VirtoCommerce.OrdersModule.Data.Services; |
| 19 | + |
| 20 | +public class CustomerOrderPaymentService( |
| 21 | + IStoreService storeService, |
| 22 | + ICustomerOrderService customerOrderService, |
| 23 | + ICustomerOrderSearchService customerOrderSearchService, |
| 24 | + IValidator<CustomerOrder> customerOrderValidator, |
| 25 | + ISettingsManager settingsManager) |
| 26 | + : ICustomerOrderPaymentService |
19 | 27 | { |
20 | | - public class CustomerOrderPaymentService( |
21 | | - IStoreService storeService, |
22 | | - ICustomerOrderService customerOrderService, |
23 | | - ICustomerOrderSearchService customerOrderSearchService, |
24 | | - IValidator<CustomerOrder> customerOrderValidator, |
25 | | - ISettingsManager settingsManager) |
26 | | - : ICustomerOrderPaymentService |
| 28 | + public virtual async Task<PostProcessPaymentRequestResult> PostProcessPaymentAsync(PaymentParameters paymentParameters) |
27 | 29 | { |
28 | | - public virtual async Task<PostProcessPaymentRequestResult> PostProcessPaymentAsync(PaymentParameters paymentParameters) |
| 30 | + ArgumentNullException.ThrowIfNull(paymentParameters); |
| 31 | + |
| 32 | + var customerOrder = await GetCustomerOrder(paymentParameters); |
| 33 | + if (customerOrder == null) |
| 34 | + { |
| 35 | + throw new InvalidOperationException($"Cannot find order with ID {paymentParameters.OrderId}"); |
| 36 | + } |
| 37 | + |
| 38 | + var store = await storeService.GetByIdAsync(customerOrder.StoreId, nameof(StoreResponseGroup.StoreInfo)); |
| 39 | + if (store == null) |
29 | 40 | { |
30 | | - ArgumentNullException.ThrowIfNull(paymentParameters); |
| 41 | + throw new InvalidOperationException($"Cannot find store with ID {customerOrder.StoreId}"); |
| 42 | + } |
| 43 | + |
| 44 | + var inPayments = GetInPayments(customerOrder, paymentParameters); |
31 | 45 |
|
32 | | - var customerOrder = await GetCustomerOrder(paymentParameters); |
33 | | - if (customerOrder == null) |
| 46 | + foreach (var inPayment in inPayments) |
| 47 | + { |
| 48 | + // Each payment method must check that these parameters are addressed to it |
| 49 | + var paymentMethodValidationResult = inPayment.PaymentMethod.ValidatePostProcessRequest(paymentParameters.Parameters); |
| 50 | + if (!paymentMethodValidationResult.IsSuccess) |
34 | 51 | { |
35 | | - throw new InvalidOperationException($"Cannot find order with ID {paymentParameters.OrderId}"); |
| 52 | + continue; |
36 | 53 | } |
37 | 54 |
|
38 | | - var store = await storeService.GetByIdAsync(customerOrder.StoreId, StoreResponseGroup.StoreInfo.ToString()); |
39 | | - if (store == null) |
| 55 | + var paymentMethodRequest = new PostProcessPaymentRequest |
| 56 | + { |
| 57 | + OrderId = customerOrder.Id, |
| 58 | + Order = customerOrder, |
| 59 | + PaymentId = inPayment.Id, |
| 60 | + Payment = inPayment, |
| 61 | + StoreId = customerOrder.StoreId, |
| 62 | + Store = store, |
| 63 | + OuterId = paymentMethodValidationResult.OuterId, |
| 64 | + Parameters = paymentParameters.Parameters, |
| 65 | + }; |
| 66 | + |
| 67 | + var paymentMethodPostProcessResult = inPayment.PaymentMethod.PostProcessPayment(paymentMethodRequest); |
| 68 | + if (paymentMethodPostProcessResult == null) |
40 | 69 | { |
41 | | - throw new InvalidOperationException($"Cannot find store with ID {customerOrder.StoreId}"); |
| 70 | + continue; |
42 | 71 | } |
43 | 72 |
|
44 | | - var inPayments = GetInPayments(customerOrder, paymentParameters); |
45 | | - foreach (var inPayment in inPayments) |
| 73 | + var customerOrderValidationResult = await ValidateAsync(customerOrder); |
| 74 | + if (!customerOrderValidationResult.IsValid) |
46 | 75 | { |
47 | | - //Each payment method must check that these parameters are addressed to it |
48 | | - var paymentMethodValidationResult = inPayment.PaymentMethod.ValidatePostProcessRequest(paymentParameters.Parameters); |
49 | | - if (!paymentMethodValidationResult.IsSuccess) |
| 76 | + return new PostProcessPaymentRequestNotValidResult |
50 | 77 | { |
51 | | - continue; |
52 | | - } |
53 | | - |
54 | | - var paymentMethodRequest = new PostProcessPaymentRequest |
55 | | - { |
56 | | - OrderId = customerOrder.Id, |
57 | | - Order = customerOrder, |
58 | | - PaymentId = inPayment.Id, |
59 | | - Payment = inPayment, |
60 | | - StoreId = customerOrder.StoreId, |
61 | | - Store = store, |
62 | | - OuterId = paymentMethodValidationResult.OuterId, |
63 | | - Parameters = paymentParameters.Parameters |
| 78 | + Errors = customerOrderValidationResult.Errors, |
| 79 | + ErrorMessage = string.Join(" ", customerOrderValidationResult.Errors.Select(x => x.ErrorMessage)), |
64 | 80 | }; |
| 81 | + } |
65 | 82 |
|
66 | | - var paymentMethodPostProcessResult = inPayment.PaymentMethod.PostProcessPayment(paymentMethodRequest); |
67 | | - if (paymentMethodPostProcessResult == null) |
68 | | - { |
69 | | - continue; |
70 | | - } |
71 | | - |
72 | | - var customerOrderValidationResult = await ValidateAsync(customerOrder); |
73 | | - if (!customerOrderValidationResult.IsValid) |
74 | | - { |
75 | | - return new PostProcessPaymentRequestNotValidResult() |
76 | | - { |
77 | | - Errors = customerOrderValidationResult.Errors, |
78 | | - ErrorMessage = string.Join(" ", customerOrderValidationResult.Errors.Select(x => x.ErrorMessage)) |
79 | | - }; |
80 | | - } |
| 83 | + await customerOrderService.SaveChangesAsync([customerOrder]); |
81 | 84 |
|
82 | | - await customerOrderService.SaveChangesAsync(new[] { customerOrder }); |
| 85 | + // Order number is required |
| 86 | + paymentMethodPostProcessResult.OrderId = customerOrder.Number; |
83 | 87 |
|
84 | | - // order Number is required |
85 | | - paymentMethodPostProcessResult.OrderId = customerOrder.Number; |
| 88 | + return paymentMethodPostProcessResult; |
| 89 | + } |
86 | 90 |
|
87 | | - return paymentMethodPostProcessResult; |
88 | | - } |
| 91 | + return new PostProcessPaymentRequestResult { ErrorMessage = "Payment method not found" }; |
| 92 | + } |
89 | 93 |
|
90 | | - return new PostProcessPaymentRequestResult { ErrorMessage = "Payment method not found" }; |
| 94 | + protected virtual async Task<CustomerOrder> GetCustomerOrder(PaymentParameters paymentParameters) |
| 95 | + { |
| 96 | + if (string.IsNullOrEmpty(paymentParameters.OrderId)) |
| 97 | + { |
| 98 | + throw new InvalidOperationException("The 'orderid' parameter must be passed"); |
91 | 99 | } |
92 | 100 |
|
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 | | - } |
| 101 | + // Some payment method require customer number to be passed and returned. First search customer order by number |
| 102 | + var searchCriteria = AbstractTypeFactory<CustomerOrderSearchCriteria>.TryCreateInstance(); |
| 103 | + searchCriteria.Number = paymentParameters.OrderId; |
| 104 | + searchCriteria.ResponseGroup = nameof(CustomerOrderResponseGroup.Full); |
99 | 105 |
|
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(); |
| 106 | + // If order is not found by order number, search by order id |
| 107 | + var orders = await customerOrderSearchService.SearchAsync(searchCriteria); |
104 | 108 |
|
105 | | - //if order not found by order number search by order id |
106 | | - var orders = await customerOrderSearchService.SearchAsync(searchCriteria); |
| 109 | + return orders.Results.FirstOrDefault() ?? await customerOrderService.GetByIdAsync(paymentParameters.OrderId, nameof(CustomerOrderResponseGroup.Full)); |
| 110 | + } |
107 | 111 |
|
108 | | - return orders.Results.FirstOrDefault() ?? await customerOrderService.GetByIdAsync(paymentParameters.OrderId, CustomerOrderResponseGroup.Full.ToString()); |
109 | | - } |
| 112 | + protected virtual IList<PaymentIn> GetInPayments(CustomerOrder customerOrder, PaymentParameters paymentParameters) |
| 113 | + { |
| 114 | + // Need to use concrete payment method if its code has been passed, otherwise use all order payment methods |
| 115 | + return customerOrder.InPayments |
| 116 | + .Where(x => x.PaymentMethod != null && (string.IsNullOrEmpty(paymentParameters.PaymentMethodCode) || x.GatewayCode.EqualsIgnoreCase(paymentParameters.PaymentMethodCode))) |
| 117 | + .ToList(); |
| 118 | + } |
110 | 119 |
|
111 | | - protected virtual IList<PaymentIn> GetInPayments(CustomerOrder customerOrder, PaymentParameters paymentParameters) |
| 120 | + protected virtual async Task<ValidationResult> ValidateAsync(CustomerOrder customerOrder) |
| 121 | + { |
| 122 | + if (await settingsManager.GetValueAsync<bool>(ModuleConstants.Settings.General.CustomerOrderValidation)) |
112 | 123 | { |
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(); |
| 124 | + return await customerOrderValidator.ValidateAsync(customerOrder); |
117 | 125 | } |
118 | 126 |
|
119 | | - protected virtual async Task<ValidationResult> ValidateAsync(CustomerOrder customerOrder) |
120 | | - { |
121 | | - if (await settingsManager.GetValueAsync<bool>(ModuleConstants.Settings.General.CustomerOrderValidation)) |
122 | | - { |
123 | | - return await customerOrderValidator.ValidateAsync(customerOrder); |
124 | | - } |
125 | | - |
126 | | - return new ValidationResult(); |
127 | | - } |
| 127 | + return new ValidationResult(); |
128 | 128 | } |
129 | 129 | } |
0 commit comments