272 lines
13 KiB
C++
272 lines
13 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's callback endpoint, over real HTTP, with the shadow run of
|
||
|
|
// Nostro's reader attached to it (see ShadowCallbackVerdict in the server's
|
||
|
|
// interface). The endpoint is how a transfer settles within seconds in
|
||
|
|
// production, and until this suite it was only ever exercised there.
|
||
|
|
//
|
||
|
|
// What it holds, in the order that matters:
|
||
|
|
//
|
||
|
|
// * OUR PARSER STILL DECIDES, ALONE. A payment it reads is credited whatever
|
||
|
|
// Nostro's reader makes of the same body, including when that reader
|
||
|
|
// refuses it. The shadow is in production to find out whether it would
|
||
|
|
// refuse real callbacks, so the one thing it must not be able to do is
|
||
|
|
// turn a refusal into a missed payment.
|
||
|
|
// * An order settles from a callback, end to end: the body the bank posts,
|
||
|
|
// the credit, the reconciler, the paid page.
|
||
|
|
// * The shadow says what it found, once per callback, in the server's log.
|
||
|
|
// * That log says nothing a callback carried: not the payer, not the
|
||
|
|
// reference, and not the address, which is the endpoint's only secret.
|
||
|
|
//
|
||
|
|
// No secret and no network: the suite plays the bank by posting to the
|
||
|
|
// endpoint, as ShouldSettleBankTransfers plays it by writing the credits file.
|
||
|
|
// The bodies are made up, in the shape of bunq's own sample of a callback.
|
||
|
|
|
||
|
|
import std;
|
||
|
|
import Crafter.Network;
|
||
|
|
import Catcrafts.E2eHarness;
|
||
|
|
|
||
|
|
using namespace Catcrafts::E2e;
|
||
|
|
namespace fs = std::filesystem;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
// Not real accounts, and not the shop's: see ShouldSettleBankTransfers.
|
||
|
|
constexpr std::string_view kIban = "NL00TEST0123456789";
|
||
|
|
constexpr std::string_view kOtherIban = "NL00OTHR0000000002";
|
||
|
|
constexpr std::string_view kBeneficiary = "Catcrafts E2E, not a real account";
|
||
|
|
|
||
|
|
// The secret address. Long enough for the server to take it for one.
|
||
|
|
constexpr std::string_view kHook = "/hooks/bank/e2e-HOOKSECRET-0123456789abcdef";
|
||
|
|
|
||
|
|
// What a PAID donation page says: see ShouldSettleBankTransfers for why this
|
||
|
|
// copy, and not a badge, is the signal.
|
||
|
|
constexpr std::string_view kPaidCopy = "Your donation funds";
|
||
|
|
|
||
|
|
constexpr std::string_view kShadow = "bank callback shadow: ";
|
||
|
|
|
||
|
|
struct Sent {
|
||
|
|
std::string_view id;
|
||
|
|
std::string_view value;
|
||
|
|
std::string_view description = "PAYERNOTE thanks";
|
||
|
|
std::string_view type = "EBA_SCT";
|
||
|
|
std::string_view account = kIban; // the payment's own side
|
||
|
|
};
|
||
|
|
|
||
|
|
// A callback as bunq'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{}",
|
||
|
|
"category": "MUTATION",
|
||
|
|
"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": "EUR", "value": "{}"}},
|
||
|
|
"description": "{}",
|
||
|
|
"type": "{}",
|
||
|
|
"alias": {{"iban": "{}", "display_name": "PAYEENAME", "country": "NL"}},
|
||
|
|
"counterparty_alias": {{"iban": "NL00PAYR0000000001",
|
||
|
|
"display_name": "PAYERNAME", "country": "NL"}}
|
||
|
|
}}}}
|
||
|
|
}}
|
||
|
|
}})", kHook, s.id, s.value, s.description, s.type, s.account);
|
||
|
|
}
|
||
|
|
|
||
|
|
// The four members our own parser reads and nothing else, which Nostro's
|
||
|
|
// reader refuses for what it lacks.
|
||
|
|
std::string Reduced(std::string_view id, std::string_view value) {
|
||
|
|
return std::format(R"({{"NotificationUrl": {{"category": "MUTATION",
|
||
|
|
"object": {{"Payment": {{"id": {}, "type": "EBA_SCT",
|
||
|
|
"description": "PAYERNOTE reduced",
|
||
|
|
"amount": {{"currency": "EUR", "value": "{}"}}}}}}}}}})", id, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 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;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
int main(int argc, char** argv) {
|
||
|
|
if (argc < 2) {
|
||
|
|
std::println(std::cerr, "usage: ShouldReceiveBankCallbacks <server-binary>");
|
||
|
|
return 2;
|
||
|
|
}
|
||
|
|
|
||
|
|
ServerOptions options;
|
||
|
|
// The REAL transfer rail in the bank slot, settling from the credits file
|
||
|
|
// the callback endpoint writes. The crypto slot is off.
|
||
|
|
options.extraArgs = { "--rail=transfer", "--crypto-rail=off" };
|
||
|
|
options.env = {
|
||
|
|
{ "TRANSFER_IBAN", std::string(kIban) },
|
||
|
|
{ "TRANSFER_BENEFICIARY", std::string(kBeneficiary) },
|
||
|
|
{ "TRANSFER_POLL_SECONDS", "1" },
|
||
|
|
{ "BUNQ_WEBHOOK_PATH", std::string(kHook) },
|
||
|
|
// A developer's shell may carry the real key, and with one the rail
|
||
|
|
// would read the real bank instead of the credits file. Never here.
|
||
|
|
{ "BUNQ_API_KEY", "" },
|
||
|
|
};
|
||
|
|
TestServer srv(argv[1], 8223, options);
|
||
|
|
|
||
|
|
const fs::path credits(srv.Orders().string() + ".transfer-credits.jsonl");
|
||
|
|
const fs::path log = srv.Work() / "server.log";
|
||
|
|
const auto Credited = [&](std::string_view id) {
|
||
|
|
return CountOccurrences(ReadFile(credits), std::format("\"id\":\"{}\"", id));
|
||
|
|
};
|
||
|
|
const auto Logged = [&](std::string_view line) {
|
||
|
|
return CountOccurrences(ReadFile(log), line);
|
||
|
|
};
|
||
|
|
const auto Send = [&](std::string body) {
|
||
|
|
return srv.Post(std::string(kHook), std::move(body), "application/json").status;
|
||
|
|
};
|
||
|
|
|
||
|
|
Check(Logged("bank callback shadow: on") == 1 && Logged("own account named") == 1,
|
||
|
|
"the shadow says at startup that it is on, and that it knows the account",
|
||
|
|
ReadFile(log));
|
||
|
|
|
||
|
|
// ── the endpoint is the secret address, and only for a POST ───────
|
||
|
|
srv.CheckStatus(std::string(kHook), "404");
|
||
|
|
{
|
||
|
|
std::string near(kHook);
|
||
|
|
near.back() = near.back() == 'f' ? 'e' : 'f';
|
||
|
|
const std::string status =
|
||
|
|
srv.Post(near, Callback({ .id = "9000099", .value = "5.00" }),
|
||
|
|
"application/json").status;
|
||
|
|
Check(status != "200", "an address one character off is not the endpoint", status);
|
||
|
|
Check(Credited("9000099") == 0, "and what is posted there credits nothing");
|
||
|
|
Check(Logged(kShadow) == 1, "nor does the shadow look at it"); // the startup line
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── a payment both readers read ───────────────────────────────────
|
||
|
|
Check(Send(Callback({ .id = "9000001", .value = "5.00" })) == "200",
|
||
|
|
"a callback is answered 200");
|
||
|
|
Check(Credited("9000001") == 1, "and its payment is credited", ReadFile(credits));
|
||
|
|
{
|
||
|
|
const std::string text = ReadFile(credits);
|
||
|
|
Check(text.find("\"amount_minor\":500") != std::string::npos
|
||
|
|
&& text.find("\"method\":\"sepa\"") != std::string::npos,
|
||
|
|
"with its amount and how it arrived", text);
|
||
|
|
}
|
||
|
|
Check(Logged("bank callback shadow: AGREE in method=sepa account=ours "
|
||
|
|
"event=transactions") == 1,
|
||
|
|
"the shadow says Nostro's reader read the same payment", ReadFile(log));
|
||
|
|
|
||
|
|
// The bank retries, and a retry must not count twice. The shadow looks at
|
||
|
|
// each one: it compares readings, it keeps no books.
|
||
|
|
Check(Send(Callback({ .id = "9000001", .value = "5.00" })) == "200",
|
||
|
|
"the same callback again is answered 200 as well");
|
||
|
|
Check(Credited("9000001") == 1, "and is credited once", ReadFile(credits));
|
||
|
|
Check(Logged("bank callback shadow: AGREE in method=sepa account=ours "
|
||
|
|
"event=transactions") == 2,
|
||
|
|
"the shadow compares the retry too", ReadFile(log));
|
||
|
|
|
||
|
|
// ── a payment Nostro's reader refuses: ours decides, alone ────────
|
||
|
|
Check(Send(Reduced("9000002", "3.00")) == "200",
|
||
|
|
"a callback Nostro's reader refuses is answered 200");
|
||
|
|
Check(Credited("9000002") == 1,
|
||
|
|
"and is CREDITED all the same: the shadow has no say", ReadFile(credits));
|
||
|
|
Check(Logged("bank callback shadow: DIFFER nostro refused what ours reads "
|
||
|
|
"as a payment: ") == 1,
|
||
|
|
"the shadow says that Nostro's reader would have refused it", ReadFile(log));
|
||
|
|
|
||
|
|
// ── money going out ───────────────────────────────────────────────
|
||
|
|
Check(Send(Callback({ .id = "9000003", .value = "-24.95", .type = "MASTERCARD" }))
|
||
|
|
== "200",
|
||
|
|
"a callback about an outgoing card payment is answered 200");
|
||
|
|
Check(Credited("9000003") == 0, "and credits nothing", ReadFile(credits));
|
||
|
|
Check(Logged("bank callback shadow: AGREE out method ours=MASTERCARD nostro=card "
|
||
|
|
"account=ours event=transactions") == 1,
|
||
|
|
"the shadow reads it the same, and names the method each reader gives",
|
||
|
|
ReadFile(log));
|
||
|
|
|
||
|
|
// ── a payment on another account ──────────────────────────────────
|
||
|
|
//
|
||
|
|
// Only the shadow's word is held here. Our parser never looked at which
|
||
|
|
// account a payment is on, and what the endpoint does with one on another
|
||
|
|
// account is for the change that replaces the parser to decide and pin.
|
||
|
|
Check(Send(Callback({ .id = "9000004", .value = "5.00", .account = kOtherIban }))
|
||
|
|
== "200",
|
||
|
|
"a callback about another account is answered 200");
|
||
|
|
Check(Logged("bank callback shadow: AGREE in method=sepa account=other "
|
||
|
|
"event=transactions") == 1,
|
||
|
|
"the shadow says the payment is not on the shop's account", ReadFile(log));
|
||
|
|
|
||
|
|
// ── what is no callback ───────────────────────────────────────────
|
||
|
|
Check(Send("not a callback PAYERNOTE") == "200",
|
||
|
|
"a body that is no callback is answered 200, so the bank stops retrying");
|
||
|
|
Check(Logged("bank callback shadow: AGREE neither reads a payment") == 1,
|
||
|
|
"and neither reader reads a payment in it", ReadFile(log));
|
||
|
|
|
||
|
|
// ── an order settles from a callback, end to end ──────────────────
|
||
|
|
{
|
||
|
|
const auto res = srv.Post("/shop/donation", "amount=7.00");
|
||
|
|
Check(res.status == "303", "a donation on the transfer rail is accepted",
|
||
|
|
res.status);
|
||
|
|
const std::string token = TokenOf(res);
|
||
|
|
const std::string reference = NewestReference(srv);
|
||
|
|
Check(!token.empty() && !reference.empty(), "and has a page and a reference");
|
||
|
|
if (token.empty() || reference.empty()) return Finish();
|
||
|
|
const std::string path = "/order/" + token;
|
||
|
|
srv.BodyHas(path, "awaiting payment", "the order starts out unpaid");
|
||
|
|
|
||
|
|
const std::string note = std::format("betaling {} bedankt", reference);
|
||
|
|
Check(Send(Callback({ .id = "9000010", .value = "7.00", .description = note }))
|
||
|
|
== "200",
|
||
|
|
"the bank's callback for it is answered 200");
|
||
|
|
const std::string body = srv.WaitForBody(path, kPaidCopy, 120);
|
||
|
|
Check(body.find(kPaidCopy) != std::string::npos,
|
||
|
|
"and the order is paid, from the callback alone");
|
||
|
|
Check(Logged("bank callback shadow: AGREE in method=sepa account=ours "
|
||
|
|
"event=transactions") == 3,
|
||
|
|
"with the shadow agreeing on that payment too", ReadFile(log));
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── one verdict a callback, and nothing a callback carried ────────
|
||
|
|
{
|
||
|
|
const std::string text = ReadFile(log);
|
||
|
|
// Seven callbacks reached the endpoint, and the startup line makes eight.
|
||
|
|
Check(CountOccurrences(text, kShadow) == 8,
|
||
|
|
"the shadow spoke once for each callback", text);
|
||
|
|
for (const std::string_view secret :
|
||
|
|
{ kHook, std::string_view("HOOKSECRET"), std::string_view("PAYERNAME"),
|
||
|
|
std::string_view("PAYEENAME"), std::string_view("PAYERNOTE"),
|
||
|
|
std::string_view("NL00PAYR"), kIban, kOtherIban,
|
||
|
|
std::string_view("shop.invalid") }) {
|
||
|
|
Check(text.find(secret) == std::string::npos,
|
||
|
|
std::format("the server's log repeats '{}' from a callback", secret),
|
||
|
|
text);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return Finish();
|
||
|
|
}
|