forked from nemiah/phpFinTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetStatementOfAccountXML.php
More file actions
174 lines (154 loc) · 6.8 KB
/
GetStatementOfAccountXML.php
File metadata and controls
174 lines (154 loc) · 6.8 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
<?php
/** @noinspection PhpUnused */
namespace Fhp\Action;
use Fhp\Model\SEPAAccount;
use Fhp\PaginateableAction;
use Fhp\Protocol\BPD;
use Fhp\Protocol\Message;
use Fhp\Protocol\UnexpectedResponseException;
use Fhp\Protocol\UPD;
use Fhp\Segment\CAZ\HICAZSv1;
use Fhp\Segment\CAZ\HICAZv1;
use Fhp\Segment\CAZ\HKCAZv1;
use Fhp\Segment\CAZ\UnterstuetzteCamtMessages;
use Fhp\Segment\Common\Kti;
use Fhp\Segment\HIRMS\Rueckmeldungscode;
use Fhp\UnsupportedException;
/**
* Retrieves statements for one specific account or for all accounts that the user has access to. A statement is a
* series of financial transactions that pertain to the account, grouped by day.
*/
class GetStatementOfAccountXML extends PaginateableAction
{
// Request (if you add a field here, update __serialize() and __unserialize() as well).
/** @var SEPAAccount */
private $account;
/** @var \DateTime */
private $from;
/** @var \DateTime */
private $to;
/** @var string */
private $camtURN;
/** @var bool */
private $allAccounts;
// Response
/** @var string[] */
protected $xml = [];
/**
* @param SEPAAccount $account The account to get the statement for. This can be constructed based on information
* that the user entered, or it can be {@link SEPAAccount} instance retrieved from {@link getAccounts()}.
* @param \DateTime|null $from If set, only transactions after this date (inclusive) are returned.
* @param \DateTime|null $to If set, only transactions before this date (inclusive) are returned.
* @param string|null $camtURN The URN/descriptor of the CAMT XML format you want the bank to return.
* Use null to just let the bank decide. Otherwise needs to be one of the reported URNs the bank supports.
* For example urn:iso:std:iso:20022:tech:xsd:camt.052.001.02
* @param bool $allAccounts If set to true, will return statements for all accounts of the user. You still need to
* pass one of the accounts into $account, though.
* @return GetStatementOfAccountXML A new action instance.
*/
public static function create(SEPAAccount $account, ?\DateTime $from = null, ?\DateTime $to = null, ?string $camtURN = null, bool $allAccounts = false): GetStatementOfAccountXML
{
if ($from !== null && $to !== null && $from > $to) {
throw new \InvalidArgumentException('From-date must be before to-date');
}
$result = new GetStatementOfAccountXML();
$result->account = $account;
$result->camtURN = $camtURN;
$result->from = $from;
$result->to = $to;
$result->allAccounts = $allAccounts;
return $result;
}
/**
* @deprecated Beginning from PHP7.4 __unserialize is used for new generated strings, then this method is only used for previously generated strings - remove after May 2023
*/
public function serialize(): string
{
return serialize($this->__serialize());
}
public function __serialize(): array
{
return [
parent::__serialize(),
$this->account, $this->camtURN, $this->from, $this->to, $this->allAccounts,
];
}
/**
* @deprecated Beginning from PHP7.4 __unserialize is used for new generated strings, then this method is only used for previously generated strings - remove after May 2023
*
* @param string $serialized
* @return void
*/
public function unserialize($serialized)
{
self::__unserialize(unserialize($serialized));
}
public function __unserialize(array $serialized): void
{
list(
$parentSerialized,
$this->account, $this->camtURN, $this->from, $this->to, $this->allAccounts,
) = $serialized;
is_array($parentSerialized) ?
parent::__unserialize($parentSerialized) :
parent::unserialize($parentSerialized);
}
/**
* @return string[] The XML-Document(s) received from the bank, or empty array if the statement is unavailable/empty.
*/
public function getBookedXML(): array
{
$this->ensureDone();
return $this->xml;
}
protected function createRequest(BPD $bpd, ?UPD $upd)
{
if ($upd === null) {
throw new UnsupportedException('The UPD is needed to be able to create a request for GetStatementOfAccountXML.');
}
if (!$upd->isRequestSupportedForAccount($this->account, 'HKCAZ')) {
throw new UnsupportedException('The bank (or the given account/user combination) does not support GetStatementOfAccountXML.');
}
/** @var HICAZSv1 $hicazs */
$hicazs = $bpd->requireLatestSupportedParameters('HICAZS');
$supportedCamtURNs = $hicazs->getParameter()->getUnterstuetzteCamtMessages()->camtDescriptor;
if (is_null($this->camtURN)) {
$camtURNs = $supportedCamtURNs;
} elseif (!in_array($this->camtURN, $supportedCamtURNs)) {
throw new \InvalidArgumentException('The bank does not support the CAMT format' . $this->camtURN . '. The following formats are supported: ' . implode(', ', $supportedCamtURNs));
} else {
$camtURNs = [$this->camtURN];
}
if ($this->allAccounts && !$hicazs->getParameter()->getAlleKontenErlaubt()) {
throw new \InvalidArgumentException('The bank do not permit the use of allAccounts=true');
}
switch ($hicazs->getVersion()) {
case 1:
$unterstuetzteCamtMessages = UnterstuetzteCamtMessages::create($camtURNs);
return HKCAZv1::create(Kti::fromAccount($this->account), $unterstuetzteCamtMessages, $this->allAccounts, $this->from, $this->to);
default:
throw new UnsupportedException('Unsupported HKCAZ version: ' . $hicazs->getVersion());
}
}
public function processResponse(Message $response)
{
parent::processResponse($response);
// Banks send just 3010 and no HICAZ in case there are no transactions.
if ($response->findRueckmeldung(Rueckmeldungscode::NICHT_VERFUEGBAR) !== null) {
return;
}
/** @var HICAZv1[] $responseHicaz */
$responseHicaz = $response->findSegments(HICAZv1::class);
$numResponseSegments = count($responseHicaz);
if ($numResponseSegments < count($this->getRequestSegmentNumbers())) {
throw new UnexpectedResponseException("Only got $numResponseSegments HICAZ response segments!");
}
if ($numResponseSegments > 1) {
throw new UnsupportedException('More than 1 HICAZ response segment is not supported at the moment!');
}
// It seems that paginated responses, always contain a whole XML Document
foreach ($responseHicaz[0]->getGebuchteUmsaetze() as $xml_string) {
$this->xml[] = $xml_string;
}
}
}