// SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® module; #include #include 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::chrono::duration_cast(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; }