/* 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. */ // What the bank's callback comes to (ReadBankCallback), how a payment is said // to have arrived (CreditMethodFor), and the credits file both end up in. The // HTTP around these is thin; this is where a stranger's bytes become a claim // on an order, which is the same reason ParseEthCallUint and ParseEurcChains // are pinned here rather than trusted to integration. // // The properties that carry real weight: // // * THE ACCOUNT. A bank posts to whatever address any of its customers // registered, so a callback about a payment on SOMEONE ELSE's account can // be the bank's own, from the bank's own addresses. Only a payment on the // shop's account may become a credit. // * THE SIGN. An outgoing payment is a negative amount, and the shop sends // refunds quoting the very reference of the order they refund. Lose the // minus and a refund pays for the order it reversed. // * THE METHOD. How the money arrived decides whether an order is safe to // post: a credit transfer is final, a card payment can be reversed for // months. A kind nobody has a word for must stay recognisably unknown. // * NOTHING OF THE BODY IS SAID BACK in a note, which goes to the log. A // body holds the payer's name and IBAN, and may repeat the secret address. // * THE ID. A callback is retried and the pull delivers the same payment // again, so a credit is recorded once per id of the bank's. // // The reading itself is Nostro's (the module Nostro.Money), which has suites // of its own for what it refuses. What is held here is what THIS shop makes of // what it reads. The bodies are made up, in the shape of bunq's own sample of // a callback; a real one was read the same way in production on 2026-11-25, // by the reader this replaced and by this one side by side. import std; import Catcrafts.Shared; import Catcrafts.Server; using namespace Catcrafts; namespace { int failures = 0; void Check(bool ok, std::string_view what, std::string_view got = {}) { if (ok) return; ++failures; std::println(std::cerr, "FAIL: {}{}{}", what, got.empty() ? "" : " got: ", got); } // Not real accounts, and not the shop's: see ShouldSettleBankTransfers. constexpr std::string_view kOwnIban = "NL00TEST0123456789"; constexpr std::string_view kOtherIban = "NL00OTHR0000000002"; constexpr std::string_view kBank = "bunq"; // Every value below that a note must never repeat. constexpr std::string_view kSecrets[] = { "SECRETMARK", "CC-2B6457", "4155999", "570", "24.95", "NL00", "Payer", "shop.invalid", "bad type", }; struct Sent { std::string_view id = "4155999"; std::string_view type = "EBA_SCT"; std::string_view description = "CC-2B6457"; std::string_view value = "570.43"; std::string_view currency = "EUR"; std::string_view account = kOwnIban; // the payment's own side std::string_view category = "MUTATION"; }; // 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/hooks/SECRETMARK", "category": "{}", "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": "{}", "value": "{}"}}, "description": "{}", "type": "{}", "alias": {{"iban": "{}", "display_name": "SECRETMARK Shop", "country": "NL"}}, "counterparty_alias": {{"iban": "NL00PAYR0000000001", "display_name": "SECRETMARK Payer", "country": "NL"}} }}}} }} }})", s.category, s.id, s.currency, s.value, s.description, s.type, s.account); } // The few members a reader could get by on, and no more: no time, no // counterparty, and not the account the payment is on. Refused whole. constexpr std::string_view kReduced = R"({ "NotificationUrl": { "category": "MUTATION", "object": {"Payment": { "id": 4155999, "type": "EBA_SCT", "description": "CC-2B6457", "amount": {"currency": "EUR", "value": "570.43"} }} } })"; std::vector notes; Server::BankCallbackResult Read(std::string_view body, std::string_view ownIban = kOwnIban, std::string_view bank = kBank) { Server::BankCallbackResult r = Server::ReadBankCallback(bank, body, ownIban); notes.push_back(r.note); return r; } // No credit, and a note that starts as said. void Nothing(std::string_view body, std::string_view noteStart, std::string_view what, std::string_view ownIban = kOwnIban, std::string_view bank = kBank) { const Server::BankCallbackResult r = Read(body, ownIban, bank); Check(!r.credit.has_value(), std::format("{}: nothing is credited", what)); Check(r.note.starts_with(noteStart), std::format("{}: the note says why", what), r.note); } std::string MethodOf(std::string_view type) { const Server::BankCallbackResult r = Read(Callback({ .type = type })); return r.credit ? r.credit->method : std::format("", r.note); } } // namespace int main() { // ── money coming in, on the shop's own account ──────────────────── { const Server::BankCallbackResult r = Read(Callback({})); Check(r.credit.has_value(), "an incoming transfer on the shop's account is a credit", r.note); if (r.credit) { Check(r.credit->id == "4155999", "the bank's own id is kept, for dedupe", r.credit->id); Check(r.credit->reference == "CC-2B6457", "the description is the reference", r.credit->reference); Check(r.credit->amountMinor == 57043, "570.43 is 57043 minor units", std::format("{}", r.credit->amountMinor)); Check(r.credit->method == "sepa", "a credit transfer is 'sepa'", r.credit->method); } Check(r.note.empty(), "and there is nothing to explain", r.note); } Check(Read(Callback({}), "nl00 test 0123 4567 89").credit.has_value(), "the shop's IBAN is compared as a person wrote it down"); { const Server::BankCallbackResult r = Read(Callback({ .value = "0.01" })); Check(r.credit && r.credit->amountMinor == 1, "one cent is one cent"); } // ── the account ─────────────────────────────────────────────────── Nothing(Callback({ .account = kOtherIban }), "the payment is on another account than the shop's", "a payment on someone else's account"); Nothing(Callback({}), "the payment is on another account than the shop's", "with no account of the shop's named", ""); Nothing(Callback({ .account = "" }), "the body was refused: ", "a payment that does not say which account it is on"); // ── the sign ────────────────────────────────────────────────────── Nothing(Callback({ .value = "-24.95" }), "the payment is not money coming in", "money going out"); Nothing(Callback({ .value = "0.00" }), "the payment is not money coming in", "a payment of nothing"); // ── what is not euro, or not an amount ──────────────────────────── Nothing(Callback({ .currency = "USD" }), "the body was refused: ", "dollars"); Nothing(Callback({ .value = "570,43" }), "the body was refused: ", "an amount with a comma"); Nothing(Callback({ .value = "1e2" }), "the body was refused: ", "an exponent"); Nothing(Callback({ .value = "184467440737095516.17" }), "the body was refused: ", "an amount that does not fit"); // ── the id ──────────────────────────────────────────────────────── Nothing(Callback({ .id = "\"4155999\"" }), "the body was refused: ", "an id in a string"); Nothing(Callback({ .id = "-1" }), "the body was refused: ", "a negative id"); Nothing(Callback({ .id = "1.5" }), "the body was refused: ", "half an id"); // ── how it arrived ──────────────────────────────────────────────── Check(MethodOf("EBA_SCT") == "sepa", "a credit transfer", MethodOf("EBA_SCT")); Check(MethodOf("IDEAL") == "ideal", "iDEAL", MethodOf("IDEAL")); Check(MethodOf("EBA_SDD") == "directdebit", "a direct debit", MethodOf("EBA_SDD")); Check(MethodOf("SWIFT") == "swift", "SWIFT", MethodOf("SWIFT")); Check(MethodOf("BUNQ") == "internal", "between two accounts at the bank itself", MethodOf("BUNQ")); Check(MethodOf("MASTERCARD") == "card", "a card payment (reversible!)", MethodOf("MASTERCARD")); Check(MethodOf("FIS") == "card", "a card payment, by its other name", MethodOf("FIS")); // Not guessed: a type the table has no word for stays the bank's own word, // so the `via` column shows what the bank said, and a link that either a // card or iDEAL may have paid is not taken for final money. Check(MethodOf("ADYEN") == "ADYEN", "a type without a word is passed on as it is", MethodOf("ADYEN")); Check(MethodOf("SOMETHING_NEW") == "SOMETHING_NEW", "and so is a new one", MethodOf("SOMETHING_NEW")); Check(MethodOf("eba_sct") == "eba_sct", "another spelling is not the word it resembles", MethodOf("eba_sct")); Check(MethodOf("") == "bank", "no type at all is 'bank'", MethodOf("")); Check(MethodOf("bad type!") == "bank", "what is no bank's word is not passed on", MethodOf("bad type!")); // The same table, asked directly: it is also what puts a pulled credit's // kind into words, so the two ways a payment arrives give it one word. Check(Server::CreditMethodFor(kBank, "EBA_SCT/SCT") == "sepa", "EBA_SCT/SCT"); Check(Server::CreditMethodFor(kBank, "IDEAL/REQUEST") == "ideal", "IDEAL/REQUEST"); Check(Server::CreditMethodFor(kBank, "MASTERCARD/PAYMENT") == "card", "MASTERCARD/PAYMENT"); Check(Server::CreditMethodFor(kBank, "BUNQ/PAYMENT") == "internal", "BUNQ/PAYMENT"); Check(Server::CreditMethodFor(kBank, "ADYEN/PAYMENT") == "ADYEN", "ADYEN/PAYMENT"); Check(Server::CreditMethodFor(kBank, "") == "bank", "no kind"); Check(Server::CreditMethodFor(kBank, "/SCT") == "bank", "a kind without a type"); Check(Server::CreditMethodFor(kBank, "