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>
This commit is contained in:
parent
06ec1c525c
commit
3c61116531
4 changed files with 683 additions and 23 deletions
|
|
@ -542,22 +542,56 @@ export namespace Catcrafts::Server {
|
|||
std::string method; // ledger `via`: "sepa", "ideal", …
|
||||
};
|
||||
|
||||
// A credit that has paid for an order.
|
||||
//
|
||||
// MONEY IS SPENT ONCE. A payment that settled one order pays for no other,
|
||||
// however many references its remittance text quotes. Without that rule a
|
||||
// payer could place several orders, send ONE transfer naming all of them,
|
||||
// and watch each order find the full amount against its own reference:
|
||||
// every order settled, one of them paid for. (Found in review on
|
||||
// 2026-11-26; three €5 orders settled on a single €5 credit.) The text is
|
||||
// a stranger's to write, so the rule cannot rest on what the text looks
|
||||
// like. It rests on this record instead: which credit, which order.
|
||||
struct CreditClaim {
|
||||
// The credit, by its key: the bank's own id for the payment, or, for
|
||||
// a line written by hand without one, a key made of its text and
|
||||
// amount (see MatchCredits).
|
||||
std::string credit;
|
||||
std::string reference; // the order it settled, "CC-2B6457"
|
||||
};
|
||||
|
||||
// What the credits say about one order.
|
||||
struct TransferMatch {
|
||||
std::int64_t paidMinor = 0; // summed over every credit carrying the reference
|
||||
int count = 0; // how many credits carried it
|
||||
std::string method; // the method of the last matching credit
|
||||
// Set when a matching credit's remittance text ALSO carries something
|
||||
// shaped like a second order reference. One transfer quoting two
|
||||
// references cannot be attributed by a per-order matcher, and at that
|
||||
// point a human should look rather than two orders both settling on the
|
||||
// same money. Advisory: the caller logs it, it does not block.
|
||||
std::int64_t paidMinor = 0; // summed over every credit counted
|
||||
int count = 0; // how many credits were counted
|
||||
std::string method; // the method of the last one counted
|
||||
// The credits that were counted, by key, each once. What the rail
|
||||
// claims for the order at the moment it settles.
|
||||
std::vector<std::string> credits;
|
||||
// Credits that quote the reference and were NOT counted, because
|
||||
// their money already settled another order. Not an error, and worth
|
||||
// saying aloud: it is either the trick described at CreditClaim or a
|
||||
// payer who meant one transfer for several orders, and in both cases
|
||||
// the order in hand is one a human settles.
|
||||
int spentElsewhere = 0;
|
||||
// Lines left out because an earlier line carried the same bank id:
|
||||
// one payment listed more than once. Counted once, and reported so
|
||||
// the log can say the file is carrying duplicates.
|
||||
int duplicates = 0;
|
||||
// Set when a counted credit's remittance text ALSO carries something
|
||||
// shaped like a second order reference. Advisory, as it always was:
|
||||
// the shape test cannot tell a second reference from a name followed
|
||||
// by a date, so it decides nothing. What stops two orders settling on
|
||||
// the same money is the claim, not this flag; the flag is so the log
|
||||
// can say which settlement to look at.
|
||||
bool ambiguous = false;
|
||||
};
|
||||
|
||||
// Sum the credits that quote `reference`, in whatever form the payer typed
|
||||
// it. Pure, and exported for the self-test, because this is the decision
|
||||
// that releases goods — the same reason ParseEthCallUint is exported.
|
||||
// it, leaving out any that `claims` says already paid for a DIFFERENT
|
||||
// order. Pure, and exported for the self-test, because this is the
|
||||
// decision that releases goods — the same reason ParseEthCallUint is
|
||||
// exported.
|
||||
//
|
||||
// Matching is on the reference BODY ("CC2B6457") after reducing both sides
|
||||
// to upper-case alphanumerics. That one choice covers every form a payer
|
||||
|
|
@ -566,8 +600,20 @@ export namespace Catcrafts::Server {
|
|||
// survives whatever spacing a bank puts in the field. The RF check digits
|
||||
// deliberately do NO work here: their job was done at the payer's own bank,
|
||||
// which refuses a mistyped reference before the transfer ever leaves.
|
||||
//
|
||||
// A claim by the SAME reference does not exclude a credit: an order whose
|
||||
// settlement was claimed but never reached the ledger (a crash between
|
||||
// the two) must find its own money still there. Claims are compared on
|
||||
// the reduced form too, so how a claim spells its reference is not part
|
||||
// of it.
|
||||
//
|
||||
// A payment is also counted ONCE within a pass: credits that share a
|
||||
// bank id are one payment, and only the first is summed. Two lines for
|
||||
// one payment are not supposed to reach the file, but summing them would
|
||||
// settle a half-paid order, so the sum does not depend on it.
|
||||
TransferMatch MatchCredits(std::span<const BankCredit> credits,
|
||||
std::string_view reference);
|
||||
std::string_view reference,
|
||||
std::span<const CreditClaim> claims = {});
|
||||
|
||||
// Where a transfer rail gets its incoming credits. The one implementation
|
||||
// reads a file (MakeFileCreditSource) that is filled from outside the rail:
|
||||
|
|
@ -587,6 +633,17 @@ export namespace Catcrafts::Server {
|
|||
// The rail itself. Takes its credit source, so that where the credits
|
||||
// come from can change without the rail being rewritten: it knows nothing
|
||||
// of any bank.
|
||||
//
|
||||
// It keeps one file of its own, "<transferCreditsPath>.claims": a line
|
||||
// for each credit that settled an order, written BEFORE the order is
|
||||
// reported paid and read back at start (CreditClaim has the reason). The
|
||||
// credits file is replaced wholesale by the pull every hour, so the
|
||||
// record of what was spent cannot live in it. A settlement whose claim
|
||||
// cannot be written is not made: the answer is "unknown, retry", and the
|
||||
// log says why. A claims file that is there and cannot be READ is a rail
|
||||
// that cannot know what was spent, so this returns nullptr for it, with
|
||||
// the reason logged. With no credits path there is nowhere to keep the
|
||||
// file, and the claims then last as long as the process.
|
||||
std::unique_ptr<PaymentRail> MakeTransferRail(const RailConfig& config,
|
||||
std::unique_ptr<CreditSource> credits);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue