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

@ -82,6 +82,11 @@ std::string gRedirectBase = "https://catcrafts.net";
std::filesystem::path gCreditsPath;
std::string gWebhookPath;
// The callback shadow (temporary, see ShadowCallbackVerdict): off until
// ConfigureCallbackShadow says which account is the shop's own.
bool gCallbackShadow = false;
std::string gShadowIban;
// The bank-derived aggregates for /financials live in Catcrafts.Server-
// Financials.cpp, which owns their file. They are read through
// CurrentFinancials() per request rather than cached: unlike the content
@ -703,6 +708,32 @@ HTTPResponse HandleBunqCallback(const HTTPRequest& req) {
return res;
}
// The shadow run (temporary, see ShadowCallbackVerdict): what Nostro's reader
// makes of the body HandleBunqCallback has just dealt with, logged beside what
// ours made of it.
//
// It is called AFTER the handler has returned its answer and written its
// credit, on purpose. The handler above is not touched by it and cannot be
// changed by it: the body is parsed a second time here rather than handed
// over, so that the code which settles money stays exactly the code that has
// been settling it. If this ever took the process down, the credit would be on
// disk already, and the bank's retry of a callback it got no answer to is
// refused as a duplicate by the id.
void ShadowBunqCallback(const HTTPRequest& req) {
if (!gCallbackShadow) return;
try {
const std::optional<BankCredit> ours = ParseBunqCallback(req.body);
std::println(std::cerr, "bank callback shadow: {}",
ShadowCallbackVerdict("bunq", req.body, ours, gShadowIban));
} catch (...) {
// Nothing here may fail a callback that was already answered.
try {
std::println(std::cerr, "bank callback shadow: FAILED before a verdict");
} catch (...) {
}
}
}
HTTPResponse HandleCheckout(const HTTPRequest& req, const Route& route) {
HTTPResponse res;
@ -1153,6 +1184,17 @@ void ConfigureBunqCallback(std::filesystem::path creditsPath, std::string secret
gWebhookPath.size());
}
void ConfigureCallbackShadow(std::string ownIban) {
gShadowIban = std::move(ownIban);
gCallbackShadow = true;
// The account is not printed: whether one was named is what a reader of
// the log needs, to know what "account=unknown" on a verdict means.
std::println(std::cerr,
"bank callback shadow: on, Nostro's reader runs beside ours and "
"decides nothing (own account {})",
gShadowIban.empty() ? "not named" : "named");
}
bool CryptoPaymentAvailable() { return gRails.crypto != nullptr; }
bool BankPaymentAvailable() { return gRails.bank != nullptr; }
@ -1391,7 +1433,14 @@ int Serve(std::uint16_t port) {
for (std::size_t i = 0; i < path.size(); ++i) {
diff |= static_cast<unsigned char>(path[i] ^ gWebhookPath[i]);
}
if (diff == 0) return HandleBunqCallback(req);
if (diff == 0) {
// Ours decides, and has finished deciding, before the
// shadow looks at the same body. The answer is already
// made and is returned as it is.
HTTPResponse answer = HandleBunqCallback(req);
ShadowBunqCallback(req);
return answer;
}
}
}
// A POST to a product page is a checkout submission.