From a3f6903782e6ac16c3c234b5a1614e0e530bb2e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Falk=20Ku=CC=88hnel?= Date: Sat, 14 Nov 2026 21:36:35 +0100 Subject: [PATCH 1/2] feat: keep statement documents as the bank sent them StatementResponse.rawDocuments holds the CAMT documents (one per booking day) or the MT940 stream exactly as the bank sent them, so callers can run their own parser, e.g. to treat downloaded and uploaded statements alike. All CAMT documents are decoded before parsing, so they are all there even when the built-in parser fails on one. --- src/interactions/customerInteraction.ts | 7 +++ src/interactions/statementInteractionCAMT.ts | 33 ++++++----- src/interactions/statementInteractionMT940.ts | 4 ++ src/tests/rawStatementDocuments.test.ts | 56 +++++++++++++++++++ 4 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 src/tests/rawStatementDocuments.test.ts diff --git a/src/interactions/customerInteraction.ts b/src/interactions/customerInteraction.ts index 5ddb8e6..8338168 100644 --- a/src/interactions/customerInteraction.ts +++ b/src/interactions/customerInteraction.ts @@ -44,6 +44,13 @@ export interface ClientResponse { export interface StatementResponse extends ClientResponse { statements: Statement[]; + /** + * The statement documents exactly as the bank sent them: one CAMT XML document per + * booking day (already decoded to a string), or the MT940 stream as one document. + * Lets callers run their own parser, e.g. to read fields the built-in parsers skip + * or to treat downloaded and uploaded statements alike. + */ + rawDocuments?: { format: 'camt' | 'mt940'; content: string }[]; } export abstract class CustomerInteraction { diff --git a/src/interactions/statementInteractionCAMT.ts b/src/interactions/statementInteractionCAMT.ts index 3644f23..3630794 100644 --- a/src/interactions/statementInteractionCAMT.ts +++ b/src/interactions/statementInteractionCAMT.ts @@ -56,24 +56,27 @@ export class StatementInteractionCAMT extends CustomerOrderInteraction { .findAllSegments(HICAZ.Id) .flatMap((segment) => segment.bookedTransactions ?? []); - if (camtMessages.length > 0) { + // Decode every document first, so callers get all of them as the bank sent them, + // even when the built-in parser fails on one + const xmlStrings = camtMessages.map((camtMessage) => { + // The regex looks for the XML declaration `` + // and checks if it contains the attribute encoding="UTF-8". + // The 'i' flag makes the match case-insensitive (e.g., for "utf-8"). + const isUtf8Encoded = /<\?xml[^>]*encoding="UTF-8"[^>]*\?>/i.test(camtMessage); + // camtMessage is initially encoded as 'latin1' (ISO-8859-1), but actually contains UTF-8 data. + // Therefore, we need to first convert it back to a buffer using 'latin1', and then decode it as 'utf8'. + return isUtf8Encoded ? Buffer.from(camtMessage, 'latin1').toString('utf8') : camtMessage; + }); + clientResponse.rawDocuments = xmlStrings.map((content) => ({ + format: 'camt' as const, + content, + })); + + if (xmlStrings.length > 0) { try { // Parse all CAMT messages (one per booking day) and combine statements const allStatements: Statement[] = []; - for (const camtMessage of camtMessages) { - // The regex looks for the XML declaration `` - // and checks if it contains the attribute encoding="UTF-8". - // The 'i' flag makes the match case-insensitive (e.g., for "utf-8"). - const isUtf8Encoded = /<\?xml[^>]*encoding="UTF-8"[^>]*\?>/i.test(camtMessage); - - let xmlString: string = camtMessage; - if (isUtf8Encoded) { - // camtMessage is initially encoded as 'latin1' (ISO-8859-1), but actually contains UTF-8 data. - // Therefore, we need to first convert it back to a buffer using 'latin1', and then decode it as 'utf8'. - const intermediateBuffer = Buffer.from(camtMessage, 'latin1'); - xmlString = intermediateBuffer.toString('utf8'); - } - + for (const xmlString of xmlStrings) { const parser = new CamtParser(xmlString); const statements = parser.parse(); allStatements.push(...statements); diff --git a/src/interactions/statementInteractionMT940.ts b/src/interactions/statementInteractionMT940.ts index 5c9c9da..e0f2416 100644 --- a/src/interactions/statementInteractionMT940.ts +++ b/src/interactions/statementInteractionMT940.ts @@ -49,6 +49,10 @@ export class StatementInteractionMT940 extends CustomerOrderInteraction { .filter((booked) => !!booked) .join(''); + clientResponse.rawDocuments = bookedTransactions + ? [{ format: 'mt940', content: bookedTransactions }] + : []; + if (bookedTransactions) { try { const parser = new Mt940Parser(bookedTransactions); diff --git a/src/tests/rawStatementDocuments.test.ts b/src/tests/rawStatementDocuments.test.ts new file mode 100644 index 0000000..1118435 --- /dev/null +++ b/src/tests/rawStatementDocuments.test.ts @@ -0,0 +1,56 @@ +import { describe, expect, it } from 'vitest'; +import { StatementInteractionCAMT } from '../interactions/statementInteractionCAMT.js'; +import { StatementInteractionMT940 } from '../interactions/statementInteractionMT940.js'; +import { Message } from '../message.js'; +import { registerSegments } from '../segments/registry.js'; + +registerSegments(); + +const CAMT_DESCRIPTOR = 'urn?:iso?:std?:iso?:20022?:tech?:xsd?:camt.052.001.08'; + +const camt = (id: string) => + `` + + `${id}2026-07-01T10:00:00+02:00` + + `${id}DE991234567123456EUR` + + `10.00DBIT` + + `
2026-07-01
2026-07-01
` + + `TXN${id}
`; + +const hicaz = (...docs: string[]) => + `HICAZ:5:1+DE991234567123456:BANK12+${CAMT_DESCRIPTOR}+${docs.map((d) => `@${d.length}@${d}`).join(':')}'`; + +const MT940 = + ':20:STARTUMS\r\n:25:12030000/1234567890\r\n:28C:00001/001\r\n:60F:C260701EUR100,00\r\n' + + ':61:2607010701DR10,00NMSCNONREF\r\n:86:166?00GUTSCHRIFT?20SVWZ+Test\r\n:62F:C260701EUR90,00\r\n-\r\n'; + +type Response = { statements: unknown[]; rawDocuments?: { format: string; content: string }[] }; + +describe('rawDocuments: statements as the bank sent them', () => { + it('CAMT: one document per booking day, in order, even when the built-in parser fails', () => { + const message = Message.decode(`${hicaz(camt('A'))}${hicaz(camt('B'))}`); + const clientResponse: Response = { statements: [] }; + new StatementInteractionCAMT('123').handleResponse(message, clientResponse as never); + + expect(clientResponse.rawDocuments).toEqual([ + { format: 'camt', content: camt('A') }, + { format: 'camt', content: camt('B') }, + ]); + // These minimal documents have no balances, which the built-in parser rejects + expect(clientResponse.statements).toEqual([]); + }); + + it('CAMT: an empty list when the bank sent no bookings', () => { + const message = Message.decode(`HIRMG:3:2+0010::Entgegengenommen.'`); + const clientResponse: Response = { statements: [] }; + new StatementInteractionCAMT('123').handleResponse(message, clientResponse as never); + expect(clientResponse.rawDocuments).toEqual([]); + }); + + it('MT940: the stream as one document', () => { + const message = Message.decode(`HIKAZ:5:7+@${MT940.length}@${MT940}'`); + const clientResponse: Response = { statements: [] }; + new StatementInteractionMT940('123').handleResponse(message, clientResponse as never); + expect(clientResponse.rawDocuments).toEqual([{ format: 'mt940', content: MT940 }]); + expect(clientResponse.statements.length).toBeGreaterThan(0); + }); +}); From 8d258b77aa8e53bed537e6950577a0b1ed8a9b94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Falk=20Ku=CC=88hnel?= Date: Sat, 14 Nov 2026 21:40:37 +0100 Subject: [PATCH 2/2] docs: describe rawDocuments in the README --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 9264dfb..7799f8c 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,18 @@ The property `activeTanMedia` contains a list of the TAN media names you can use The `bankingInformation` is primarily obtained through the `synchronize()` calls as demonstrated above. However, it is possible that the banking information may have changed since the last synchronization call. To address this, the BPD and UPD are versioned, and with every transaction made, not just synchronizations, the currently used versions are provided to the bank. If any changes have occurred, the bank will send back new versions of the BPD and UPD respectively. This process is managed by the client, but it is essential to check the `bankingInformationUpdated` property, which is available in every response. This property indicates if there have been any changes, and it is important to persist the new version for future sessions. The most up-to-date version of the `bankingInformation` object can always be retrieved using `config.bankingInformation`. +### Statements as the bank sent them + +Besides the parsed `statements`, a statement response carries `rawDocuments`: the documents exactly as the bank sent them. For CAMT that is one XML document per booking day (already decoded to a string), for MT940 the whole stream as one document. Use them to run your own parser, to archive the original, or to treat downloaded and uploaded statements alike. All CAMT documents are present even when the built-in parser fails on one of them. + +```typescript +const response = await client.getAccountStatements(account, from, to); +for (const document of response.rawDocuments ?? []) { + // document.format is 'camt' or 'mt940', document.content the text as the bank sent it + archive(document.format, document.content); +} +``` + ### Debugging If you need to debug issues and the `response.bankAnswers` don't provide enough information, you can enable debugging of messages with: