/* 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: what Nostro's reader makes of a bank callback, set // beside what this shop's own parser made of it. TEMPORARY, and it decides // nothing. The interface (ShadowCallbackVerdict) says why it exists and when // it goes. // // This is the one unit that imports Nostro.Money. Everything it takes from // there is read here and turned into this shop's own words before it leaves, // so nothing else in the server knows the module is there. // // The rule every line below is written to: NOTHING OF THE BODY COMES OUT. The // verdict is built from fixed words, from whether two values are equal, and // from the few words a bank has for a category or a type of payment, each // checked to be such a word before it is printed. An id, an amount, a name, an // IBAN or a description is compared and never quoted. module; module Catcrafts.Server; import std; import Catcrafts.Shared; import Nostro.Money; namespace Catcrafts::Server { namespace { // A word that is safe to print whoever wrote the body: letters, digits and // the three marks a bank's own word or ours is made with, and not many of // them. Anything else is not printed at all. Nostro already holds a bank's // category to capitals, digits and underscores; this does not lean on that. std::string Word(std::string_view s) { constexpr std::size_t most = 48; if (s.empty() || s.size() > most) return "?"; for (const char c : s) { const bool plain = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == ':' || c == '-'; if (!plain) return "?"; } return std::string(s); } // The sentence Nostro refused a body with. By its contract that sentence is // Nostro's own and holds nothing of the body, which is what makes it the one // piece of text here that is passed on. Still held to printable ASCII and a // length, so a contract that was broken cannot put a control character or a // page of text into the journal. std::string Sentence(std::string_view s) { constexpr std::size_t most = 200; std::string out; out.reserve(std::min(s.size(), most)); for (const char c : s.substr(0, most)) { out += (c >= 0x20 && c < 0x7F) ? c : '?'; } return out; } // IBANs compared as the rest of the server compares them: spacing and case // are how a person wrote one down, not part of it. std::string FoldIban(std::string_view s) { std::string out; out.reserve(s.size()); for (const char c : s) { if (c == ' ' || c == '\t') continue; out += static_cast(c >= 'a' && c <= 'z' ? c - 'a' + 'A' : c); } return out; } // Nostro's answer to how a payment arrived, in the ledger's `via` words, so // it can be set beside what BunqMethodFor said. Empty for Unknown: the caller // then falls back to the bank's own word, as BunqMethodFor does. std::string_view ViaOf(Nostro::PaymentMethod method) { switch (method) { case Nostro::PaymentMethod::CreditTransfer: return "sepa"; case Nostro::PaymentMethod::DirectDebit: return "directdebit"; case Nostro::PaymentMethod::Card: return "card"; case Nostro::PaymentMethod::Ideal: return "ideal"; case Nostro::PaymentMethod::Internal: return "bunq"; case Nostro::PaymentMethod::Swift: return "swift"; case Nostro::PaymentMethod::Unknown: break; } return {}; } // The `via` word Nostro's reading leads to for a payment of this kind. std::string NostroVia(std::string_view bank, std::string_view kind) { Nostro::PaymentMethod method = Nostro::PaymentMethod::Unknown; try { method = Nostro::PaymentMethodOf(bank, kind); } catch (...) { return "?"; } if (const std::string_view via = ViaOf(method); !via.empty()) { return std::string(via); } // A kind Nostro's table has not. A kind is "/", and the // type alone is what BunqMethodFor passes through for one IT does not // know, with "bank" where the bank sent none. const std::string_view type = kind.substr(0, kind.find('/')); return type.empty() ? std::string("bank") : std::string(type); } std::string_view Same(bool same) { return same ? "same" : "DIFFERENT"; } } // namespace std::string ShadowCallbackVerdict(std::string_view bank, std::string_view body, const std::optional& ours, std::string_view ownIban) { try { Nostro::CallbackEvent event; try { event = Nostro::ReadCallback(bank, body); } catch (const std::runtime_error& e) { // What Nostro promises to throw, and the only thing whose words // are passed on. const std::string why = Sentence(e.what()); return ours ? std::format("DIFFER nostro refused what ours reads as a payment: {}", why) : std::format("AGREE neither reads a payment (nostro: {})", why); } catch (...) { // Anything else is not in its contract, and its words could be // the body's: a JSON library quotes what it choked on. return "DIFFER nostro threw something other than it promises"; } const std::string eventWord = Word(event.event); if (!event.transaction) { return ours ? std::format("DIFFER nostro reads no payment in it (event={}), ours does", eventWord) : std::format("AGREE no payment in it (event={})", eventWord); } const Nostro::Transaction& theirs = *event.transaction; const std::string_view direction = theirs.amount > 0 ? "in" : theirs.amount < 0 ? "out" : "zero"; const std::string_view account = ownIban.empty() ? "unknown" : FoldIban(event.account) == FoldIban(ownIban) ? "ours" : "other"; if (!ours) { return std::format( "DIFFER nostro reads a payment, ours does not ({} account={} event={})", direction, account, eventWord); } const std::string theirVia = NostroVia(bank, theirs.kind); const std::string method = theirVia == ours->method ? std::format("method={}", Word(theirVia)) : std::format("method ours={} nostro={}", Word(ours->method), Word(theirVia)); const std::string rest = std::format("{} {} account={} event={}", direction, method, account, eventWord); const bool sameId = theirs.id == ours->id; const bool sameAmount = theirs.amount == ours->amountMinor; const bool sameReference = theirs.description == ours->reference; if (sameId && sameAmount && sameReference) { return std::format("AGREE {}", rest); } return std::format("DIFFER id={} amount={} reference={} {}", Same(sameId), Same(sameAmount), Same(sameReference), rest); } catch (...) { // Out of memory while building a line, in practice. The comparison // must never be the thing that fails a callback. return "FAILED the comparison itself threw"; } } } // namespace Catcrafts::Server