catcrafts.net/tests/ShouldSettleBankTransfers/main.cpp
Jorijn van der Graaf 3c61116531
bank: a payment is spent once
A payer could place several orders and send one transfer naming all of
them. Each order looked for its own reference, found it, and saw the
full amount, so every order settled on one payment: three EUR 5 orders
on a single EUR 5 credit, reproduced. The rail logged "settle this one
by hand" and settled each of them. The README called it a known
limitation; it was a way to pay once for several orders.

The rail now records which payment settled which order, by the bank's
id, in <credits>.claims, before it reports the order paid, and a claimed
payment is left out of every other order's sum. The record cannot live
in the credits file, which the pull replaces every hour. It is read back
at start. If it cannot be written the rail settles nothing and says why;
if it is there and cannot be read the rail does not start. A claims
file that ends mid-line gets its line break before the next claim, or
that claim would be lost at the next start and the payment free again.

It does not rest on what a description looks like, because the payer
writes it. The "also quotes another reference" flag stays as a pointer
in the log and decides nothing: a name followed by a date has the same
shape. A payer who really sends one transfer for two orders gets the
first settled and the other by hand, with one line in the journal.

One payment is also counted once however many lines of the credits file
carry its id. Two lines are not meant to happen and were reproduced all
the same (one callback delivered on two connections at once passed the
id check twice), and summed they settle an order on half its price. The
callback's look-then-append is now under a lock as well.

ShouldMatchBankTransfers drives the matcher and the rail itself across
a restart, a torn claims file, an unreadable one and a duplicated line;
ShouldSettleBankTransfers shows over HTTP that one of three orders
settles.

Found in the security review of 2026-11-26; the torn claims file and
the duplicate line by the adversarial review of the first fix.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
2026-11-26 11:05:35 +01:00

363 lines
18 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-transfer rail end to end, against the REAL rail rather than a
// stand-in: a donation placed over HTTP, the account details read back off the
// order page exactly as a buyer would, then the credit the bank would have
// reported written to the credits file, and the reconciler settling the order.
//
// The counterpart of ShouldSettleEurcOnTestnet, with one structural difference
// worth stating plainly. That suite has to talk to real nodes because the rail
// depends on a conversation with machines we do not control, so a mistake in it
// costs money and only a live call can catch it. This rail depends on no such
// conversation: the money arrives at our own account, and the only thing
// between a payer and a settled order is OUR code — the reference matching, the
// covering-amount rule, the window, the ledger. All of which is exactly what a
// suite can drive with no network and no credential at all.
//
// So this needs no secret, never flakes on someone else's RPC, and runs on
// every deploy unconditionally. The bank stands in for itself only in the sense
// that the suite writes the credits file — the same file the bank's callback
// and tools/pull-and-ship-credits.sh fill from the real account, read by the
// same parser, matched by the same matcher.
// Everything downstream of that line is production code.
//
// What the unit suites already cover, and this one deliberately does not
// re-prove: the matching table (ShouldMatchBankTransfers) and what the bank's
// callback comes to (ShouldReadBankCallbacks). What only this suite can show is that the
// pieces are wired to each other — that the reference the PAGE prints is the
// reference the MATCHER looks for, which is the seam where a rename or a
// refactor would quietly break settlement while every unit test still passed.
import std;
import Crafter.Network;
import Catcrafts.E2eHarness;
using namespace Catcrafts::E2e;
namespace fs = std::filesystem;
namespace {
// Deliberately not the shop's real account: a suite that embedded the live IBAN
// would publish it into CI logs on every run, and would keep passing if
// production's configuration silently changed.
constexpr std::string_view kIban = "NL00TEST0123456789";
constexpr std::string_view kBeneficiary = "Catcrafts E2E, not a real account";
constexpr std::string_view kBic = "TESTNL2A";
// What a PAID donation page says. There is no "paid" badge to look for — the
// renderer deliberately omits one, because the thank-you notice below already
// carries the state and two stacked pills read as a bug. So this copy is the
// signal, and if it ever changes this suite is supposed to fail: a buyer who
// paid and sees no acknowledgement is the failure being guarded against.
constexpr std::string_view kPaidCopy = "Your donation funds";
// Append one credit exactly as the bank would have reported it. `reference` is
// free text on purpose: the whole point is to send it through the same mangling
// a real payer and a real bank apply.
void Credit(const fs::path& creditsFile, std::string_view id,
std::string_view reference, std::int64_t amountMinor,
std::string_view method = "sepa") {
std::ofstream out(creditsFile, std::ios::app | std::ios::binary);
out << std::format(
R"({{"id":"{}","reference":"{}","amount_minor":{},"method":"{}"}})",
id, reference, amountMinor, method) << "\n";
}
// 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;
}
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);
}
// Every order this suite places is a donation, for the same reason the donation
// item exists in the other suites: it is purchasable in BOTH shop states, so
// this runs whether or not any priced product is listed or open, and it needs no shipping
// address or rate table.
struct Order {
std::string token;
std::string reference;
};
Order PlaceDonation(TestServer& srv, std::string_view amount) {
const auto res = srv.Post("/shop/donation", std::format("amount={}", amount));
Order out;
if (res.status != "303") {
Check(false, "a donation on the transfer rail is accepted",
std::format("status {}", res.status));
return out;
}
out.token = TokenOf(res);
out.reference = NewestReference(srv);
return out;
}
} // namespace
int main(int argc, char** argv) {
if (argc < 2) {
std::println(std::cerr, "usage: ShouldSettleBankTransfers <server-binary>");
return 2;
}
ServerOptions options;
// The REAL transfer rail in the bank slot. The crypto slot is off, so any
// order this suite places must have gone through the rail under test.
options.extraArgs = { "--rail=transfer", "--crypto-rail=off" };
options.env = {
{ "TRANSFER_IBAN", std::string(kIban) },
{ "TRANSFER_BENEFICIARY", std::string(kBeneficiary) },
{ "TRANSFER_BIC", std::string(kBic) },
// Short enough that the lapse case below does not need a fifteen-minute
// suite, long enough that nothing else races it.
{ "TRANSFER_WINDOW_HOURS", "1" },
// The rail's production cadence is 60 s, which would make this suite
// minutes long for no benefit: there is no real bank here to be polite
// to, only a local file.
{ "TRANSFER_POLL_SECONDS", "1" },
};
TestServer srv(argv[1], 8222, options);
const fs::path credits(srv.Orders().string() + ".transfer-credits.jsonl");
// ── what the buyer is told ────────────────────────────────────────
//
// Asserted from the rendered page rather than from configuration, because
// the failure being guarded against is the page and the rail disagreeing.
{
const Order order = PlaceDonation(srv, "12.50");
Check(!order.token.empty(), "checkout returns an order page URL");
if (order.token.empty()) return Finish();
const std::string path = "/order/" + order.token;
srv.BodyHas(path, "Pay by bank transfer", "the page names the method");
srv.BodyHas(path, kIban, "the IBAN the money must go to");
srv.BodyHas(path, "Catcrafts E2E", "the beneficiary name is shown");
srv.BodyHas(path, kBic, "the BIC is shown when one is configured");
srv.BodyHas(path, order.reference, "the short reference is shown");
srv.BodyHas(path, "€12.50", "the exact amount to transfer");
// The payto link is a second copy of the same instructions, filled
// into the payer's app unread, so every field in it must be the one
// the manual details print. The message must be exactly the reference
// the matcher looks for, and the name must survive percent-encoding:
// the test name's space and comma are there to prove that.
srv.BodyHas(path,
std::format("href=\"payto://iban/{}?amount=EUR:12.50"
"&amp;receiver-name=Catcrafts%20E2E%2C%20not%20a%20real%20account"
"&amp;message={}\"", kIban, order.reference),
"a payto link carries the same IBAN, amount, name and reference");
// No hosted checkout exists, so nothing may invite the buyer to leave.
srv.BodyLacks(path, "Resume payment",
"a self-hosted rail offers no hosted checkout button");
srv.BodyLacks(path, "EURC",
"the bank rail's page says nothing about tokens");
// ── settlement, through a reference a human retyped ────────────
//
// Lower case, the hyphen replaced by a space, and buried in words: the
// shape a real remittance field arrives in. If this fails while
// ShouldMatchBankTransfers passes, the page and the matcher have drifted
// apart, which is the whole reason this assertion is here and not there.
std::string mangled = order.reference;
for (char& c : mangled) {
if (c >= 'A' && c <= 'Z') c = static_cast<char>(c - 'A' + 'a');
if (c == '-') c = ' ';
}
// "ideal" rather than "sepa" on purpose, and it is not fiction even
// though checkout offers no iDEAL. `via` is not a method this shop
// OFFERS — it is what the bank reports about how the money reached the
// account, from the bank's own classification of the payment. Money
// can arrive there iDEAL-funded without our checkout being involved: a
// payment link of the bank's, for one, lands in the same account, and
// the rail settles on the REFERENCE regardless of how the payer funded
// it. Using a non-default method here is what proves that passthrough
// works, which matters because the `via` column is what tells the
// operator whether an order is safe to ship: sepa is final, anything
// card- or Wero-funded carries a dispute window.
Credit(credits, "e2e-1", std::format("betaling {} bedankt", mangled),
1250, "ideal");
// Deliberately NOT looking for the word "paid": the paid state renders
// no badge on purpose (see RenderOrder — two stacked "paid" pills read
// as a rendering bug), so the thank-you copy is what marks it.
const std::string body = srv.WaitForBody(path, kPaidCopy, 120);
Check(body.find(kPaidCopy) != std::string::npos,
"the order settles once the credit appears",
"the order page never reached its paid state");
srv.BodyLacks(path, "awaiting payment",
"and it stops asking to be paid");
// The method has to survive into the ledger: it is what tells the
// operator whether an order is safe to ship.
Check(srv.OrdersText().find("\"via\":\"ideal\"") != std::string::npos,
"the settling method reaches the ledger");
}
// ── a partial payment does not settle, and then completes ─────────
//
// The page promises that sending too little can be topped up with a second
// transfer. That promise is only true if the rail sums credits, so it is
// worth proving over HTTP rather than trusting the unit test alone.
{
const Order order = PlaceDonation(srv, "40.00");
if (order.token.empty()) return Finish();
const std::string path = "/order/" + order.token;
Credit(credits, "e2e-2a", order.reference, 1500);
// Long enough for several reconciler sweeps to have seen it.
std::this_thread::sleep_for(std::chrono::seconds(4));
srv.BodyLacks(path, kPaidCopy,
"a part payment does not settle the order");
// And the buyer is told what actually happened. This assertion caught a
// real bug: the in-flight badge was written for the crypto rail and
// told a bank payer their transfer was "awaiting network confirmation",
// which is nonsense about a mechanism a SEPA transfer never touches.
srv.BodyHas(path, "part payment received",
"a part payment says so, in bank terms");
Credit(credits, "e2e-2b", order.reference, 2500);
const std::string body = srv.WaitForBody(path, kPaidCopy, 120);
Check(body.find(kPaidCopy) != std::string::npos,
"the balance arriving later settles it",
"two credits summing to the total did not settle");
}
// ── money that is not for this order is left alone ────────────────
//
// The dangerous failure is the opposite of a missed payment: an order
// settling on somebody else's money, which ships goods nobody paid for.
{
const Order order = PlaceDonation(srv, "25.00");
if (order.token.empty()) return Finish();
const std::string path = "/order/" + order.token;
// Enough money, wrong reference.
Credit(credits, "e2e-3a", "CC-ZZZZZZ", 2500);
// The right reference, but leaving the account rather than entering it:
// a refund quotes the very reference of the order it reverses.
Credit(credits, "e2e-3b", order.reference, -2500);
// A reference that merely contains ours as a prefix must not match
// either — this is the assertion that a sloppier "starts with" rule
// would fail.
std::this_thread::sleep_for(std::chrono::seconds(4));
srv.BodyLacks(path, kPaidCopy,
"another order's credit and an outgoing payment settle nothing");
// Nothing landed for THIS order, so it is still plainly awaiting: not
// the part-payment state, which would mean we had counted money that
// was not for it.
srv.BodyHas(path, "awaiting payment",
"and the order still reads as simply unpaid");
Credit(credits, "e2e-3c", order.reference, 2500);
const std::string body = srv.WaitForBody(path, kPaidCopy, 120);
Check(body.find(kPaidCopy) != std::string::npos,
"the order's own credit still settles it afterwards");
}
// ── one payment pays for one order ────────────────────────────────
//
// Three orders, then ONE transfer for the price of one whose text names
// all three. Each order looks for its own reference and finds the full
// amount against it, so before the rail recorded what a payment had been
// spent on, all three settled: three thank-you pages, three entries in
// the public donations total, one payment. Found in review on 2026-11-26.
//
// Over HTTP and not only in ShouldMatchBankTransfers, because the rule is
// not in the matcher alone: it needs the rail to write the claim when it
// settles and to judge the next order against it, across separate polls
// on separate threads. That wiring is what this proves.
{
const std::array<Order, 3> orders = {
PlaceDonation(srv, "5.00"), PlaceDonation(srv, "5.00"),
PlaceDonation(srv, "5.00") };
for (const Order& o : orders) {
if (o.token.empty()) return Finish();
}
Credit(credits, "e2e-4",
std::format("{} {} {}", orders[0].reference, orders[1].reference,
orders[2].reference),
500);
auto settled = [&](const Order& o) {
return srv.Body("/order/" + o.token).find(kPaidCopy) != std::string::npos;
};
auto settledCount = [&] {
int n = 0;
for (const Order& o : orders) n += settled(o) ? 1 : 0;
return n;
};
int paid = 0;
for (int i = 0; i < 120 && paid == 0; ++i) {
paid = settledCount();
if (paid == 0) std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
Check(paid >= 1, "a payment naming three orders settles one of them");
// Long enough for several sweeps to have judged the other two against
// the same credit, which is still sitting in the file naming them.
std::this_thread::sleep_for(std::chrono::seconds(4));
paid = settledCount();
Check(paid == 1, "and only one: the same money does not settle the others",
std::format("{} of 3 settled", paid));
// The two it did not pay for read as plainly unpaid. Not "part
// payment received": that would be counting the spent money toward
// them after all, only short of the total.
const Order* passedOver = nullptr;
for (const Order& o : orders) {
if (settled(o)) continue;
if (!passedOver) passedOver = &o;
srv.BodyHas("/order/" + o.token, "awaiting payment",
"an order the payment did not settle still reads as unpaid");
srv.BodyLacks("/order/" + o.token, "part payment received",
"and is not shown the spent money as a part payment");
}
// What was spent is on disk, next to the credits, so a restart does
// not forget it, and the log says why two orders are still waiting.
const std::string claims = ReadFile(fs::path(credits.string() + ".claims"));
Check(CountOccurrences(claims, "\"credit\":\"e2e-4\"") == 1,
"the settlement recorded one claim for that payment",
std::format("{} claim line(s) for it",
CountOccurrences(claims, "\"credit\":\"e2e-4\"")));
Check(ReadFile(srv.Work() / "server.log").find("one payment pays for one order")
!= std::string::npos,
"the log says why the other orders are not settled");
// An order that was passed over is not stuck: a payment of its own
// settles it, and the third still waits.
if (passedOver) {
Credit(credits, "e2e-4b", passedOver->reference, 500);
const std::string body =
srv.WaitForBody("/order/" + passedOver->token, kPaidCopy, 120);
Check(body.find(kPaidCopy) != std::string::npos,
"an order passed over settles on a payment of its own");
Check(settledCount() == 2, "and the third order is still unpaid",
std::format("{} of 3 settled", settledCount()));
}
}
return Finish();
}