forked from nemiah/phpFinTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCAMT.php
More file actions
684 lines (590 loc) · 25.2 KB
/
CAMT.php
File metadata and controls
684 lines (590 loc) · 25.2 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
<?php
namespace Fhp\CAMT;
use Fhp\MT940\MT940;
/**
* Parser for CAMT XML format (ISO 20022)
* Supports camt.052 (Account Report) format versions
*/
class CAMT
{
/**
* Parse CAMT XML string(s) into an array structure compatible with MT940 parser output
*
* @param string[] $xmlStrings Array of XML strings (one or more CAMT documents)
* @return array Associative array with dates as keys, containing statement and transaction data
*/
public function parse(array $xmlStrings): array
{
$result = [];
foreach ($xmlStrings as $xmlString) {
if (empty($xmlString)) {
continue;
}
// Try to load the XML
$previousValue = libxml_use_internal_errors(true);
$doc = simplexml_load_string($xmlString);
libxml_use_internal_errors($previousValue);
if ($doc === false) {
continue; // Skip invalid XML
}
// Register namespace - support multiple camt versions
$namespaces = $doc->getNamespaces(true);
$ns = null;
foreach ($namespaces as $prefix => $uri) {
if (str_contains($uri, 'camt.052')) {
$ns = $uri;
break;
}
}
if ($ns === null) {
continue; // Not a camt.052 document
}
$doc->registerXPathNamespace('c', $ns);
// Parse each report
$reports = $doc->xpath('//c:Rpt');
if ($reports === false) {
continue;
}
foreach ($reports as $report) {
$this->parseReport($report, $ns, $result);
}
}
return $result;
}
/**
* Parse a single report (Rpt) element
*
* @param string $ns Namespace URI
* @param array &$result Result array to populate
*/
private function parseReport(\SimpleXMLElement $report, string $ns, array &$result): void
{
$report->registerXPathNamespace('c', $ns);
// Get account balances
$balances = $this->parseBalances($report, $ns);
// Parse entries (transactions)
$entries = $report->xpath('.//c:Ntry');
if ($entries === false) {
return;
}
foreach ($entries as $entry) {
$entry->registerXPathNamespace('c', $ns);
$transaction = $this->parseEntry($entry, $ns);
if ($transaction === null) {
continue;
}
// Group transactions by booking date
$dateKey = $transaction['booking_date'];
if (!isset($result[$dateKey])) {
$result[$dateKey] = [
'start_balance' => $balances,
'transactions' => [],
];
}
$result[$dateKey]['transactions'][] = $transaction;
}
// If we have balances but no transactions, still create an entry
if (!empty($balances) && empty($entries)) {
$dateKey = $balances['date'] ?? date('Y-m-d');
if (!isset($result[$dateKey])) {
$result[$dateKey] = [
'start_balance' => $balances,
'transactions' => [],
];
}
}
// Set end balances
foreach ($result as $dateKey => &$statement) {
if (!isset($statement['end_balance']) && !empty($balances)) {
$statement['end_balance'] = [
'amount' => $balances['amount'] ?? 0,
'credit_debit' => $balances['credit_debit'] ?? MT940::CD_CREDIT,
'date' => $dateKey,
];
}
}
}
/**
* Parse balance information from report
*
* @return array Balance information
*/
private function parseBalances(\SimpleXMLElement $report, string $ns): array
{
$report->registerXPathNamespace('c', $ns);
// Try to find opening balance (OPBD) or closing balance (CLBD)
$balances = $report->xpath('.//c:Bal');
if ($balances === false || empty($balances)) {
return [];
}
$result = [];
foreach ($balances as $balance) {
$balance->registerXPathNamespace('c', $ns);
$type = (string) $balance->xpath('.//c:Tp/c:CdOrPrtry/c:Cd')[0] ?? '';
$amount = (float) ($balance->xpath('.//c:Amt')[0] ?? 0);
$currency = (string) ($balance->xpath('.//c:Amt/@Ccy')[0] ?? 'EUR');
$creditDebit = (string) ($balance->xpath('.//c:CdtDbtInd')[0] ?? 'CRDT');
$date = (string) ($balance->xpath('.//c:Dt/c:Dt')[0] ?? '');
// Use opening balance if available
if ($type === 'OPBD' || empty($result)) {
$result = [
'amount' => $amount,
'currency' => $currency,
'credit_debit' => $creditDebit === 'DBIT' ? MT940::CD_DEBIT : MT940::CD_CREDIT,
'date' => $date,
];
}
}
return $result;
}
/**
* Parse a single entry (transaction) from CAMT XML
*
* @return array|null Transaction data or null if parsing fails
*/
private function parseEntry(\SimpleXMLElement $entry, string $ns): ?array
{
$entry->registerXPathNamespace('c', $ns);
// Get booking date
$bookingDate = (string) ($entry->xpath('.//c:BookgDt/c:Dt')[0] ?? $entry->xpath('.//c:BookgDt/c:DtTm')[0] ?? '');
if (empty($bookingDate)) {
return null;
}
// Parse date - handle both date and datetime formats
if (str_contains($bookingDate, 'T')) {
$bookingDate = substr($bookingDate, 0, 10);
}
// Get value date
$valutaDate = (string) ($entry->xpath('.//c:ValDt/c:Dt')[0] ?? $entry->xpath('.//c:ValDt/c:DtTm')[0] ?? $bookingDate);
if (str_contains($valutaDate, 'T')) {
$valutaDate = substr($valutaDate, 0, 10);
}
// Get amount
$amount = (float) ($entry->xpath('.//c:Amt')[0] ?? 0);
// Get credit/debit indicator
$creditDebit = (string) ($entry->xpath('.//c:CdtDbtInd')[0] ?? 'CRDT');
$creditDebit = $creditDebit === 'DBIT' ? MT940::CD_DEBIT : MT940::CD_CREDIT;
// Check if it's a reversal/storno
$reversalIndicator = (string) ($entry->xpath('.//c:RvslInd')[0] ?? 'false');
$isStorno = strtolower($reversalIndicator) === 'true';
// Get status - check if booked or pending
$status = (string) ($entry->xpath('.//c:Sts')[0] ?? 'BOOK');
$booked = strtoupper($status) === 'BOOK';
// Parse transaction details
$details = $this->parseEntryDetails($entry, $ns);
return [
'booking_date' => $bookingDate,
'valuta_date' => $valutaDate,
'amount' => $amount,
'credit_debit' => $creditDebit,
'is_storno' => $isStorno,
'booked' => $booked,
'description' => $details,
];
}
/**
* Parse detailed transaction information
*
* @return array Transaction details
*/
private function parseEntryDetails(\SimpleXMLElement $entry, string $ns): array
{
$entry->registerXPathNamespace('c', $ns);
$details = [
'booking_code' => '',
'booking_text' => '',
'description_1' => '',
'description_2' => '',
'description' => [],
'bank_code' => '',
'account_number' => '',
'name' => '',
'primanoten_nr' => '',
'text_key_addition' => '',
];
// Get transaction details from NtryDtls/TxDtls
$txDetails = $entry->xpath('.//c:NtryDtls/c:TxDtls');
if ($txDetails === false || empty($txDetails)) {
return $details;
}
$txDetail = $txDetails[0];
$txDetail->registerXPathNamespace('c', $ns);
// Get booking code and text from BkTxCd
$this->parseBookingCode($txDetail, $ns, $details);
// Get remittance information (Verwendungszweck)
$remittanceInfo = $txDetail->xpath('.//c:RmtInf');
if ($remittanceInfo !== false && !empty($remittanceInfo)) {
$remittanceInfo[0]->registerXPathNamespace('c', $ns);
$this->parseRemittanceInfo($remittanceInfo[0], $ns, $details);
}
// Get counterparty information
$relatedParties = $txDetail->xpath('.//c:RltdPties');
if ($relatedParties !== false && !empty($relatedParties)) {
$relatedParties[0]->registerXPathNamespace('c', $ns);
$this->parseRelatedParties($relatedParties[0], $ns, $details);
}
// Get agent information (BIC codes)
$relatedAgents = $txDetail->xpath('.//c:RltdAgts');
if ($relatedAgents !== false && !empty($relatedAgents)) {
$relatedAgents[0]->registerXPathNamespace('c', $ns);
$this->parseRelatedAgents($relatedAgents[0], $ns, $details);
}
// Get references (EREF, MREF, CRED, etc.)
$refs = $txDetail->xpath('.//c:Refs');
if ($refs !== false && !empty($refs)) {
$refs[0]->registerXPathNamespace('c', $ns);
$this->parseReferences($refs[0], $ns, $details);
}
// Get mandate information and creditor ID
$this->parseMandateInfo($txDetail, $ns, $details);
return $details;
}
/**
* Parse booking code and text
*/
private function parseBookingCode(\SimpleXMLElement $txDetail, string $ns, array &$details): void
{
$txDetail->registerXPathNamespace('c', $ns);
// Get domain code
$domainCode = (string) ($txDetail->xpath('.//c:BkTxCd/c:Domn/c:Cd')[0] ?? '');
$details['booking_code'] = $domainCode;
// Get family and subfamily codes
$family = (string) ($txDetail->xpath('.//c:BkTxCd/c:Domn/c:Fmly/c:Cd')[0] ?? '');
$subfamily = (string) ($txDetail->xpath('.//c:BkTxCd/c:Domn/c:Fmly/c:SubFmlyCd')[0] ?? '');
// Get proprietary code (often more descriptive)
$proprietary = (string) ($txDetail->xpath('.//c:BkTxCd/c:Prtry/c:Cd')[0] ?? '');
$proprietaryIssuer = (string) ($txDetail->xpath('.//c:BkTxCd/c:Prtry/c:Issr')[0] ?? '');
// Build booking text - prefer proprietary code as it's more descriptive
if (!empty($proprietary)) {
$details['booking_text'] = $proprietary;
// Extract booking code and text key addition from proprietary code
// Format is often like: NTRF+118+05801 or NDDT+105+00931
if (preg_match('/([A-Z]{4})\+(\d{3})\+(\d{5})/', $proprietary, $matches)) {
$details['booking_code'] = $matches[2];
$details['text_key_addition'] = $matches[3];
} elseif (preg_match('/(\d{3})/', $proprietary, $matches)) {
// Fallback: just extract the 3-digit code
if (empty($details['booking_code'])) {
$details['booking_code'] = $matches[1];
}
}
} elseif (!empty($family) || !empty($subfamily)) {
$details['booking_text'] = trim($family . ' ' . $subfamily);
}
}
/**
* Parse remittance information
*/
private function parseRemittanceInfo(\SimpleXMLElement $remittanceInfo, string $ns, array &$details): void
{
$remittanceInfo->registerXPathNamespace('c', $ns);
// Unstructured remittance info - this is the main "Verwendungszweck"
$unstructured = $remittanceInfo->xpath('.//c:Ustrd');
if ($unstructured !== false && !empty($unstructured)) {
$ustrd = (string) $unstructured[0];
// Parse structured SEPA fields from unstructured text
$structuredFields = $this->extractStructuredFieldsFromText($ustrd);
// Extract BIC and IBAN if present in text and not yet set
if (empty($details['bank_code']) && preg_match('/BIC:\s*([A-Z0-9]{8,11})/', $ustrd, $matches)) {
$details['bank_code'] = $matches[1];
}
if (empty($details['account_number']) && preg_match('/IBAN:\s*([A-Z]{2}[0-9]{2}[A-Z0-9]+)/', $ustrd, $matches)) {
$details['account_number'] = $matches[1];
}
// Set SVWZ (main description) - extract clean text without structured fields
$cleanText = $this->removeStructuredFieldsFromText($ustrd);
$details['description']['SVWZ'] = trim($cleanText);
$details['description_1'] = trim($cleanText);
// Merge extracted structured fields
$details['description'] = array_merge($details['description'], $structuredFields);
}
// Structured remittance info
$structured = $remittanceInfo->xpath('.//c:Strd/c:CdtrRefInf/c:Ref');
if ($structured !== false && !empty($structured)) {
$details['description_2'] = (string) $structured[0];
}
}
/**
* Parse related parties (Debtor/Creditor)
*/
private function parseRelatedParties(\SimpleXMLElement $relatedParties, string $ns, array &$details): void
{
// Use children() to access elements with namespace
$children = $relatedParties->children($ns);
// Try without namespace as fallback
$childrenNoNs = $relatedParties->children();
// Debtor information
$debtorName = '';
$debtorAccount = '';
$debtorOtherAccount = '';
// Try with namespace first, then without
if (isset($children->Dbtr)) {
$dbtr = $children->Dbtr->children($ns);
if (isset($dbtr->Pty)) {
$pty = $dbtr->Pty->children($ns);
$debtorName = (string) ($pty->Nm ?? '');
} else {
$debtorName = (string) ($dbtr->Nm ?? '');
}
} elseif (isset($childrenNoNs->Dbtr)) {
$dbtr = $childrenNoNs->Dbtr->children();
if (isset($dbtr->Pty)) {
$pty = $dbtr->Pty->children();
$debtorName = (string) ($pty->Nm ?? '');
} else {
$debtorName = (string) ($dbtr->Nm ?? '');
}
}
if (isset($children->DbtrAcct)) {
$dbtrAcct = $children->DbtrAcct->children($ns);
if (isset($dbtrAcct->Id)) {
$id = $dbtrAcct->Id->children($ns);
$debtorAccount = (string) ($id->IBAN ?? '');
if (empty($debtorAccount) && isset($id->Othr)) {
$othr = $id->Othr->children($ns);
$debtorOtherAccount = (string) ($othr->Id ?? '');
}
}
} elseif (isset($childrenNoNs->DbtrAcct)) {
$dbtrAcct = $childrenNoNs->DbtrAcct->children();
if (isset($dbtrAcct->Id)) {
$id = $dbtrAcct->Id->children();
$debtorAccount = (string) ($id->IBAN ?? '');
if (empty($debtorAccount) && isset($id->Othr)) {
$othr = $id->Othr->children();
$debtorOtherAccount = (string) ($othr->Id ?? '');
}
}
}
// Creditor information
$creditorName = '';
$creditorAccount = '';
$creditorOtherAccount = '';
if (isset($children->Cdtr)) {
$cdtr = $children->Cdtr->children($ns);
if (isset($cdtr->Pty)) {
$pty = $cdtr->Pty->children($ns);
$creditorName = (string) ($pty->Nm ?? '');
} else {
$creditorName = (string) ($cdtr->Nm ?? '');
}
} elseif (isset($childrenNoNs->Cdtr)) {
$cdtr = $childrenNoNs->Cdtr->children();
if (isset($cdtr->Pty)) {
$pty = $cdtr->Pty->children();
$creditorName = (string) ($pty->Nm ?? '');
} else {
$creditorName = (string) ($cdtr->Nm ?? '');
}
}
if (isset($children->CdtrAcct)) {
$cdtrAcct = $children->CdtrAcct->children($ns);
if (isset($cdtrAcct->Id)) {
$id = $cdtrAcct->Id->children($ns);
$creditorAccount = (string) ($id->IBAN ?? '');
if (empty($creditorAccount) && isset($id->Othr)) {
$othr = $id->Othr->children($ns);
$creditorOtherAccount = (string) ($othr->Id ?? '');
}
}
} elseif (isset($childrenNoNs->CdtrAcct)) {
$cdtrAcct = $childrenNoNs->CdtrAcct->children();
if (isset($cdtrAcct->Id)) {
$id = $cdtrAcct->Id->children();
$creditorAccount = (string) ($id->IBAN ?? '');
if (empty($creditorAccount) && isset($id->Othr)) {
$othr = $id->Othr->children();
$creditorOtherAccount = (string) ($othr->Id ?? '');
}
}
}
// Ultimate parties
$ultimateDebtorName = '';
$ultimateCreditorName = '';
if (isset($children->UltmtDbtr)) {
$ultDbtr = $children->UltmtDbtr->children($ns);
if (isset($ultDbtr->Pty)) {
$pty = $ultDbtr->Pty->children($ns);
$ultimateDebtorName = (string) ($pty->Nm ?? '');
} else {
$ultimateDebtorName = (string) ($ultDbtr->Nm ?? '');
}
} elseif (isset($childrenNoNs->UltmtDbtr)) {
$ultDbtr = $childrenNoNs->UltmtDbtr->children();
if (isset($ultDbtr->Pty)) {
$pty = $ultDbtr->Pty->children();
$ultimateDebtorName = (string) ($pty->Nm ?? '');
} else {
$ultimateDebtorName = (string) ($ultDbtr->Nm ?? '');
}
}
if (isset($children->UltmtCdtr)) {
$ultCdtr = $children->UltmtCdtr->children($ns);
if (isset($ultCdtr->Pty)) {
$pty = $ultCdtr->Pty->children($ns);
$ultimateCreditorName = (string) ($pty->Nm ?? '');
} else {
$ultimateCreditorName = (string) ($ultCdtr->Nm ?? '');
}
} elseif (isset($childrenNoNs->UltmtCdtr)) {
$ultCdtr = $childrenNoNs->UltmtCdtr->children();
if (isset($ultCdtr->Pty)) {
$pty = $ultCdtr->Pty->children();
$ultimateCreditorName = (string) ($pty->Nm ?? '');
} else {
$ultimateCreditorName = (string) ($ultCdtr->Nm ?? '');
}
}
// Determine name priority
$details['name'] = !empty($ultimateCreditorName) ? $ultimateCreditorName :
(!empty($ultimateDebtorName) ? $ultimateDebtorName :
(!empty($creditorName) ? $creditorName :
(!empty($debtorName) ? $debtorName : '')));
// Set account
$iban = !empty($creditorAccount) ? $creditorAccount :
(!empty($debtorAccount) ? $debtorAccount :
(!empty($creditorOtherAccount) ? $creditorOtherAccount : $debtorOtherAccount));
$details['account_number'] = $iban;
}
/**
* Parse related agents (BIC information)
*/
private function parseRelatedAgents(\SimpleXMLElement $relatedAgents, string $ns, array &$details): void
{
// Use children() to access elements with namespace
$children = $relatedAgents->children($ns);
$debtorBIC = '';
$creditorBIC = '';
// Debtor Agent (for outgoing transactions)
if (isset($children->DbtrAgt)) {
$dbtrAgt = $children->DbtrAgt->children($ns);
if (isset($dbtrAgt->FinInstnId)) {
$finInstn = $dbtrAgt->FinInstnId->children($ns);
$debtorBIC = (string) ($finInstn->BICFI ?? $finInstn->BIC ?? '');
// Try ClrSysMmbId if BIC not found
if (empty($debtorBIC) && isset($finInstn->ClrSysMmbId)) {
$clrSys = $finInstn->ClrSysMmbId->children($ns);
$debtorBIC = (string) ($clrSys->MmbId ?? '');
}
}
}
// Creditor Agent (for incoming transactions)
if (isset($children->CdtrAgt)) {
$cdtrAgt = $children->CdtrAgt->children($ns);
if (isset($cdtrAgt->FinInstnId)) {
$finInstn = $cdtrAgt->FinInstnId->children($ns);
$creditorBIC = (string) ($finInstn->BICFI ?? $finInstn->BIC ?? '');
// Try ClrSysMmbId if BIC not found
if (empty($creditorBIC) && isset($finInstn->ClrSysMmbId)) {
$clrSys = $finInstn->ClrSysMmbId->children($ns);
$creditorBIC = (string) ($clrSys->MmbId ?? '');
}
}
}
// Set BIC - only if not already set
if (empty($details['bank_code'])) {
$details['bank_code'] = !empty($creditorBIC) ? $creditorBIC : $debtorBIC;
}
}
/**
* Parse references (EREF, MREF, CRED, etc.)
*/
private function parseReferences(\SimpleXMLElement $refs, string $ns, array &$details): void
{
$refs->registerXPathNamespace('c', $ns);
// End to end reference (EREF)
$endToEndId = (string) ($refs->xpath('.//c:EndToEndId')[0] ?? '');
if (!empty($endToEndId) && $endToEndId !== 'NOTPROVIDED' && $endToEndId !== 'NONE') {
$details['description']['EREF'] = $endToEndId;
}
// Mandate reference (MREF)
$mandateId = (string) ($refs->xpath('.//c:MndtId')[0] ?? '');
if (!empty($mandateId)) {
$details['description']['MREF'] = $mandateId;
}
// Creditor reference (KREF)
$creditorRef = (string) ($refs->xpath('.//c:CdtrRef')[0] ?? '');
if (!empty($creditorRef)) {
$details['description']['KREF'] = $creditorRef;
}
// Account servicer reference
$acctSvcrRef = (string) ($refs->xpath('.//c:AcctSvcrRef')[0] ?? '');
if (!empty($acctSvcrRef)) {
$details['primanoten_nr'] = $acctSvcrRef;
}
}
/**
* Parse mandate information and creditor scheme ID
*/
private function parseMandateInfo(\SimpleXMLElement $txDetail, string $ns, array &$details): void
{
$txDetail->registerXPathNamespace('c', $ns);
// Get creditor scheme identification from related parties
$creditorSchemeId = (string) ($txDetail->xpath('.//c:RltdPties/c:Cdtr/c:Id/c:OrgId/c:Othr/c:Id')[0] ?? '');
if (empty($creditorSchemeId)) {
$creditorSchemeId = (string) ($txDetail->xpath('.//c:RltdPties/c:Cdtr/c:Id/c:PrvtId/c:Othr/c:Id')[0] ?? '');
}
if (empty($creditorSchemeId)) {
// Try ultimate creditor
$creditorSchemeId = (string) ($txDetail->xpath('.//c:RltdPties/c:UltmtCdtr/c:Id/c:OrgId/c:Othr/c:Id')[0] ?? '');
}
if (empty($creditorSchemeId)) {
$creditorSchemeId = (string) ($txDetail->xpath('.//c:RltdPties/c:UltmtCdtr/c:Id/c:PrvtId/c:Othr/c:Id')[0] ?? '');
}
if (!empty($creditorSchemeId)) {
$details['description']['CRED'] = $creditorSchemeId;
}
}
/**
* Extract structured SEPA fields from unstructured text
*
* @return array Structured fields found in text
*/
private function extractStructuredFieldsFromText(string $text): array
{
$fields = [];
// Common SEPA field patterns
$patterns = [
'EREF' => '/EREF\+([^\s]+)/',
'MREF' => '/MREF\+([^\s]+)/',
'CRED' => '/CRED\+([^\s]+)/',
'DEBT' => '/DEBT\+([^\s]+)/',
'SVWZ' => '/SVWZ\+(.+?)(?=(?:EREF|MREF|CRED|DEBT|\s*$))/',
'KREF' => '/KREF\+([^\s]+)/',
'IBAN' => '/IBAN\+([A-Z]{2}[0-9]{2}[A-Z0-9]+)/',
];
foreach ($patterns as $key => $pattern) {
if (preg_match($pattern, $text, $matches)) {
$fields[$key] = trim($matches[1]);
}
}
return $fields;
}
/**
* Remove structured SEPA fields from text to get clean description
*
* @return string Clean text without structured fields
*/
private function removeStructuredFieldsFromText(string $text): string
{
// Remove common SEPA structured fields - allow space after colon
$text = preg_replace('/\s*EREF:\s*[^\s]+/', '', $text);
$text = preg_replace('/\s*MREF:\s*[^\s]+/', '', $text);
$text = preg_replace('/\s*CRED:\s*[^\s]+/', '', $text);
$text = preg_replace('/\s*DEBT:\s*[^\s]+/', '', $text);
$text = preg_replace('/\s*KREF:\s*[^\s]+/', '', $text);
$text = preg_replace('/\s*SVWZ:\s*/', '', $text);
$text = preg_replace('/\s*IBAN:\s*[A-Z]{2}[0-9]{2}[A-Z0-9]+/', '', $text);
$text = preg_replace('/\s*BIC:\s*[A-Z0-9]{8,11}/', '', $text);
// Also handle + separator variants
$text = preg_replace('/\s*EREF\+[^\s]+/', '', $text);
$text = preg_replace('/\s*MREF\+[^\s]+/', '', $text);
$text = preg_replace('/\s*CRED\+[^\s]+/', '', $text);
$text = preg_replace('/\s*DEBT\+[^\s]+/', '', $text);
$text = preg_replace('/\s*KREF\+[^\s]+/', '', $text);
$text = preg_replace('/\s*SVWZ\+/', '', $text);
// Clean up multiple spaces and trim
$text = preg_replace('/\s+/', ' ', $text);
return trim($text);
}
}