1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of byrokrat\giroapp. |
5
|
|
|
* |
6
|
|
|
* byrokrat\giroapp is free software: you can redistribute it and/or |
7
|
|
|
* modify it under the terms of the GNU General Public License as published |
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* byrokrat\giroapp is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
* |
19
|
|
|
* Copyright 2016-21 Hannes Forsgård |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
declare(strict_types=1); |
23
|
|
|
|
24
|
|
|
namespace byrokrat\giroapp\Autogiro; |
25
|
|
|
|
26
|
|
|
use byrokrat\giroapp\CommandBus\AttemptState; |
27
|
|
|
use byrokrat\giroapp\CommandBus\ForceState; |
28
|
|
|
use byrokrat\giroapp\CommandBus\UpdateState; |
29
|
|
|
use byrokrat\giroapp\DependencyInjection; |
30
|
|
|
use byrokrat\giroapp\Config\ConfigInterface; |
31
|
|
|
use byrokrat\giroapp\Exception\InvalidAutogiroFileException; |
32
|
|
|
use byrokrat\giroapp\Exception\DonorDoesNotExistException; |
33
|
|
|
use byrokrat\giroapp\Domain\Donor; |
34
|
|
|
use byrokrat\giroapp\Domain\State\Error; |
35
|
|
|
use byrokrat\giroapp\Domain\State\Revoked; |
36
|
|
|
use byrokrat\giroapp\Event; |
37
|
|
|
use byrokrat\giroapp\Workflow\Transitions; |
38
|
|
|
use byrokrat\autogiro\Visitor\Visitor; |
39
|
|
|
use byrokrat\autogiro\Tree\Node; |
40
|
|
|
use byrokrat\banking\AccountNumber; |
41
|
|
|
use byrokrat\id\IdInterface; |
42
|
|
|
|
43
|
|
|
class AutogiroVisitor extends Visitor |
44
|
|
|
{ |
45
|
|
|
use DependencyInjection\CommandBusProperty; |
46
|
|
|
use DependencyInjection\DispatcherProperty; |
47
|
|
|
use DependencyInjection\DonorQueryProperty; |
48
|
|
|
|
49
|
|
|
/** @var ConfigInterface */ |
50
|
|
|
private $orgBgcNr; |
51
|
|
|
|
52
|
|
|
/** @var AccountNumber */ |
53
|
|
|
private $orgBankgiro; |
54
|
|
|
|
55
|
|
|
public function __construct(ConfigInterface $orgBgcNr, AccountNumber $orgBankgiro) |
56
|
|
|
{ |
57
|
|
|
$this->orgBgcNr = $orgBgcNr; |
58
|
|
|
$this->orgBankgiro = $orgBankgiro; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function beforeOpening(Node $node): void |
62
|
|
|
{ |
63
|
|
|
/** @var string $payeeBgcNr */ |
64
|
|
|
$payeeBgcNr = (string)$node->getChild('PayeeBgcNumber')->getValue(); |
65
|
|
|
|
66
|
|
|
/** @var ?AccountNumber $payeeBankgiro */ |
67
|
|
|
$payeeBankgiro = $node->getChild('PayeeBankgiro')->getValueFrom('Object'); |
68
|
|
|
|
69
|
|
|
if ($payeeBgcNr && $payeeBgcNr != $this->orgBgcNr->getValue()) { |
70
|
|
|
// Hard failure, implicit rollback |
71
|
|
|
throw new InvalidAutogiroFileException( |
72
|
|
|
sprintf( |
73
|
|
|
'File contains invalid payee BGC customer number, found: %s, expexting: %s', |
74
|
|
|
$payeeBgcNr, |
75
|
|
|
$this->orgBgcNr->getValue() |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($payeeBankgiro && !$payeeBankgiro->equals($this->orgBankgiro)) { |
81
|
|
|
// Hard failure, implicit rollback |
82
|
|
|
throw new InvalidAutogiroFileException( |
83
|
|
|
sprintf( |
84
|
|
|
'File contains invalid payee bankgiro account number, found: %s, expexting: %s', |
85
|
|
|
$payeeBankgiro->getNumber(), |
86
|
|
|
$this->orgBankgiro->getNumber() |
87
|
|
|
) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function beforeMandateResponse(Node $node): void |
93
|
|
|
{ |
94
|
|
|
$donor = $this->readDonor($node->getValueFrom('PayerNumber')); |
95
|
|
|
|
96
|
|
|
if (!$donor) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** @var ?IdInterface $nodeId */ |
101
|
|
|
$nodeId = $node->getChild('StateId')->getValueFrom('Object'); |
102
|
|
|
|
103
|
|
|
if ($nodeId) { |
104
|
|
|
$this->validateDonorId($nodeId, $donor); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** @var ?AccountNumber $nodeAccount */ |
108
|
|
|
$nodeAccount = $node->getChild('Account')->getValueFrom('Object'); |
109
|
|
|
|
110
|
|
|
if ($nodeAccount) { |
111
|
|
|
$this->validateDonorAccountNumber($nodeAccount, $donor); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$desc = (string)$node->getChild('Status')->getValueFrom('Text'); |
115
|
|
|
|
116
|
|
|
if ($node->hasChild('CreatedFlag')) { |
117
|
|
|
$this->commandBus->handle(new UpdateState($donor, Transitions::IMPORT_MANDATE_REGISTERED, $desc)); |
118
|
|
|
|
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($node->hasChild('DeletedFlag')) { |
123
|
|
|
$this->commandBus->handle(new ForceState($donor, Revoked::getStateId(), $desc)); |
124
|
|
|
|
125
|
|
|
return; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($node->hasChild('ErrorFlag')) { |
129
|
|
|
$this->commandBus->handle(new ForceState($donor, Error::getStateId(), $desc)); |
130
|
|
|
|
131
|
|
|
return; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Hard failure, implicit rollback |
135
|
|
|
throw new InvalidAutogiroFileException( |
136
|
|
|
sprintf( |
137
|
|
|
'%s: invalid mandate status code: %s', |
138
|
|
|
$donor->getMandateKey(), |
139
|
|
|
(string)$node->getChild('Status')->getValueFrom('Number') |
140
|
|
|
) |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function beforeSuccessfulIncomingAmendmentResponse(Node $node): void |
145
|
|
|
{ |
146
|
|
|
$donor = $this->readDonor($node->getValueFrom('PayerNumber')); |
147
|
|
|
|
148
|
|
|
if (!$donor) { |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** @var \DateTimeImmutable $date */ |
153
|
|
|
$date = $node->getChild('Date')->getValueFrom('Object'); |
154
|
|
|
|
155
|
|
|
if ($node->hasChild('RevocationFlag')) { |
156
|
|
|
$this->commandBus->handle( |
157
|
|
|
new UpdateState( |
158
|
|
|
$donor, |
159
|
|
|
Transitions::IMPORT_TRANSACTION_REMOVED, |
160
|
|
|
'Transaction paused on ' . $date->format('Y-m-d') |
161
|
|
|
) |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function beforeSuccessfulIncomingPaymentResponse(Node $node): void |
167
|
|
|
{ |
168
|
|
|
$this->processIncomingPayment($node, true); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function beforeFailedIncomingPaymentResponse(Node $node): void |
172
|
|
|
{ |
173
|
|
|
$this->processIncomingPayment($node, false); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
private function processIncomingPayment(Node $node, bool $success): void |
177
|
|
|
{ |
178
|
|
|
$donor = $this->readDonor($node->getValueFrom('PayerNumber')); |
179
|
|
|
|
180
|
|
|
if (!$donor) { |
181
|
|
|
return; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** @var \DateTimeImmutable $date */ |
185
|
|
|
$date = $node->getChild('Date')->getValueFrom('Object'); |
186
|
|
|
|
187
|
|
|
$this->commandBus->handle( |
188
|
|
|
new AttemptState( |
189
|
|
|
$donor, |
190
|
|
|
Transitions::IMPORT_TRANSACTION_ACTIVE, |
191
|
|
|
'Transaction active on ' . $date->format('Y-m-d') |
192
|
|
|
) |
193
|
|
|
); |
194
|
|
|
|
195
|
|
|
/** @var \Money\Money $amount */ |
196
|
|
|
$amount = $node->getChild('Amount')->getValueFrom('Object'); |
197
|
|
|
|
198
|
|
|
$eventClassName = $success ? Event\TransactionPerformed::class : Event\TransactionFailed::class; |
199
|
|
|
|
200
|
|
|
$this->dispatcher->dispatch(new $eventClassName($donor, $amount, $date)); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
private function readDonor(string $payerNumber): ?Donor |
204
|
|
|
{ |
205
|
|
|
try { |
206
|
|
|
return $this->donorQuery->requireByPayerNumber($payerNumber); |
207
|
|
|
} catch (DonorDoesNotExistException $exception) { |
208
|
|
|
// Dispatching error means that failure can be picked up in an outer layer |
209
|
|
|
$this->dispatcher->dispatch( |
210
|
|
|
new Event\ErrorEvent( |
211
|
|
|
"{$exception->getMessage()}", |
212
|
|
|
['payer_number' => $payerNumber] |
213
|
|
|
) |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return null; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
private function validateDonorAccountNumber(AccountNumber $nodeAccount, Donor $donor): void |
221
|
|
|
{ |
222
|
|
|
if (!$nodeAccount->equals($donor->getAccount())) { |
223
|
|
|
// Dispatching error means that failure can be picked up in an outer layer |
224
|
|
|
$this->dispatcher->dispatch( |
225
|
|
|
new Event\ErrorEvent( |
226
|
|
|
sprintf( |
227
|
|
|
"Invalid mandate response for payer number '%s', found account '%s', expecting '%s'", |
228
|
|
|
$donor->getPayerNumber(), |
229
|
|
|
$nodeAccount->getNumber(), |
230
|
|
|
$donor->getAccount()->getNumber() |
231
|
|
|
), |
232
|
|
|
['payer_number' => $donor->getPayerNumber(), 'mandate_key' => $donor->getMandateKey()] |
233
|
|
|
) |
234
|
|
|
); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
private function validateDonorId(IdInterface $nodeId, Donor $donor): void |
239
|
|
|
{ |
240
|
|
|
if ($nodeId->format('S-sk') != $donor->getDonorId()->format('S-sk')) { |
241
|
|
|
// Dispatching error means that failure can be picked up in an outer layer |
242
|
|
|
$this->dispatcher->dispatch( |
243
|
|
|
new Event\ErrorEvent( |
244
|
|
|
sprintf( |
245
|
|
|
"Invalid mandate response for payer number '%s', found donor id '%s', expecting '%s'", |
246
|
|
|
$donor->getPayerNumber(), |
247
|
|
|
$nodeId->format('S-sk'), |
248
|
|
|
$donor->getDonorId()->format('S-sk') |
249
|
|
|
), |
250
|
|
|
['payer_number' => $donor->getPayerNumber(), 'mandate_key' => $donor->getMandateKey()] |
251
|
|
|
) |
252
|
|
|
); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|