// SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® // Moving money through the bunq connector, against a fake bunq (tests/Support/FakeBunq.inc): counterparties and what // they refuse, payments, draft payments, scheduled payments, requests both ways, and how each lists and formats. #include "../../lib/json.hpp" #include #include import std; import Nostro; import Crafter.Network; #include "../Support/Check.inc" #include "../Support/FakeBunq.inc" using namespace Nostro; using namespace std::chrono; using Test::Check; using Test::Equal; using Test::Throws; using json = nlohmann::json; using Fault = Fake::Fault; namespace { constexpr std::uint16_t port = 47303; constexpr std::string_view payerIban = "NL23BUNQ2025123456"; constexpr std::string_view payeeIban = "NL67BUNQ2025654321"; json LastBody(Fake::Bunq& bunq, std::string_view method) { const std::vector log = bunq.Log(); const auto last = std::ranges::find_if(log.rbegin(), log.rend(), [method](const Fake::Logged& entry) { return entry.method == method; }); return json::parse(last->body); } void Counterparties() { const BunqCounterparty email = BunqCounterparty::Parse(" someone@example.com ", "Someone"); Check(email.type == "EMAIL" && email.value == "someone@example.com" && email.name == "Someone", "an e-mail address, trimmed"); const BunqCounterparty phone = BunqCounterparty::Parse("+31 6-1234 (5678)", ""); Check(phone.type == "PHONE_NUMBER" && phone.value == "+31612345678", "a phone number without its spaces, dashes and brackets"); Throws([] { BunqCounterparty::Parse("+31x6", ""); }, "is not a phone number", "a phone number with a letter"); Throws([] { BunqCounterparty::Parse("+3161", ""); }, "is not a phone number", "a phone number too short"); const BunqCounterparty iban = BunqCounterparty::Parse(" nl23 bunq 2025 1234 56 ", "Payer"); Check(iban.type == "IBAN" && iban.value == payerIban, "an IBAN, upper case and without spaces"); for(const std::string_view bad : {"NL23BUNQ2025123457", "NL23BUNQ", "1L23BUNQ2025123456", "N123BUNQ2025123456", "NLX3BUNQ2025123456", "NL2XBUNQ2025123456", "NL23BUNQ2025-23456", "NL23BUNQ20251234561234567890123456"}) { Throws([bad] { BunqCounterparty::Parse(bad, ""); }, "is not a valid IBAN", std::format("'{}' is refused", bad)); } } void Payments(Fake::Bunq& bunq, BunqAccount& payer, BunqAccount& payee) { const std::uint64_t id = payer.Send(payeeIban, "Payee", 1250, Currency::Euro, "lunch \"together\""); Check(id != 0, "a payment gets an id"); const json sent = LastBody(bunq, "POST"); Check(sent.at("amount") == json{{"value", "12.50"}, {"currency", "EUR"}}, "the amount goes as a decimal string"); Check(sent.at("counterparty_alias") == json{{"type", "IBAN"}, {"value", payeeIban}, {"name", "Payee"}}, "the counterparty, with its name"); Equal(sent.at("description").get(), std::string("lunch \"together\""), "the description, quotes and all"); Check(bunq.Log().back().signed_ && bunq.Log().back().signatureValid, "a signed payment"); payee.connector->GetNewTransactions(payee); Equal(payee.FindTransactions(payee.StorageKey(), {}).at(0).amount, std::int64_t{1250}, "the payee gets it"); payer.Send("someone@example.com", "", 100, Currency::Euro, ""); Check(!LastBody(bunq, "POST").at("counterparty_alias").contains("name"), "no name, no name field"); Throws([&payer] { payer.Send(payeeIban, "Payee", 0, Currency::Euro, ""); }, "more than zero", "nothing to send"); Throws([&payer] { payer.Send(payeeIban, "Payee", -5, Currency::Euro, ""); }, "more than zero", "a negative amount"); } void Drafts(Fake::Bunq& bunq, BunqAccount& payer) { const std::uint64_t accepted = payer.Draft(payeeIban, "Payee", 200, Currency::Euro, "to accept"); const std::uint64_t rejected = payer.Draft(payeeIban, "Payee", 300, Currency::Euro, "to reject"); Check(LastBody(bunq, "POST").at("number_of_required_accepts") == 1, "one accept makes a draft a payment"); std::vector drafts = payer.DraftPayments(); Equal(drafts.size(), std::size_t{2}, "both drafts list"); Check(drafts.at(0).id == rejected && drafts.at(0).status == "PENDING" && drafts.at(0).createdBy == "Payer", "newest first, pending, with who made it"); Check(drafts.at(0).entries.size() == 1 && drafts.at(0).entries.at(0).amount == 300 && drafts.at(0).entries.at(0).counterparty.iban == payeeIban, "its entry"); Equal(std::format("{}", drafts.at(0)), std::format("{}\tPENDING\tPayer\n\tPayee {}\t€3.00\tto reject", rejected, payeeIban), "a draft formats with a line per entry"); const std::size_t paid = bunq.Count("POST", "/"); const BunqDraftPayment shown = payer.DraftPayment(accepted); Check(shown.id == accepted && shown.entries.at(0).amount == 200 && !shown.updated.empty(), "one draft, as bunq has it, with when it last changed"); payer.RespondToDraftPayment(shown, true); Check(LastBody(bunq, "PUT").at("status") == "ACCEPTED" && LastBody(bunq, "PUT").at("previous_updated_timestamp") == shown.updated, "accepting names the draft as it was shown"); payer.RespondToDraftPayment(payer.DraftPayment(rejected), false); Equal(LastBody(bunq, "PUT").at("status").get(), std::string("REJECTED"), "rejecting"); drafts = payer.DraftPayments(); Check(drafts.at(0).status == "REJECTED" && drafts.at(1).status == "ACCEPTED", "the drafts' new states"); Equal(bunq.Count("POST", "/"), paid, "a response is a PUT, not a new payment"); // A draft bunq lists without an updated time, a creator or entries. { std::lock_guard lock(bunq.mutex); json& draft = bunq.drafts[payer.id].at(0).at("DraftPayment"); draft.erase("updated"); bunq.drafts[payer.id].push_back(json{{"DraftPayment", json{{"id", 1}, {"status", "PENDING"}, {"entries", "none"}}}}); bunq.drafts[payer.id].push_back(json{{"DraftPayment", json{{"id", 3}, {"status", "PENDING"}, {"entries", json::array({json{{"amount", Fake::Amount(250)}, {"counterparty_alias", json{{"display_name", "Positive"}}}}})}}}}); bunq.drafts[payer.id].push_back(json{{"SomethingElse", json{{"id", 2}}}}); } payer.RespondToDraftPayment(payer.DraftPayment(rejected), false); Check(!LastBody(bunq, "PUT").contains("previous_updated_timestamp"), "a draft without an updated time is answered without one"); drafts = payer.DraftPayments(); Equal(drafts.size(), std::size_t{4}, "an object that is no draft is skipped"); Check(drafts.at(2).createdBy.empty() && drafts.at(2).entries.empty(), "a draft without a creator or entries"); Check(drafts.at(3).entries.at(0).amount == 250, "an entry bunq lists as positive stays positive"); // A draft that changed after it was shown is refused, not accepted unseen. const BunqDraftPayment before = payer.DraftPayment(payer.Draft(payeeIban, "Payee", 400, Currency::Euro, "to change")); { std::lock_guard lock(bunq.mutex); json& draft = bunq.drafts[payer.id].at(0).at("DraftPayment"); draft.at("entries").at(0)["amount"] = Fake::Amount(-40000); draft["updated"] = Fake::Time(999); } Throws([&payer, &before] { payer.RespondToDraftPayment(before, true); }, "changed in the meantime", "accepting a draft that changed since it was shown"); } void Schedules(Fake::Bunq& bunq, BunqAccount& payer) { const system_clock::time_point start = sys_days(year(2027) / 1 / 1); const std::uint64_t monthly = payer.Schedule(payeeIban, "Payee", 300, Currency::Euro, "rent", BunqSchedule{.start = start, .end = start + days(365), .unit = "MONTHLY", .every = 1}); const json body = LastBody(bunq, "POST"); Check(body.at("schedule") == json{{"time_start", "2027-01-01 00:00:00.000000"}, {"time_end", "2028-01-01 00:00:00.000000"}, {"recurrence_unit", "MONTHLY"}, {"recurrence_size", 1}}, "the schedule in UTC"); Check(body.at("payment").at("amount") == json{{"value", "3.00"}, {"currency", "EUR"}}, "the payment it makes"); const std::uint64_t weekly = payer.Schedule(payeeIban, "Payee", 400, Currency::Euro, "allowance", BunqSchedule{.start = start, .unit = "WEEKLY", .every = 2}); Check(!LastBody(bunq, "POST").at("schedule").contains("time_end"), "no end, no end time"); Throws([&] { payer.Schedule(payeeIban, "Payee", 1, Currency::Euro, "", BunqSchedule{.start = start, .unit = "FORTNIGHTLY"}); }, "is not a schedule", "an unknown recurrence"); Throws([&] { payer.Schedule(payeeIban, "Payee", 1, Currency::Euro, "", BunqSchedule{.start = start, .unit = "DAILY", .every = 0}); }, "at least every 1", "every 0"); std::vector scheduled = payer.ScheduledPayments(); Equal(scheduled.size(), std::size_t{2}, "both schedules list"); Check(scheduled.at(0).id == weekly && scheduled.at(0).amount == 400 && scheduled.at(0).schedule.unit == "WEEKLY" && scheduled.at(0).schedule.every == 2 && !scheduled.at(0).schedule.end, "the weekly one, its amount positive"); Check(scheduled.at(1).id == monthly && scheduled.at(1).schedule.end == start + days(365) && scheduled.at(1).schedule.next == start && scheduled.at(1).schedule.status == "ACTIVE", "the monthly one, with its end and next run"); { std::lock_guard lock(bunq.mutex); bunq.scheduledName = "SchedulePayment"; bunq.schedules[payer.id].push_back(json{{"ScheduledPayment", json{{"id", 3}, {"payment", json{{"amount", Fake::Amount(100)}, {"counterparty_alias", json{{"display_name", "Old"}}}}}, {"schedule", json{{"time_start", "2026-01-01 00:00:00"}, {"recurrence_unit", "ONCE"}, {"status", "FINISHED"}}}}}}); } scheduled = payer.ScheduledPayments(); Equal(scheduled.size(), std::size_t{3}, "listed under the spec's name too"); Check(scheduled.back().schedule.status == "FINISHED" && !scheduled.back().schedule.next && scheduled.back().schedule.every == 1, "a status inside the schedule, no next run, every 1 by default"); Equal(scheduled.back().amount, std::int64_t{100}, "an amount bunq lists as positive stays positive"); payer.CancelScheduledPayment(weekly); Equal(payer.ScheduledPayments().size(), std::size_t{2}, "a cancelled schedule is gone"); } void Requests(Fake::Bunq& bunq, BunqAccount& payer, BunqAccount& payee) { const BunqPaymentRequest asked = payee.Request(payerIban, "Payer", 500, Currency::Euro, "pay me back"); Check(asked.status == "PENDING" && asked.amount == 500 && !asked.amountPaid && asked.shareUrl.starts_with("https://bunq.me/"), "a request, with a bunq.me link"); Check(LastBody(bunq, "POST").at("allow_bunqme") == true, "bunq.me is allowed"); std::vector incoming = payer.IncomingRequests(); Equal(incoming.size(), std::size_t{1}, "the payer sees it"); Check(incoming.at(0).type == "INTERNAL" && incoming.at(0).status == "PENDING" && incoming.at(0).counterparty.iban == payeeIban, "who asks, and how"); const BunqIncomingRequest one = payer.IncomingRequest(incoming.at(0).id); Check(one.id == incoming.at(0).id && one.amount == 500 && one.counterparty.iban == payeeIban, "one request, as bunq has it"); payer.RespondToRequest(incoming.at(0), true); Check(LastBody(bunq, "PUT").at("amount_responded") == json{{"value", "5.00"}, {"currency", "EUR"}}, "accepting pays the amount asked"); const BunqPaymentRequest paid = payee.Requests().at(0); Check(paid.status == "ACCEPTED" && paid.amountPaid == 500, "the asker sees it paid"); payee.Request(payerIban, "Payer", 600, Currency::Euro, "again"); payer.RespondToRequest(payer.IncomingRequests().at(0), false); Equal(LastBody(bunq, "PUT"), json{{"status", "REJECTED"}}, "rejecting"); const BunqPaymentRequest revoked = payee.Request(payerIban, "Payer", 700, Currency::Euro, "never mind"); payee.RevokeRequest(revoked.id); Equal(LastBody(bunq, "PUT"), json{{"status", "REVOKED"}}, "revoking"); const std::vector requests = payee.Requests(); Check(requests.at(0).status == "REVOKED" && requests.at(1).status == "REJECTED", "the requests' states"); // A request whose amount changes after it was shown is paid as shown. payee.Request(payerIban, "Payer", 1000, Currency::Euro, "changes"); const BunqIncomingRequest shown = payer.IncomingRequests().at(0); { std::lock_guard lock(bunq.mutex); bunq.responses[payer.id].at(0).at("RequestResponse")["amount_inquired"] = Fake::Amount(99900); } payer.RespondToRequest(shown, true); Check(LastBody(bunq, "PUT").at("amount_responded") == json{{"value", "10.00"}, {"currency", "EUR"}}, "accepting pays the amount shown, not the one asked since"); // A request that was made but cannot be read back is still the request it is, and says so in a note. std::vector notes; const std::function stderrSink = noteSink; noteSink = [¬es](std::string_view note) { notes.emplace_back(note); }; bunq.Fail(Fault{.method = "GET", .path = std::format("/user/{}/monetary-account/{}/request-inquiry/", payee.connector->id, payee.id), .status = 500, .body = "{}"}); const BunqPaymentRequest unread = payee.Request(payerIban, "Payer", 800, Currency::Euro, "unread"); noteSink = stderrSink; Check(unread.id != 0 && unread.status == "PENDING" && unread.counterparty.iban == payerIban && unread.shareUrl.empty(), "made, pending, without a link"); Check(notes.size() == 1 && notes.at(0).contains("was made, but reading it back failed"), "the failed read-back goes to the note sink"); bunq.Fail(Fault{.method = "GET", .path = std::format("/user/{}/monetary-account/{}/request-inquiry/", payee.connector->id, payee.id), .status = 500, .body = "{}"}); Check(payee.Request("someone@example.com", "Someone", 900, Currency::Euro, "").counterparty.iban.empty(), "one asked by e-mail has no IBAN"); Throws([&payee] { payee.Request(payerIban, "Payer", 0, Currency::Euro, ""); }, "more than zero", "asking for nothing"); { std::lock_guard lock(bunq.mutex); bunq.inquiries[payee.id].push_back(json{{"RequestInquiry", json{{"id", 1}, {"amount_inquired", Fake::Amount(100)}, {"counterparty_alias", json::object()}}}}); bunq.inquiries[payee.id].push_back(json{{"Other", json{{"id", 2}}}}); } const BunqPaymentRequest bare = payee.Requests().back(); Check(bare.created == system_clock::time_point() && bare.status.empty(), "a request without a time or status"); } void Formats() { const system_clock::time_point at = sys_days(year(2026) / 10 / 31) + hours(9); Equal(std::format("{}", BunqLabel{"Name", "NL01"}), std::string("Name NL01"), "a label with an IBAN"); Equal(std::format("{}", BunqLabel{"Name", ""}), std::string("Name"), "a label without one"); BunqPaymentRequest request{.id = 1, .status = "ACCEPTED", .counterparty = {"Payer", ""}, .currency = Currency::Euro, .amount = 500, .amountPaid = 500, .description = "pay", .created = at, .shareUrl = ""}; Equal(std::format("{}", request), std::string("1\t2026-10-31 09:00\tACCEPTED\tPayer\t€5.00\tpay"), "a request paid in full"); request.amountPaid = 400; request.shareUrl = "https://bunq.me/x"; Equal(std::format("{}", request), std::string("1\t2026-10-31 09:00\tACCEPTED\tPayer\t€5.00 (paid €4.00)\tpay\thttps://bunq.me/x"), "a request paid in part, with its link"); const BunqIncomingRequest incoming{.id = 2, .status = "PENDING", .type = "DIRECT_DEBIT", .counterparty = {"Utility", "NL02"}, .currency = Currency::Euro, .amount = 3000, .description = "energy", .created = at}; Equal(std::format("{}", incoming), std::string("2\t2026-10-31 09:00\tPENDING\tDIRECT_DEBIT\tUtility NL02\t€30.00\tenergy"), "an incoming request"); BunqScheduledPayment scheduled{.id = 3, .counterparty = {"Landlord", ""}, .currency = Currency::Euro, .amount = 90000, .description = "rent", .schedule = BunqSchedule{.start = at, .end = std::nullopt, .unit = "ONCE", .every = 1, .status = "ACTIVE", .next = at}}; Equal(std::format("{}", scheduled), std::string("3\tACTIVE\tonce on 2026-10-31 09:00, next 2026-10-31 09:00\tLandlord\t€900.00\trent"), "a payment scheduled once"); scheduled.schedule.unit = "MONTHLY"; scheduled.schedule.end = at + days(1); scheduled.schedule.status = "FINISHED"; Equal(std::format("{}", scheduled), std::string("3\tFINISHED\tmonthly from 2026-10-31 09:00 until 2026-11-01 09:00\tLandlord\t€900.00\trent"), "a finished monthly one, without its next run"); for(const auto& [unit, words] : std::map{{"HOURLY", "every 3 hours"}, {"DAILY", "every 3 days"}, {"WEEKLY", "every 3 weeks"}, {"MONTHLY", "every 3 months"}, {"YEARLY", "every 3 years"}}) { scheduled.schedule.unit = unit; scheduled.schedule.every = 3; Check(std::format("{}", scheduled).contains(words), std::format("{} every 3 is '{}'", unit, words)); } } } int main() { const Test::Directory directory("money"); configPath = directory.path / "config"; cachePath = directory.path / "cache"; setenv("TZ", "UTC", 1); tzset(); Fake::Bunq bunq(port); bunq.Redirect(); bunq.AddAccount(bunq.AddPerson("Payer", "key-payer"), payerIban, "Payer", 100000); bunq.AddAccount(bunq.AddPerson("Payee", "key-payee"), payeeIban, "Payee", 0); BunqConnector::Create("key-payer", "*", BunqEnvironment::Sandbox); BunqConnector::Create("key-payee", "*", BunqEnvironment::Sandbox); BunqAccount& payer = BunqConnector::connectors.at(0).accounts.at(0); BunqAccount& payee = BunqConnector::connectors.at(1).accounts.at(0); Counterparties(); Payments(bunq, payer, payee); Drafts(bunq, payer); Schedules(bunq, payer); Requests(bunq, payer, payee); Formats(); return Test::Result(); }