-
Notifications
You must be signed in to change notification settings - Fork 61
/
Hsbc.php
279 lines (249 loc) · 8.44 KB
/
Hsbc.php
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
namespace Kingsquare\Parser\Banking\Mt940\Engine;
use Kingsquare\Banking\Statement;
use Kingsquare\Banking\Hsbc\HsbcTransaction;
use Kingsquare\Banking\Transaction;
use Kingsquare\Parser\Banking\Mt940\Engine;
/**
* @author jun ([email protected])
* @license http://opensource.org/licenses/MIT MIT
*/
class Hsbc extends Engine
{
const PATTERN_TAG_61 = '/^:61:(\d{6})(\d{4}?)(C|D|EC|ED|RC|RD)[A-Z](\d+,\d+)([FNS])([A-Z]{3})(.{16})/m';
/**
* returns the name of the bank.
*
* @return string
*/
protected function parseStatementBank()
{
return 'HSBC';
}
/**
* Overloaded
*
* @return array
*/
protected function parseStatementData()
{
$results = preg_split(
'/(^:20:|^-X{,3}$|\Z)/m',
$this->getRawData(),
-1,
PREG_SPLIT_NO_EMPTY
);
return $results;
}
/**
* Overloaded: add support for virtual account.
*
* @return Statement[]
*/
public function parse()
{
$results = [];
foreach ($this->parseStatementData() as $this->currentStatementData) {
$statement = new Statement();
if ($this->debug) {
$statement->rawData = $this->currentStatementData;
}
$statement->setBank($this->parseStatementBank());
$statement->setAccount($this->parseStatementAccount());
$statement->setStartPrice($this->parseStatementStartPrice());
$statement->setEndPrice($this->parseStatementEndPrice());
$statement->setStartTimestamp($this->parseStatementStartTimestamp());
$statement->setEndTimestamp($this->parseStatementEndTimestamp());
$statement->setNumber($this->parseStatementNumber());
$statement->setCurrency($this->parseStatementCurrency());
foreach ($this->parseTransactionData() as $this->currentTransactionData) {
$transaction = new HsbcTransaction();
if ($this->debug) {
$transaction->rawData = $this->currentTransactionData;
}
$transaction->setAccount($this->parseTransactionAccount());
$transaction->setAccountName($this->parseTransactionAccountName());
$transaction->setPrice($this->parseTransactionPrice());
$transaction->setDebitCredit($this->parseTransactionDebitCredit());
$transaction->setCancellation($this->parseTransactionCancellation());
$transaction->setDescription($this->parseTransactionDescription());
$transaction->setValueTimestamp($this->parseTransactionValueTimestamp());
$transaction->setEntryTimestamp($this->parseTransactionEntryTimestamp());
$transaction->setTransactionCode($this->parseTransactionCode());
$transaction->setVirtualAccount($this->parseVirtualAccount());
$statement->addTransaction($transaction);
}
$results[] = $statement;
}
return $results;
}
/**
* Overloaded: 16x, like 808XXXXXX292 .
*
* @return string accountnumber
*/
protected function parseStatementAccount()
{
$results = [];
if (preg_match('/:25:([0-9X]+)*/', $this->getCurrentStatementData(), $results)
&& !empty($results[1])
) {
return $this->sanitizeAccount($results[1]);
}
return '';
}
/**
* Overloaded: HSBC has different way of storing account info.
*
* {@inheritdoc}
*/
protected function parseTransactionAccount()
{
$results = [];
// YYMMDD[MMDD]2a[1!a]15d1!a3!c16x[//16x][34x]
// eg.: :61:1203290329DD20000,00NTRF1004688128 //6128522200250001
// Reference for the Account Owner (16x): 1004688128
// Reference of the Account Servicing Institution [//16x]: 6128522200250001
// Supplementary Details [34x]: null
if (preg_match(self::PATTERN_TAG_61, $this->getCurrentTransactionData(), $results)) {
return $this->sanitizeAccount($results[7]);
}
return '';
}
/**
* Overloaded: debit or credit of the transaction.
*
* @return string
*/
protected function parseTransactionDebitCredit()
{
$results = [];
if (preg_match(self::PATTERN_TAG_61, $this->getCurrentTransactionData(), $results)) {
return $this->sanitizeDebitCredit($results[3]);
}
return '';
}
/**
* Overloaded: HSBC has different way of storing account name.
*
* {@inheritdoc}
*/
protected function parseTransactionAccountName()
{
$results = [];
// SEPA MT940 Structured
if (preg_match('#/NAME/(.+?)\n?/(REMI|ADDR|ISDT|CSID)/#ms', $this->getCurrentTransactionData(), $results)) {
$accountName = trim($results[1]);
if (!empty($accountName)) {
return $this->sanitizeAccountName($accountName);
}
}
if (preg_match('/^:61:.*? (.+)/m', $this->getCurrentTransactionData(), $results)) {
$accountName = trim($results[1]);
if (!empty($accountName)) {
return $this->sanitizeAccountName($accountName);
}
}
if (preg_match('/(.*) Betaalautomaat/', $this->parseTransactionDescription(), $results)) {
$accountName = trim($results[1]);
if (!empty($accountName)) {
return $this->sanitizeAccountName($accountName);
}
}
return '';
}
/**
* Overloaded: HSBC has different way of storing transaction value timestamps (ymd).
*
* {@inheritdoc}
*/
protected function parseTransactionEntryTimestamp()
{
$results = [];
if (preg_match('/^:60F:[C|D]([\d]{6})/m', $this->getCurrentStatementData(), $results)) {
return $this->sanitizeTimestamp($results[1]);
}
return 0;
}
/**
* Overloaded: HSBC has different way of storing transaction value timestamps (ymd).
*
* {@inheritdoc}
*/
protected function parseTransactionValueTimestamp()
{
$results = [];
if (preg_match(self::PATTERN_TAG_61, $this->getCurrentTransactionData(), $results)) {
return $this->sanitizeTimestamp($results[1]);
}
return 0;
}
/**
* retrieve the virtual account from full description of the transaction.
*
* @return string
*/
protected function parseVirtualAccount()
{
$results = [];
if (preg_match('/[\n].*?(?:\/VA\/(.+)$)/m', $this->getCurrentTransactionData(), $results)) {
return $this->sanitizeVirtualAccount($results[1]);
}
return '';
}
/**
* Overloaded: HSBC encapsulates the description with /REMI/ for SEPA.
*
* {@inheritdoc}
*/
protected function sanitizeDescription($string)
{
$description = parent::sanitizeDescription($string);
if (strpos($description, '/REMI/') !== false
&& preg_match('#/REMI/(.*?)(/((PURP|ISDT|CSID|RTRN)/)|$)#s', $description, $results) && !empty($results[1])
) {
return $results[1];
}
if (strpos($description, '/EREF/') !== false
&& preg_match('#/EREF/(.*?)/(ORDP)/#s', $description, $results) && !empty($results[1])
) {
return $results[1];
}
return $description;
}
/**
* Overloaded: HSBC has some specific debit/credit marks.
*
* @param string $string
*
* @return string
*/
protected function sanitizeDebitCredit($string)
{
$debitOrCredit = strtoupper(substr((string)$string, -1, 1));
if ($debitOrCredit !== Transaction::DEBIT && $debitOrCredit !== Transaction::CREDIT) {
trigger_error('wrong value for debit/credit (' . $string . ')', E_USER_ERROR);
$debitOrCredit = '';
}
return $debitOrCredit;
}
/**
* @param string $string
*
* @return string
*/
protected function sanitizeVirtualAccount($string)
{
return trim($string);
}
/**
* Overloaded: Is applicable if first line starts with :20:AI.
*
* {@inheritdoc}
*/
public static function isApplicable($string)
{
$firstline = strtok($string, "\r\n\t");
return strpos($firstline, ':20:AI') !== false && strlen($firstline) === 20;
}
}