367 lines
19 KiB
C++
367 lines
19 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.
|
||
|
|
*/
|
||
|
|
|
||
|
|
// 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<std::string> 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("<no credit: {}>", 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, "<script>/x") == "bank", "a kind that is no word");
|
||
|
|
Check(Server::CreditMethodFor(kBank, std::string(60, 'A')) == "bank", "a word too long");
|
||
|
|
// A bank this build was not made with has no table: its kinds are unknown.
|
||
|
|
Check(Server::CreditMethodFor("nobank", "EBA_SCT/SCT") == "EBA_SCT",
|
||
|
|
"another bank's kind is not read by this bank's table",
|
||
|
|
Server::CreditMethodFor("nobank", "EBA_SCT/SCT"));
|
||
|
|
Check(Server::CreditMethodFor("", "EBA_SCT/SCT") == "EBA_SCT", "nor is no bank's");
|
||
|
|
|
||
|
|
// ── what is no callback about a payment ───────────────────────────
|
||
|
|
Nothing(kReduced, "the body was refused: ", "a payment with only a few of its members");
|
||
|
|
for (const std::string_view none : { std::string_view(""), std::string_view("not json"),
|
||
|
|
std::string_view("SECRETMARK"),
|
||
|
|
std::string_view("[1,2]"),
|
||
|
|
std::string_view(R"({"SECRETMARK": 1})"),
|
||
|
|
std::string_view(R"({"NotificationUrl": {}})") }) {
|
||
|
|
Nothing(none, "the body was refused: ",
|
||
|
|
std::format("a body that begins '{}'", none.substr(0, 12)));
|
||
|
|
}
|
||
|
|
Nothing(R"({"NotificationUrl": {"category": "REQUEST",
|
||
|
|
"event_type": "REQUEST_INQUIRY_CREATED",
|
||
|
|
"object": {"RequestInquiry": {"id": 7}}}})",
|
||
|
|
"no payment in it (event=bunq:REQUEST)", "an event about something else");
|
||
|
|
Nothing(R"({"NotificationUrl": {"category": "MUTATION"}})",
|
||
|
|
"no payment in it (event=transactions)", "an event with nothing in it");
|
||
|
|
// A payment under another event than the one payments are counted from.
|
||
|
|
// The same payment also arrives under that one, where it is counted.
|
||
|
|
Nothing(Callback({ .category = "PAYMENT" }),
|
||
|
|
"not the event payments are counted from (event=bunq:PAYMENT)",
|
||
|
|
"a payment under another event");
|
||
|
|
{
|
||
|
|
// Two things where a callback is about one.
|
||
|
|
std::string two(kReduced);
|
||
|
|
const std::size_t at = two.rfind("}}");
|
||
|
|
two.insert(at + 1, R"(, "Other": {})");
|
||
|
|
Nothing(two, "no payment in it (event=transactions)",
|
||
|
|
"a second object beside the payment");
|
||
|
|
}
|
||
|
|
{
|
||
|
|
// Past what the reader takes at all.
|
||
|
|
std::string big = Callback({});
|
||
|
|
big.insert(big.find("\"category\""),
|
||
|
|
std::format("\"padding\": \"{}\", ", std::string(70000, 'x')));
|
||
|
|
const Server::BankCallbackResult r = Read(big);
|
||
|
|
Check(!r.credit.has_value(), "a body of 70 kB credits nothing");
|
||
|
|
Check(r.note.starts_with("the body was refused: ") && r.note.size() < 300,
|
||
|
|
"and its note stays a line's worth", r.note);
|
||
|
|
}
|
||
|
|
Nothing(Callback({}), "the body was refused: ",
|
||
|
|
"a bank this build was not made with", kOwnIban, "nobank");
|
||
|
|
Nothing(Callback({}), "the body was refused: ", "no bank named", kOwnIban, "");
|
||
|
|
|
||
|
|
// ── and no note says anything a body carried ──────────────────────
|
||
|
|
Check(notes.size() >= 40, "the notes above were all collected",
|
||
|
|
std::format("{}", notes.size()));
|
||
|
|
for (const std::string& note : notes) {
|
||
|
|
for (const std::string_view secret : kSecrets) {
|
||
|
|
Check(note.find(secret) == std::string::npos,
|
||
|
|
std::format("a note repeats '{}' from a body", secret), note);
|
||
|
|
}
|
||
|
|
Check(note.find('\n') == std::string::npos, "a note is one line", note);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── the credits file ──────────────────────────────────────────────
|
||
|
|
{
|
||
|
|
const std::filesystem::path dir =
|
||
|
|
std::filesystem::temp_directory_path() / "cc-bank-callback-test";
|
||
|
|
std::error_code ec;
|
||
|
|
std::filesystem::remove_all(dir, ec);
|
||
|
|
std::filesystem::create_directories(dir, ec);
|
||
|
|
const std::filesystem::path credits = dir / "credits.jsonl";
|
||
|
|
|
||
|
|
// One credit per id of the bank's: a retried callback, or the pull
|
||
|
|
// delivering the same payment, must not count the money twice.
|
||
|
|
const Server::BankCallbackResult read = Read(Callback({ .id = "999",
|
||
|
|
.value = "25.00" }));
|
||
|
|
Check(read.credit.has_value(), "the callback to record reads as a credit");
|
||
|
|
if (read.credit) {
|
||
|
|
Check(Server::AppendCreditTo(credits, *read.credit), "the first append succeeds");
|
||
|
|
Check(!Server::AppendCreditTo(credits, *read.credit),
|
||
|
|
"the same payment id is refused the second time");
|
||
|
|
Server::BankCredit noId = *read.credit;
|
||
|
|
noId.id.clear();
|
||
|
|
Check(!Server::AppendCreditTo(credits, noId), "a credit with no id is refused");
|
||
|
|
}
|
||
|
|
{
|
||
|
|
auto src = Server::MakeFileCreditSource(credits);
|
||
|
|
const auto all = src->Recent();
|
||
|
|
Check(all && all->size() == 1, "the file holds exactly one copy",
|
||
|
|
all ? std::format("{}", all->size()) : "nullopt");
|
||
|
|
if (all && all->size() == 1) {
|
||
|
|
Check(Server::MatchCredits(*all, "CC-2B6457").paidMinor == 2500,
|
||
|
|
"so the order sees 25.00 once, not twice");
|
||
|
|
Check((*all)[0].method == "sepa", "with how it arrived", (*all)[0].method);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// What the pull ships: the bank's own kind with the bank it is of,
|
||
|
|
// put into the ledger's words when the file is read. A line that names
|
||
|
|
// the method outright keeps it, and one with neither is a transfer.
|
||
|
|
{
|
||
|
|
std::ofstream out(credits, std::ios::app | std::ios::binary);
|
||
|
|
out << R"({"id":"p1","reference":"a","amount_minor":100,"bank":"bunq","kind":"EBA_SCT/SCT"})" "\n"
|
||
|
|
<< R"({"id":"p2","reference":"b","amount_minor":100,"bank":"bunq","kind":"IDEAL/REQUEST"})" "\n"
|
||
|
|
<< R"({"id":"p3","reference":"c","amount_minor":100,"bank":"bunq","kind":"MASTERCARD/PAYMENT"})" "\n"
|
||
|
|
<< R"({"id":"p4","reference":"d","amount_minor":100,"bank":"bunq","kind":"ADYEN/PAYMENT"})" "\n"
|
||
|
|
<< R"({"id":"p5","reference":"e","amount_minor":100,"bank":"bunq","kind":"BUNQ/PAYMENT"})" "\n"
|
||
|
|
<< R"({"id":"p6","reference":"f","amount_minor":100,"method":"ideal","bank":"bunq","kind":"EBA_SCT/SCT"})" "\n"
|
||
|
|
<< R"({"id":"p7","reference":"g","amount_minor":100})" "\n"
|
||
|
|
<< R"({"id":"p8","reference":"h","amount_minor":100,"kind":"EBA_SCT/SCT"})" "\n";
|
||
|
|
}
|
||
|
|
{
|
||
|
|
auto src = Server::MakeFileCreditSource(credits);
|
||
|
|
const auto all = src->Recent();
|
||
|
|
Check(all && all->size() == 9, "every line is read",
|
||
|
|
all ? std::format("{}", all->size()) : "nullopt");
|
||
|
|
if (all && all->size() == 9) {
|
||
|
|
const auto method = [&](std::string_view id) {
|
||
|
|
for (const Server::BankCredit& c : *all) {
|
||
|
|
if (c.id == id) return c.method;
|
||
|
|
}
|
||
|
|
return std::string("<missing>");
|
||
|
|
};
|
||
|
|
Check(method("p1") == "sepa", "a pulled transfer", method("p1"));
|
||
|
|
Check(method("p2") == "ideal", "a pulled iDEAL payment", method("p2"));
|
||
|
|
Check(method("p3") == "card", "a pulled card payment", method("p3"));
|
||
|
|
Check(method("p4") == "ADYEN", "a pulled kind without a word", method("p4"));
|
||
|
|
Check(method("p5") == "internal", "a pulled payment from inside the bank",
|
||
|
|
method("p5"));
|
||
|
|
Check(method("p6") == "ideal", "a method said outright wins", method("p6"));
|
||
|
|
Check(method("p7") == "sepa", "a line that says neither is a transfer",
|
||
|
|
method("p7"));
|
||
|
|
Check(method("p8") == "sepa", "and so is a kind without its bank",
|
||
|
|
method("p8"));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
std::filesystem::remove_all(dir, ec);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (failures != 0) {
|
||
|
|
std::println(std::cerr, "{} check(s) failed", failures);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
std::println("ShouldReadBankCallbacks: all checks passed");
|
||
|
|
return 0;
|
||
|
|
}
|