-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPaymentsManager.cs
More file actions
198 lines (166 loc) · 9.91 KB
/
PaymentsManager.cs
File metadata and controls
198 lines (166 loc) · 9.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using Microsoft.EntityFrameworkCore;
using Stateless;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unity.GrantManager.Applications;
using Unity.Payments.Domain.PaymentRequests;
using Unity.Payments.Domain.Shared;
using Unity.Payments.Domain.Workflow;
using Unity.Payments.Enums;
using Unity.Payments.Codes;
using Unity.Payments.PaymentRequests;
using Unity.Payments.Permissions;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Domain.Services;
using Volo.Abp.Uow;
using Volo.Abp.Users;
namespace Unity.Payments.Domain.Services
{
public class PaymentsManager(
IApplicationRepository applicationRepository,
IApplicationFormRepository applicationFormRepository,
CasPaymentRequestCoordinator casPaymentRequestCoordinator,
IPaymentRequestRepository paymentRequestRepository,
IUnitOfWorkManager unitOfWorkManager,
IPermissionChecker permissionChecker,
ICurrentUser currentUser) : DomainService, IPaymentsManager
{
private void ConfigureWorkflow(StateMachine<PaymentRequestStatus, PaymentApprovalAction> paymentStateMachine)
{
paymentStateMachine.Configure(PaymentRequestStatus.L1Pending)
.PermitIf(PaymentApprovalAction.L1Approve, PaymentRequestStatus.L2Pending, () => HasPermission(PaymentsPermissions.Payments.L1ApproveOrDecline))
.PermitIf(PaymentApprovalAction.L1Decline, PaymentRequestStatus.L1Declined, () => HasPermission(PaymentsPermissions.Payments.L1ApproveOrDecline));
paymentStateMachine.Configure(PaymentRequestStatus.L1Declined)
.PermitIf(PaymentApprovalAction.L1Approve, PaymentRequestStatus.L2Pending, () => HasPermission(PaymentsPermissions.Payments.L1ApproveOrDecline));
paymentStateMachine.Configure(PaymentRequestStatus.L2Pending)
.PermitIf(PaymentApprovalAction.L2Approve, PaymentRequestStatus.L3Pending, () => HasPermission(PaymentsPermissions.Payments.L2ApproveOrDecline))
.PermitIf(PaymentApprovalAction.Submit, PaymentRequestStatus.Submitted, () => HasPermission(PaymentsPermissions.Payments.L2ApproveOrDecline))
.PermitIf(PaymentApprovalAction.L2Decline, PaymentRequestStatus.L2Declined, () => HasPermission(PaymentsPermissions.Payments.L2ApproveOrDecline));
paymentStateMachine.Configure(PaymentRequestStatus.L2Declined)
.PermitIf(PaymentApprovalAction.L2Approve, PaymentRequestStatus.L3Pending, () => HasPermission(PaymentsPermissions.Payments.L2ApproveOrDecline))
.PermitIf(PaymentApprovalAction.Submit, PaymentRequestStatus.Submitted, () => HasPermission(PaymentsPermissions.Payments.L2ApproveOrDecline));
paymentStateMachine.Configure(PaymentRequestStatus.L3Pending)
.PermitIf(PaymentApprovalAction.Submit, PaymentRequestStatus.Submitted, () => HasPermission(PaymentsPermissions.Payments.L3ApproveOrDecline))
.PermitIf(PaymentApprovalAction.L3Decline, PaymentRequestStatus.L3Declined, () => HasPermission(PaymentsPermissions.Payments.L3ApproveOrDecline));
paymentStateMachine.Configure(PaymentRequestStatus.L2Declined)
.PermitIf(PaymentApprovalAction.Submit, PaymentRequestStatus.Submitted, () => HasPermission(PaymentsPermissions.Payments.L2ApproveOrDecline));
}
private bool HasPermission(string permission)
{
return permissionChecker.IsGrantedAsync(permission).Result;
}
public async Task<List<PaymentActionResultItem>> GetActions(Guid paymentRequestsId)
{
var paymentRequest = await paymentRequestRepository.GetAsync(paymentRequestsId, true);
var Workflow = new PaymentsWorkflow<PaymentRequestStatus, PaymentApprovalAction>(
() => paymentRequest.Status,
s => paymentRequest.SetPaymentRequestStatus(s), ConfigureWorkflow);
var allActions = Workflow.GetAllActions().Distinct().ToList();
var permittedActions = Workflow.GetPermittedActions().ToList();
var actionsList = allActions
.Select(trigger =>
new PaymentActionResultItem
{
PaymentApprovalAction = trigger,
IsPermitted = permittedActions.Contains(trigger),
IsInternal = trigger.ToString().StartsWith("Internal_")
})
.OrderBy(x => (int)x.PaymentApprovalAction)
.ToList();
return actionsList;
}
public async Task<PaymentRequest> TriggerAction(Guid paymentRequestsId, PaymentApprovalAction triggerAction)
{
var paymentRequest = await paymentRequestRepository.GetAsync(paymentRequestsId, true);
var currentUserId = currentUser.GetId();
var statusChange = paymentRequest.Status;
var Workflow = new PaymentsWorkflow<PaymentRequestStatus, PaymentApprovalAction>(
() => statusChange,
s => statusChange = s,
ConfigureWorkflow);
await Workflow.ExecuteActionAsync(triggerAction);
var statusChangedTo = PaymentRequestStatus.L1Pending;
if (triggerAction == PaymentApprovalAction.L1Approve)
{
var index = paymentRequest.ExpenseApprovals.FindIndex(i => i.Type == ExpenseApprovalType.Level1);
paymentRequest.ExpenseApprovals[index].Approve(currentUserId);
statusChangedTo = PaymentRequestStatus.L2Pending;
}
else if (triggerAction == PaymentApprovalAction.L1Decline)
{
var index = paymentRequest.ExpenseApprovals.FindIndex(i => i.Type == ExpenseApprovalType.Level1);
paymentRequest.ExpenseApprovals[index].Decline(currentUserId);
statusChangedTo = PaymentRequestStatus.L1Declined;
}
else if (triggerAction == PaymentApprovalAction.L2Approve)
{
var index = paymentRequest.ExpenseApprovals.FindIndex(i => i.Type == ExpenseApprovalType.Level2);
paymentRequest.ExpenseApprovals[index].Approve(currentUserId);
statusChangedTo = PaymentRequestStatus.L3Pending;
}
else if (triggerAction == PaymentApprovalAction.L2Decline)
{
var index = paymentRequest.ExpenseApprovals.FindIndex(i => i.Type == ExpenseApprovalType.Level2);
paymentRequest.ExpenseApprovals[index].Decline(currentUserId);
statusChangedTo = PaymentRequestStatus.L2Declined;
}
else if (triggerAction == PaymentApprovalAction.L3Decline)
{
var index = paymentRequest.ExpenseApprovals.FindIndex(i => i.Type == ExpenseApprovalType.Level3);
paymentRequest.ExpenseApprovals[index].Decline(currentUserId);
statusChangedTo = PaymentRequestStatus.L3Declined;
}
else if (triggerAction == PaymentApprovalAction.Submit)
{
if (HasPermission(PaymentsPermissions.Payments.L2ApproveOrDecline) && paymentRequest.Status == PaymentRequestStatus.L2Pending)
{
var index = paymentRequest.ExpenseApprovals.FindIndex(i => i.Type == ExpenseApprovalType.Level2);
paymentRequest.ExpenseApprovals[index].Approve(currentUserId);
}
else if (HasPermission(PaymentsPermissions.Payments.L3ApproveOrDecline) && paymentRequest.Status == PaymentRequestStatus.L3Pending)
{
var index = paymentRequest.ExpenseApprovals.FindIndex(i => i.Type == ExpenseApprovalType.Level3);
paymentRequest.ExpenseApprovals[index].Approve(currentUserId);
}
bool preventPayment = await GetFormPreventPaymentStatusByApplicationId(paymentRequest.CorrelationId);
if (preventPayment)
{
statusChangedTo = PaymentRequestStatus.FSB;
paymentRequest.SetInvoiceStatus(CasPaymentRequestStatus.SentToAccountsPayable);
}
else
{
statusChangedTo = PaymentRequestStatus.Submitted;
await casPaymentRequestCoordinator.AddPaymentRequestsToInvoiceQueue(paymentRequest);
}
}
paymentRequest.SetPaymentRequestStatus(statusChangedTo);
return await paymentRequestRepository.UpdateAsync(paymentRequest);
}
public async Task<bool> GetFormPreventPaymentStatusByPaymentRequestId(Guid paymentRequestId)
{
PaymentRequest paymentRequest = await paymentRequestRepository.GetAsync(paymentRequestId);
Guid applicationId = paymentRequest.CorrelationId;
var applicationQueryable = await applicationRepository.GetQueryableAsync();
var applicationWithIncludes = await applicationQueryable.Where(a => a.Id == applicationId)
.Include(a => a.ApplicationForm).ToListAsync();
var appForm = applicationWithIncludes.FirstOrDefault()?.ApplicationForm;
return appForm != null && appForm.PreventPayment;
}
public async Task<bool> GetFormPreventPaymentStatusByApplicationId(Guid applicationId)
{
Application application = await applicationRepository.GetAsync(applicationId);
Guid formId = application.ApplicationForm.Id;
ApplicationForm appForm = await applicationFormRepository.GetAsync(formId);
return appForm.PreventPayment;
}
public async Task UpdatePaymentStatusAsync(Guid paymentRequestId, PaymentApprovalAction triggerAction)
{
using var uow = unitOfWorkManager.Begin();
await TriggerAction(paymentRequestId, triggerAction);
await uow.SaveChangesAsync();
}
}
}