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>
158 lines
7.4 KiB
C++
158 lines
7.4 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.
|
|
*/
|
|
|
|
// MatchCredits: the decision that releases goods on the bank-transfer rail.
|
|
//
|
|
// Everything here is a case a real payer or a real bank actually produces. The
|
|
// reference travels through a human retyping it and a bank reformatting the
|
|
// field, so the interesting failures are all cosmetic-looking: a lower-case
|
|
// reference, a space inserted every four characters, the structured RF form
|
|
// quoted instead of the short one. Each of those arriving as "unpaid" would be
|
|
// money sitting in the account against an order the shop thinks was abandoned.
|
|
//
|
|
// The other half is refusing to over-match. A matcher that credits an order
|
|
// from money that was not for it is worse than one that misses: it ships goods
|
|
// nobody paid for.
|
|
|
|
import std;
|
|
import Catcrafts.Shared;
|
|
import Catcrafts.Server;
|
|
|
|
using namespace Catcrafts;
|
|
using Server::BankCredit;
|
|
|
|
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);
|
|
}
|
|
|
|
BankCredit Credit(std::string reference, std::int64_t amountMinor,
|
|
std::string method = "sepa", std::string id = "p1") {
|
|
BankCredit c;
|
|
c.id = std::move(id);
|
|
c.reference = std::move(reference);
|
|
c.amountMinor = amountMinor;
|
|
c.method = std::move(method);
|
|
return c;
|
|
}
|
|
|
|
// The braced lists below land in a vector on purpose. MatchCredits takes a
|
|
// span, and the libc++ 23 inside the Crafter.Build 2.1.0 Flatpak (what CI
|
|
// builds with) has no span(initializer_list) constructor; libc++ 22 had one
|
|
// under C++26, which is the only reason the lists ever compiled.
|
|
Server::TransferMatch Match(std::vector<BankCredit> credits, std::string_view reference) {
|
|
return Server::MatchCredits(credits, reference);
|
|
}
|
|
|
|
std::int64_t PaidFor(std::vector<BankCredit> credits, std::string_view reference) {
|
|
return Match(std::move(credits), reference).paidMinor;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
const std::string ref = "CC-2B6457";
|
|
|
|
// ── the forms a payer actually quotes ─────────────────────────────
|
|
Check(PaidFor({ Credit("CC-2B6457 catcrafts.net", 5000) }, ref) == 5000,
|
|
"the reference exactly as we printed it");
|
|
Check(PaidFor({ Credit("cc2b6457", 5000) }, ref) == 5000,
|
|
"lower case and no hyphen");
|
|
Check(PaidFor({ Credit("RF70CC2B6457", 5000) }, ref) == 5000,
|
|
"the structured RF form, whose body IS the reference");
|
|
Check(PaidFor({ Credit("CC 2B 64 57", 5000) }, ref) == 5000,
|
|
"a bank grouping the field into pairs");
|
|
Check(PaidFor({ Credit("Payment for order CC-2B6457, thanks!", 5000) }, ref) == 5000,
|
|
"the reference buried in a sentence");
|
|
Check(PaidFor({ Credit("betaling cc/2b/6457", 5000) }, ref) == 5000,
|
|
"separators a payer invented");
|
|
|
|
// ── refusing to over-match ────────────────────────────────────────
|
|
Check(PaidFor({ Credit("CC-2B6458", 5000) }, ref) == 0,
|
|
"one character different is a different order");
|
|
Check(PaidFor({ Credit("no reference at all", 5000) }, ref) == 0,
|
|
"a transfer with no reference pays for nothing");
|
|
Check(PaidFor({ Credit("", 5000) }, ref) == 0,
|
|
"an empty remittance field pays for nothing");
|
|
// The needle guard: an empty or near-empty reference must not match every
|
|
// credit on the account. This is the difference between one order settling
|
|
// and the whole ledger settling from a single payment.
|
|
Check(PaidFor({ Credit("anything", 5000), Credit("something", 900) }, "") == 0,
|
|
"an empty reference matches nothing");
|
|
Check(PaidFor({ Credit("anything", 5000) }, "CC") == 0,
|
|
"a too-short reference matches nothing");
|
|
|
|
// Outgoing money must never pay for an order. A refund we sent quotes the
|
|
// very reference of the order it refunds, so counting signed amounts
|
|
// blindly would let a refund settle the thing it reversed.
|
|
Check(PaidFor({ Credit("CC-2B6457 refund", -5000) }, ref) == 0,
|
|
"an outgoing payment quoting the reference is not income");
|
|
Check(PaidFor({ Credit("CC-2B6457", 5000), Credit("CC-2B6457 refund", -2000) },
|
|
ref) == 5000,
|
|
"a later refund does not reduce what arrived");
|
|
|
|
// ── partials accumulate ───────────────────────────────────────────
|
|
// The buyer is told to send the difference to the same IBAN with the same
|
|
// reference, so two credits for one order is a supported path and not an
|
|
// anomaly. Summing is what makes that instruction true.
|
|
Check(PaidFor({ Credit("CC-2B6457", 3000, "sepa", "a"),
|
|
Credit("CC-2B6457", 2000, "sepa", "b") }, ref) == 5000,
|
|
"two credits for one order are summed");
|
|
{
|
|
const Server::TransferMatch m = Match(
|
|
{ Credit("CC-2B6457", 3000, "sepa", "a"),
|
|
Credit("CC-2B6457", 2000, "sepa", "b") }, ref);
|
|
Check(m.count == 2, "the count reports how many credits carried it",
|
|
std::format("{}", m.count));
|
|
}
|
|
|
|
// ── the method reaches the ledger ─────────────────────────────────
|
|
// The `via` column decides whether an order is safe to ship: a plain SEPA
|
|
// transfer is final, and anything with a dispute window is not.
|
|
{
|
|
const Server::TransferMatch m =
|
|
Match({ Credit("CC-2B6457", 5000, "ideal") }, ref);
|
|
Check(m.method == "ideal", "the settling method is carried out", m.method);
|
|
}
|
|
|
|
// ── other orders' money is left alone ────────────────────────────
|
|
Check(PaidFor({ Credit("CC-AAAAAA", 90000), Credit("CC-2B6457", 5000),
|
|
Credit("CC-BBBBBB", 12345) }, ref) == 5000,
|
|
"only the credits quoting THIS reference are counted");
|
|
|
|
// ── the ambiguity tripwire ───────────────────────────────────────
|
|
// One transfer quoting two orders cannot be attributed by a per-order
|
|
// matcher: both orders would see the full amount and both would settle on
|
|
// the same money. The matcher cannot fix it, so it flags it for a human.
|
|
{
|
|
const Server::TransferMatch m =
|
|
Match({ Credit("CC-2B6457 and CC-AAAAAA", 10000) }, ref);
|
|
Check(m.ambiguous,
|
|
"a credit quoting a second order reference is flagged ambiguous");
|
|
const Server::TransferMatch clean =
|
|
Match({ Credit("CC-2B6457 thanks", 10000) }, ref);
|
|
Check(!clean.ambiguous, "an ordinary credit is not flagged");
|
|
}
|
|
|
|
// An empty account is a clean zero rather than anything alarming: it is
|
|
// simply the state of every order between checkout and payment.
|
|
Check(PaidFor({}, ref) == 0, "no credits at all is zero, not an error");
|
|
|
|
if (failures != 0) {
|
|
std::println(std::cerr, "{} check(s) failed", failures);
|
|
return 1;
|
|
}
|
|
std::println("ShouldMatchBankTransfers: all checks passed");
|
|
return 0;
|
|
}
|