feat: open payto links on the pay form, and take the checkout over D-Bus out
An app could have nostrod pay an order: it called Pay of net.catcrafts.Nostro1.Checkout on the system bus, and nostrod paid from an account the user had chosen, after a dialog of its own. On 2026-11-22 the owner decided that a link takes its place. The app registers for payto links (RFC 8905), so that an app or a website can hand one to the desktop, and shows a link as its pay form filled in. The link: - The app's desktop entry takes the scheme (Exec=nostro-app %u, MimeType=x-scheme-handler/payto), and nostro-app takes an argument that is no option as a link: it overwrites it in argv and hands it to App::Open once the app has started. - ReadPaymentLink (Nostro.App:Forms) reads payto://iban/<IBAN> with the options amount, receiver-name and message, and refuses a link rather than guess at it, in a sentence of the app's own with nothing of the link in it. Among what it refuses: an IBAN whose check digits do not hold, one of the three options given twice, instruction, a comma in the amount, and a name or a message with a code point that is Unshown, or Blank and no plain space. - A link that is read waits until the accounts are read and DefaultCheckoutAccount has answered or failed, with a line on the accounts' page. Then App::TakeLink fills in the pay form, which says in a banner that a link filled it in, and with more than one account opens it under the choice of accounts that the form's From row has. A link pays nothing: the form's button sends, and polkit's dialog confirms as for any payment (design rule 12, and 14, which is new). - Unshown and Blank are the two tables the worker's Shown() works from. They move into Nostro:Utils, where the app's reader asks them too. The move is within the library. The reader holds a link's name to those two and to no more of Shown(): a name with an & is read, and the dialog shows it with a + as it does any name. The checkout: - nostrod serves one interface under one bus name. Gone are net.catcrafts.Nostro1.Checkout with its XML and its activation file, the polkit action Checkout, the module Nostro.Checkout with --product=checkout, and the worker's record of orders. That changes the published contract after a release, 0.0.0, and design rule 10 records it as the owner's decision. - DefaultCheckoutAccount and SetDefaultCheckoutAccount keep their names and signatures. The account they keep is where a link's pay form starts: nostrod's dialog for choosing it says "have pay links start from", the app's setting reads "Pay links start from", and `nostro checkout account` prints "pay links start from". - The D-Bus policy still lets nostrod own the old name: the nostrod of 0.0.0 asks for it when it starts, and the system files can be newer than the Flatpak it runs from. install.sh removes the old name's activation file. A user's checkouts.cdb is read by nothing any more, and is left where it is. - The Arch package stops installing the checkout's two files, and its pkgrel goes up to 2. Where makepkg -si finds a package of the version already built it installs that one again, which would bring the activation file back. The guide now says that pkgrel goes up with a change to what the package installs. - Bus::Interface goes too: the worker called it in two places, to tell a call to the checkout from one to the main interface. The review's own files follow. gate0 loses the build of --product=checkout, and the three lens sections that named the checkout no longer do, and ask about a link. The README has "Pay links" in place of "Checkout for apps", and AGENTS.consumer.md says what the app reads of a link. Its line on lib/json.hpp no longer gives a release: the file is of none, and the guide names its commit. The pay form and the accounts' page change only by a banner each, shown while a link is in play, which docs/screenshots/take.sh never has: the README's pictures stand. Tests: ShouldValidateForms reads and refuses links and fills the form from one. ShouldDriveTheApp opens links against a real nostrod on the fake bus: before the accounts are known, with no account, with one and with several, with the accounts known and the system bus gone, for a second user whose first connection pays their own accounts only, one that cannot be read, one whose name has an &, and through polkit's dialog to the payment. It now counts nostrod's answers to hold that the account pay links start from is read again once a connection is removed: the check it had there passed without that. ShouldHandleMoneyAndText holds the two tables at the ends of their ranges. ShouldServeEveryUser holds that nostrod serves no checkout, that the bus's policy lets an older nostrod own the old name, and the account pay links start from. ShouldRunCommands holds the CLI's wording. The review's gate0 passes at this commit: the version check, 17 tests, the builds of the CLI, the library, nostrod, the fake system, the app and its --debug-api build, and five lint runs that report nothing. tests/coverage.sh reports lines, functions and regions at 100.00%. Looked at on 2026-11-22 on the made-up system (run fake), on a desktop (KWin at a scale of 1.85). At 420x760, at --scale 3 --size 360x760 and at 1100x720 in the light look: a link's pay form under the choice of accounts, the form after a tap beside that choice, and the accounts' page while a link waits, its line above the rows that stand in for the accounts (--bank-delay 10000). At 420x760: that line above what the page says when the accounts cannot be read (--polkit deny) and above the accounts themselves (--polkit slow, the link opened in the running app), the settings' row and page for the account pay links start from, the sheet for a link that cannot be read, and with --empty the sheet for no account.
This commit is contained in:
parent
25d991658f
commit
b43ab124a6
42 changed files with 1234 additions and 994 deletions
|
|
@ -36,10 +36,6 @@ std::string Bus::Sender(sd_bus_message* message) {
|
|||
return Text(sd_bus_message_get_sender(message));
|
||||
}
|
||||
|
||||
std::string Bus::Interface(sd_bus_message* message) {
|
||||
return Text(sd_bus_message_get_interface(message));
|
||||
}
|
||||
|
||||
std::string Bus::Member(sd_bus_message* message) {
|
||||
return Text(sd_bus_message_get_member(message));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -77,8 +77,8 @@ namespace {
|
|||
if(changes && (name == SD_BUS_ERROR_NO_REPLY || name == SD_BUS_ERROR_TIMEOUT)) {
|
||||
throw OutcomeUnknown(std::format("nostrod did not answer in time ({}): what you asked for may have been carried out, check before trying again", message));
|
||||
}
|
||||
// bunq's error to a write that may have gone through all the same says so at its end, as the checkout's
|
||||
// published interface documents for net.catcrafts.Nostro1.Error.Bank.
|
||||
// bunq's error to a write that may have gone through all the same says so at its end, as the published
|
||||
// interface documents for net.catcrafts.Nostro1.Error.Bank.
|
||||
if(changes && name == errorBank && message.ends_with(BunqError::maybeCarriedOut)) {
|
||||
throw OutcomeUnknown(message);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,8 +13,6 @@ module;
|
|||
|
||||
module Nostro:DaemonWorker_impl;
|
||||
import std;
|
||||
import Crafter.DB;
|
||||
import Nostro.Checkout;
|
||||
import :Daemon;
|
||||
import :Bus;
|
||||
import :Account;
|
||||
|
|
@ -27,48 +25,17 @@ import :Utils;
|
|||
|
||||
using namespace Nostro;
|
||||
using namespace Nostro::Bus;
|
||||
using namespace Crafter;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
// One user's worker. Every call is read and checked first (the account, the counterparty, what is to be paid), then
|
||||
// polkit decides with a summary built from the worker's own data, and only then does the call run. On nostrod's own
|
||||
// interface nothing of the user's is read, and the bank is asked nothing, for a caller polkit has not said yes to: a
|
||||
// call that must look something up to word its question asks first whether the caller may see the accounts
|
||||
// (HandleLookup). The checkout is the one that reads before it asks: an order it knows is answered from its record,
|
||||
// and the account that pays a new one is chosen for the dialog to name (OnPay). The wire carries bunq's types, so the
|
||||
// worker works with bunq's connector directly.
|
||||
// polkit decides with a summary built from the worker's own data, and only then does the call run. Nothing of the
|
||||
// user's is read, and the bank is asked nothing, for a caller polkit has not said yes to: a call that must look
|
||||
// something up to word its question asks first whether the caller may see the accounts (HandleLookup). That has no
|
||||
// exception: the one it had, a checkout over D-Bus that answered an order it knew before anyone was asked, went on
|
||||
// 2026-11-22. The wire carries bunq's types, so the worker works with bunq's connector directly.
|
||||
|
||||
namespace {
|
||||
using Work = std::function<void(sd_bus_message* reply)>;
|
||||
// What runs once a call that went through polkit is answered, with the answer it got.
|
||||
using After = std::function<void(sd_bus_message* answer)>;
|
||||
|
||||
// An order paid at checkout, or being paid, which makes a retry safe. It is written before the payment is sent,
|
||||
// with no payment yet, so a worker that dies or a bank whose answer is lost leaves it saying the order may have been
|
||||
// paid. This is the table's schema: a change to it bumps `version` (see the Crafter.DB notes).
|
||||
struct CheckoutRecord {
|
||||
std::string payee; // the payee's IBAN, without spaces
|
||||
std::string reference; // the app's reference for the order
|
||||
std::int64_t amount = 0; // in the currency's minor unit
|
||||
Currency currency = Currency::Euro;
|
||||
std::string account; // the IBAN it is paid from
|
||||
std::uint64_t payment = 0; // the bank's id of the payment, 0 while the bank's answer is unknown
|
||||
std::chrono::system_clock::time_point time;
|
||||
|
||||
static constexpr std::uint32_t version = 1;
|
||||
};
|
||||
static_assert(DB::Layout<CheckoutRecord>::rowSize == 80);
|
||||
static_assert(DB::Layout<CheckoutRecord>::fingerprint == 0x87a27f8a68f38c52);
|
||||
|
||||
// Orders are told apart by payee and reference: an app's references are its own.
|
||||
using OrderKey = std::pair<std::string, std::string>;
|
||||
|
||||
// An order whose call is under way, and the calls for the same order that wait for its answer.
|
||||
struct Paying {
|
||||
std::int64_t amount = 0;
|
||||
Currency currency = Currency::Euro;
|
||||
std::vector<MessagePtr> waiting;
|
||||
};
|
||||
|
||||
struct Worker {
|
||||
DaemonOptions options;
|
||||
|
|
@ -76,9 +43,8 @@ namespace {
|
|||
std::string name;
|
||||
fs::path directory;
|
||||
sd_event* event = nullptr;
|
||||
// Before the connection they are registered with, which then goes first however RunWorker ends.
|
||||
// Before the connection it is registered with, which then goes first however RunWorker ends.
|
||||
std::vector<sd_bus_vtable> vtable;
|
||||
std::vector<sd_bus_vtable> checkoutVtable;
|
||||
BusPtr link;
|
||||
BusPtr system;
|
||||
sd_event_source* idle = nullptr;
|
||||
|
|
@ -87,9 +53,6 @@ namespace {
|
|||
bool loaded = false;
|
||||
std::size_t checking = 0;
|
||||
std::uint64_t checks = 0;
|
||||
// The checkouts paid, opened with the first; and the orders being paid now.
|
||||
std::optional<DB::Table<CheckoutRecord>> records;
|
||||
std::map<OrderKey, Paying> paying;
|
||||
};
|
||||
|
||||
// Answered as AccessDenied.
|
||||
|
|
@ -102,11 +65,6 @@ namespace {
|
|||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
// A checkout, and no account to pay it from.
|
||||
struct NoCheckoutAccount : std::runtime_error {
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
// The library's warnings, sent with the next answer that is not an error. The process's own, as noteSink is.
|
||||
std::vector<std::string> notes;
|
||||
|
||||
|
|
@ -150,9 +108,6 @@ namespace {
|
|||
} catch(const Refused& e) {
|
||||
name = SD_BUS_ERROR_ACCESS_DENIED;
|
||||
message = e.what();
|
||||
} catch(const NoCheckoutAccount& e) {
|
||||
name = errorNoCheckoutAccount;
|
||||
message = e.what();
|
||||
} catch(const OAuthPaymentRefused& e) {
|
||||
name = errorOAuthCannotPay;
|
||||
message = e.what();
|
||||
|
|
@ -165,13 +120,6 @@ namespace {
|
|||
} catch(const std::exception& e) {
|
||||
message = e.what();
|
||||
}
|
||||
// The connector says which call went wrong, "bunq POST /user/…/monetary-account/…/payment: …", and that names
|
||||
// the user and the account at the bank. An app at checkout never sees the account, so it is told what the bank
|
||||
// said and whether the payment may have been made all the same, and not what was asked of the bank.
|
||||
const std::size_t said = message.find(": ");
|
||||
if(Interface(call) == Checkout::interfaceName && message.starts_with("bunq ") && said != std::string::npos) {
|
||||
message.replace(0, said, "bunq");
|
||||
}
|
||||
return ErrorReply(call, name, message);
|
||||
}
|
||||
|
||||
|
|
@ -179,38 +127,18 @@ namespace {
|
|||
sd_bus_send(nullptr, answer.get(), nullptr);
|
||||
}
|
||||
|
||||
// The most a password dialog shows of a name, a description or a reference, in characters; and of an address,
|
||||
// which it shows whole: the longest an e-mail address gets.
|
||||
// The most a password dialog shows of a name, in characters; and of an address, which it shows whole: the longest
|
||||
// an e-mail address gets.
|
||||
constexpr std::size_t shownLimit = 64;
|
||||
constexpr std::size_t addressLimit = 254;
|
||||
|
||||
using Range = std::pair<std::uint32_t, std::uint32_t>;
|
||||
|
||||
// What draws nothing, or tells the reader's own text where to go: control and format characters, the line and
|
||||
// paragraph separators, what Unicode leaves out of a display by default (soft hyphens, joiners, fillers, variation
|
||||
// selectors, tags), surrogates, private use, and what is no character at all.
|
||||
constexpr std::array<Range, 31> unshown = {{
|
||||
{0x0000, 0x001F}, {0x007F, 0x009F}, {0x00AD, 0x00AD}, {0x034F, 0x034F}, {0x0600, 0x0605}, {0x061C, 0x061C}, {0x06DD, 0x06DD}, {0x070F, 0x070F},
|
||||
{0x0890, 0x0891}, {0x08E2, 0x08E2}, {0x115F, 0x1160}, {0x17B4, 0x17B5}, {0x180B, 0x180F}, {0x200B, 0x200F}, {0x2028, 0x202E}, {0x2060, 0x206F},
|
||||
{0x3164, 0x3164}, {0xD800, 0xF8FF}, {0xFDD0, 0xFDEF}, {0xFE00, 0xFE0F}, {0xFEFF, 0xFEFF}, {0xFFA0, 0xFFA0}, {0xFFF0, 0xFFFC}, {0xFFFE, 0xFFFF},
|
||||
{0x110BD, 0x110BD}, {0x110CD, 0x110CD}, {0x13430, 0x1343F}, {0x1BCA0, 0x1BCA3}, {0x1D173, 0x1D17A}, {0xE0000, 0xE0FFF}, {0xF0000, 0xFFFFFFFF},
|
||||
}};
|
||||
|
||||
// A space of any width, and the Braille pattern without a dot, which is one to the eye.
|
||||
constexpr std::array<Range, 8> blank = {{
|
||||
{0x0020, 0x0020}, {0x00A0, 0x00A0}, {0x1680, 0x1680}, {0x2000, 0x200A}, {0x202F, 0x202F}, {0x205F, 0x205F}, {0x2800, 0x2800}, {0x3000, 0x3000},
|
||||
}};
|
||||
|
||||
bool Among(std::span<const Range> ranges, std::uint32_t code) {
|
||||
return std::ranges::any_of(ranges, [code](const Range& range) { return code >= range.first && code <= range.second; });
|
||||
}
|
||||
|
||||
// Text from a caller or a bank, made fit for a password dialog, which is where the user reads what a password is
|
||||
// for. Nothing of `unshown`, so no text hides, turns around or starts a line of its own; a space of any kind is one
|
||||
// plain space, and none leads or trails, so none pushes what follows onto another line; nothing a dialog could take
|
||||
// for markup, or polkit for one of its $(…) fields; and at most `limit` characters, so a long name cannot push what
|
||||
// matters out of sight. An & goes with < and >: an agent that reads its message as Pango markup, as GNOME's older
|
||||
// one and MATE's do, shows nothing at all of a message with a bare & in it, the amount included.
|
||||
// for. Nothing that is Unshown (the library's list, which the app holds a link's text to as well), so no text
|
||||
// hides, turns around or starts a line of its own; a space of any kind, whatever is Blank, is one plain space, and
|
||||
// none leads or trails, so none pushes what follows onto another line; nothing a dialog could take for markup, or
|
||||
// polkit for one of its $(…) fields; and at most `limit` characters, so a long name cannot push what matters out
|
||||
// of sight. An & goes with < and >: an agent that reads its message as Pango markup, as GNOME's older one and
|
||||
// MATE's do, shows nothing at all of a message with a bare & in it, the amount included.
|
||||
std::string Shown(std::string_view text, std::size_t limit = shownLimit) {
|
||||
// Each character as it is shown, up to the first that does not fit, which is there to say that there is more.
|
||||
std::vector<std::string_view> shown;
|
||||
|
|
@ -227,10 +155,10 @@ namespace {
|
|||
}
|
||||
const std::string_view character = text.substr(i, length);
|
||||
i += length;
|
||||
if(Among(unshown, code)) {
|
||||
if(Unshown(code)) {
|
||||
continue;
|
||||
}
|
||||
if(Among(blank, code)) {
|
||||
if(Blank(code)) {
|
||||
spaced = !shown.empty();
|
||||
continue;
|
||||
}
|
||||
|
|
@ -399,8 +327,8 @@ namespace {
|
|||
throw std::invalid_argument(std::format("no account is called or has IBAN '{}'", key));
|
||||
}
|
||||
|
||||
// The user's connectors, read for the first call polkit says yes to, or the first new order at checkout: that call
|
||||
// also records whose directory this is. Reading them refreshes each one's accounts, which asks the bank.
|
||||
// The user's connectors, read for the first call polkit says yes to: that call also records whose directory this
|
||||
// is. Reading them refreshes each one's accounts, which asks the bank.
|
||||
void Load(Worker& worker) {
|
||||
if(worker.loaded) {
|
||||
return;
|
||||
|
|
@ -415,17 +343,13 @@ namespace {
|
|||
worker.loaded = true;
|
||||
}
|
||||
|
||||
// Runs `work` and makes the answer to `call` of what it appended, and of the notes, which only nostrod's own
|
||||
// interface carries: they are the user's, and an app at checkout is someone else.
|
||||
// Runs `work` and makes the answer to `call` of what it appended, and of the notes.
|
||||
MessagePtr Answer(sd_bus_message* call, const Work& work) {
|
||||
sd_bus_message* raw = nullptr;
|
||||
ThrowOnSdBus(sd_bus_message_new_method_return(call, &raw), "answer a call");
|
||||
MessagePtr reply(raw);
|
||||
work(raw);
|
||||
const std::vector<std::string> said = std::exchange(notes, {});
|
||||
if(Interface(call) == interfaceName) {
|
||||
Append(raw, said);
|
||||
}
|
||||
Append(raw, std::exchange(notes, {}));
|
||||
return reply;
|
||||
}
|
||||
|
||||
|
|
@ -433,7 +357,6 @@ namespace {
|
|||
Worker* worker = nullptr;
|
||||
MessagePtr call;
|
||||
Work work;
|
||||
After after;
|
||||
// What a yes is followed by in place of an answer: the rest of a call that asked before it looked (HandleLookup).
|
||||
std::function<void()> next;
|
||||
std::string action;
|
||||
|
|
@ -492,17 +415,13 @@ namespace {
|
|||
answer = ErrorFor(call, std::current_exception());
|
||||
}
|
||||
Deliver(answer);
|
||||
if(check->after) {
|
||||
check->after(answer.get());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Asks polkit whether the caller may do what `prompt` says. On a yes it runs `next` when there is one, which
|
||||
// answers the call itself, and else runs `work` and answers with what that appended; on anything else it answers
|
||||
// why not. Then it runs `after` with the answer it gave. The check does not block: other calls are served while a
|
||||
// password dialog is open.
|
||||
std::int32_t Ask(Worker& worker, sd_bus_message* call, const Prompt& prompt, Work work, After after, std::function<void()> next) {
|
||||
// why not. The check does not block: other calls are served while a password dialog is open.
|
||||
std::int32_t Ask(Worker& worker, sd_bus_message* call, const Prompt& prompt, Work work, std::function<void()> next) {
|
||||
const bool interactive = sd_bus_message_get_allow_interactive_authorization(call) > 0;
|
||||
sd_bus_message* raw = nullptr;
|
||||
ThrowOnSdBus(sd_bus_message_new_method_call(worker.system.get(), &raw, "org.freedesktop.PolicyKit1", "/org/freedesktop/PolicyKit1/Authority", "org.freedesktop.PolicyKit1.Authority", "CheckAuthorization"), "ask polkit");
|
||||
|
|
@ -536,7 +455,7 @@ namespace {
|
|||
std::string cancellation = std::format("nostro-{}-{}", getpid(), ++worker.checks);
|
||||
Append(raw, cancellation);
|
||||
|
||||
std::unique_ptr<Check> check = std::make_unique<Check>(&worker, MessagePtr(sd_bus_message_ref(call)), std::move(work), std::move(after), std::move(next), std::string(prompt.action), interactive, std::move(cancellation));
|
||||
std::unique_ptr<Check> check = std::make_unique<Check>(&worker, MessagePtr(sd_bus_message_ref(call)), std::move(work), std::move(next), std::string(prompt.action), interactive, std::move(cancellation));
|
||||
ThrowOnSdBus(sd_bus_call_async(worker.system.get(), nullptr, raw, OnChecked, check.get(), Microseconds(worker.options.promptTimeout)), "ask polkit");
|
||||
// OnChecked owns it now.
|
||||
check.release();
|
||||
|
|
@ -544,10 +463,9 @@ namespace {
|
|||
return 1;
|
||||
}
|
||||
|
||||
// Asks polkit whether the caller may do what `prompt` says, then runs `work` and answers; or answers why not; and
|
||||
// then runs `after` with the answer.
|
||||
std::int32_t Gate(Worker& worker, sd_bus_message* call, const Prompt& prompt, Work work, After after = {}) {
|
||||
return Ask(worker, call, prompt, std::move(work), std::move(after), {});
|
||||
// Asks polkit whether the caller may do what `prompt` says, then runs `work` and answers; or answers why not.
|
||||
std::int32_t Gate(Worker& worker, sd_bus_message* call, const Prompt& prompt, Work work) {
|
||||
return Ask(worker, call, prompt, std::move(work), {});
|
||||
}
|
||||
|
||||
// Runs `body` for `call`: it answers through Gate, or throws, and then the call is answered with the error. Nothing
|
||||
|
|
@ -576,7 +494,7 @@ namespace {
|
|||
template<typename Body>
|
||||
std::int32_t HandleLookup(sd_bus_message* call, void* userdata, std::string_view seen, Body body) {
|
||||
return Handle(call, userdata, [call, seen, &body](Worker& worker) {
|
||||
return Ask(worker, call, Prompt{actionView, std::string(seen), {}}, {}, {}, [&worker, body = std::move(body)] {
|
||||
return Ask(worker, call, Prompt{actionView, std::string(seen), {}}, {}, [&worker, body = std::move(body)] {
|
||||
body(worker);
|
||||
});
|
||||
});
|
||||
|
|
@ -927,12 +845,12 @@ namespace {
|
|||
});
|
||||
}
|
||||
|
||||
// Checkout (Nostro.Checkout): an app has nostrod pay an order. The order is read and checked, the account that
|
||||
// pays it is chosen, the user confirms both at a password prompt, and it is paid and recorded. The record answers a
|
||||
// retry with the first payment, and a retry while the first call is still under way waits for its answer.
|
||||
|
||||
// The account chosen to pay checkouts from, kept beside connectors.json as its connector's storage key and its id:
|
||||
// none when none is chosen, or it is gone.
|
||||
// The account a pay link's form starts from in the app, which the user chooses once: "Pay links start from" in
|
||||
// its settings. It is where a form starts and no more: the app asks which account pays when a link opens, the
|
||||
// form's own row changes which account pays, and what a link pays is a payment like any other, asked about as
|
||||
// one. nostrod only keeps it, and pays nothing from it by itself. Until 2026-11-22 it was the account that paid an
|
||||
// app's order at a checkout over D-Bus, which is where these methods and the file have their names from. It is
|
||||
// kept beside connectors.json as its connector's storage key and its id: none when none is chosen, or it is gone.
|
||||
const BunqAccount* CheckoutAccount(const Worker& worker) {
|
||||
std::ifstream in(worker.directory / "checkout-account");
|
||||
AccountKey key;
|
||||
|
|
@ -951,7 +869,7 @@ namespace {
|
|||
|
||||
std::int32_t OnDefaultCheckoutAccount(sd_bus_message* call, void* userdata, sd_bus_error*) {
|
||||
return Handle(call, userdata, [call](Worker& worker) {
|
||||
return Gate(worker, call, Prompt{actionView, "see which account pays at checkout", {}}, [&worker](sd_bus_message* reply) {
|
||||
return Gate(worker, call, Prompt{actionView, "see which account pay links start from", {}}, [&worker](sd_bus_message* reply) {
|
||||
const BunqAccount* chosen = CheckoutAccount(worker);
|
||||
Append(reply, chosen ? std::optional<Account>(*chosen) : std::nullopt);
|
||||
});
|
||||
|
|
@ -961,11 +879,12 @@ namespace {
|
|||
std::int32_t OnSetDefaultCheckoutAccount(sd_bus_message* call, void* userdata, sd_bus_error*) {
|
||||
return HandleLookup(call, userdata, "see your accounts and balances", [call](Worker& worker) {
|
||||
const BunqAccount& account = FindAccount(Read<std::string>(call));
|
||||
// bunq lets an OAuth connection pay the user's own accounts only, so through one no shop could be paid.
|
||||
// bunq lets an OAuth connection pay the user's own accounts only: from one, what a shop's link asks for
|
||||
// could do no more than become a draft.
|
||||
if(account.connector->IsOAuth()) {
|
||||
throw std::invalid_argument(std::format("'{}' is reached through OAuth, which pays only your own accounts: checkouts need an account added with an API key", account.name));
|
||||
throw std::invalid_argument(std::format("'{}' is reached through OAuth, which pays only your own accounts: a shop's pay link needs an account added with an API key", account.name));
|
||||
}
|
||||
const Prompt prompt{actionConnectors, std::format("let apps ask you to pay from {} at checkout", Party(account)), {{"account_iban", account.iban}, {"environment", std::string(EnvironmentName(account.connector->environment))}}};
|
||||
const Prompt prompt{actionConnectors, std::format("have pay links start from {}", Party(account)), {{"account_iban", account.iban}, {"environment", std::string(EnvironmentName(account.connector->environment))}}};
|
||||
return Gate(worker, call, prompt, [&worker, key = KeyOf(account)](sd_bus_message* reply) {
|
||||
const BunqAccount& chosen = AccountBy(key);
|
||||
const fs::path file = worker.directory / "checkout-account";
|
||||
|
|
@ -977,173 +896,6 @@ namespace {
|
|||
});
|
||||
}
|
||||
|
||||
// An order, as Pay() reads and checks it. The options after its arguments are not read: there are none yet, and
|
||||
// those of a later nostro this one does not know.
|
||||
struct Order {
|
||||
BunqCounterparty payee; // an IBAN, and the name given
|
||||
std::int64_t amount = 0;
|
||||
Currency currency = Currency::Euro;
|
||||
std::string reference;
|
||||
|
||||
OrderKey Key() const {
|
||||
return {payee.value, reference};
|
||||
}
|
||||
|
||||
static Order Read(sd_bus_message* call) {
|
||||
const std::string iban = Bus::Read<std::string>(call);
|
||||
const std::string name = Bus::Read<std::string>(call);
|
||||
Order order;
|
||||
order.amount = Bus::Read<std::int64_t>(call);
|
||||
const std::string currency = Bus::Read<std::string>(call);
|
||||
order.reference = Bus::Read<std::string>(call);
|
||||
order.payee = BunqCounterparty::Parse(iban, name);
|
||||
if(order.payee.type != "IBAN") {
|
||||
throw std::invalid_argument(std::format("checkout pays an IBAN, and '{}' is none", iban));
|
||||
}
|
||||
RequirePositive(order.amount);
|
||||
try {
|
||||
order.currency = GetCurrency(currency);
|
||||
} catch(const std::runtime_error& e) {
|
||||
throw std::invalid_argument(e.what());
|
||||
}
|
||||
if(order.reference.empty()) {
|
||||
throw std::invalid_argument("an order needs a reference, such as its number: it is what keeps paying it again from paying twice");
|
||||
}
|
||||
return order;
|
||||
}
|
||||
};
|
||||
|
||||
// One reference to one payee is one order, of one amount. Which amount the order has is not said: any app that
|
||||
// knows the payee and the reference of an order another app had paid would learn from it what was paid for it.
|
||||
void RequireSameOrder(std::int64_t amount, Currency currency, const Order& order) {
|
||||
if(amount != order.amount || currency != order.currency) {
|
||||
throw std::invalid_argument(std::format("reference {} to {} is an order of another amount than {}: another order needs another reference", Shown(order.reference), order.payee.value, FormatMoney(order.amount, order.currency)));
|
||||
}
|
||||
}
|
||||
|
||||
DB::Table<CheckoutRecord>& Records(Worker& worker) {
|
||||
if(!worker.records) {
|
||||
// A few rows per order: a reservation of a gigabyte, where the transaction tables take Crafter.DB's 32.
|
||||
worker.records.emplace(worker.directory / "checkouts.cdb", DB::OpenOptions{.reserveBytes = std::uint64_t(1) << 30});
|
||||
}
|
||||
return *worker.records;
|
||||
}
|
||||
|
||||
// Pay()'s answer: the payment, and the results, of which `repeated` says the payment was made by an earlier call.
|
||||
void AppendPaid(sd_bus_message* reply, std::uint64_t payment, bool repeated) {
|
||||
ThrowOnSdBus(sd_bus_message_append(reply, "ta{sv}", payment, 1u, "repeated", "b", static_cast<std::int32_t>(repeated)), "answer a call");
|
||||
}
|
||||
|
||||
// Answers a call for an order paid before, or being paid, and returns true; false for an order not seen before.
|
||||
bool AnswerKnownOrder(Worker& worker, sd_bus_message* call, const Order& order) {
|
||||
static constexpr DB::Column<&CheckoutRecord::payee> payee{};
|
||||
static constexpr DB::Column<&CheckoutRecord::reference> reference{};
|
||||
if(const std::optional<DB::Row<CheckoutRecord>> record = Records(worker).Where(payee == order.payee.value && reference == order.reference).First()) {
|
||||
RequireSameOrder((*record)[&CheckoutRecord::amount], (*record)[&CheckoutRecord::currency], order);
|
||||
const std::uint64_t payment = (*record)[&CheckoutRecord::payment];
|
||||
if(payment == 0) {
|
||||
throw std::runtime_error(std::format("order {} to {} may have been paid already: the bank's answer to it was lost, so check whether the payment arrived before paying it under another reference", Shown(order.reference), order.payee.value));
|
||||
}
|
||||
Deliver(Answer(call, [payment](sd_bus_message* reply) { AppendPaid(reply, payment, true); }));
|
||||
return true;
|
||||
}
|
||||
if(const auto paying = worker.paying.find(order.Key()); paying != worker.paying.end()) {
|
||||
RequireSameOrder(paying->second.amount, paying->second.currency, order);
|
||||
paying->second.waiting.push_back(MessagePtr(sd_bus_message_ref(call)));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// The order's call is answered: the calls that waited for it get the same answer, the payment marked as made by
|
||||
// an earlier call. The order is there: it went in before its account was chosen, and only this takes it out.
|
||||
// Answering fails only when memory runs out, and then the callers' own timeouts answer for them.
|
||||
void Settle(Worker& worker, const OrderKey& key, sd_bus_message* answer) {
|
||||
const std::vector<MessagePtr> waiting = std::move(worker.paying.extract(key).mapped().waiting);
|
||||
const bool paid = sd_bus_message_is_method_error(answer, nullptr) == 0;
|
||||
std::uint64_t payment = 0;
|
||||
sd_bus_message_rewind(answer, 1);
|
||||
sd_bus_message_read_basic(answer, 't', &payment);
|
||||
for(const MessagePtr& call : waiting) {
|
||||
if(paid) {
|
||||
sd_bus_reply_method_return(call.get(), "ta{sv}", payment, 1u, "repeated", "b", 1);
|
||||
} else {
|
||||
sd_bus_reply_method_error(call.get(), sd_bus_message_get_error(answer));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Pays an order the user confirmed, and records it. The record goes first, with no payment, so should the worker
|
||||
// die or the bank's answer be lost, a retry finds it and is refused rather than paid twice. Only a refusal bunq
|
||||
// gives before doing anything (below 500) takes it away again.
|
||||
std::uint64_t PayOrder(Worker& worker, const BunqAccount& account, const Order& order) {
|
||||
DB::Table<CheckoutRecord>& records = Records(worker);
|
||||
const DB::RowId row = records.Append(CheckoutRecord{order.payee.value, order.reference, order.amount, order.currency, account.iban, 0, std::chrono::system_clock::now()});
|
||||
records.Flush();
|
||||
std::uint64_t payment = 0;
|
||||
try {
|
||||
payment = account.Send(order.payee.value, order.payee.name, order.amount, order.currency, order.reference);
|
||||
} catch(const BunqError& e) {
|
||||
if(e.status < 500) {
|
||||
records.Erase(row);
|
||||
records.Flush();
|
||||
}
|
||||
throw;
|
||||
}
|
||||
records.Set<&CheckoutRecord::payment>(row, payment);
|
||||
records.Flush();
|
||||
return payment;
|
||||
}
|
||||
|
||||
// Which account pays an order. Today the one the user chose for checkouts; an account picker, once nostro has a
|
||||
// GUI, asks the user here instead, through an agent in their session, and may take its time. It answers through
|
||||
// `then`, with the account or with what the call is to be refused with, and nothing else of a checkout changes with
|
||||
// it. One that answers later keeps a reference to `call` until it does.
|
||||
using Choice = std::variant<AccountKey, std::exception_ptr>;
|
||||
void ChooseAccount(const Worker& worker, sd_bus_message*, const Order&, const std::function<void(const Choice&)>& then) {
|
||||
if(const BunqAccount* chosen = CheckoutAccount(worker)) {
|
||||
then(KeyOf(*chosen));
|
||||
} else {
|
||||
then(std::make_exception_ptr(NoCheckoutAccount("no account is chosen to pay checkouts from, or it is gone: choose one in Nostro's settings, or with `nostro checkout account`")));
|
||||
}
|
||||
}
|
||||
|
||||
std::int32_t OnPay(sd_bus_message* call, void* userdata, sd_bus_error*) {
|
||||
return Handle(call, userdata, [call](Worker& worker) {
|
||||
const Order order = Order::Read(call);
|
||||
if(AnswerKnownOrder(worker, call, order)) {
|
||||
return 1;
|
||||
}
|
||||
// The account that pays is chosen before the user is asked, since the dialog names it: an order not seen
|
||||
// before has the connectors read for it, which no call of nostrod's own interface does before polkit's yes.
|
||||
Load(worker);
|
||||
// From here on, a call for the same order waits for this one, and Settle answers it.
|
||||
worker.paying[order.Key()] = Paying{order.amount, order.currency, {}};
|
||||
ChooseAccount(worker, call, order, [&worker, call, order](const Choice& choice) {
|
||||
try {
|
||||
if(const std::exception_ptr* refusal = std::get_if<std::exception_ptr>(&choice)) {
|
||||
std::rethrow_exception(*refusal);
|
||||
}
|
||||
const AccountKey key = std::get<AccountKey>(choice);
|
||||
const BunqAccount& account = AccountBy(key);
|
||||
std::vector<std::pair<std::string, std::string>> details = PaymentDetails(account, order.amount, order.currency, order.payee.value);
|
||||
details.emplace_back("reference", Shown(order.reference));
|
||||
const Prompt prompt{actionCheckout, std::format("pay {} to {} from {}, as an app asks at checkout, reference {}", FormatMoney(order.amount, order.currency), Party(order.payee), Party(account), Shown(order.reference)), std::move(details)};
|
||||
Gate(worker, call, prompt, [&worker, key, order](sd_bus_message* reply) {
|
||||
AppendPaid(reply, PayOrder(worker, AccountBy(key), order), false);
|
||||
}, [&worker, settled = order.Key()](sd_bus_message* answer) {
|
||||
Settle(worker, settled, answer);
|
||||
});
|
||||
} catch(...) {
|
||||
const MessagePtr answer = ErrorFor(call, std::current_exception());
|
||||
Deliver(answer);
|
||||
Settle(worker, order.Key(), answer.get());
|
||||
}
|
||||
});
|
||||
return 1;
|
||||
});
|
||||
}
|
||||
|
||||
const std::map<std::string_view, sd_bus_message_handler_t> handlers = {
|
||||
{"Connectors", OnConnectors},
|
||||
{"AddBunq", OnAddBunq},
|
||||
|
|
@ -1214,8 +966,6 @@ std::int32_t Nostro::RunWorker(std::int32_t fd, std::uint32_t uid, std::string_v
|
|||
ThrowOnSdBus(sd_bus_start(link), "serve the front");
|
||||
worker.vtable = Vtable(methods, [](std::string_view member) { return handlers.at(member); });
|
||||
ThrowOnSdBus(sd_bus_add_object_vtable(link, nullptr, objectPath.data(), interfaceName.data(), worker.vtable.data(), &worker), "serve the front");
|
||||
worker.checkoutVtable = Vtable(checkoutMethods, [](std::string_view) { return &OnPay; });
|
||||
ThrowOnSdBus(sd_bus_add_object_vtable(link, nullptr, objectPath.data(), Checkout::interfaceName.data(), worker.checkoutVtable.data(), &worker), "serve the front");
|
||||
ThrowOnSdBus(sd_bus_match_signal(link, nullptr, "org.freedesktop.DBus.Local", "/org/freedesktop/DBus/Local", "org.freedesktop.DBus.Local", "Disconnected", OnFrontGone, &worker), "watch the front");
|
||||
ThrowOnSdBus(sd_bus_attach_event(link, worker.event, SD_EVENT_PRIORITY_NORMAL), "serve the front");
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@ module;
|
|||
|
||||
module Nostro:Daemon_impl;
|
||||
import std;
|
||||
import Nostro.Checkout;
|
||||
import :Daemon;
|
||||
import :Bus;
|
||||
|
||||
|
|
@ -49,14 +48,9 @@ namespace {
|
|||
struct Front {
|
||||
DaemonOptions options;
|
||||
sd_event* event = nullptr;
|
||||
// Before the connections, so the connection each is registered with goes first however RunDaemon ends.
|
||||
// Before the connection it is registered with, which then goes first however RunDaemon ends.
|
||||
std::vector<sd_bus_vtable> vtable;
|
||||
std::vector<sd_bus_vtable> checkoutVtable;
|
||||
BusPtr bus;
|
||||
// Checkout::busName, with net.catcrafts.Nostro1.Checkout alone, on a connection of its own: an app allowed to
|
||||
// talk to that name and no other (a Flatpak's --system-talk-name) may call its owner's unique name too, and this
|
||||
// one serves nothing else.
|
||||
BusPtr checkout;
|
||||
std::map<std::uint32_t, std::unique_ptr<Worker>> workers;
|
||||
std::size_t inFlight = 0;
|
||||
sd_event_source* idle = nullptr;
|
||||
|
|
@ -305,16 +299,16 @@ namespace {
|
|||
}
|
||||
}
|
||||
|
||||
// A system bus connection that serves `interface` under `name`, every method passed on by Forward, and ends the
|
||||
// loop when the bus goes.
|
||||
BusPtr Serve(Front& front, std::string_view description, std::string_view name, std::string_view interface, std::vector<sd_bus_vtable>& vtable) {
|
||||
// The system bus connection that serves nostrod's interface under its name, every method passed on by Forward, and
|
||||
// ends the loop when the bus goes.
|
||||
BusPtr Serve(Front& front) {
|
||||
sd_bus* bus = nullptr;
|
||||
ThrowOnSdBus(sd_bus_open_system_with_description(&bus, description.data()), "connect to the system bus");
|
||||
ThrowOnSdBus(sd_bus_open_system_with_description(&bus, "nostrod"), "connect to the system bus");
|
||||
BusPtr owned(bus);
|
||||
ThrowOnSdBus(sd_bus_add_object_vtable(bus, nullptr, objectPath.data(), interface.data(), vtable.data(), &front), std::format("serve {}", interface));
|
||||
ThrowOnSdBus(sd_bus_add_object_vtable(bus, nullptr, objectPath.data(), interfaceName.data(), front.vtable.data(), &front), std::format("serve {}", interfaceName));
|
||||
ThrowOnSdBus(sd_bus_match_signal(bus, nullptr, "org.freedesktop.DBus.Local", "/org/freedesktop/DBus/Local", "org.freedesktop.DBus.Local", "Disconnected", OnBusGone, &front), "watch the system bus");
|
||||
ThrowOnSdBus(sd_bus_request_name(bus, name.data(), 0), std::format("own {}", name));
|
||||
ThrowOnSdBus(sd_bus_attach_event(bus, front.event, SD_EVENT_PRIORITY_NORMAL), std::format("serve {}", interface));
|
||||
ThrowOnSdBus(sd_bus_request_name(bus, busName.data(), 0), std::format("own {}", busName));
|
||||
ThrowOnSdBus(sd_bus_attach_event(bus, front.event, SD_EVENT_PRIORITY_NORMAL), std::format("serve {}", interfaceName));
|
||||
return owned;
|
||||
}
|
||||
}
|
||||
|
|
@ -331,10 +325,7 @@ std::int32_t Nostro::RunDaemon(const DaemonOptions& options) {
|
|||
ThrowOnSdBus(sd_event_add_signal(front.event, nullptr, SIGTERM, nullptr, nullptr), "watch for SIGTERM");
|
||||
|
||||
front.vtable = Vtable(methods, [](std::string_view) { return &Forward; });
|
||||
front.checkoutVtable = Vtable(checkoutMethods, [](std::string_view) { return &Forward; });
|
||||
// The main name last: systemd counts the service as started once it is owned, and by then both are.
|
||||
front.checkout = Serve(front, "nostrod checkout", Checkout::busName, Checkout::interfaceName, front.checkoutVtable);
|
||||
front.bus = Serve(front, "nostrod", busName, interfaceName, front.vtable);
|
||||
front.bus = Serve(front);
|
||||
ThrowOnSdBus(sd_event_add_time_relative(front.event, &front.idle, CLOCK_MONOTONIC, Microseconds(options.idle), 0, OnIdle, &front), "set the idle timer");
|
||||
|
||||
const std::int32_t result = sd_event_loop(front.event);
|
||||
|
|
@ -346,7 +337,6 @@ std::int32_t Nostro::RunDaemon(const DaemonOptions& options) {
|
|||
}
|
||||
front.workers.clear();
|
||||
front.bus.reset();
|
||||
front.checkout.reset();
|
||||
sd_event_source_unref(front.idle);
|
||||
sd_event_unref(front.event);
|
||||
ThrowOnSdBus(result, "run the event loop");
|
||||
|
|
|
|||
|
|
@ -213,6 +213,11 @@ namespace Nostro::App {
|
|||
} else {
|
||||
overview.state = LoadState::Loading;
|
||||
}
|
||||
// A link that waits for the accounts starts from the one chosen for pay links: where that could not be read
|
||||
// either, it is read again with them.
|
||||
if(waiting && checkout.state == LoadState::Failed) {
|
||||
LoadCheckout();
|
||||
}
|
||||
Fetch<std::vector<ConnectorSummary>>("overview", "Connectors", [](Client& bank, Client::Then<std::vector<ConnectorSummary>> then) { return bank.Connectors(std::move(then)); }, [this, again](Answer<std::vector<ConnectorSummary>> answer) {
|
||||
if(answer.Failed()) {
|
||||
OverviewFailed(Explain(answer.Error(), Operation::Load));
|
||||
|
|
@ -276,6 +281,10 @@ namespace Nostro::App {
|
|||
if(!Find(filtering)) {
|
||||
filtering.clear();
|
||||
}
|
||||
// A link's payment waited for this. Last, so that it finds the accounts and the pages as they now are.
|
||||
if(waiting) {
|
||||
TakeLink();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -400,10 +409,14 @@ namespace Nostro::App {
|
|||
if(answer.Failed()) {
|
||||
checkout.state = LoadState::Failed;
|
||||
checkout.problem = Explain(answer.Error(), Operation::Load);
|
||||
return;
|
||||
} else {
|
||||
checkout.value = std::move(answer.Get());
|
||||
checkout.state = LoadState::Loaded;
|
||||
}
|
||||
// A link's payment starts from this account, and waited to hear which it is, or that it cannot be read.
|
||||
if(waiting) {
|
||||
TakeLink();
|
||||
}
|
||||
checkout.value = std::move(answer.Get());
|
||||
checkout.state = LoadState::Loaded;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -575,6 +588,34 @@ namespace Nostro::App {
|
|||
return false;
|
||||
}
|
||||
|
||||
bool App::Open(std::string_view link) {
|
||||
// Not from under a change, as no place is gone to from under one.
|
||||
if(changing) {
|
||||
return false;
|
||||
}
|
||||
Read<PaymentLink> read = ReadPaymentLink(link);
|
||||
if(!read.value) {
|
||||
// Said over wherever the user is, with nothing of the link in it: what a stranger wrote is not shown as
|
||||
// the app's own words. A link that waits is left waiting.
|
||||
Dialog problem;
|
||||
problem.kind = Dialog::Kind::Problem;
|
||||
problem.name = "link";
|
||||
problem.title = "This link cannot be opened";
|
||||
problem.text = {std::move(read.error)};
|
||||
problem.choices.push_back({"link.close", "Close", Ui::ButtonKind::Tonal, true, {}, [this] { Close(); }});
|
||||
Show(std::move(problem));
|
||||
return true;
|
||||
}
|
||||
Go(Place::Accounts);
|
||||
waiting = std::move(*read.value);
|
||||
// The form starts from the account the user chose for pay links to start from, so that is read if it was not.
|
||||
if(checkout.state == LoadState::Idle || checkout.state == LoadState::Failed) {
|
||||
LoadCheckout();
|
||||
}
|
||||
TakeLink();
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string App::Screen() const {
|
||||
static constexpr std::array<std::string_view, 3> places = {"accounts", "cards", "settings"};
|
||||
// The three steps of adding a connection are one name, as far as it has been gone: "add:bunq:key".
|
||||
|
|
@ -624,6 +665,57 @@ namespace Nostro::App {
|
|||
Push(Page::Request);
|
||||
}
|
||||
|
||||
// Tried when a link is opened, and again as the accounts and the account pay links start from are read: it goes
|
||||
// ahead once both are known, or the second is known not to be readable. It is safe to try again: a reading that
|
||||
// fails at once, as one without a system bus does, has tried it already by the time whoever asked for the reading
|
||||
// tries it, and then no link waits any more.
|
||||
void App::TakeLink() {
|
||||
if(!waiting || overview.state != LoadState::Loaded || checkout.state == LoadState::Loading) {
|
||||
return;
|
||||
}
|
||||
const PaymentLink link = std::move(*waiting);
|
||||
waiting.reset();
|
||||
// The user went elsewhere while the accounts were read, or is being told something: what they are doing is not
|
||||
// taken from under them for what a link asks. The link is theirs to open again.
|
||||
if(place != Place::Accounts || !stack.empty() || dialog.has_value()) {
|
||||
Tell("The link's payment was not opened: open the link again");
|
||||
return;
|
||||
}
|
||||
// No bank connected, or none whose accounts could be read: there is nothing to pay from, and no form to show.
|
||||
if(Accounts().empty()) {
|
||||
Dialog none;
|
||||
none.kind = Dialog::Kind::Problem;
|
||||
none.name = "link";
|
||||
none.title = "No account to pay from";
|
||||
none.text = {"This link asks for a payment, and there is no account to pay it from. Once your bank is connected and its accounts show here, open the link again."};
|
||||
none.choices.push_back({"link.close", "Close", Ui::ButtonKind::Tonal, true, {}, [this] { Close(); }});
|
||||
Show(std::move(none));
|
||||
return;
|
||||
}
|
||||
// From the account the user chose in the settings for pay links to start from. Without one, from the first
|
||||
// whose connection pays anybody: one that pays between the user's own accounts only could do no more than
|
||||
// save a draft.
|
||||
std::string from;
|
||||
if(checkout.state == LoadState::Loaded && checkout.value && Find(AccountKey(*checkout.value))) {
|
||||
from = AccountKey(*checkout.value);
|
||||
} else {
|
||||
for(const AccountRef& account : Accounts()) {
|
||||
if(!account.connection->ownAccountsOnly) {
|
||||
from = account.key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
payment.Fill(link);
|
||||
OpenPay(std::move(from));
|
||||
// Which account pays is the user's to say, and a link is where that is asked rather than assumed: the form
|
||||
// opens with its choice of accounts over it, the one it starts from marked, and a tap beside the choice keeps
|
||||
// that one. With one account there is nothing to choose, and the form opens as it is.
|
||||
if(Accounts().size() > 1) {
|
||||
picking = Picking::PayFrom;
|
||||
}
|
||||
}
|
||||
|
||||
// The pay form's button: what is filled in is checked, and sent. The app asks nothing more itself: a payment, a
|
||||
// schedule and a draft are each confirmed in the system's password dialog, which nostrod words from its own data,
|
||||
// and a draft is not paid until it is accepted, which is confirmed there once more.
|
||||
|
|
@ -1010,8 +1102,8 @@ namespace Nostro::App {
|
|||
Change<std::string>(Operation::RemoveConnection, "RemoveConnector", std::format("Removing {}", name), {}, [connector](Client& bank, Client::Then<std::string> then) { return bank.RemoveConnector(connector, std::move(then)); }, [this](std::string& removed) {
|
||||
dialog.reset();
|
||||
Tell(std::format("Removed {}", removed));
|
||||
// Its cards and its say at checkout went with it: the cards are read when they are next looked at, the
|
||||
// checkout's account now, the settings being where connections are removed from.
|
||||
// Its cards went with it, and it may have held the account pay links start from: the cards are read
|
||||
// when they are next looked at, that account now, the settings being where connections are removed from.
|
||||
cards = {};
|
||||
LoadCheckout();
|
||||
Refresh();
|
||||
|
|
@ -1094,11 +1186,11 @@ namespace Nostro::App {
|
|||
}
|
||||
|
||||
void App::ChooseCheckout(const std::string& key, const std::string& name) {
|
||||
Change<Account>(Operation::SetCheckoutAccount, "SetDefaultCheckoutAccount", std::format("Letting apps ask you to pay from {}", name), {}, [key](Client& bank, Client::Then<Account> then) { return bank.SetDefaultCheckoutAccount(key, std::move(then)); }, [this](Account& chosen) {
|
||||
Change<Account>(Operation::SetCheckoutAccount, "SetDefaultCheckoutAccount", std::format("Having pay links start from {}", name), {}, [key](Client& bank, Client::Then<Account> then) { return bank.SetDefaultCheckoutAccount(key, std::move(then)); }, [this](Account& chosen) {
|
||||
checkout.value = chosen;
|
||||
checkout.state = LoadState::Loaded;
|
||||
dialog.reset();
|
||||
Tell(std::format("Apps now ask you to pay from {}", chosen.name));
|
||||
Tell(std::format("Pay links now start from {}", chosen.name));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,6 +22,67 @@ namespace {
|
|||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
// The most of a link that is read, in bytes, and of the name and the description in one, in characters: SEPA's
|
||||
// limits for the name of an account's holder and for what a payment is for, the second of which RFC 8905 gives for
|
||||
// an IBAN's message. A form's field shows what fits its width and no more: what is longer than a bank carries is
|
||||
// refused, not cut, since what is cut off is still sent. The system's password dialog shows less of a name than
|
||||
// this takes, 64 characters: nostrod cuts a longer one short there.
|
||||
constexpr std::size_t linkLimit = 4096;
|
||||
constexpr std::size_t nameLimit = 70;
|
||||
constexpr std::size_t messageLimit = 140;
|
||||
|
||||
bool Digits(std::string_view text) {
|
||||
return !text.empty() && std::ranges::all_of(text, [](char c) { return c >= '0' && c <= '9'; });
|
||||
}
|
||||
|
||||
bool Alphanumeric(std::string_view text) {
|
||||
return !text.empty() && std::ranges::all_of(text, [](char c) { return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); });
|
||||
}
|
||||
|
||||
// In small letters, as far as ASCII has them: a link's scheme and its options' names are in that.
|
||||
std::string Lowered(std::string_view text) {
|
||||
std::string lowered(text);
|
||||
for(char& c : lowered) {
|
||||
if(c >= 'A' && c <= 'Z') {
|
||||
c = static_cast<char>(c - 'A' + 'a');
|
||||
}
|
||||
}
|
||||
return lowered;
|
||||
}
|
||||
|
||||
// Whether every % in `text` begins an escape, two hex digits: UrlDecode leaves one that does not as it is, which
|
||||
// is a guess at what was meant.
|
||||
bool Escaped(std::string_view text) {
|
||||
const auto hex = [](char c) { return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); };
|
||||
for(std::size_t i = 0; i < text.size(); i++) {
|
||||
if(text[i] == '%' && !(i + 2 < text.size() && hex(text[i + 1]) && hex(text[i + 2]))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Whether `text` is fit to be a link's name or description: UTF-8 as the bus takes it, each character in its
|
||||
// shortest form and none that is no character, none that the system's password dialog leaves out as drawing
|
||||
// nothing or turning text around, and no space but the ordinary one. The toolkit's decoder takes a sequence longer
|
||||
// than its character needs, which reads as that character and is not valid: the bus refuses it, after the form
|
||||
// has shown it.
|
||||
bool Plain(std::string_view text) {
|
||||
for(std::size_t i = 0; i < text.size();) {
|
||||
const std::size_t at = i;
|
||||
const std::uint32_t code = Ui::DecodeUtf8(text, i);
|
||||
const std::size_t shortest = code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4;
|
||||
// U+FFFD is what the decoder gives for bytes that are no character, and what text that was damaged before
|
||||
// it got here holds. The last two code points of every plane are no characters either, and sd-bus refuses
|
||||
// text with one as it does a sequence that is too long: Unshown has those of the first plane and of the
|
||||
// two planes of private use, not the rest.
|
||||
if(code == 0xFFFD || i - at != shortest || Unshown(code) || (Blank(code) && code != ' ') || (code & 0xFFFEU) == 0xFFFEU) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Read<std::int64_t> App::ReadAmount(std::string_view text, Currency currency) {
|
||||
|
|
@ -110,6 +171,103 @@ Read<std::uint32_t> App::ReadCount(std::string_view text) {
|
|||
return {count, {}};
|
||||
}
|
||||
|
||||
Read<PaymentLink> App::ReadPaymentLink(std::string_view link) {
|
||||
constexpr std::string_view scheme = "payto://iban/";
|
||||
constexpr std::string_view notOne = "It is not a link to pay an IBAN, which is the kind Nostro reads.";
|
||||
constexpr std::string_view damaged = "It is not written as such a link is: something in it is there twice, or cannot be read.";
|
||||
constexpr std::string_view unreadable = "The amount in it cannot be read.";
|
||||
// The scheme and the kind of account it pays may be in capitals. A # begins a part of an address that such a link
|
||||
// does not have.
|
||||
if(link.size() > linkLimit || link.contains('#') || Lowered(link.substr(0, scheme.size())) != scheme) {
|
||||
return {std::nullopt, std::string(notOne)};
|
||||
}
|
||||
const std::string_view rest = link.substr(scheme.size());
|
||||
const std::size_t question = rest.find('?');
|
||||
std::string_view path = rest.substr(0, question);
|
||||
// A bank's code may come before the IBAN, "payto://iban/ABNANL2A/NL91…". Nostro pays without one: it is held to
|
||||
// its shape and left out.
|
||||
if(const std::size_t slash = path.find('/'); slash != std::string_view::npos) {
|
||||
const std::string_view code = path.substr(0, slash);
|
||||
if(!Alphanumeric(code) || (code.size() != 8 && code.size() != 11)) {
|
||||
return {std::nullopt, std::string(notOne)};
|
||||
}
|
||||
path.remove_prefix(slash + 1);
|
||||
}
|
||||
// Letters and digits and nothing else: no escape that reads otherwise than it looks, and nothing ReadParty would
|
||||
// take for an e-mail address or a phone number. What is left is an IBAN whose check digits hold, or nothing.
|
||||
const Read<BunqCounterparty> party = Alphanumeric(path) ? ReadParty(path) : Read<BunqCounterparty>{};
|
||||
if(!party.value) {
|
||||
return {std::nullopt, "The IBAN in it is not a valid one."};
|
||||
}
|
||||
PaymentLink asked;
|
||||
asked.iban = party.value->value;
|
||||
|
||||
// The options are walked here, and not asked of the library's QueryParameter: that gives the first of a name that
|
||||
// is there twice, and takes a name's letters as they are written. A link is refused for the one, and its options
|
||||
// are known in capitals too.
|
||||
std::vector<std::string> said;
|
||||
const std::string_view options = question == std::string_view::npos ? std::string_view() : rest.substr(question + 1);
|
||||
for(const auto part : std::views::split(options, '&')) {
|
||||
const std::string_view option(part.begin(), part.end());
|
||||
const std::size_t equals = option.find('=');
|
||||
// An option's name as any reader of the link takes it, its escapes undone and in small letters: "AMOUNT" and
|
||||
// "%61mount" are the amount, so a link cannot say one amount to Nostro and another to whoever reads it next.
|
||||
const std::string name = Lowered(UrlDecode(option.substr(0, equals)));
|
||||
// The sender's name is the user's own to give, and what Nostro does not know may be a later version's.
|
||||
if(name != "amount" && name != "receiver-name" && name != "message" && name != "instruction") {
|
||||
continue;
|
||||
}
|
||||
if(std::ranges::contains(said, name)) {
|
||||
return {std::nullopt, std::string(damaged)};
|
||||
}
|
||||
said.push_back(name);
|
||||
// An instruction is for the payee's bank to pass on unchanged, which no bank Nostro pays through is asked to
|
||||
// do: RFC 8905 has a link refused whose instruction cannot be kept, rather than paid without it.
|
||||
if(name == "instruction") {
|
||||
return {std::nullopt, "It asks for a reference of a kind Nostro cannot send along."};
|
||||
}
|
||||
const std::string_view written = equals == std::string_view::npos ? std::string_view() : option.substr(equals + 1);
|
||||
if(!Escaped(written)) {
|
||||
return {std::nullopt, std::string(damaged)};
|
||||
}
|
||||
const std::string value = UrlDecode(written);
|
||||
if(name == "amount") {
|
||||
// "EUR:12.50": the currency, and digits with at most one point among them and digits on both sides of it.
|
||||
// No sign and no space. And no comma: RFC 8905 has a reader pass over commas, by which "EUR:12,50" is
|
||||
// twelve hundred and fifty, and whoever wrote that meant twelve and a half. It is refused, not guessed at.
|
||||
const std::size_t colon = value.find(':');
|
||||
const std::string_view sum = colon == std::string::npos ? std::string_view() : std::string_view(value).substr(colon + 1);
|
||||
const std::size_t point = sum.find('.');
|
||||
if(!Digits(sum.substr(0, point)) || (point != std::string_view::npos && !Digits(sum.substr(point + 1)))) {
|
||||
return {std::nullopt, std::string(unreadable)};
|
||||
}
|
||||
try {
|
||||
// The one currency there is. With a second one the form's amount, which is read in the paying account's
|
||||
// currency, has to be held to the link's as well.
|
||||
asked.currency = GetCurrency(std::string_view(value).substr(0, colon));
|
||||
} catch(const std::runtime_error&) {
|
||||
return {std::nullopt, "It asks for a currency Nostro does not pay in."};
|
||||
}
|
||||
// What the form itself refuses: nothing, more decimals than the currency has, more than it can hold.
|
||||
asked.amount = ReadAmount(sum, asked.currency).value;
|
||||
if(!asked.amount) {
|
||||
return {std::nullopt, std::string(unreadable)};
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(!Plain(value)) {
|
||||
return {std::nullopt, "The name or the description in it holds what a payment cannot carry."};
|
||||
}
|
||||
const std::string_view text = Trimmed(value);
|
||||
const bool message = name == "message";
|
||||
if(Ui::CountCodepoints(text) > (message ? messageLimit : nameLimit)) {
|
||||
return {std::nullopt, "The name or the description in it is too long."};
|
||||
}
|
||||
(message ? asked.description : asked.name) = std::string(text);
|
||||
}
|
||||
return {std::move(asked), {}};
|
||||
}
|
||||
|
||||
bool PaymentForm::Errors::Any() const noexcept {
|
||||
return !to.empty() || !name.empty() || !amount.empty() || !day.empty() || !time.empty() || !every.empty() || !until.empty();
|
||||
}
|
||||
|
|
@ -163,6 +321,17 @@ PaymentForm::Errors PaymentForm::Check(Currency currency, Moment now, PaymentOrd
|
|||
return errors;
|
||||
}
|
||||
|
||||
void PaymentForm::Fill(const PaymentLink& link) {
|
||||
Clear();
|
||||
to.Set(link.iban);
|
||||
name.Set(link.name);
|
||||
if(link.amount) {
|
||||
amount.Set(FormatAmount(*link.amount, link.currency));
|
||||
}
|
||||
description.Set(link.description);
|
||||
linked = true;
|
||||
}
|
||||
|
||||
void PaymentForm::Clear() {
|
||||
to.Clear();
|
||||
name.Clear();
|
||||
|
|
@ -175,6 +344,7 @@ void PaymentForm::Clear() {
|
|||
every.Clear();
|
||||
until.Clear();
|
||||
tried = false;
|
||||
linked = false;
|
||||
}
|
||||
|
||||
bool RequestForm::Errors::Any() const noexcept {
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ namespace Nostro::App {
|
|||
return std::string(places[std::to_underlying(place)]);
|
||||
}
|
||||
const Route& top = stack.back();
|
||||
static constexpr std::array<std::string_view, 9> pages = {"Account", "Pay", "Request", "Bank connections", "Add a connection", "", "", "Pay at checkout", "About and security"};
|
||||
static constexpr std::array<std::string_view, 9> pages = {"Account", "Pay", "Request", "Bank connections", "Add a connection", "", "", "Pay links", "About and security"};
|
||||
std::string title(pages[std::to_underlying(top.page)]);
|
||||
// An account's page is called what the account is.
|
||||
if(const std::optional<AccountRef> account = Find(top.account)) {
|
||||
|
|
@ -238,6 +238,11 @@ namespace Nostro::App {
|
|||
|
||||
void App::HomePage(Ui::Frame& frame, Ui::Column& column) {
|
||||
const Theme& theme = frame.Colors();
|
||||
// A link's payment that waits for the accounts is said to, above whatever stands where they will be: its form
|
||||
// is not sprung on a user who was never told that one is coming.
|
||||
if(waiting) {
|
||||
column.Advance(Banner(frame, Padded(frame, column.Next()), {.key = "link", .text = "A link asks for a payment. Its form opens once your accounts have been read."}));
|
||||
}
|
||||
if(overview.state == LoadState::Failed) {
|
||||
Trouble(frame, column, overview.problem, "overview", [this] { Refresh(); });
|
||||
return;
|
||||
|
|
@ -456,6 +461,12 @@ namespace Nostro::App {
|
|||
}
|
||||
PaymentOrder unused;
|
||||
const PaymentForm::Errors errors = payment.tried ? payment.Check(currency, Today(), unused) : PaymentForm::Errors();
|
||||
// What a link filled in was chosen by whoever wrote the link, the payee and the amount among it. The form says
|
||||
// so for as long as it holds that, and that is all the app does about it: the form's button still sends, and
|
||||
// the system's password dialog still confirms.
|
||||
if(payment.linked) {
|
||||
column.Advance(Banner(frame, Padded(frame, column.Next()), {.key = "pay.linked", .kind = BannerKind::Warning, .text = "A link filled this in, not you. Check who is paid and how much."}));
|
||||
}
|
||||
AccountField(frame, column, "pay.from", "From", payFrom, Picking::PayFrom);
|
||||
column.Advance(TextField(frame, Padded(frame, column.Next()), payment.to, {.key = "pay.to", .label = "To", .placeholder = "An IBAN, an e-mail address or a phone number", .error = errors.to, .helper = PartyKind(payment.to.value)}));
|
||||
column.Advance(TextField(frame, Padded(frame, column.Next()), payment.name, {.key = "pay.name", .label = "Name", .placeholder = "Who the account belongs to", .error = errors.name, .purpose = TextPurpose::Name}));
|
||||
|
|
@ -556,7 +567,9 @@ namespace Nostro::App {
|
|||
} else if(checkout.state == LoadState::Failed) {
|
||||
paying = checkout.problem.title;
|
||||
}
|
||||
ListRow(frame, column.Take(frame.Dp(Size::row)), {.key = "settings.checkout", .title = "Pay at checkout from", .subtitle = std::move(paying), .chevron = true, .onTap = [this] { Push(Page::Checkout); }});
|
||||
// The account a link's pay form starts from. Its names in the code are the wire's (DefaultCheckoutAccount), from
|
||||
// the checkout over D-Bus it was first chosen for.
|
||||
ListRow(frame, column.Take(frame.Dp(Size::row)), {.key = "settings.checkout", .title = "Pay links start from", .subtitle = std::move(paying), .chevron = true, .onTap = [this] { Push(Page::Checkout); }});
|
||||
ListRow(frame, column.Take(frame.Dp(Size::row)), {.key = "settings.about", .title = "About and security", .subtitle = host.version.empty() ? std::string("Nostro") : std::format("Nostro {}", host.version), .chevron = true, .onTap = [this] { Push(Page::About); }});
|
||||
}
|
||||
|
||||
|
|
@ -599,7 +612,7 @@ namespace Nostro::App {
|
|||
}
|
||||
|
||||
void App::CheckoutPage(Ui::Frame& frame, Ui::Column& column) {
|
||||
column.Advance(Paragraph(frame, Padded(frame, column.Next()), "Other apps can ask Nostro to pay an order at their checkout. Every such payment still asks for your password, and says what is paid and to whom. This is the account they are paid from.", Body(frame)));
|
||||
column.Advance(Paragraph(frame, Padded(frame, column.Next()), "A shop's pay link opens Nostro with the payment filled in. This is the account its form starts from. You can choose another one when it opens, and nothing is paid before you press Pay and confirm it with your password.", Body(frame)));
|
||||
if(checkout.state == LoadState::Failed) {
|
||||
Trouble(frame, column, checkout.problem, "checkout.problem", [this] { LoadCheckout(); });
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -1,121 +0,0 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module;
|
||||
|
||||
#include <systemd/sd-bus.h>
|
||||
#include <errno.h>
|
||||
|
||||
module Nostro.Checkout;
|
||||
import std;
|
||||
|
||||
using namespace Nostro;
|
||||
|
||||
Checkout::Error::Error(std::string name, const std::string& message) : std::runtime_error(message), name(std::move(name)) {
|
||||
}
|
||||
|
||||
namespace {
|
||||
// What sd-bus hands out, freed at the end of the scope.
|
||||
struct Connection {
|
||||
sd_bus* bus = nullptr;
|
||||
Connection() = default;
|
||||
Connection(const Connection&) = delete;
|
||||
Connection& operator=(const Connection&) = delete;
|
||||
~Connection() {
|
||||
sd_bus_flush_close_unref(bus);
|
||||
}
|
||||
};
|
||||
|
||||
struct Message {
|
||||
sd_bus_message* message = nullptr;
|
||||
Message() = default;
|
||||
Message(const Message&) = delete;
|
||||
Message& operator=(const Message&) = delete;
|
||||
~Message() {
|
||||
sd_bus_message_unref(message);
|
||||
}
|
||||
};
|
||||
|
||||
struct BusError {
|
||||
sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
BusError() = default;
|
||||
BusError(const BusError&) = delete;
|
||||
BusError& operator=(const BusError&) = delete;
|
||||
~BusError() {
|
||||
sd_bus_error_free(&error);
|
||||
}
|
||||
};
|
||||
|
||||
// Throws a std::system_error naming `what` when an sd-bus call returned a negative errno; returns the result.
|
||||
std::int32_t ThrowOn(std::int32_t result, std::string_view what) {
|
||||
if(result < 0) {
|
||||
throw std::system_error(-result, std::generic_category(), std::format("cannot {}", what));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// sd-bus hands strings out as C strings, the ones of an error possibly none.
|
||||
// lint-disable-next-line no-char-pointer
|
||||
std::string Text(const char* text) {
|
||||
return text ? std::string(text) : std::string();
|
||||
}
|
||||
|
||||
std::string ReadString(sd_bus_message* message) {
|
||||
// lint-disable-next-line no-char-pointer
|
||||
const char* text = nullptr;
|
||||
ThrowOn(sd_bus_message_read_basic(message, 's', &text), "read nostrod's answer");
|
||||
return Text(text);
|
||||
}
|
||||
}
|
||||
|
||||
Checkout::Paid Checkout::Pay(const Order& order, std::chrono::seconds timeout) {
|
||||
Connection connection;
|
||||
ThrowOn(sd_bus_open_system_with_description(&connection.bus, "nostro checkout"), "connect to the system bus");
|
||||
// The user confirms the order at a password prompt.
|
||||
ThrowOn(sd_bus_set_allow_interactive_authorization(connection.bus, 1), "connect to the system bus");
|
||||
Message call;
|
||||
ThrowOn(sd_bus_message_new_method_call(connection.bus, &call.message, busName.data(), objectPath.data(), interfaceName.data(), "Pay"), "call nostrod");
|
||||
// No options yet; a nostrod skips the ones it does not know, so adding one breaks no older nostrod.
|
||||
const std::int32_t appended = sd_bus_message_append(call.message, "ssxssa{sv}", order.payeeIban.c_str(), order.payeeName.c_str(), order.amount, order.currency.c_str(), order.reference.c_str(), 0u);
|
||||
if(appended == -EINVAL) {
|
||||
throw std::invalid_argument("text that is not UTF-8 cannot be sent to nostrod");
|
||||
}
|
||||
ThrowOn(appended, "call nostrod");
|
||||
|
||||
BusError error;
|
||||
Message reply;
|
||||
if(sd_bus_call(connection.bus, call.message, static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>(timeout).count()), &error.error, &reply.message) < 0) {
|
||||
std::string name = Text(error.error.name);
|
||||
const std::string message = Text(error.error.message);
|
||||
if(name == errorCancelled) {
|
||||
throw Cancelled(std::move(name), message);
|
||||
}
|
||||
if(name == errorNoCheckoutAccount) {
|
||||
throw NoCheckoutAccount(std::move(name), message);
|
||||
}
|
||||
if(name == SD_BUS_ERROR_SERVICE_UNKNOWN) {
|
||||
throw Error(std::move(name), std::format("nostro is not installed, or the system bus cannot start it ({})", message));
|
||||
}
|
||||
if(name == SD_BUS_ERROR_NO_REPLY || name == SD_BUS_ERROR_TIMEOUT) {
|
||||
throw Error(std::move(name), std::format("nostrod did not answer in time ({}): the order may have been paid, and paying the same order again answers with its payment rather than paying twice", message));
|
||||
}
|
||||
throw Error(std::move(name), message);
|
||||
}
|
||||
|
||||
Paid paid;
|
||||
ThrowOn(sd_bus_message_read_basic(reply.message, 't', &paid.paymentId), "read nostrod's answer");
|
||||
// The results: a later nostrod may add to them, and what this one does not know it skips.
|
||||
ThrowOn(sd_bus_message_enter_container(reply.message, 'a', "{sv}"), "read nostrod's answer");
|
||||
while(ThrowOn(sd_bus_message_enter_container(reply.message, 'e', "sv"), "read nostrod's answer") > 0) {
|
||||
if(ReadString(reply.message) == "repeated") {
|
||||
std::int32_t repeated = 0;
|
||||
ThrowOn(sd_bus_message_read(reply.message, "v", "b", &repeated), "read nostrod's answer");
|
||||
paid.repeated = repeated != 0;
|
||||
} else {
|
||||
ThrowOn(sd_bus_message_skip(reply.message, "v"), "read nostrod's answer");
|
||||
}
|
||||
ThrowOn(sd_bus_message_exit_container(reply.message), "read nostrod's answer");
|
||||
}
|
||||
ThrowOn(sd_bus_message_exit_container(reply.message), "read nostrod's answer");
|
||||
return paid;
|
||||
}
|
||||
|
|
@ -62,6 +62,10 @@ namespace {
|
|||
Debug::Required<std::string> route;
|
||||
};
|
||||
|
||||
struct LinkArgs {
|
||||
Debug::Required<std::string> link;
|
||||
};
|
||||
|
||||
struct ThemeArgs {
|
||||
Debug::Required<std::string> scheme;
|
||||
};
|
||||
|
|
@ -329,6 +333,20 @@ void Debugger::Register() {
|
|||
}
|
||||
return {};
|
||||
});
|
||||
// The app's own path for a link, the one the program's argument takes: a debugger opens one in a window that is
|
||||
// already there, which no browser can.
|
||||
Debug::Command("App_Open", "Open a payment link as the app does one it is started with: payto://iban/<IBAN>?amount=EUR:12.50&receiver-name=<name>&message=<what for>. The pay form opens filled in once the accounts are known, under the choice of the account that pays where there is more than one (wait 'app.screen == \"pay+accounts\"', or 'app.screen == \"pay\"' with a single account); a link the app cannot read gets a sheet saying why (app.screen ends in +link). Nothing is sent.", [this](const LinkArgs& args) -> Debug::Result<> {
|
||||
if(targets.app == nullptr) {
|
||||
return Debug::Error{"NoApp", "the toolkit's gallery is shown, not the app; start without --gallery"};
|
||||
}
|
||||
if(Debug::Result<> ready = Ready(); !ready) {
|
||||
return ready;
|
||||
}
|
||||
if(!targets.app->Open(*args.link)) {
|
||||
return Debug::Error{"Changing", "a change is under way, which the screen waits out; wait 'app.changing == false' first"};
|
||||
}
|
||||
return {};
|
||||
});
|
||||
|
||||
// -- what can be read ----------------------------------------------------------------------------------------
|
||||
Debug::Watch("ui.regions", [this, &context, scale] {
|
||||
|
|
|
|||
|
|
@ -23,12 +23,12 @@ namespace {
|
|||
nostro draft <account> <to> <name> <amount> [description]
|
||||
nostro drafts <account> [accept|reject <id>]
|
||||
nostro cards [freeze|unfreeze|lost|stolen <card id>]
|
||||
nostro checkout account [<account>] the account apps may ask you to pay from at checkout
|
||||
nostro checkout account [<account>] the account a pay link's form starts from in the Nostro app
|
||||
|
||||
<account> is an account's name or IBAN. <to> and <from> are an IBAN, an e-mail address, or a phone number
|
||||
starting with +. Amounts are in the smallest unit of the account's currency: 1250 is 12.50. Dates are
|
||||
YYYY-MM-DD or "YYYY-MM-DD hh:mm", in local time. A sandbox API key is recognised by its prefix.
|
||||
nostrod does the work, and asks for your password before it pays or changes anything, a checkout included.)";
|
||||
nostrod does the work, and asks for your password before it pays or changes anything.)";
|
||||
|
||||
// Positional arguments, with "--name value" options (and the "--sandbox" and "--help" flags) taken out wherever they are.
|
||||
struct Arguments {
|
||||
|
|
@ -311,18 +311,20 @@ nostrod does the work, and asks for your password before it pays or changes anyt
|
|||
std::println("card {} is now {}", id, card.status);
|
||||
}
|
||||
|
||||
// Apps pay at checkout from one account, and the user confirms each payment with their password.
|
||||
// The account a shop's pay link is paid from to begin with: where the Nostro app's pay form starts when a
|
||||
// payto:// link opens it. A choice kept for the user and no more: the form changes it, and nothing is paid from it
|
||||
// without the user's press and password.
|
||||
void CheckoutAccount(Client& client, const Arguments& args) {
|
||||
const std::string_view verb = args.At(1, "checkout command");
|
||||
if(verb != "account") {
|
||||
throw std::invalid_argument(std::format("unknown checkout command '{}'", verb));
|
||||
}
|
||||
if(args.positional.size() > 2) {
|
||||
std::println("checkouts pay from {}", client.SetDefaultCheckoutAccount(args.positional[2]));
|
||||
std::println("pay links start from {}", client.SetDefaultCheckoutAccount(args.positional[2]));
|
||||
} else if(const std::optional<Account> chosen = client.DefaultCheckoutAccount()) {
|
||||
std::println("{}", *chosen);
|
||||
} else {
|
||||
std::println("no account pays at checkout: `nostro checkout account <account>` chooses one");
|
||||
std::println("no account is chosen for pay links to start from: `nostro checkout account <account>` chooses one");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,10 @@
|
|||
// fonts, the toolkit's Context, the desktop portal, the app (Nostro.App) and the window that shows it.
|
||||
// nostro-app [--size <width>x<height>] [--scale <scale>] [--theme light|dark] [--route <place>] [--verbose]
|
||||
// [--screenshot <file.png>] [--screenshot-after <seconds>] [--poke <script>] [--quit-after <seconds>]
|
||||
// [--gallery <widgets|list|text>]
|
||||
// [--gallery <widgets|list|text>] [<link>]
|
||||
// A link is a payment to make, "payto://iban/NL91…?amount=EUR:12.50&receiver-name=…&message=…", as the desktop hands
|
||||
// one on from a website's pay button (the desktop entry's %u): the app opens its pay form filled in, and sends nothing
|
||||
// by itself. The window decides nothing about it: the link goes to the app as it came (App::Open).
|
||||
// --size is in dp and holds where the compositor leaves the size to the app. --route starts on a place of the app:
|
||||
// accounts, cards, settings, connections, checkout, about, account:<IBAN>, or a step of adding a connection: add,
|
||||
// add:<bank> or add:<bank>:<way>, as in add:bunq:key. The rest is for looking at the window without a hand on it.
|
||||
|
|
@ -474,6 +477,7 @@ int main(int argc, char* argv[]) {
|
|||
float screenshotAfter = 1;
|
||||
std::optional<float> quitAfter;
|
||||
std::string route;
|
||||
std::string link;
|
||||
std::string galleryPlace;
|
||||
std::string poke;
|
||||
bool verbose = false;
|
||||
|
|
@ -508,10 +512,24 @@ int main(int argc, char* argv[]) {
|
|||
poke = value(++i);
|
||||
} else if(args[i] == "--verbose") {
|
||||
verbose = true;
|
||||
} else if(!args[i].starts_with('-') && link.empty()) {
|
||||
// What is no option is a link to open. It says who is to be paid, how much and what for, and a command
|
||||
// line is there for every local user to read for as long as the program runs, and is what polkit
|
||||
// writes to its log of who asked for a password. So it is taken off it once it is kept: the bytes of
|
||||
// this argument are the program's own to overwrite. What started the program may still show it, as
|
||||
// the sandbox of a Flatpak does its command.
|
||||
link = args[i];
|
||||
std::fill_n(argv[i + 1], link.size(), '\0');
|
||||
} else if(!args[i].starts_with('-')) {
|
||||
// Neither this nor the next names what was given: a link is not for the log.
|
||||
throw std::invalid_argument("a second link was given: one is opened at a time");
|
||||
} else {
|
||||
throw std::invalid_argument(std::format("unknown argument '{}'", args[i]));
|
||||
}
|
||||
}
|
||||
if(!link.empty() && (!route.empty() || !galleryPlace.empty())) {
|
||||
throw std::invalid_argument("a link goes to its own place of the app: it is not opened with --route or --gallery");
|
||||
}
|
||||
options.assets = AssetDirectory();
|
||||
options.cache = CacheDirectory();
|
||||
|
||||
|
|
@ -616,6 +634,11 @@ int main(int argc, char* argv[]) {
|
|||
if(!route.empty() && app->Navigate(route)) {
|
||||
route.clear();
|
||||
}
|
||||
// The link the program was started with. The app reads it, and opens its form once it knows the
|
||||
// accounts: nothing is waited for or decided here.
|
||||
if(!link.empty()) {
|
||||
app->Open(link);
|
||||
}
|
||||
}
|
||||
window->Start();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue