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>
298 lines
14 KiB
C++
298 lines
14 KiB
C++
/*
|
|
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. 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:
|
|
//
|
|
// * An order settles from a callback, end to end: the body the bank posts,
|
|
// the credit, the reconciler, the paid page.
|
|
// * 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;
|
|
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 kLine = "bank callback: ";
|
|
|
|
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 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": {{
|
|
"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);
|
|
}
|
|
|
|
// 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",
|
|
"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;
|
|
}
|
|
|
|
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.
|
|
options.extraArgs = { "--rail=transfer", "--crypto-rail=off" };
|
|
options.env = {
|
|
{ "TRANSFER_IBAN", std::string(kIban) },
|
|
{ "TRANSFER_BENEFICIARY", std::string(kBeneficiary) },
|
|
{ "TRANSFER_POLL_SECONDS", "1" },
|
|
{ "BANK_CALLBACK_PATH", std::string(kHook) },
|
|
{ "BANK_CALLBACK_BANK", std::string(bank) },
|
|
};
|
|
return options;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 2) {
|
|
std::println(std::cerr, "usage: ShouldReceiveBankCallbacks <server-binary>");
|
|
return 2;
|
|
}
|
|
|
|
{
|
|
TestServer srv(argv[1], 8223, Options("bunq"));
|
|
|
|
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: enabled on a secret path") == 1,
|
|
"the endpoint says at startup that it is on", 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(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();
|
|
}
|