|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Data.Entity; |
3 | 4 | using System.Linq; |
4 | 5 | using System.Threading.Tasks; |
5 | 6 | using JoinRpg.Data.Write.Interfaces; |
@@ -40,7 +41,6 @@ private ApiConfiguration GetApiConfiguration(int projectId, int claimId) |
40 | 41 | ApiEndpoint = _bankSecrets.ApiEndpoint, |
41 | 42 | ApiDebugEndpoint = _bankSecrets.ApiDebugEndpoint, |
42 | 43 | MerchantId = _bankSecrets.MerchantId, |
43 | | - MerchantIdFastPayments = _bankSecrets.MerchantIdFastPayments, |
44 | 44 | ApiKey = _bankSecrets.ApiKey, |
45 | 45 | ApiDebugKey = _bankSecrets.ApiDebugKey, |
46 | 46 | DefaultSuccessUrl = _uriService.Get(new PaymentSuccessUrl(projectId, claimId)), |
@@ -155,7 +155,7 @@ private async Task<Comment> AddPaymentCommentAsync( |
155 | 155 | claim, |
156 | 156 | CurrentUserId, |
157 | 157 | Now, |
158 | | - request.CommentText ?? "", |
| 158 | + request.CommentText, |
159 | 159 | true, |
160 | 160 | null); |
161 | 161 | comment.Finance = new FinanceOperation |
@@ -204,6 +204,22 @@ private async Task<FinanceOperation> LoadFinanceOperationAsync(int projectId, in |
204 | 204 | return fo; |
205 | 205 | } |
206 | 206 |
|
| 207 | + private async Task<FinanceOperation?> LoadLastUnapprovedFinanceOperationAsync(int projectId, int claimId) |
| 208 | + { |
| 209 | + return await (from fo in UnitOfWork.GetDbSet<FinanceOperation>() |
| 210 | + join pt in UnitOfWork.GetDbSet<PaymentType>() |
| 211 | + on fo.PaymentTypeId equals pt.PaymentTypeId |
| 212 | + where fo.ProjectId == projectId |
| 213 | + && fo.ClaimId == claimId |
| 214 | + && fo.OperationType == FinanceOperationType.Online |
| 215 | + && pt.TypeKind == PaymentTypeKind.Online |
| 216 | + && fo.State == FinanceOperationState.Proposed |
| 217 | + select fo) |
| 218 | + .Include(fo => fo.PaymentType) |
| 219 | + .OrderByDescending(fo => fo.Created) |
| 220 | + .FirstOrDefaultAsync(); |
| 221 | + } |
| 222 | + |
207 | 223 | private void UpdateFinanceOperationStatus(FinanceOperation fo, PaymentData paymentData) |
208 | 224 | { |
209 | 225 | switch (paymentData.Status) |
@@ -241,64 +257,79 @@ private void UpdateFinanceOperationStatus(FinanceOperation fo, PaymentData payme |
241 | 257 | } |
242 | 258 | } |
243 | 259 |
|
244 | | - /// <inheritdoc /> |
245 | | - public async Task UpdateClaimPaymentAsync(int projectId, int claimId, int orderId) |
| 260 | + private async Task UpdateClaimPaymentAsync(FinanceOperation fo) |
246 | 261 | { |
247 | | - var fo = await LoadFinanceOperationAsync(projectId, claimId, orderId); |
248 | | - |
249 | | - if (fo.State == FinanceOperationState.Proposed) |
| 262 | + if (fo.State != FinanceOperationState.Proposed) |
250 | 263 | { |
251 | | - var api = GetApi(projectId, claimId); |
252 | | - string orderIdStr = orderId.ToString().PadLeft(10, '0'); |
| 264 | + return; |
| 265 | + } |
| 266 | + |
| 267 | + var api = GetApi(fo.ProjectId, fo.ClaimId); |
| 268 | + string orderIdStr = fo.CommentId.ToString().PadLeft(10, '0'); |
| 269 | + |
| 270 | + // Asking bank |
| 271 | + PaymentInfo paymentInfo = await api.GetPaymentInfoAsync( |
| 272 | + PscbPaymentMethod.BankCards, |
| 273 | + orderIdStr); |
253 | 274 |
|
254 | | - // Asking bank |
255 | | - PaymentInfo paymentInfo = await api.GetPaymentInfoAsync( |
256 | | - PscbPaymentMethod.BankCards, |
| 275 | + if (paymentInfo.Status == PaymentInfoQueryStatus.Failure |
| 276 | + && paymentInfo.ErrorCode == ApiErrorCode.UnknownPayment) |
| 277 | + { |
| 278 | + paymentInfo = await api.GetPaymentInfoAsync( |
| 279 | + PscbPaymentMethod.FastPaymentsSystem, |
257 | 280 | orderIdStr); |
| 281 | + } |
258 | 282 |
|
259 | | - if (paymentInfo.Status == PaymentInfoQueryStatus.Failure |
260 | | - && paymentInfo.ErrorCode == ApiErrorCode.UnknownPayment) |
| 283 | + // Updating status |
| 284 | + if (paymentInfo.Status == PaymentInfoQueryStatus.Success) |
| 285 | + { |
| 286 | + if (paymentInfo.ErrorCode == ApiErrorCode.UnknownPayment) |
261 | 287 | { |
262 | | - paymentInfo = await api.GetPaymentInfoAsync( |
263 | | - PscbPaymentMethod.FastPaymentsSystem, |
264 | | - orderIdStr); |
| 288 | + fo.State = FinanceOperationState.Declined; |
| 289 | + fo.Changed = Now; |
265 | 290 | } |
266 | | - |
267 | | - // Updating status |
268 | | - if (paymentInfo.Status == PaymentInfoQueryStatus.Success) |
| 291 | + else if (paymentInfo.ErrorCode == null) |
269 | 292 | { |
270 | | - if (paymentInfo.ErrorCode == ApiErrorCode.UnknownPayment) |
271 | | - { |
272 | | - fo.State = FinanceOperationState.Declined; |
273 | | - fo.Changed = Now; |
274 | | - } |
275 | | - else if (paymentInfo.ErrorCode == null) |
| 293 | + UpdateFinanceOperationStatus(fo, paymentInfo.Payment); |
| 294 | + if (fo.State == FinanceOperationState.Approved) |
276 | 295 | { |
277 | | - UpdateFinanceOperationStatus(fo, paymentInfo.Payment); |
278 | | - if (fo.State == FinanceOperationState.Approved) |
279 | | - { |
280 | | - Claim claim = await GetClaimAsync(projectId, claimId); |
281 | | - claim.UpdateClaimFeeIfRequired(Now); |
282 | | - } |
| 296 | + Claim claim = await GetClaimAsync(fo.ProjectId, fo.ClaimId); |
| 297 | + claim.UpdateClaimFeeIfRequired(Now); |
283 | 298 | } |
284 | 299 | } |
285 | | - else if (paymentInfo.ErrorCode == ApiErrorCode.UnknownPayment) |
286 | | - { |
287 | | - fo.State = FinanceOperationState.Invalid; |
288 | | - fo.Changed = Now; |
289 | | - } |
290 | | - else if (IsCurrentUserAdmin) |
291 | | - { |
292 | | - throw new PaymentException(fo.Project, $"Payment status check failed: {paymentInfo.ErrorDescription}"); |
293 | | - } |
| 300 | + } |
| 301 | + else if (paymentInfo.ErrorCode == ApiErrorCode.UnknownPayment) |
| 302 | + { |
| 303 | + fo.State = FinanceOperationState.Invalid; |
| 304 | + fo.Changed = Now; |
| 305 | + } |
| 306 | + else if (IsCurrentUserAdmin) |
| 307 | + { |
| 308 | + throw new PaymentException( |
| 309 | + fo.Project, |
| 310 | + $"Payment status check failed: {paymentInfo.ErrorDescription}"); |
| 311 | + } |
294 | 312 |
|
295 | | - // Saving if status was updated |
296 | | - if (fo.State != FinanceOperationState.Proposed) |
297 | | - { |
298 | | - await UnitOfWork.SaveChangesAsync(); |
299 | | - } |
| 313 | + // Saving if status was updated |
| 314 | + if (fo.State != FinanceOperationState.Proposed) |
| 315 | + { |
| 316 | + await UnitOfWork.SaveChangesAsync(); |
| 317 | + } |
| 318 | + |
| 319 | + // TODO: Probably need to send some notifications? |
| 320 | + } |
| 321 | + |
| 322 | + /// <inheritdoc /> |
| 323 | + public async Task UpdateClaimPaymentAsync(int projectId, int claimId, int orderId) |
| 324 | + => await UpdateClaimPaymentAsync(await LoadFinanceOperationAsync(projectId, claimId, orderId)); |
300 | 325 |
|
301 | | - // TODO: Probably need to send some notifications? |
| 326 | + /// <inheritdoc /> |
| 327 | + public async Task UpdateLastClaimPaymentAsync(int projectId, int claimId) |
| 328 | + { |
| 329 | + var fo = await LoadLastUnapprovedFinanceOperationAsync(projectId, claimId); |
| 330 | + if (fo is not null) |
| 331 | + { |
| 332 | + await UpdateClaimPaymentAsync(fo); |
302 | 333 | } |
303 | 334 | } |
304 | 335 |
|
|
0 commit comments