bank: run Nostro's callback reader beside ours, as a shadow
All checks were successful
Deploy / build-deploy (push) Successful in 7m33s

ParseBunqCallback is to be replaced by ReadCallback of Nostro.Money
(Nostro 1.1.0), so that this repo stops carrying bunq's wire format.
That reader is stricter than ours: it wants a time, a counterparty and
the account's own IBAN where ours reads four members, and nobody has
run it on a callback bunq really sent. Ours has settled real payments
since 2026-08. So before anything is swapped, both run.

- The handler is untouched and still decides alone. Once it has
  answered and written its credit, Nostro's reader reads the same body
  and one line goes to the log: `bank callback shadow: AGREE ...` when
  the id, the amount and the reference are the same in both readings,
  and `DIFFER ...` with what parted them otherwise.
- The line carries no payment data: no id, amount, name or reference,
  only whether values match and a few fixed words. A body holds the
  payer's name and IBAN, and may repeat the secret address.
- It is on wherever BUNQ_WEBHOOK_PATH is set, with no setting of its
  own.
- project.cpp takes Nostro with --product=money --banks=bunq, at the
  commit of its 1.1.0 release: the module alone, which brings no
  credential and no Crafter.Network, Crafter.DB or OpenSSL of its own.

ShouldCompareCallbackReaders pins every verdict, and that none repeats
anything a body carried. ShouldReceiveBankCallbacks posts callbacks to
the real endpoint: an order settles from one, a payment Nostro's reader
refuses is credited all the same, and the server's log holds nothing of
what was posted.

Built 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 the same way without this change
where the post media is not mirrored. The binary needs the same shared
libraries as before.

Temporary: once real payments read AGREE the reader takes over, and
this goes together with ParseBunqCallback. deploy/README.md says how to
read the result.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Jorijn van der Graaf 2026-11-25 14:21:55 +01:00
commit 36a635fd48
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
8 changed files with 906 additions and 5 deletions

View file

@ -0,0 +1,269 @@
/*
catcrafts.net
Copyright (C) 2026 Catcrafts
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 callback shadow's verdict: what Nostro's reader makes of a bank callback
// beside what our own parser made of it (ShadowCallbackVerdict). Temporary,
// like the shadow itself.
//
// Three things are held here, and the third is the one that must not regress:
//
// * AGREE is said when, and only when, the id, the amount and the reference
// are the same in both readings. Those are what settlement rests on.
// * Each way the two can part is said in words that tell them apart: Nostro
// refusing a body ours reads, reading none where ours does, reading one
// where ours does not, and reading another value.
// * NOTHING OF THE BODY COMES OUT. The verdict goes to the server's log, and
// a callback holds the payer's name and IBAN and may repeat the secret
// address it was posted to. Every verdict this suite produces is searched
// for the values its bodies carry.
//
// The bodies are made up. Their shape is bunq's own sample of a MUTATION
// callback (bunq/sdk_python, tests/assets/NotificationUrlJsons/Mutation.json),
// which is also what Nostro's reader was written against. Whether bunq still
// writes one like it is exactly what the shadow is in production to find out,
// and no suite can.
import std;
import Catcrafts.Shared;
import Catcrafts.Server;
using namespace Catcrafts;
namespace {
int failures = 0;
void Check(bool ok, std::string_view what, std::string_view got = {}) {
if (ok) return;
++failures;
std::println(std::cerr, "FAIL: {}{}{}", what,
got.empty() ? "" : " got: ", got);
}
// Not real accounts, and not the shop's: see ShouldSettleBankTransfers.
constexpr std::string_view kOwnIban = "NL00TEST0123456789";
constexpr std::string_view kOtherIban = "NL00OTHR0000000002";
// Every value below that a verdict must never repeat.
constexpr std::string_view kSecrets[] = {
"SECRETMARK", "CC-2B6457", "4155999", "570", "24.95", "NL00", "Payer",
"shop.invalid", "bad type",
};
struct Sent {
std::string_view id = "4155999";
std::string_view type = "EBA_SCT";
std::string_view description = "CC-2B6457";
std::string_view value = "570.43";
std::string_view account = kOwnIban; // 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.
std::string Callback(const Sent& s) {
return std::format(R"({{
"NotificationUrl": {{
"target_url": "https://shop.invalid/hooks/SECRETMARK",
"category": "MUTATION",
"event_type": "MUTATION_CREATED",
"object": {{"Payment": {{
"id": {},
"created": "2026-11-25 10:15:30.498520",
"updated": "2026-11-25 10:15:30.498520",
"monetary_account_id": 42,
"amount": {{"currency": "EUR", "value": "{}"}},
"description": "{}",
"type": "{}",
"alias": {{"iban": "{}", "display_name": "SECRETMARK Shop", "country": "NL"}},
"counterparty_alias": {{"iban": "NL00PAYR0000000001",
"display_name": "SECRETMARK Payer", "country": "NL"}}
}}}}
}}
}})", s.id, s.value, s.description, s.type, s.account);
}
// The four members our own parser reads, and nothing else: the shape this
// repo's suites have always used for a callback, which Nostro's reader
// refuses for what it lacks.
constexpr std::string_view kReduced = R"({
"NotificationUrl": {
"category": "MUTATION",
"object": {"Payment": {
"id": 4155999,
"type": "EBA_SCT",
"description": "CC-2B6457",
"amount": {"currency": "EUR", "value": "570.43"}
}}
}
})";
std::vector<std::string> verdicts;
// The verdict on `body`, with our own parser's reading of the same body, as
// the server calls it.
std::string Verdict(std::string_view body, std::string_view ownIban = kOwnIban,
std::string_view bank = "bunq") {
const std::optional<Server::BankCredit> ours = Server::ParseBunqCallback(body);
verdicts.push_back(Server::ShadowCallbackVerdict(bank, body, ours, ownIban));
return verdicts.back();
}
void Equal(const std::string& got, std::string_view want, std::string_view what) {
Check(got == want, what, got);
}
void Has(const std::string& got, std::string_view part, std::string_view what) {
Check(got.find(part) != std::string::npos, what, got);
}
} // namespace
int main() {
// ── the two readings agree ────────────────────────────────────────
Equal(Verdict(Callback({})),
"AGREE in method=sepa account=ours event=transactions",
"an incoming transfer on the shop's account reads the same both ways");
Equal(Verdict(Callback({}), "nl00 test 0123 4567 89"),
"AGREE in method=sepa account=ours event=transactions",
"the shop's IBAN is compared as a person wrote it down");
Equal(Verdict(Callback({ .account = kOtherIban })),
"AGREE in method=sepa account=other event=transactions",
"a payment on another account is said to be on another account");
Equal(Verdict(Callback({}), ""),
"AGREE in method=sepa account=unknown event=transactions",
"with no account named, whose it is on is not guessed");
Equal(Verdict(Callback({ .value = "-24.95" })),
"AGREE out method=sepa account=ours event=transactions",
"money going out keeps its sign in both readings");
// ── the method, which may differ without the readings differing ───
Equal(Verdict(Callback({ .type = "IDEAL" })),
"AGREE in method=ideal account=ours event=transactions", "iDEAL");
Equal(Verdict(Callback({ .type = "BUNQ" })),
"AGREE in method=bunq account=ours event=transactions",
"between two bunq accounts");
Equal(Verdict(Callback({ .type = "MASTERCARD", .value = "-24.95" })),
"AGREE out method ours=MASTERCARD nostro=card account=ours event=transactions",
"a card payment, which ours passes through and Nostro names, is still AGREE");
Equal(Verdict(Callback({ .type = "ADYEN" })),
"AGREE in method=ADYEN account=ours event=transactions",
"a type neither table has is the bank's own word in both");
Equal(Verdict(Callback({ .type = "" })),
"AGREE in method=bank account=ours event=transactions",
"no type at all is 'bank' in both");
{
const std::string v = Verdict(Callback({ .type = "bad type!" }));
Has(v, "method=?", "a type that is no bank's word is not printed");
Has(v, "AGREE in ", "and the readings still agree");
}
// ── Nostro refuses what ours reads ────────────────────────────────
//
// The outcome the shadow is in production to rule out, or to find.
{
const std::string v = Verdict(kReduced);
Has(v, "DIFFER nostro refused what ours reads as a payment: ",
"the four members ours reads are not enough for Nostro's reader");
}
{
const std::string v = Verdict(Callback({ .account = "" }));
Has(v, "DIFFER nostro refused what ours reads as a payment: ",
"a payment that does not say which account it is on is refused");
Has(v, "which account", "and the refusal says that is why");
}
{
// Past what Nostro reads at all, with a payment ours still decodes.
std::string big = Callback({});
const std::size_t at = big.find("\"category\"");
big.insert(at, std::format("\"padding\": \"{}\", ", std::string(70000, 'x')));
const std::string v = Verdict(big);
Has(v, "DIFFER nostro refused what ours reads as a payment: ",
"a body past Nostro's limit is refused");
Check(v.size() < 400, "and the verdict stays one line's worth", v);
}
Has(Verdict(Callback({}), kOwnIban, "nobank"),
"DIFFER nostro refused what ours reads as a payment: ",
"a bank Nostro was not built with reads nothing");
// ── one reads a payment where the other reads none ────────────────
{
// Two things under `object`: ours looks for the Payment among them,
// Nostro takes a callback to be about one thing.
std::string two(kReduced);
const std::size_t at = two.rfind("}}");
two.insert(at + 1, R"(, "Other": {})");
Equal(Verdict(two),
"DIFFER nostro reads no payment in it (event=transactions), ours does",
"a second object beside the payment");
}
{
verdicts.push_back(Server::ShadowCallbackVerdict("bunq", Callback({}),
std::nullopt, kOwnIban));
Equal(verdicts.back(),
"DIFFER nostro reads a payment, ours does not (in account=ours event=transactions)",
"a payment only Nostro reads");
}
// ── both read a payment, and not the same one ─────────────────────
{
const std::string body = Callback({});
const std::optional<Server::BankCredit> ours = Server::ParseBunqCallback(body);
Check(ours.has_value(), "our parser reads the sample's shape");
if (ours) {
Server::BankCredit other = *ours;
other.amountMinor += 1;
verdicts.push_back(Server::ShadowCallbackVerdict("bunq", body, other, kOwnIban));
Equal(verdicts.back(),
"DIFFER id=same amount=DIFFERENT reference=same in method=sepa "
"account=ours event=transactions",
"another amount");
other = *ours;
other.id = "1";
verdicts.push_back(Server::ShadowCallbackVerdict("bunq", body, other, kOwnIban));
Has(verdicts.back(), "DIFFER id=DIFFERENT amount=same reference=same ",
"another id");
other = *ours;
other.reference = "something else";
verdicts.push_back(Server::ShadowCallbackVerdict("bunq", body, other, kOwnIban));
Has(verdicts.back(), "DIFFER id=same amount=same reference=DIFFERENT ",
"another reference");
}
}
// ── neither reads a payment ───────────────────────────────────────
for (const std::string_view none : { std::string_view(""), std::string_view("not json"),
std::string_view("SECRETMARK"),
std::string_view(R"({"SECRETMARK": 1})"),
std::string_view(R"({"NotificationUrl": {}})") }) {
Has(Verdict(none), "AGREE neither reads a payment (nostro: ",
std::format("a body that is no callback: '{}'", none.substr(0, 16)));
}
Equal(Verdict(R"({"NotificationUrl": {"category": "REQUEST",
"event_type": "REQUEST_INQUIRY_CREATED",
"object": {"RequestInquiry": {"id": 7}}}})"),
"AGREE no payment in it (event=bunq:REQUEST)",
"a callback about something that is no payment");
// ── and none of it says anything a body carried ───────────────────
Check(verdicts.size() >= 25, "the verdicts above were all collected",
std::format("{}", verdicts.size()));
for (const std::string& v : verdicts) {
for (const std::string_view secret : kSecrets) {
Check(v.find(secret) == std::string::npos,
std::format("a verdict repeats '{}' from a body", secret), v);
}
Check(v.find('\n') == std::string::npos, "a verdict is one line", v);
}
if (failures != 0) {
std::println(std::cerr, "{} check(s) failed", failures);
return 1;
}
std::println("ShouldCompareCallbackReaders: all checks passed");
return 0;
}

View file

@ -0,0 +1,272 @@
/*
catcrafts.net
Copyright (C) 2026 Catcrafts
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.
//
// 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.
//
// 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.
import std;
import Crafter.Network;
import Catcrafts.E2eHarness;
using namespace Catcrafts::E2e;
namespace fs = std::filesystem;
namespace {
// Not real accounts, and not the shop's: see ShouldSettleBankTransfers.
constexpr std::string_view kIban = "NL00TEST0123456789";
constexpr std::string_view kOtherIban = "NL00OTHR0000000002";
constexpr std::string_view kBeneficiary = "Catcrafts E2E, not a real account";
// The secret address. Long enough for the server to take it for one.
constexpr std::string_view kHook = "/hooks/bank/e2e-HOOKSECRET-0123456789abcdef";
// What a PAID donation page says: see ShouldSettleBankTransfers for why this
// copy, and not a badge, is the signal.
constexpr std::string_view kPaidCopy = "Your donation funds";
constexpr std::string_view kShadow = "bank callback shadow: ";
struct Sent {
std::string_view id;
std::string_view value;
std::string_view description = "PAYERNOTE thanks";
std::string_view type = "EBA_SCT";
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.
std::string Callback(const Sent& s) {
return std::format(R"({{
"NotificationUrl": {{
"target_url": "https://shop.invalid{}",
"category": "MUTATION",
"event_type": "MUTATION_CREATED",
"object": {{"Payment": {{
"id": {},
"created": "2026-11-25 10:15:30.498520",
"updated": "2026-11-25 10:15:30.498520",
"monetary_account_id": 42,
"amount": {{"currency": "EUR", "value": "{}"}},
"description": "{}",
"type": "{}",
"alias": {{"iban": "{}", "display_name": "PAYEENAME", "country": "NL"}},
"counterparty_alias": {{"iban": "NL00PAYR0000000001",
"display_name": "PAYERNAME", "country": "NL"}}
}}}}
}}
}})", 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.
std::string Reduced(std::string_view id, std::string_view value) {
return std::format(R"({{"NotificationUrl": {{"category": "MUTATION",
"object": {{"Payment": {{"id": {}, "type": "EBA_SCT",
"description": "PAYERNOTE reduced",
"amount": {{"currency": "EUR", "value": "{}"}}}}}}}}}})", id, value);
}
std::string TokenOf(const Crafter::HTTPResponse& res) {
const auto loc = res.headers.find("location");
if (loc == res.headers.end()) return {};
const std::string& url = loc->second;
const std::size_t at = url.rfind('/');
return at == std::string::npos ? std::string{} : url.substr(at + 1);
}
// The order reference ("CC-XXXXXX") out of the ledger's newest order event.
std::string NewestReference(const TestServer& srv) {
const std::string text = srv.OrdersText();
const std::string key = "\"ref\":\"";
std::string found;
for (std::size_t at = text.find(key); at != std::string::npos;
at = text.find(key, at + 1)) {
const std::size_t start = at + key.size();
const std::size_t end = text.find('"', start);
if (end == std::string::npos) break;
found = text.substr(start, end - start);
}
return found;
}
} // namespace
int main(int argc, char** argv) {
if (argc < 2) {
std::println(std::cerr, "usage: ShouldReceiveBankCallbacks <server-binary>");
return 2;
}
ServerOptions options;
// The REAL transfer rail in the bank slot, settling from the credits file
// the callback endpoint writes. The crypto slot is off.
options.extraArgs = { "--rail=transfer", "--crypto-rail=off" };
options.env = {
{ "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", "" },
};
TestServer srv(argv[1], 8223, 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;
};
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
}
// ── 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));
// 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));
// ── 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));
// ── 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);
}
}
return Finish();
}