Skip to content

feat: SEPA credit transfers with Verification of Payee - #50

Open
davidvornholt wants to merge 31 commits into
robocode13:mainfrom
davidvornholt:feat/sepa-credit-transfer
Open

davidvornholt wants to merge 31 commits into
robocode13:mainfrom
davidvornholt:feat/sepa-credit-transfer

Conversation

@davidvornholt

Copy link
Copy Markdown

Hi, and thanks for lib-fints!

I'd like to pay invoices from my own application, and since October 2025 every bank in the euro area runs Verification of Payee (VoP) on a transfer, so a transfer feature without it doesn't get far. #27 by @tanacosa started on this with the segments, and your review there described the whole feature you'd want. This PR builds it that way: segments with their parameter segments, an interaction, FinTSClient methods behind canTransfer(), a pain.001 builder, pain.002 parsing, response handling, tests and an explicit README change. No code is taken from #27. Its Sparkasse Berlin observation on the execution date sits as a code comment at the relevant spot, marked as observed at one bank, like the bank behaviour other libraries reported, and the commits credit @tanacosa as co-author.

What this adds

  • Segments: HKCCS/HICCSS, HKVPP/HIVPP/HIVPPS and HKVPA/HIVPAS, after FinTS 3.0 (C.10.2.1) and the DK's VoP specification 1.02 (C.10.7), registered and each with a round-trip test.
  • pain.001 builder (buildPain001()): structured input, validated before anything is sent (IBAN characters, country length and checksum, BIC per schema, amount, lengths, SEPA character set), nothing transliterated or shortened. It prefers pain.001.001.09 and falls back to pain.001.001.03 when the bank lists only that. Validation errors name the field, never the value, so they can be logged.
  • pain.002 parser (parsePain002()): the VoP result at transaction, payment and group level, split documents joined, numeric character references decoded.
  • Client API: canTransfer(account), transfer(account, transfer), confirmTransfer(transferId), transferWithTan(tanReference, tan?) and cancelTransfer(transferId). TransferResponse extends ClientResponse with transferId, requiresVopConfirmation, vop (result, code, suggested name, the bank's notice, the report), endToEndId, outcomeUncertain and failureReason; the TAN flow works as it does for statements.
  • VoP flow: HKVPP + HKCCS + HKTAN; polling with the touchdown point (3040) for up to 60 seconds; a match with a TAN request goes to the TAN step, every other result back to the caller. The library never confirms on its own. confirmTransfer() sends HKVPA + HKCCS + HKTAN, or HKVPA alone when the bank holds the order after 3076.
  • Response handling for the high-stakes codes: every return code is read against the segment of the message it refers to. The order goes to the bank once: one guard before every outgoing HKCCS allows only the DK continuation above. Executed means 0020 on the payment, or 3091 with 3076. Anything that leaves open whether the bank executed the payment (a receipt followed by an error or a lost answer, 9000, 9390, an ambiguous 9210, contradicting answers) returns success: false with outcomeUncertain: true and the bank answers, so the application can tell the user to check the account instead of paying twice. 3040 with a touchdown point drives the VoP poll, and the answers of all polls reach the caller in bankAnswers.
  • README: a section on transfers that ends the read-only statement explicitly and documents the flow and its guarantees; .github/copilot-instructions.md says the same.

Changes to shared code

  • splitBySeparator() read a ? at the end of a binary value as escaping the next delimiter, which shifted the following fields. It now reads left to right and skips binary data whole (test in parser.test.ts).
  • Dialog gets hooks for an order that pauses for the caller's decision, and a beforeSend hook on CustomerInteraction (a no-op by default).
  • synchronize() requests the full BPD once when the saved bankingInformation lists HKVPP without parameters the library can read, keeping the system ID, so existing users can transfer after upgrading.

Not included

Instant transfers (HKIPZ with 3045/HKIPS and 3270), batch (HKCCM) and scheduled (HKCSE) transfers, standing orders and direct debits. They can build on the same pieces.

Tests

Round-trip tests for the seven segments, the builder and the parser, and transfer tests against mocked bank answers: the VoP results, polling, the TAN and decoupled flows, the outcome codes, and the safety cases (a payment is never sent twice, a decision applies only to the result it was made on, a report about another order never counts). pnpm run build passes and pnpm run test -- --run gives 426/426 (177 before). All test data is synthetic.

I haven't run this against a real bank yet, so the behaviour of individual banks comes from the DK specifications and from what other FinTS libraries report. Reports from a live bank would be very welcome.

Review

I built this with AI coding agents: Claude Opus 5.5 wrote it, and GPT-6.2 Astra reviewed it in three rounds (behaviour, premise and money movement, then the fixes, then the repair). The review record with every finding and the commit that fixed it is in davidvornholt#1. The branch is unchanged since then; its last four commits fix the third review's findings and are covered by tests.

The money-movement review also found that HttpClient can print the PIN and TAN in debug output and prints undecodable responses; I left that code alone, since #46 and #49 already address it.

It's a big change, about 3,500 lines of source and 4,450 of tests. Thanks for taking a look!

davidvornholt and others added 30 commits November 18, 2026 18:46
…h Verification of Payee

Segment definitions for a single SEPA credit transfer (HKCCS v1, FinTS 3.0
C.10.2.1) and for Verification of Payee (HKVPP/HIVPP, HKVPA, DK VoP
specification 1.02), together with their parameter segments HICCSS, HIVPPS
and HIVPAS, registered in the segment registry.

HKVPP's touchdown point is deliberately not named continuationMark, so
the parted-response handling does not resend the whole message on 3040.

The note on the execution date 1999-01-01 and return code 9150 in HKCCS
follows the observation in PR robocode13#27 at one bank.

Co-authored-by: Sina Ahmadi <14019938+tanacosa@users.noreply.github.com>
buildPain001 writes a single credit transfer for immediate execution as
pain.001.001.09 or pain.001.001.03, chosen from the formats the bank lists
in HISPAS. It checks every value before anything is sent: IBAN check
digits, BIC format, the amount (positive, at most two decimals, within the
SEPA limit), lengths and the SEPA character set of names, remittance
information and end-to-end ID. Nothing is trimmed, shortened or
transliterated; invalid input throws a SepaValidationError naming the field.

parsePain002 reads the payment status report that banks return as the
Verification of Payee result: group, payment information and transaction
status with their reason lines, and the payee name and IBAN as submitted.
An order with Verification of Payee has to stop after the bank's answer
so the caller can decide whether to go on, without a TAN being involved,
and it continues with a different business transaction in the same
dialog. Dialog gains what that needs, all inert for existing interactions:

- CustomerInteraction.awaitsCaller(): a successful response that holds
  the dialog open instead of moving on (default false)
- Dialog.continueWith(): run a further interaction next in the open dialog
- Dialog.end(): end an open dialog with HKEND, skipping queued interactions
- CustomerInteraction.getTanMessageSegments(): segments sent with the TAN,
  before HKTAN (default none)

start() and continue() now share one loop instead of two copies of it.
FinTSClient.transfer() sends a single SEPA credit transfer (HKCCS),
executed as soon as possible. Where the BPD require Verification of Payee
for HKCCS (HIVPPS), HKVPP goes in front of the order and the VoP result
is returned to the caller before the order can be executed:

- canTransfer(account?): HKCCS, a pain.001 format from HISPAS and, where
  required, readable VoP parameters
- transfer(account, transfer): validates the input, builds the pain.001,
  submits HKVPP + HKCCS and polls an asynchronous result (polling ID,
  touchdown point, wait time; at most 60 seconds of waiting)
- confirmTransfer(): authorises the order despite the result, HKVPA with
  the VoP ID together with the identical HKCCS (DK VoP spec E.8.1)
- transferWithTan(tanReference, tan?): the TAN step, decoupled included;
  after a match without 3091, HKVPA goes with the first TAN message
- cancelTransfer(): drops the transfer and ends its dialog

Nothing is confirmed automatically. A match goes straight to the TAN only
when the bank asks for one; every other result, and a match without a TAN
request, waits for the caller. An unknown result code, a missing VoP ID,
3945 after the confirmation or any other unexpected answer ends the
transfer with success false and the bank answers. A bank that executes
the order on the first message (3091 with 3076, or 0010/0020 on HKCCS)
is reported as accepted rather than offered for confirmation.

The responses are TransferResponse objects with requiresVopConfirmation,
vop (result, code, suggested name, the bank's notice, the pain.002
report), endToEndId and failureReason. Reading features are unchanged.

Counting 0010 on HKCCS as acceptance draws on the observation in PR robocode13#27 at
one bank; the comment on isExecuted says so.

Co-authored-by: Sina Ahmadi <14019938+tanacosa@users.noreply.github.com>
The README says that transfer() deliberately ends the library's
read-only boundary and documents the transfer API with an example, the
response states, the VoP results and what an application must know: no
automatic confirmation, a first call that can be final, input that is
checked but never changed, fail-closed answers, an unknown outcome after
an exception, one transfer at a time and the polling limit. The tables,
the TAN continuation list and the limitations name the new methods and
what is still unsupported (HKIPZ, HKCCM, HKCSE, standing orders, direct
debits). The agent instructions no longer say that there are no payment
transactions.
…nswer

A second confirmTransfer() or transferWithTan() before the first one's
answer sent the order or the TAN again, and cancelTransfer() dropped a
transfer whose answer could still execute it. The transfer is now marked
in flight before its first message goes out; overlapping calls throw
without sending anything, and a finished step clears only its own
transfer.
A delayed confirmTransfer() or cancelTransfer() meant for one transfer
acted on whichever transfer was in progress, and transferWithTan() sent
any TAN reference it was given. Every TransferResponse now carries an
opaque transferId; confirmTransfer(transferId) and
cancelTransfer(transferId) require it, and transferWithTan() refuses a
reference other than the outstanding challenge, sending nothing and
keeping the transfer.

BREAKING CHANGE: confirmTransfer() and cancelTransfer() take the
transferId of the transfer response.
transfer() built the order from the banking information before the
dialog started, so parameters the dialog initialisation brought (Verification
of Payee newly required, another pain.001 format, an account no longer
allowed to transfer) were not used, and it sent HKCCS from accounts
canTransfer() refused. The order is now checked when created, with the
checks of canTransfer(), and built again right before the first HKCCS;
the confirmation reuses those bytes. HKVPP or HKVPA always goes with
HKTAN (DK VoP 1.02 E.8.1), and a Verification of Payee result for an
order sent without one stops the transfer instead of going on to the TAN.
The dialog builds a message's segments before taking its number, so an
order refused at that point leaves no gap.
0010 to HKCCS on the first message no longer counts as execution; 0020, or
3091 with 3076, does. Codes are read per referenced segment. 9000, or an
accepted order next to an error code, ends with success: false and the new
outcomeUncertain flag. 3958 after a decoupled approval, 3040 alone and a
TAN answer without acceptance are not reported as success, and after 3070
(processed under a reference) no confirmation is offered.
After a match with 3076 and without 3091 or 3945 the bank holds the order
and needs no TAN (DK VoP 1.02 E.8.1, figure 3): confirmTransfer() now sends
HKVPA alone instead of HKVPA, HKCCS and HKTAN, which would submit the order
a second time.
…result

A Verification of Payee result naming another IBAN, or a pain.002 repeating
another payee name, stops the transfer. A result that comes with a later
answer, such as a fresh RVNM with 3945 at the TAN step, replaces the earlier
one, and its VoP ID, before any continuation is offered.
The polling ID is optional in HIVPP (DK VoP 1.02 C.10.7.1.1): a 3040 with a
touchdown point now continues polling without one. The touchdown point is
taken from each answer anew instead of carrying an old one forward.
Read the group and transaction status at their own level, so that a
transaction RVMC below the aggregate RVCM is a close match. Take the
status from an unambiguous single-transaction NbOfTxsPerSts when the
report lists no transaction, and stop when the counts or the aggregate
alone do not name one result or contradict it. Keep the transaction
details of an earlier delivery when a later one gives only counts.
Join a report the bank cuts across HIVPP answers in wire order, keeping
complete replacement reports apart. Join a payee name continued after
105 characters, keeping its boundary spaces and dropping the leading
quote mark; read StsRsnInf/Rsn/Cd for RVNA; decode numeric character
references.
Split segments and data elements from left to right, skipping an escaped character and binary data as a whole, instead of counting the question marks before a separator, which took a ? at the end of binary data for an escape.
transfer() and the other steps poll HKVPP until the Verification of Payee result is there, and returned only the answers to the last poll, hiding codes such as 3040 and 3905 from the earlier ones.
…r the caller

Dialog.continueWith() accepted a dialog whose interaction waited for a TAN: it advanced past that interaction and sent the next one. It now throws before sending unless the current interaction's last response paused the dialog for the caller (awaitsCaller).
…ks its parameters

An earlier release saved the version of HIVPPS without its content. With that state canTransfer() was false and synchronize() sent the cached BPD version, so the bank did not send the BPD again. A synchronization now asks for BPD version 0 in that case and keeps the system ID; canTransfer() stays false until the parameters are there.
A transfer response carried bankingInformationUpdated of the transfer interaction alone, so an update in the dialog initialisation, such as new bank messages (HIKIM), new BPD or UPD, was reported as false and not persisted.
Upper-casing turned non-ASCII letters into ASCII ones (GB82WEſT… became the checksum-valid GB82WEST…), and an IBAN with a digit too many for its country passed when its check digits happened to fit (DE890370400440532013000). Only ASCII letters, digits and spaces are accepted now, and the length is checked against a table of the SEPA countries before the checksum.
pain.001.001.03 restricts the location code (BIC); pain.001.001.09 allows digits in the first four characters (BICFI). One pattern for both accepted TESTDE0O for .03, whose XSD then rejected the document, and refused BICFI values .09 allows. Like the IBAN, the BIC is now checked for ASCII letters and digits before upper-casing.
…alue

The errors of normalizeIban and normalizeBic repeated the whole IBAN or BIC, and the character check listed the characters it rejected. They now name the field, and the character check the positions.
The example handled the decision once and then only TANs, so a TAN for
the login followed by a mismatch, or a 3945 in answer to the TAN, ended
with "The bank accepted the transfer". It now loops for as long as either
flag is set and reports acceptance only after that; it shows the notice
lines of the pain.002 and the HIVPP notice before asking. A test runs the
README's code against both cases.

Name the real boundary instead of "the only method that can move money":
transfer() can be final on its first call, confirmTransfer() and
transferWithTan() can authorise a pending payment. Say that the default
endToEndId is the shared NOTPROVIDED, that a reused reference does not
prevent a second payment, and that finding a transfer may need the
account, amount, payee and date.
…e root

List the root exports of pain001Builder and pain002Parser, so that normalizeIban, normalizeBic and selectPain001Format stay internal. Replace the client-level table of invalid amounts and characters, which the builder's tests cover, with one test that invalid input sends no message.
Every answer of a transfer is recorded by message and by the segment it
refers to (payment, check, authorisation, TAN, message), and one classifier
decides the outcome in a fixed order: explicit execution wins and closes
the dialog (uncertain if the same answer contradicts it); a rejection of
the payment itself without a receipt means rejected; once the bank has
received the payment (0010, 3070, a held order) any later failure,
timeout or 9000 means outcomeUncertain; a receipt alone is never an
execution; only then a decision, a TAN or a poll.

This replaces the scattered checks for 0020, 0010 and 3070 and fixes an
executed order offered for confirmation next to 3945 (R2), a receipt next
to a failed check at the TAN (R4), a failed poll or timeout after a
receipt reported as rejection (R6), and outcomes attributed to the wrong
step (R7). The README no longer promises that a timeout leaves the
transfer unexecuted.
A fresh Verification of Payee result replaces the result, VoP ID, notice
and flow codes (3076/3091/3945) at once, and a fresh deviation asks for
the caller's decision again. A result counts only when its counts,
OrgnlEndToEndId, OrgnlMsgId and creditor agree with the order, also for
details lent from an earlier delivery. White space after </Document>
belongs to the completed report, and a poll goes out only with a current
touchdown point. A match whose result came by polling after 3076 is
authorised with HKVPA alone.
transfer() copies the payment fields and the account reference before
the login, so a caller changing its objects meanwhile cannot change the
order. The TAN reference a transfer waits for, also for a login TAN, is
kept apart from the response the caller holds, so changing that response
lets no other reference or step through.
A transfer whose check or order needs HKTAN is refused before HKCCS goes
out when no TAN method of version 6 or later is selected (canTransfer()
is false then). With UPD usage 1 an account that does not list HKCCS is
left to the bank. The update of the banking information is reported also
when only closing an executed transfer failed. A synchronization asks for
the whole BPD again only for an HIVPPS version this library reads, and
SepaValidationError no longer repeats an unsupported descriptor.
…message

TransferEvidence now keeps every message of the transfer by number with what
each segment is, and reads each answer against the message its HNHBK
references. Only answers to the payment count for it: a poll and the HKTAN in
it are read-only, and 0020 on HKVPA with a failed approval is a contradiction.

Every outgoing message passes the evidence first (CustomerInteraction
.beforeSend, also for the resend of a parted answer). It refuses a second
HKCCS unless it is the DK continuation (HKVPA + HKCCS) the caller confirmed,
once, while nothing the bank has can execute; the transfer then ends
uncertain. Evidence is recorded before the interaction branches on errors,
TAN requests or polling.

A duplicate (9390) after a receipt, 9210 to HKVPA, a failed status query
while an approval is open and a lost answer after a receipt now end the
transfer uncertain instead of "not executed" or a bare throw. 0030 on the
payment counts as received.

The transfer test fixtures move to src/tests/creditTransferBank.ts.
The order keeps a private copy of the material fields (VoP ID, result,
code, IBAN, suggested name, reason, notice, reason lines) of the result it
hands the caller with a confirmation or a TAN request. A later result that
differs in any of them ends the transfer, not executed unless the bank may
still execute what it has, instead of asking again; so neither a changed
returned object nor a late confirmTransfer() can apply a decision to another
result. The response carries a copy of the result.
Each pain.002 of a check must be about the order before any of it is read:
the original message and payment information IDs, at most one payment block
and one transaction, transaction counts of one and control sums equal to the
amount, where the report gives them, and the end-to-end ID, IBAN and payee
name. A count of zero for the status claimed and a pending group or block
status next to a final result are contradictions. The reasons of every
delivery of a check now reach the result, not only those of the latest one
and the one that details the transaction.

parsePain002 reads OrgnlNbOfTxs, OrgnlCtrlSum and the payment blocks'
OrgnlPmtInfId; Pain001Message carries paymentInfoId and controlSum.
…whole

- transferWithTan() and confirmTransfer() throw before anything is sent,
  with the transfer still pending, when no TAN method of version 6 or later
  is selected or, for a TAN, when it is not the method the challenge was
  issued for; HKVPA no longer goes out without its HKTAN.
- A TAN request without the reference of its challenge is unreadable when
  it arrives; the evidence so far decides the outcome, and no response
  offers a TAN step with an empty reference.
- The payee check polls only with the touchdown point of a 3040 to HKVPP.
- transfer() reads each payment field explicitly, so a getter counts.
- The README states the guard, the decision binding, the TAN method check
  and the uncertainty after a lost answer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant