bank: Nostro reads the bank's callback, and the shop's bunq code is gone
All checks were successful
Deploy / build-deploy (push) Successful in 7m20s
All checks were successful
Deploy / build-deploy (push) Successful in 7m20s
The shadow run of 36a635f had Nostro's ReadCallback read a real
callback beside our own parser on 2026-11-25, for order CC-BCD155: the
same id, amount and reference, on the shop's own account. So the reader
takes over, and what this repo carried of bunq goes: the callback
parser, the payment-type table, the API client with its RSA signing,
`--pull-credits`, the mode that read the bank from the server with an
API key, and tools/register-bunq-webhook.sh.
The server now knows no bank. Catcrafts.Server-BankCallback.cpp is the
one unit that imports Nostro.Money: ReadBankCallback has a body read
and says what it comes to, CreditMethodFor says how a payment arrived.
Which bank it is is configuration: --banks= in project.cpp for what the
build can read, BANK_CALLBACK_BANK on the server for what arrives.
What changes for a running shop:
- The endpoint is configured with BANK_CALLBACK_PATH and
BANK_CALLBACK_BANK; BUNQ_WEBHOOK_PATH is no longer read. Without the
bank's name, or without TRANSFER_IBAN, the endpoint stays off and the
startup log says so: one that answered 200 and credited nothing would
look like working. Production's payments.env got both names before
this was pushed.
- Only a payment on TRANSFER_IBAN is credited. A bank posts to whatever
address any of its customers registered, so a callback about someone
else's account can be the bank's own. Until now a payment in a
callback was credited whichever account it was on.
- `via` says `internal` for a payment from another account at the bank
itself, where it said `bunq`, and `card` where it would have said
`MASTERCARD`. A kind without a word is still passed on as the bank
wrote it, now only where that is a plain word.
- Every callback leaves one line in the log, `bank callback: credited
...` or `bank callback: nothing credited: <why>`, with nothing of the
body in it beyond the credit.
- A line of the credits file may carry the bank's kind and the bank
(`"bank":"bunq","kind":"EBA_SCT/SCT"`) in place of `method`, put into
the ledger's words when the file is read. The pull script ships those
and takes the bank's name from nostrod, so it holds no table of a
bank's words either.
Also gone, with their last user: ParseAmountToMinor, PullCreditsInto
and the explicit -lcrypto (Crafter.Network names what it needs).
ShouldReadBankCallbacks replaces ShouldParseBunqPayments and
ShouldCompareCallbackReaders: what a callback comes to for each shape,
each method, the account and the sign, and that no note repeats
anything a body carried. ShouldReceiveBankCallbacks posts to the real
endpoint: an order settles from a callback, one from another account
quoting the same order does not pay for it, and a server without the
bank's name has no endpoint.
Built from scratch and run in a copy of this tree with crafter-build
6.4.1: the server and the wasm bundle build, and every suite passes but
ShouldServePostPages, which fails there for want of mirrored post media
and passed in CI on the commit before this. A dry run of the pull
script against nostrod gave 19 credits with their kinds.
Not tried: a real callback through the new handler, and `nostro
callbacks` against the registration that is in place, which was made
with the script this removes. deploy/README.md has the new setup.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
parent
36a635fd48
commit
33ce8f45d8
24 changed files with 1218 additions and 2385 deletions
|
|
@ -1,269 +0,0 @@
|
|||
/*
|
||||
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's verdict: what Nostro's reader makes of a bank callback
|
||||
// beside what our own parser made of it (ShadowCallbackVerdict). Temporary,
|
||||
// like the shadow itself.
|
||||
//
|
||||
// Three things are held here, and the third is the one that must not regress:
|
||||
//
|
||||
// * AGREE is said when, and only when, the id, the amount and the reference
|
||||
// are the same in both readings. Those are what settlement rests on.
|
||||
// * Each way the two can part is said in words that tell them apart: Nostro
|
||||
// refusing a body ours reads, reading none where ours does, reading one
|
||||
// where ours does not, and reading another value.
|
||||
// * NOTHING OF THE BODY COMES OUT. The verdict goes to the server's log, and
|
||||
// a callback holds the payer's name and IBAN and may repeat the secret
|
||||
// address it was posted to. Every verdict this suite produces is searched
|
||||
// for the values its bodies carry.
|
||||
//
|
||||
// The bodies are made up. Their shape is bunq's own sample of a MUTATION
|
||||
// callback (bunq/sdk_python, tests/assets/NotificationUrlJsons/Mutation.json),
|
||||
// which is also what Nostro's reader was written against. Whether bunq still
|
||||
// writes one like it is exactly what the shadow is in production to find out,
|
||||
// and no suite can.
|
||||
|
||||
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";
|
||||
|
||||
// Every value below that a verdict 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 account = kOwnIban; // 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/hooks/SECRETMARK",
|
||||
"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": "SECRETMARK Shop", "country": "NL"}},
|
||||
"counterparty_alias": {{"iban": "NL00PAYR0000000001",
|
||||
"display_name": "SECRETMARK Payer", "country": "NL"}}
|
||||
}}}}
|
||||
}}
|
||||
}})", s.id, s.value, s.description, s.type, s.account);
|
||||
}
|
||||
|
||||
// The four members our own parser reads, and nothing else: the shape this
|
||||
// repo's suites have always used for a callback, which Nostro's reader
|
||||
// refuses for what it lacks.
|
||||
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> verdicts;
|
||||
|
||||
// The verdict on `body`, with our own parser's reading of the same body, as
|
||||
// the server calls it.
|
||||
std::string Verdict(std::string_view body, std::string_view ownIban = kOwnIban,
|
||||
std::string_view bank = "bunq") {
|
||||
const std::optional<Server::BankCredit> ours = Server::ParseBunqCallback(body);
|
||||
verdicts.push_back(Server::ShadowCallbackVerdict(bank, body, ours, ownIban));
|
||||
return verdicts.back();
|
||||
}
|
||||
|
||||
void Equal(const std::string& got, std::string_view want, std::string_view what) {
|
||||
Check(got == want, what, got);
|
||||
}
|
||||
|
||||
void Has(const std::string& got, std::string_view part, std::string_view what) {
|
||||
Check(got.find(part) != std::string::npos, what, got);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
// ── the two readings agree ────────────────────────────────────────
|
||||
Equal(Verdict(Callback({})),
|
||||
"AGREE in method=sepa account=ours event=transactions",
|
||||
"an incoming transfer on the shop's account reads the same both ways");
|
||||
Equal(Verdict(Callback({}), "nl00 test 0123 4567 89"),
|
||||
"AGREE in method=sepa account=ours event=transactions",
|
||||
"the shop's IBAN is compared as a person wrote it down");
|
||||
Equal(Verdict(Callback({ .account = kOtherIban })),
|
||||
"AGREE in method=sepa account=other event=transactions",
|
||||
"a payment on another account is said to be on another account");
|
||||
Equal(Verdict(Callback({}), ""),
|
||||
"AGREE in method=sepa account=unknown event=transactions",
|
||||
"with no account named, whose it is on is not guessed");
|
||||
Equal(Verdict(Callback({ .value = "-24.95" })),
|
||||
"AGREE out method=sepa account=ours event=transactions",
|
||||
"money going out keeps its sign in both readings");
|
||||
|
||||
// ── the method, which may differ without the readings differing ───
|
||||
Equal(Verdict(Callback({ .type = "IDEAL" })),
|
||||
"AGREE in method=ideal account=ours event=transactions", "iDEAL");
|
||||
Equal(Verdict(Callback({ .type = "BUNQ" })),
|
||||
"AGREE in method=bunq account=ours event=transactions",
|
||||
"between two bunq accounts");
|
||||
Equal(Verdict(Callback({ .type = "MASTERCARD", .value = "-24.95" })),
|
||||
"AGREE out method ours=MASTERCARD nostro=card account=ours event=transactions",
|
||||
"a card payment, which ours passes through and Nostro names, is still AGREE");
|
||||
Equal(Verdict(Callback({ .type = "ADYEN" })),
|
||||
"AGREE in method=ADYEN account=ours event=transactions",
|
||||
"a type neither table has is the bank's own word in both");
|
||||
Equal(Verdict(Callback({ .type = "" })),
|
||||
"AGREE in method=bank account=ours event=transactions",
|
||||
"no type at all is 'bank' in both");
|
||||
{
|
||||
const std::string v = Verdict(Callback({ .type = "bad type!" }));
|
||||
Has(v, "method=?", "a type that is no bank's word is not printed");
|
||||
Has(v, "AGREE in ", "and the readings still agree");
|
||||
}
|
||||
|
||||
// ── Nostro refuses what ours reads ────────────────────────────────
|
||||
//
|
||||
// The outcome the shadow is in production to rule out, or to find.
|
||||
{
|
||||
const std::string v = Verdict(kReduced);
|
||||
Has(v, "DIFFER nostro refused what ours reads as a payment: ",
|
||||
"the four members ours reads are not enough for Nostro's reader");
|
||||
}
|
||||
{
|
||||
const std::string v = Verdict(Callback({ .account = "" }));
|
||||
Has(v, "DIFFER nostro refused what ours reads as a payment: ",
|
||||
"a payment that does not say which account it is on is refused");
|
||||
Has(v, "which account", "and the refusal says that is why");
|
||||
}
|
||||
{
|
||||
// Past what Nostro reads at all, with a payment ours still decodes.
|
||||
std::string big = Callback({});
|
||||
const std::size_t at = big.find("\"category\"");
|
||||
big.insert(at, std::format("\"padding\": \"{}\", ", std::string(70000, 'x')));
|
||||
const std::string v = Verdict(big);
|
||||
Has(v, "DIFFER nostro refused what ours reads as a payment: ",
|
||||
"a body past Nostro's limit is refused");
|
||||
Check(v.size() < 400, "and the verdict stays one line's worth", v);
|
||||
}
|
||||
Has(Verdict(Callback({}), kOwnIban, "nobank"),
|
||||
"DIFFER nostro refused what ours reads as a payment: ",
|
||||
"a bank Nostro was not built with reads nothing");
|
||||
|
||||
// ── one reads a payment where the other reads none ────────────────
|
||||
{
|
||||
// Two things under `object`: ours looks for the Payment among them,
|
||||
// Nostro takes a callback to be about one thing.
|
||||
std::string two(kReduced);
|
||||
const std::size_t at = two.rfind("}}");
|
||||
two.insert(at + 1, R"(, "Other": {})");
|
||||
Equal(Verdict(two),
|
||||
"DIFFER nostro reads no payment in it (event=transactions), ours does",
|
||||
"a second object beside the payment");
|
||||
}
|
||||
{
|
||||
verdicts.push_back(Server::ShadowCallbackVerdict("bunq", Callback({}),
|
||||
std::nullopt, kOwnIban));
|
||||
Equal(verdicts.back(),
|
||||
"DIFFER nostro reads a payment, ours does not (in account=ours event=transactions)",
|
||||
"a payment only Nostro reads");
|
||||
}
|
||||
|
||||
// ── both read a payment, and not the same one ─────────────────────
|
||||
{
|
||||
const std::string body = Callback({});
|
||||
const std::optional<Server::BankCredit> ours = Server::ParseBunqCallback(body);
|
||||
Check(ours.has_value(), "our parser reads the sample's shape");
|
||||
if (ours) {
|
||||
Server::BankCredit other = *ours;
|
||||
other.amountMinor += 1;
|
||||
verdicts.push_back(Server::ShadowCallbackVerdict("bunq", body, other, kOwnIban));
|
||||
Equal(verdicts.back(),
|
||||
"DIFFER id=same amount=DIFFERENT reference=same in method=sepa "
|
||||
"account=ours event=transactions",
|
||||
"another amount");
|
||||
other = *ours;
|
||||
other.id = "1";
|
||||
verdicts.push_back(Server::ShadowCallbackVerdict("bunq", body, other, kOwnIban));
|
||||
Has(verdicts.back(), "DIFFER id=DIFFERENT amount=same reference=same ",
|
||||
"another id");
|
||||
other = *ours;
|
||||
other.reference = "something else";
|
||||
verdicts.push_back(Server::ShadowCallbackVerdict("bunq", body, other, kOwnIban));
|
||||
Has(verdicts.back(), "DIFFER id=same amount=same reference=DIFFERENT ",
|
||||
"another reference");
|
||||
}
|
||||
}
|
||||
|
||||
// ── neither reads a payment ───────────────────────────────────────
|
||||
for (const std::string_view none : { std::string_view(""), std::string_view("not json"),
|
||||
std::string_view("SECRETMARK"),
|
||||
std::string_view(R"({"SECRETMARK": 1})"),
|
||||
std::string_view(R"({"NotificationUrl": {}})") }) {
|
||||
Has(Verdict(none), "AGREE neither reads a payment (nostro: ",
|
||||
std::format("a body that is no callback: '{}'", none.substr(0, 16)));
|
||||
}
|
||||
Equal(Verdict(R"({"NotificationUrl": {"category": "REQUEST",
|
||||
"event_type": "REQUEST_INQUIRY_CREATED",
|
||||
"object": {"RequestInquiry": {"id": 7}}}})"),
|
||||
"AGREE no payment in it (event=bunq:REQUEST)",
|
||||
"a callback about something that is no payment");
|
||||
|
||||
// ── and none of it says anything a body carried ───────────────────
|
||||
Check(verdicts.size() >= 25, "the verdicts above were all collected",
|
||||
std::format("{}", verdicts.size()));
|
||||
for (const std::string& v : verdicts) {
|
||||
for (const std::string_view secret : kSecrets) {
|
||||
Check(v.find(secret) == std::string::npos,
|
||||
std::format("a verdict repeats '{}' from a body", secret), v);
|
||||
}
|
||||
Check(v.find('\n') == std::string::npos, "a verdict is one line", v);
|
||||
}
|
||||
|
||||
if (failures != 0) {
|
||||
std::println(std::cerr, "{} check(s) failed", failures);
|
||||
return 1;
|
||||
}
|
||||
std::println("ShouldCompareCallbackReaders: all checks passed");
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -149,68 +149,6 @@ int main() {
|
|||
// simply the state of every order between checkout and payment.
|
||||
Check(PaidFor({}, ref) == 0, "no credits at all is zero, not an error");
|
||||
|
||||
// ── pulling credits into the file the rail reads ──────────────────
|
||||
//
|
||||
// Every pull re-reads an overlapping window of the account, so the SAME
|
||||
// credit arrives on every run. Appending it twice would double a payment
|
||||
// and settle an order nobody paid twice for, which makes deduplication by
|
||||
// the bank's own payment id the load-bearing property here. A file source
|
||||
// stands in for the bank so this needs no network.
|
||||
{
|
||||
const std::filesystem::path dir =
|
||||
std::filesystem::temp_directory_path() / "cc-transfer-pull-test";
|
||||
std::error_code ec;
|
||||
std::filesystem::remove_all(dir, ec);
|
||||
std::filesystem::create_directories(dir, ec);
|
||||
const std::filesystem::path bank = dir / "bank.jsonl";
|
||||
const std::filesystem::path target = dir / "credits.jsonl";
|
||||
|
||||
{
|
||||
std::ofstream out(bank);
|
||||
out << R"({"id":"p1","reference":"CC-2B6457","amount_minor":2500,)"
|
||||
R"("method":"sepa"})" << "\n";
|
||||
out << R"({"id":"p2","reference":"CC-AAAAAA","amount_minor":900,)"
|
||||
R"("method":"ideal"})" << "\n";
|
||||
}
|
||||
|
||||
auto pull = [&] {
|
||||
auto src = Server::MakeFileCreditSource(bank);
|
||||
return Server::PullCreditsInto(*src, target);
|
||||
};
|
||||
|
||||
const std::optional<int> first = pull();
|
||||
Check(first.has_value() && *first == 2, "the first pull appends both credits",
|
||||
first ? std::format("{}", *first) : "nullopt");
|
||||
const std::optional<int> second = pull();
|
||||
Check(second.has_value() && *second == 0,
|
||||
"pulling the same window again appends nothing",
|
||||
second ? std::format("{}", *second) : "nullopt");
|
||||
|
||||
// A new payment arrives at the bank; only it should be appended.
|
||||
{
|
||||
std::ofstream out(bank, std::ios::app);
|
||||
out << R"({"id":"p3","reference":"CC-2B6457","amount_minor":100,)"
|
||||
R"("method":"sepa"})" << "\n";
|
||||
}
|
||||
const std::optional<int> third = pull();
|
||||
Check(third.has_value() && *third == 1, "only the new credit is appended",
|
||||
third ? std::format("{}", *third) : "nullopt");
|
||||
|
||||
// And what the rail now reads settles correctly: 25.00 + 1.00.
|
||||
{
|
||||
auto reader = Server::MakeFileCreditSource(target);
|
||||
const auto all = reader->Recent();
|
||||
Check(all.has_value() && all->size() == 3,
|
||||
"the credits file holds exactly the three distinct credits",
|
||||
all ? std::format("{}", all->size()) : "nullopt");
|
||||
if (all) {
|
||||
Check(Server::MatchCredits(*all, ref).paidMinor == 2600,
|
||||
"the deduplicated file sums to the real total");
|
||||
}
|
||||
}
|
||||
std::filesystem::remove_all(dir, ec);
|
||||
}
|
||||
|
||||
if (failures != 0) {
|
||||
std::println(std::cerr, "{} check(s) failed", failures);
|
||||
return 1;
|
||||
|
|
|
|||
|
|
@ -1,260 +0,0 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
// Decoding bunq's payment list into credits. The HTTP around this is thin; the
|
||||
// decoding is where a mistake costs money and says nothing, which is the same
|
||||
// reason ParseEthCallUint and ParseEurcChains are pinned here rather than
|
||||
// trusted to integration.
|
||||
//
|
||||
// Two properties carry real weight:
|
||||
//
|
||||
// * The SIGN. bunq quotes an outgoing payment as a negative value, 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. `Payment.type` decides whether an order is safe to post: a
|
||||
// SEPA credit transfer is final, a card payment can be reversed for
|
||||
// months. Collapsing them to "paid" is how a chargeback becomes a
|
||||
// shipped parcel.
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// The shape bunq actually answers with: everything wrapped in Response, one
|
||||
// object per entry keyed by type.
|
||||
constexpr std::string_view kList = R"({
|
||||
"Response": [
|
||||
{"Payment": {
|
||||
"id": 4155551,
|
||||
"type": "EBA_SCT",
|
||||
"description": "CC-2B6457 catcrafts.net",
|
||||
"amount": {"currency": "EUR", "value": "57.38"}
|
||||
}},
|
||||
{"Payment": {
|
||||
"id": 4155552,
|
||||
"type": "IDEAL",
|
||||
"description": "donation cc2b6457",
|
||||
"amount": {"currency": "EUR", "value": "5.00"}
|
||||
}},
|
||||
{"Payment": {
|
||||
"id": 4155553,
|
||||
"type": "EBA_SCT",
|
||||
"description": "supplier invoice",
|
||||
"amount": {"currency": "EUR", "value": "-513.00"}
|
||||
}},
|
||||
{"Payment": {
|
||||
"id": 4155554,
|
||||
"type": "FIS",
|
||||
"description": "card payment",
|
||||
"amount": {"currency": "USD", "value": "20.00"}
|
||||
}}
|
||||
]
|
||||
})";
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
const std::vector<Server::BankCredit> credits = Server::ParseBunqPayments(kList);
|
||||
|
||||
// The USD entry is dropped: counting 20 of something else as 20 euro is
|
||||
// the kind of bug that only shows up as a shortfall nobody can explain.
|
||||
Check(credits.size() == 3, "only the euro payments are decoded",
|
||||
std::format("{}", credits.size()));
|
||||
|
||||
if (credits.size() == 3) {
|
||||
Check(credits[0].id == "4155551", "the bank's own id is kept, for dedupe",
|
||||
credits[0].id);
|
||||
Check(credits[0].reference == "CC-2B6457 catcrafts.net",
|
||||
"the description is the remittance text, verbatim", credits[0].reference);
|
||||
Check(credits[0].amountMinor == 5738, "57.38 decodes to 5738 minor units",
|
||||
std::format("{}", credits[0].amountMinor));
|
||||
Check(credits[0].method == "sepa", "EBA_SCT is a plain SEPA transfer",
|
||||
credits[0].method);
|
||||
Check(credits[1].method == "ideal", "IDEAL is carried as its own method",
|
||||
credits[1].method);
|
||||
// The load-bearing one.
|
||||
Check(credits[2].amountMinor == -51300,
|
||||
"an outgoing payment keeps its minus sign",
|
||||
std::format("{}", credits[2].amountMinor));
|
||||
}
|
||||
|
||||
// End to end through the matcher: the outgoing line must not pay for
|
||||
// anything, and the two incoming ones must sum.
|
||||
{
|
||||
const Server::TransferMatch m = Server::MatchCredits(credits, "CC-2B6457");
|
||||
Check(m.paidMinor == 6238,
|
||||
"the two incoming credits sum and the outgoing one is ignored",
|
||||
std::format("{}", m.paidMinor));
|
||||
Check(m.count == 2, "two credits matched", std::format("{}", m.count));
|
||||
}
|
||||
|
||||
// ── the method mapping, pinned individually ───────────────────────
|
||||
Check(Server::BunqMethodFor("EBA_SCT") == "sepa", "EBA_SCT -> sepa");
|
||||
Check(Server::BunqMethodFor("IDEAL") == "ideal", "IDEAL -> ideal");
|
||||
Check(Server::BunqMethodFor("FIS") == "card", "FIS -> card (reversible!)");
|
||||
Check(Server::BunqMethodFor("BUNQ") == "bunq", "BUNQ -> bunq");
|
||||
Check(Server::BunqMethodFor("SWIFT") == "swift", "SWIFT -> swift");
|
||||
Check(Server::BunqMethodFor("EBA_SDD") == "directdebit", "EBA_SDD -> directdebit");
|
||||
// An unknown type reaches the ledger verbatim rather than as a comfortable
|
||||
// guess: the `via` column should show what bunq said, so a new payment
|
||||
// type is visible instead of silently filed as an ordinary transfer.
|
||||
Check(Server::BunqMethodFor("SOMETHING_NEW") == "SOMETHING_NEW",
|
||||
"an unknown type is passed through, not guessed at");
|
||||
Check(Server::BunqMethodFor("") == "bank", "an absent type falls back to 'bank'");
|
||||
|
||||
// ── signed amount parsing ─────────────────────────────────────────
|
||||
Check(Server::ParseSignedAmountToMinor("0.01") == 1, "one cent");
|
||||
Check(Server::ParseSignedAmountToMinor("-0.01") == -1, "minus one cent");
|
||||
Check(Server::ParseSignedAmountToMinor("57.4") == 5740, "one decimal is tenths");
|
||||
Check(Server::ParseSignedAmountToMinor("665") == 66500, "no decimal point");
|
||||
Check(!Server::ParseSignedAmountToMinor("1.234").has_value(),
|
||||
"three decimals is not money");
|
||||
Check(!Server::ParseSignedAmountToMinor("1,00").has_value(),
|
||||
"a comma decimal is refused rather than guessed");
|
||||
Check(!Server::ParseSignedAmountToMinor("1e2").has_value(), "no exponents");
|
||||
Check(!Server::ParseSignedAmountToMinor("").has_value(), "empty is not zero");
|
||||
Check(!Server::ParseSignedAmountToMinor("-").has_value(), "a bare sign is not zero");
|
||||
Check(!Server::ParseSignedAmountToMinor(" 1.00").has_value(), "no leading space");
|
||||
|
||||
// ── malformed input yields nothing, never a wrong number ──────────
|
||||
Check(Server::ParseBunqPayments("").empty(), "empty input decodes to nothing");
|
||||
Check(Server::ParseBunqPayments("not json").empty(), "garbage decodes to nothing");
|
||||
Check(Server::ParseBunqPayments(R"({"Response":[]})").empty(),
|
||||
"an empty account decodes to nothing");
|
||||
Check(Server::ParseBunqPayments(R"({"Response":"nope"})").empty(),
|
||||
"a Response that is not an array decodes to nothing");
|
||||
// A payment whose amount will not parse is SKIPPED, not counted as zero:
|
||||
// an unparseable amount means we do not know what arrived.
|
||||
Check(Server::ParseBunqPayments(
|
||||
R"({"Response":[{"Payment":{"id":1,"type":"EBA_SCT","description":"x",)"
|
||||
R"("amount":{"currency":"EUR","value":"1.234"}}}]})").empty(),
|
||||
"an unparseable amount is skipped rather than read as zero");
|
||||
|
||||
// ── the callback shape ────────────────────────────────────────────
|
||||
//
|
||||
// bunq does not sign these, so the parser is the only thing between an
|
||||
// arbitrary POST body and a line in the credits file. It must therefore be
|
||||
// strict about what it accepts and must never invent a field.
|
||||
{
|
||||
constexpr std::string_view kHook = R"({
|
||||
"NotificationUrl": {
|
||||
"category": "MUTATION",
|
||||
"object": {"Payment": {
|
||||
"id": 4155999,
|
||||
"type": "EBA_SCT",
|
||||
"description": "CC-2B6457",
|
||||
"amount": {"currency": "EUR", "value": "570.43"}
|
||||
}}
|
||||
}
|
||||
})";
|
||||
const auto c = Server::ParseBunqCallback(kHook);
|
||||
Check(c.has_value(), "a MUTATION callback decodes to a credit");
|
||||
if (c) {
|
||||
Check(c->id == "4155999", "the payment id is kept", c->id);
|
||||
Check(c->amountMinor == 57043, "the amount decodes",
|
||||
std::format("{}", c->amountMinor));
|
||||
Check(c->reference == "CC-2B6457", "the description is the reference",
|
||||
c->reference);
|
||||
Check(c->method == "sepa", "the type maps to a via", c->method);
|
||||
}
|
||||
|
||||
// An id is what deduplication rests on, and bunq RETRIES callbacks. A
|
||||
// credit with no id could therefore be applied twice, so it must be
|
||||
// refused outright rather than stored with a blank.
|
||||
Check(!Server::ParseBunqCallback(R"({"NotificationUrl":{"object":{"Payment":{)"
|
||||
R"("type":"EBA_SCT","description":"x",)"
|
||||
R"("amount":{"currency":"EUR","value":"1.00"}}}}})").has_value(),
|
||||
"a payment with no id is refused, because retries need one");
|
||||
|
||||
// Everything that is not a payment notification: bunq sends several
|
||||
// categories, and none of the others may produce a credit.
|
||||
for (const std::string_view junk : {
|
||||
std::string_view(""),
|
||||
std::string_view("not json"),
|
||||
std::string_view(R"({})"),
|
||||
std::string_view(R"({"NotificationUrl":{}})"),
|
||||
std::string_view(R"({"NotificationUrl":{"object":{}}})"),
|
||||
std::string_view(R"({"NotificationUrl":{"object":{"Payment":{}}}})"),
|
||||
// A Payment whose amount is another currency, or unparseable.
|
||||
std::string_view(R"({"NotificationUrl":{"object":{"Payment":{"id":1,)"
|
||||
R"("amount":{"currency":"USD","value":"1.00"}}}}})"),
|
||||
std::string_view(R"({"NotificationUrl":{"object":{"Payment":{"id":1,)"
|
||||
R"("amount":{"currency":"EUR","value":"1.234"}}}}})"),
|
||||
}) {
|
||||
Check(!Server::ParseBunqCallback(junk).has_value(),
|
||||
"a body with no usable euro payment yields nothing", junk);
|
||||
}
|
||||
|
||||
// The sign survives here too: an outgoing payment must not be able to
|
||||
// arrive through the webhook as income.
|
||||
const auto out = Server::ParseBunqCallback(
|
||||
R"({"NotificationUrl":{"object":{"Payment":{"id":7,"type":"EBA_SCT",)"
|
||||
R"("description":"supplier","amount":{"currency":"EUR","value":"-99.00"}}}}})");
|
||||
Check(out.has_value() && out->amountMinor == -9900,
|
||||
"an outgoing callback keeps its sign for the caller to reject",
|
||||
out ? std::format("{}", out->amountMinor) : "nullopt");
|
||||
}
|
||||
|
||||
// ── appending, and refusing to append twice ───────────────────────
|
||||
//
|
||||
// bunq retries a callback about six times, and a periodic pull re-reads an
|
||||
// overlapping window, so the same payment WILL arrive more than once. If a
|
||||
// second copy landed in the file it would double the money against one
|
||||
// reference — enough to settle a part-paid order whose balance never came.
|
||||
{
|
||||
const std::filesystem::path dir =
|
||||
std::filesystem::temp_directory_path() / "cc-bunq-append-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";
|
||||
|
||||
Server::BankCredit c;
|
||||
c.id = "999";
|
||||
c.reference = "CC-2B6457";
|
||||
c.amountMinor = 2500;
|
||||
c.method = "sepa";
|
||||
|
||||
Check(Server::AppendCreditTo(credits, c), "the first append succeeds");
|
||||
Check(!Server::AppendCreditTo(credits, c),
|
||||
"the same payment id is refused the second time");
|
||||
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) {
|
||||
Check(Server::MatchCredits(*all, "CC-2B6457").paidMinor == 2500,
|
||||
"so the order sees 25.00 once, not twice");
|
||||
}
|
||||
// A credit with no id cannot be deduplicated and must be refused.
|
||||
Server::BankCredit noId = c;
|
||||
noId.id.clear();
|
||||
Check(!Server::AppendCreditTo(credits, noId), "a credit with no id is refused");
|
||||
std::filesystem::remove_all(dir, ec);
|
||||
}
|
||||
|
||||
if (failures != 0) {
|
||||
std::println(std::cerr, "{} check(s) failed", failures);
|
||||
return 1;
|
||||
}
|
||||
std::println("ShouldParseBunqPayments: all checks passed");
|
||||
return 0;
|
||||
}
|
||||
367
tests/ShouldReadBankCallbacks/main.cpp
Normal file
367
tests/ShouldReadBankCallbacks/main.cpp
Normal file
|
|
@ -0,0 +1,367 @@
|
|||
/*
|
||||
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;
|
||||
}
|
||||
|
|
@ -6,27 +6,30 @@ 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.
|
||||
// The bank's callback endpoint, over real HTTP. It is how a transfer settles
|
||||
// within seconds in production, and the one place where bytes a stranger can
|
||||
// send become a line in the file orders are settled from.
|
||||
//
|
||||
// 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.
|
||||
// * ONLY a payment to the shop's own account credits anything. A payment on
|
||||
// another account, money going out, and a body the reader refuses each get
|
||||
// the bank its 200 and credit nothing.
|
||||
// * A payment is credited once, however often the bank sends it.
|
||||
// * Every callback leaves one line in the server's log, and the log says
|
||||
// nothing a callback carried beyond the credit itself: not the payer, not
|
||||
// the reference, and not the address, which is the endpoint's only secret.
|
||||
// * Without the bank's name the endpoint does not exist at all, and says so
|
||||
// at startup: an endpoint that answered 200 and credited nothing would
|
||||
// look like working.
|
||||
//
|
||||
// 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.
|
||||
// What the reader makes of each shape is pinned in ShouldReadBankCallbacks;
|
||||
// what only this suite can show is that the endpoint is wired to it.
|
||||
|
||||
import std;
|
||||
import Crafter.Network;
|
||||
|
|
@ -49,7 +52,7 @@ constexpr std::string_view kHook = "/hooks/bank/e2e-HOOKSECRET-0123456789abcdef"
|
|||
// copy, and not a badge, is the signal.
|
||||
constexpr std::string_view kPaidCopy = "Your donation funds";
|
||||
|
||||
constexpr std::string_view kShadow = "bank callback shadow: ";
|
||||
constexpr std::string_view kLine = "bank callback: ";
|
||||
|
||||
struct Sent {
|
||||
std::string_view id;
|
||||
|
|
@ -59,8 +62,8 @@ struct Sent {
|
|||
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.
|
||||
// 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": {{
|
||||
|
|
@ -83,8 +86,8 @@ std::string Callback(const Sent& s) {
|
|||
}})", 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.
|
||||
// A payment with only a few of its members: no time, no counterparty, and not
|
||||
// the account it is on. The reader refuses it whole.
|
||||
std::string Reduced(std::string_view id, std::string_view value) {
|
||||
return std::format(R"({{"NotificationUrl": {{"category": "MUTATION",
|
||||
"object": {{"Payment": {{"id": {}, "type": "EBA_SCT",
|
||||
|
|
@ -115,14 +118,7 @@ std::string NewestReference(const TestServer& srv) {
|
|||
return found;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc < 2) {
|
||||
std::println(std::cerr, "usage: ShouldReceiveBankCallbacks <server-binary>");
|
||||
return 2;
|
||||
}
|
||||
|
||||
ServerOptions Options(std::string_view bank) {
|
||||
ServerOptions options;
|
||||
// The REAL transfer rail in the bank slot, settling from the credits file
|
||||
// the callback endpoint writes. The crypto slot is off.
|
||||
|
|
@ -131,141 +127,171 @@ int main(int argc, char** argv) {
|
|||
{ "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", "" },
|
||||
{ "BANK_CALLBACK_PATH", std::string(kHook) },
|
||||
{ "BANK_CALLBACK_BANK", std::string(bank) },
|
||||
};
|
||||
TestServer srv(argv[1], 8223, options);
|
||||
return 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;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
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
|
||||
int main(int argc, char** argv) {
|
||||
if (argc < 2) {
|
||||
std::println(std::cerr, "usage: ShouldReceiveBankCallbacks <server-binary>");
|
||||
return 2;
|
||||
}
|
||||
|
||||
// ── 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));
|
||||
TestServer srv(argv[1], 8223, Options("bunq"));
|
||||
|
||||
// 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));
|
||||
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;
|
||||
};
|
||||
|
||||
// ── 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));
|
||||
Check(Logged("bank callback: enabled on a secret path") == 1,
|
||||
"the endpoint says at startup that it is on", 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);
|
||||
// ── 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(kLine) == 1, "nor is it read at all"); // the startup line
|
||||
}
|
||||
|
||||
// ── a payment to the shop ─────────────────────────────────────
|
||||
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: credited 5.00 via sepa (id 9000001)") == 1,
|
||||
"the log says what was credited", ReadFile(log));
|
||||
|
||||
// The bank retries, and a retry must not count twice.
|
||||
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: nothing credited: the payment was credited before") == 1,
|
||||
"the log says the retry added nothing", ReadFile(log));
|
||||
|
||||
// ── a payment on another account ──────────────────────────────
|
||||
//
|
||||
// The bank's own callback, from the bank's own addresses, about an
|
||||
// account that is not the shop's: what someone who registered this
|
||||
// address for an account of their own, and paid themselves, would
|
||||
// have the bank send.
|
||||
Check(Send(Callback({ .id = "9000004", .value = "5.00", .account = kOtherIban }))
|
||||
== "200",
|
||||
"a callback about another account is answered 200");
|
||||
Check(Credited("9000004") == 0, "and credits NOTHING", ReadFile(credits));
|
||||
Check(Logged("bank callback: nothing credited: the payment is on another account "
|
||||
"than the shop's") == 1,
|
||||
"the log says why", 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: nothing credited: the payment is not money coming in")
|
||||
== 1,
|
||||
"the log says why", ReadFile(log));
|
||||
|
||||
// ── what the reader refuses ───────────────────────────────────
|
||||
Check(Send(Reduced("9000002", "3.00")) == "200",
|
||||
"a payment without most of its members is answered 200");
|
||||
Check(Credited("9000002") == 0, "and credits nothing", ReadFile(credits));
|
||||
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: nothing credited: the body was refused: ") == 2,
|
||||
"the log says both were refused", 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");
|
||||
|
||||
// The same payment on someone else's account must not pay for it.
|
||||
const std::string note = std::format("betaling {} bedankt", reference);
|
||||
Check(Send(Callback({ .id = "9000011", .value = "7.00", .description = note,
|
||||
.account = kOtherIban })) == "200",
|
||||
"a callback quoting the order from another account is answered 200");
|
||||
std::this_thread::sleep_for(std::chrono::seconds(3));
|
||||
srv.BodyHas(path, "awaiting payment",
|
||||
"and the order is still unpaid after the reconciler has run");
|
||||
|
||||
Check(Send(Callback({ .id = "9000010", .value = "7.00", .description = note }))
|
||||
== "200",
|
||||
"the bank's callback for the payment to the shop 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");
|
||||
}
|
||||
|
||||
// ── one line a callback, and nothing a callback carried ───────
|
||||
{
|
||||
const std::string text = ReadFile(log);
|
||||
// Eight callbacks reached the endpoint, and the startup line makes nine.
|
||||
Check(CountOccurrences(text, kLine) == 9,
|
||||
"the log has one line 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("betaling"), 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── without the bank's name there is no endpoint ──────────────────
|
||||
//
|
||||
// The state a server is in when its configuration names the address and
|
||||
// not whose callbacks arrive there. It must not answer the bank 200 and
|
||||
// drop the payment: the bank would take that for delivered, and nothing
|
||||
// anywhere would say that no order can settle from a callback.
|
||||
{
|
||||
TestServer srv(argv[1], 8224, Options(""));
|
||||
const fs::path credits(srv.Orders().string() + ".transfer-credits.jsonl");
|
||||
const std::string logText = ReadFile(srv.Work() / "server.log");
|
||||
Check(logText.find("BANK_CALLBACK_BANK") != std::string::npos
|
||||
&& logText.find("the endpoint is NOT enabled") != std::string::npos,
|
||||
"a server not told whose callbacks arrive says the endpoint is off",
|
||||
logText);
|
||||
const std::string status =
|
||||
srv.Post(std::string(kHook), Callback({ .id = "9000020", .value = "5.00" }),
|
||||
"application/json").status;
|
||||
Check(status != "200", "and does not answer a callback as if it took it", status);
|
||||
Check(ReadFile(credits).find("9000020") == std::string::npos,
|
||||
"nor credit what it carried");
|
||||
}
|
||||
|
||||
return Finish();
|
||||
|
|
|
|||
|
|
@ -22,14 +22,14 @@ No permission is granted to copy, modify, distribute, or create derivative works
|
|||
//
|
||||
// So this needs no secret, never flakes on someone else's RPC, and runs on
|
||||
// every deploy unconditionally. The bank stands in for itself only in the sense
|
||||
// that the suite writes the credits file — the same file the bunq webhook and
|
||||
// tools/pull-and-ship-credits.sh fill from the real account, read by the same
|
||||
// parser, matched by the same matcher.
|
||||
// that the suite writes the credits file — the same file the bank's callback
|
||||
// and tools/pull-and-ship-credits.sh fill from the real account, read by the
|
||||
// same parser, matched by the same matcher.
|
||||
// Everything downstream of that line is production code.
|
||||
//
|
||||
// What the unit suites already cover, and this one deliberately does not
|
||||
// re-prove: the matching table (ShouldMatchBankTransfers) and bunq's payload
|
||||
// decoding (ShouldParseBunqPayments). What only this suite can show is that the
|
||||
// re-prove: the matching table (ShouldMatchBankTransfers) and what the bank's
|
||||
// callback comes to (ShouldReadBankCallbacks). What only this suite can show is that the
|
||||
// pieces are wired to each other — that the reference the PAGE prints is the
|
||||
// reference the MATCHER looks for, which is the seam where a rename or a
|
||||
// refactor would quietly break settlement while every unit test still passed.
|
||||
|
|
@ -182,9 +182,9 @@ int main(int argc, char** argv) {
|
|||
// "ideal" rather than "sepa" on purpose, and it is not fiction even
|
||||
// though checkout offers no iDEAL. `via` is not a method this shop
|
||||
// OFFERS — it is what the bank reports about how the money reached the
|
||||
// account, straight out of bunq's Payment.type. Money can arrive there
|
||||
// iDEAL-funded without our checkout being involved: an old bunq.me
|
||||
// link, or a bunq-to-bunq payment, both land in the same account, and
|
||||
// account, from the bank's own classification of the payment. Money
|
||||
// can arrive there iDEAL-funded without our checkout being involved: a
|
||||
// payment link of the bank's, for one, lands in the same account, and
|
||||
// the rail settles on the REFERENCE regardless of how the payer funded
|
||||
// it. Using a non-default method here is what proves that passthrough
|
||||
// works, which matters because the `via` column is what tells the
|
||||
|
|
|
|||
|
|
@ -152,7 +152,8 @@ public:
|
|||
// .env must not leak real provider keys into the test server — live
|
||||
// Sendcloud rates would silently change the shipping totals the
|
||||
// suites assert.
|
||||
for (const char* v : { "TRANSFER_IBAN", "EURC_CHAINS", "EURC_POOL",
|
||||
for (const char* v : { "TRANSFER_IBAN", "BANK_CALLBACK_PATH",
|
||||
"BANK_CALLBACK_BANK", "EURC_CHAINS", "EURC_POOL",
|
||||
"SENDCLOUD_PUBLIC_KEY", "SENDCLOUD_SECRET_KEY",
|
||||
"SENDCLOUD_METHOD",
|
||||
"INVOICE_GPG_KEY", "MAIL_COMMAND", "MAIL_FROM" }) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue