bank: Nostro reads the bank's callback, and the shop's bunq code is gone
All checks were successful
Deploy / build-deploy (push) Successful in 7m20s

The shadow run of 36a635f had Nostro's ReadCallback read a real
callback beside our own parser on 2026-11-25, for order CC-BCD155: the
same id, amount and reference, on the shop's own account. So the reader
takes over, and what this repo carried of bunq goes: the callback
parser, the payment-type table, the API client with its RSA signing,
`--pull-credits`, the mode that read the bank from the server with an
API key, and tools/register-bunq-webhook.sh.

The server now knows no bank. Catcrafts.Server-BankCallback.cpp is the
one unit that imports Nostro.Money: ReadBankCallback has a body read
and says what it comes to, CreditMethodFor says how a payment arrived.
Which bank it is is configuration: --banks= in project.cpp for what the
build can read, BANK_CALLBACK_BANK on the server for what arrives.

What changes for a running shop:

- The endpoint is configured with BANK_CALLBACK_PATH and
  BANK_CALLBACK_BANK; BUNQ_WEBHOOK_PATH is no longer read. Without the
  bank's name, or without TRANSFER_IBAN, the endpoint stays off and the
  startup log says so: one that answered 200 and credited nothing would
  look like working. Production's payments.env got both names before
  this was pushed.
- Only a payment on TRANSFER_IBAN is credited. A bank posts to whatever
  address any of its customers registered, so a callback about someone
  else's account can be the bank's own. Until now a payment in a
  callback was credited whichever account it was on.
- `via` says `internal` for a payment from another account at the bank
  itself, where it said `bunq`, and `card` where it would have said
  `MASTERCARD`. A kind without a word is still passed on as the bank
  wrote it, now only where that is a plain word.
- Every callback leaves one line in the log, `bank callback: credited
  ...` or `bank callback: nothing credited: <why>`, with nothing of the
  body in it beyond the credit.
- A line of the credits file may carry the bank's kind and the bank
  (`"bank":"bunq","kind":"EBA_SCT/SCT"`) in place of `method`, put into
  the ledger's words when the file is read. The pull script ships those
  and takes the bank's name from nostrod, so it holds no table of a
  bank's words either.

Also gone, with their last user: ParseAmountToMinor, PullCreditsInto
and the explicit -lcrypto (Crafter.Network names what it needs).

ShouldReadBankCallbacks replaces ShouldParseBunqPayments and
ShouldCompareCallbackReaders: what a callback comes to for each shape,
each method, the account and the sign, and that no note repeats
anything a body carried. ShouldReceiveBankCallbacks posts to the real
endpoint: an order settles from a callback, one from another account
quoting the same order does not pay for it, and a server without the
bank's name has no endpoint.

Built from scratch and run in a copy of this tree with crafter-build
6.4.1: the server and the wasm bundle build, and every suite passes but
ShouldServePostPages, which fails there for want of mirrored post media
and passed in CI on the commit before this. A dry run of the pull
script against nostrod gave 19 credits with their kinds.

Not tried: a real callback through the new handler, and `nostro
callbacks` against the registration that is in place, which was made
with the script this removes. deploy/README.md has the new setup.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Jorijn van der Graaf 2026-11-25 15:08:42 +01:00
commit 33ce8f45d8
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
24 changed files with 1218 additions and 2385 deletions

View file

@ -6,27 +6,30 @@ The source code of this website is made available for viewing purposes only.
No permission is granted to copy, modify, distribute, or create derivative works.
*/
// The bank's callback endpoint, over real HTTP, with the shadow run of
// Nostro's reader attached to it (see ShadowCallbackVerdict in the server's
// interface). The endpoint is how a transfer settles within seconds in
// production, and until this suite it was only ever exercised there.
// The bank's callback endpoint, over real HTTP. It is how a transfer settles
// within seconds in production, and the one place where bytes a stranger can
// send become a line in the file orders are settled from.
//
// What it holds, in the order that matters:
//
// * OUR PARSER STILL DECIDES, ALONE. A payment it reads is credited whatever
// Nostro's reader makes of the same body, including when that reader
// refuses it. The shadow is in production to find out whether it would
// refuse real callbacks, so the one thing it must not be able to do is
// turn a refusal into a missed payment.
// * An order settles from a callback, end to end: the body the bank posts,
// the credit, the reconciler, the paid page.
// * The shadow says what it found, once per callback, in the server's log.
// * That log says nothing a callback carried: not the payer, not the
// reference, and not the address, which is the endpoint's only secret.
// * ONLY a payment to the shop's own account credits anything. A payment on
// another account, money going out, and a body the reader refuses each get
// the bank its 200 and credit nothing.
// * A payment is credited once, however often the bank sends it.
// * Every callback leaves one line in the server's log, and the log says
// nothing a callback carried beyond the credit itself: not the payer, not
// the reference, and not the address, which is the endpoint's only secret.
// * Without the bank's name the endpoint does not exist at all, and says so
// at startup: an endpoint that answered 200 and credited nothing would
// look like working.
//
// No secret and no network: the suite plays the bank by posting to the
// endpoint, as ShouldSettleBankTransfers plays it by writing the credits file.
// The bodies are made up, in the shape of bunq's own sample of a callback.
// What the reader makes of each shape is pinned in ShouldReadBankCallbacks;
// what only this suite can show is that the endpoint is wired to it.
import std;
import Crafter.Network;
@ -49,7 +52,7 @@ constexpr std::string_view kHook = "/hooks/bank/e2e-HOOKSECRET-0123456789abcdef"
// copy, and not a badge, is the signal.
constexpr std::string_view kPaidCopy = "Your donation funds";
constexpr std::string_view kShadow = "bank callback shadow: ";
constexpr std::string_view kLine = "bank callback: ";
struct Sent {
std::string_view id;
@ -59,8 +62,8 @@ struct Sent {
std::string_view account = kIban; // the payment's own side
};
// A callback as bunq's sample has one: the envelope with the address it was
// sent to, and a Payment with both sides on it.
// A callback as the bank's sample has one: the envelope with the address it
// was sent to, and a Payment with both sides on it.
std::string Callback(const Sent& s) {
return std::format(R"({{
"NotificationUrl": {{
@ -83,8 +86,8 @@ std::string Callback(const Sent& s) {
}})", kHook, s.id, s.value, s.description, s.type, s.account);
}
// The four members our own parser reads and nothing else, which Nostro's
// reader refuses for what it lacks.
// A payment with only a few of its members: no time, no counterparty, and not
// the account it is on. The reader refuses it whole.
std::string Reduced(std::string_view id, std::string_view value) {
return std::format(R"({{"NotificationUrl": {{"category": "MUTATION",
"object": {{"Payment": {{"id": {}, "type": "EBA_SCT",
@ -115,14 +118,7 @@ std::string NewestReference(const TestServer& srv) {
return found;
}
} // namespace
int main(int argc, char** argv) {
if (argc < 2) {
std::println(std::cerr, "usage: ShouldReceiveBankCallbacks <server-binary>");
return 2;
}
ServerOptions Options(std::string_view bank) {
ServerOptions options;
// The REAL transfer rail in the bank slot, settling from the credits file
// the callback endpoint writes. The crypto slot is off.
@ -131,141 +127,171 @@ int main(int argc, char** argv) {
{ "TRANSFER_IBAN", std::string(kIban) },
{ "TRANSFER_BENEFICIARY", std::string(kBeneficiary) },
{ "TRANSFER_POLL_SECONDS", "1" },
{ "BUNQ_WEBHOOK_PATH", std::string(kHook) },
// A developer's shell may carry the real key, and with one the rail
// would read the real bank instead of the credits file. Never here.
{ "BUNQ_API_KEY", "" },
{ "BANK_CALLBACK_PATH", std::string(kHook) },
{ "BANK_CALLBACK_BANK", std::string(bank) },
};
TestServer srv(argv[1], 8223, options);
return options;
}
const fs::path credits(srv.Orders().string() + ".transfer-credits.jsonl");
const fs::path log = srv.Work() / "server.log";
const auto Credited = [&](std::string_view id) {
return CountOccurrences(ReadFile(credits), std::format("\"id\":\"{}\"", id));
};
const auto Logged = [&](std::string_view line) {
return CountOccurrences(ReadFile(log), line);
};
const auto Send = [&](std::string body) {
return srv.Post(std::string(kHook), std::move(body), "application/json").status;
};
} // namespace
Check(Logged("bank callback shadow: on") == 1 && Logged("own account named") == 1,
"the shadow says at startup that it is on, and that it knows the account",
ReadFile(log));
// ── the endpoint is the secret address, and only for a POST ───────
srv.CheckStatus(std::string(kHook), "404");
{
std::string near(kHook);
near.back() = near.back() == 'f' ? 'e' : 'f';
const std::string status =
srv.Post(near, Callback({ .id = "9000099", .value = "5.00" }),
"application/json").status;
Check(status != "200", "an address one character off is not the endpoint", status);
Check(Credited("9000099") == 0, "and what is posted there credits nothing");
Check(Logged(kShadow) == 1, "nor does the shadow look at it"); // the startup line
int main(int argc, char** argv) {
if (argc < 2) {
std::println(std::cerr, "usage: ShouldReceiveBankCallbacks <server-binary>");
return 2;
}
// ── a payment both readers read ───────────────────────────────────
Check(Send(Callback({ .id = "9000001", .value = "5.00" })) == "200",
"a callback is answered 200");
Check(Credited("9000001") == 1, "and its payment is credited", ReadFile(credits));
{
const std::string text = ReadFile(credits);
Check(text.find("\"amount_minor\":500") != std::string::npos
&& text.find("\"method\":\"sepa\"") != std::string::npos,
"with its amount and how it arrived", text);
}
Check(Logged("bank callback shadow: AGREE in method=sepa account=ours "
"event=transactions") == 1,
"the shadow says Nostro's reader read the same payment", ReadFile(log));
TestServer srv(argv[1], 8223, Options("bunq"));
// The bank retries, and a retry must not count twice. The shadow looks at
// each one: it compares readings, it keeps no books.
Check(Send(Callback({ .id = "9000001", .value = "5.00" })) == "200",
"the same callback again is answered 200 as well");
Check(Credited("9000001") == 1, "and is credited once", ReadFile(credits));
Check(Logged("bank callback shadow: AGREE in method=sepa account=ours "
"event=transactions") == 2,
"the shadow compares the retry too", ReadFile(log));
const fs::path credits(srv.Orders().string() + ".transfer-credits.jsonl");
const fs::path log = srv.Work() / "server.log";
const auto Credited = [&](std::string_view id) {
return CountOccurrences(ReadFile(credits), std::format("\"id\":\"{}\"", id));
};
const auto Logged = [&](std::string_view line) {
return CountOccurrences(ReadFile(log), line);
};
const auto Send = [&](std::string body) {
return srv.Post(std::string(kHook), std::move(body), "application/json").status;
};
// ── a payment Nostro's reader refuses: ours decides, alone ────────
Check(Send(Reduced("9000002", "3.00")) == "200",
"a callback Nostro's reader refuses is answered 200");
Check(Credited("9000002") == 1,
"and is CREDITED all the same: the shadow has no say", ReadFile(credits));
Check(Logged("bank callback shadow: DIFFER nostro refused what ours reads "
"as a payment: ") == 1,
"the shadow says that Nostro's reader would have refused it", ReadFile(log));
Check(Logged("bank callback: enabled on a secret path") == 1,
"the endpoint says at startup that it is on", ReadFile(log));
// ── money going out ───────────────────────────────────────────────
Check(Send(Callback({ .id = "9000003", .value = "-24.95", .type = "MASTERCARD" }))
== "200",
"a callback about an outgoing card payment is answered 200");
Check(Credited("9000003") == 0, "and credits nothing", ReadFile(credits));
Check(Logged("bank callback shadow: AGREE out method ours=MASTERCARD nostro=card "
"account=ours event=transactions") == 1,
"the shadow reads it the same, and names the method each reader gives",
ReadFile(log));
// ── a payment on another account ──────────────────────────────────
//
// Only the shadow's word is held here. Our parser never looked at which
// account a payment is on, and what the endpoint does with one on another
// account is for the change that replaces the parser to decide and pin.
Check(Send(Callback({ .id = "9000004", .value = "5.00", .account = kOtherIban }))
== "200",
"a callback about another account is answered 200");
Check(Logged("bank callback shadow: AGREE in method=sepa account=other "
"event=transactions") == 1,
"the shadow says the payment is not on the shop's account", ReadFile(log));
// ── what is no callback ───────────────────────────────────────────
Check(Send("not a callback PAYERNOTE") == "200",
"a body that is no callback is answered 200, so the bank stops retrying");
Check(Logged("bank callback shadow: AGREE neither reads a payment") == 1,
"and neither reader reads a payment in it", ReadFile(log));
// ── an order settles from a callback, end to end ──────────────────
{
const auto res = srv.Post("/shop/donation", "amount=7.00");
Check(res.status == "303", "a donation on the transfer rail is accepted",
res.status);
const std::string token = TokenOf(res);
const std::string reference = NewestReference(srv);
Check(!token.empty() && !reference.empty(), "and has a page and a reference");
if (token.empty() || reference.empty()) return Finish();
const std::string path = "/order/" + token;
srv.BodyHas(path, "awaiting payment", "the order starts out unpaid");
const std::string note = std::format("betaling {} bedankt", reference);
Check(Send(Callback({ .id = "9000010", .value = "7.00", .description = note }))
== "200",
"the bank's callback for it is answered 200");
const std::string body = srv.WaitForBody(path, kPaidCopy, 120);
Check(body.find(kPaidCopy) != std::string::npos,
"and the order is paid, from the callback alone");
Check(Logged("bank callback shadow: AGREE in method=sepa account=ours "
"event=transactions") == 3,
"with the shadow agreeing on that payment too", ReadFile(log));
}
// ── one verdict a callback, and nothing a callback carried ────────
{
const std::string text = ReadFile(log);
// Seven callbacks reached the endpoint, and the startup line makes eight.
Check(CountOccurrences(text, kShadow) == 8,
"the shadow spoke once for each callback", text);
for (const std::string_view secret :
{ kHook, std::string_view("HOOKSECRET"), std::string_view("PAYERNAME"),
std::string_view("PAYEENAME"), std::string_view("PAYERNOTE"),
std::string_view("NL00PAYR"), kIban, kOtherIban,
std::string_view("shop.invalid") }) {
Check(text.find(secret) == std::string::npos,
std::format("the server's log repeats '{}' from a callback", secret),
text);
// ── the endpoint is the secret address, and only for a POST ───
srv.CheckStatus(std::string(kHook), "404");
{
std::string near(kHook);
near.back() = near.back() == 'f' ? 'e' : 'f';
const std::string status =
srv.Post(near, Callback({ .id = "9000099", .value = "5.00" }),
"application/json").status;
Check(status != "200", "an address one character off is not the endpoint",
status);
Check(Credited("9000099") == 0, "and what is posted there credits nothing");
Check(Logged(kLine) == 1, "nor is it read at all"); // the startup line
}
// ── a payment to the shop ─────────────────────────────────────
Check(Send(Callback({ .id = "9000001", .value = "5.00" })) == "200",
"a callback is answered 200");
Check(Credited("9000001") == 1, "and its payment is credited", ReadFile(credits));
{
const std::string text = ReadFile(credits);
Check(text.find("\"amount_minor\":500") != std::string::npos
&& text.find("\"method\":\"sepa\"") != std::string::npos,
"with its amount and how it arrived", text);
}
Check(Logged("bank callback: credited 5.00 via sepa (id 9000001)") == 1,
"the log says what was credited", ReadFile(log));
// The bank retries, and a retry must not count twice.
Check(Send(Callback({ .id = "9000001", .value = "5.00" })) == "200",
"the same callback again is answered 200 as well");
Check(Credited("9000001") == 1, "and is credited once", ReadFile(credits));
Check(Logged("bank callback: nothing credited: the payment was credited before") == 1,
"the log says the retry added nothing", ReadFile(log));
// ── a payment on another account ──────────────────────────────
//
// The bank's own callback, from the bank's own addresses, about an
// account that is not the shop's: what someone who registered this
// address for an account of their own, and paid themselves, would
// have the bank send.
Check(Send(Callback({ .id = "9000004", .value = "5.00", .account = kOtherIban }))
== "200",
"a callback about another account is answered 200");
Check(Credited("9000004") == 0, "and credits NOTHING", ReadFile(credits));
Check(Logged("bank callback: nothing credited: the payment is on another account "
"than the shop's") == 1,
"the log says why", ReadFile(log));
// ── money going out ───────────────────────────────────────────
Check(Send(Callback({ .id = "9000003", .value = "-24.95", .type = "MASTERCARD" }))
== "200",
"a callback about an outgoing card payment is answered 200");
Check(Credited("9000003") == 0, "and credits nothing", ReadFile(credits));
Check(Logged("bank callback: nothing credited: the payment is not money coming in")
== 1,
"the log says why", ReadFile(log));
// ── what the reader refuses ───────────────────────────────────
Check(Send(Reduced("9000002", "3.00")) == "200",
"a payment without most of its members is answered 200");
Check(Credited("9000002") == 0, "and credits nothing", ReadFile(credits));
Check(Send("not a callback PAYERNOTE") == "200",
"a body that is no callback is answered 200, so the bank stops retrying");
Check(Logged("bank callback: nothing credited: the body was refused: ") == 2,
"the log says both were refused", ReadFile(log));
// ── an order settles from a callback, end to end ──────────────
{
const auto res = srv.Post("/shop/donation", "amount=7.00");
Check(res.status == "303", "a donation on the transfer rail is accepted",
res.status);
const std::string token = TokenOf(res);
const std::string reference = NewestReference(srv);
Check(!token.empty() && !reference.empty(), "and has a page and a reference");
if (token.empty() || reference.empty()) return Finish();
const std::string path = "/order/" + token;
srv.BodyHas(path, "awaiting payment", "the order starts out unpaid");
// The same payment on someone else's account must not pay for it.
const std::string note = std::format("betaling {} bedankt", reference);
Check(Send(Callback({ .id = "9000011", .value = "7.00", .description = note,
.account = kOtherIban })) == "200",
"a callback quoting the order from another account is answered 200");
std::this_thread::sleep_for(std::chrono::seconds(3));
srv.BodyHas(path, "awaiting payment",
"and the order is still unpaid after the reconciler has run");
Check(Send(Callback({ .id = "9000010", .value = "7.00", .description = note }))
== "200",
"the bank's callback for the payment to the shop is answered 200");
const std::string body = srv.WaitForBody(path, kPaidCopy, 120);
Check(body.find(kPaidCopy) != std::string::npos,
"and the order is paid, from the callback alone");
}
// ── one line a callback, and nothing a callback carried ───────
{
const std::string text = ReadFile(log);
// Eight callbacks reached the endpoint, and the startup line makes nine.
Check(CountOccurrences(text, kLine) == 9,
"the log has one line for each callback", text);
for (const std::string_view secret :
{ kHook, std::string_view("HOOKSECRET"), std::string_view("PAYERNAME"),
std::string_view("PAYEENAME"), std::string_view("PAYERNOTE"),
std::string_view("betaling"), std::string_view("NL00PAYR"), kIban,
kOtherIban, std::string_view("shop.invalid") }) {
Check(text.find(secret) == std::string::npos,
std::format("the server's log repeats '{}' from a callback", secret),
text);
}
}
}
// ── without the bank's name there is no endpoint ──────────────────
//
// The state a server is in when its configuration names the address and
// not whose callbacks arrive there. It must not answer the bank 200 and
// drop the payment: the bank would take that for delivered, and nothing
// anywhere would say that no order can settle from a callback.
{
TestServer srv(argv[1], 8224, Options(""));
const fs::path credits(srv.Orders().string() + ".transfer-credits.jsonl");
const std::string logText = ReadFile(srv.Work() / "server.log");
Check(logText.find("BANK_CALLBACK_BANK") != std::string::npos
&& logText.find("the endpoint is NOT enabled") != std::string::npos,
"a server not told whose callbacks arrive says the endpoint is off",
logText);
const std::string status =
srv.Post(std::string(kHook), Callback({ .id = "9000020", .value = "5.00" }),
"application/json").status;
Check(status != "200", "and does not answer a callback as if it took it", status);
Check(ReadFile(credits).find("9000020") == std::string::npos,
"nor credit what it carried");
}
return Finish();