2026-10-06 15:39:17 +02:00
|
|
|
//SPDX-License-Identifier: LGPL-3.0-only
|
|
|
|
|
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
|
|
|
|
|
|
module;
|
|
|
|
|
|
|
|
|
|
#include "../lib/json.hpp"
|
|
|
|
|
#include <openssl/types.h>
|
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
|
|
|
|
|
module Nostro:Bunq_impl;
|
|
|
|
|
|
|
|
|
|
import std;
|
|
|
|
|
import :Bunq;
|
2026-10-31 20:50:57 +01:00
|
|
|
import :Connector;
|
2026-10-06 15:39:17 +02:00
|
|
|
import :Crypto;
|
2026-10-31 20:50:57 +01:00
|
|
|
import :Utils;
|
2026-10-06 15:39:17 +02:00
|
|
|
import Crafter.Network;
|
|
|
|
|
|
|
|
|
|
using namespace Nostro;
|
|
|
|
|
using namespace Nostro::Crypto;
|
|
|
|
|
using namespace nlohmann;
|
|
|
|
|
using namespace Crafter;
|
|
|
|
|
|
|
|
|
|
std::vector<BunqConnector> BunqConnector::connectors;
|
|
|
|
|
|
|
|
|
|
json InResponse(const json& doc, std::string_view key) {
|
|
|
|
|
for (const json& item : doc.at("Response")) {
|
|
|
|
|
if (item.is_object()) {
|
|
|
|
|
if (const json::const_iterator v = item.find(key); v != item.end()) {
|
|
|
|
|
return *v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw std::runtime_error(std::format("bunq response has no '{}' object", key));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json GetUser(std::string_view body) {
|
|
|
|
|
try {
|
|
|
|
|
return InResponse(json::parse(body), "UserCompany");
|
|
|
|
|
} catch(std::runtime_error e) {
|
|
|
|
|
return InResponse(json::parse(body), "UserPerson");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-31 20:50:57 +01:00
|
|
|
std::string StringOrEmpty(const json& object, std::string_view key) {
|
|
|
|
|
const json::const_iterator value = object.find(key);
|
|
|
|
|
if(value == object.end() || !value->is_string()) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
return value->get<std::string>();
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-06 15:39:17 +02:00
|
|
|
BunqAccount::BunqAccount(std::string&& name, std::string&& bic, std::string&& iban, Currency currency, std::int64_t balance, std::uint32_t id, BunqConnector* connector) : Account(std::move(name), std::move(bic), std::move(iban), currency, balance), id(id), connector(connector) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-31 20:50:57 +01:00
|
|
|
void BunqAccount::Send(std::string_view iban, std::string_view name, std::int64_t amount, Currency currency, std::string_view description) const {
|
|
|
|
|
const std::string body = std::format("{{\"amount\": {{\"value\":\"{}\", \"currency\":\"{}\"}},\"counterparty_alias\":{{\"type\":\"IBAN\",\"value\":\"{}\", \"name\":\"{}\"}}, \"description\":\"{}\"}}", amount, GetCurrencyCode(currency), iban, name, description);
|
|
|
|
|
const std::string route = std::format("/v1/user/{}/monetary-account/{}/payment", connector->id, id);
|
2026-10-06 15:39:17 +02:00
|
|
|
|
|
|
|
|
HTTPResponse response = connector->client.Send(CreateRequestHTTP("POST", route, "public-api.sandbox.bunq.com", {{"User-Agent", "Nostro Crafter.Network"}, {"Content-Type", "application/json"}, {"Accept", "application/json"}, {"X-Bunq-Client-Authentication", connector->sessionToken}, {"X-Bunq-Client-Signature", EscB(SignBody(connector->privateKey, body))}},
|
|
|
|
|
body
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
if(response.status != "200") {
|
|
|
|
|
throw std::runtime_error(std::format("{} non 200 status code: {}", route, response.body));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-31 20:50:57 +01:00
|
|
|
void BunqConnector::GetNewTransactions() {
|
|
|
|
|
for(BunqAccount& account : accounts) {
|
|
|
|
|
account.transactions.clear();
|
2026-10-06 15:39:17 +02:00
|
|
|
|
2026-10-31 20:50:57 +01:00
|
|
|
const std::chrono::system_clock::time_point last = account.GetLastTransactionTime(connectorId);
|
|
|
|
|
|
|
|
|
|
std::string route = std::format("/v1/user/{}/monetary-account/{}/payment?count=200", id, account.id);
|
|
|
|
|
while(!route.empty()) {
|
|
|
|
|
HTTPResponse response = client.Send(CreateRequestHTTP("GET", route, "public-api.sandbox.bunq.com", {{"User-Agent", "Nostro Crafter.Network"}, {"Content-Type", "application/json"}, {"Accept", "application/json"}, {"X-Bunq-Client-Authentication", sessionToken}}));
|
|
|
|
|
|
|
|
|
|
if(response.status == "429") {
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(2));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(response.status != "200") {
|
|
|
|
|
throw std::runtime_error(std::format("{} non 200 status code: {}", route, response.body));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const json doc = json::parse(response.body);
|
|
|
|
|
|
|
|
|
|
const json& olderUrl = doc.at("Pagination").at("older_url");
|
|
|
|
|
if( olderUrl.is_null()) {
|
|
|
|
|
route.clear();
|
|
|
|
|
} else {
|
|
|
|
|
route = olderUrl.get<std::string>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(const json& item : doc.at("Response")) {
|
|
|
|
|
const json::const_iterator paymentIt = item.find("Payment");
|
|
|
|
|
if(paymentIt == item.end()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const json& paymentJ = *paymentIt;
|
|
|
|
|
const std::chrono::system_clock::time_point timePoint = ParseTimestamp(paymentJ.at("created").get<std::string>());
|
|
|
|
|
if(timePoint <= last) {
|
|
|
|
|
route.clear();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const json& counterpartyJ = paymentJ.at("counterparty_alias");
|
|
|
|
|
const Currency currency = GetCurrency(paymentJ.at("amount").at("currency").get<std::string>());
|
|
|
|
|
|
|
|
|
|
account.transactions.emplace_back(
|
|
|
|
|
StringOrEmpty(counterpartyJ, "display_name"),
|
|
|
|
|
StringOrEmpty(counterpartyJ, "swift_bic"),
|
|
|
|
|
StringOrEmpty(counterpartyJ, "iban"),
|
|
|
|
|
StringOrEmpty(paymentJ, "description"),
|
|
|
|
|
currency,
|
|
|
|
|
ParseAmount(paymentJ.at("amount").at("value").get<std::string>(), currency),
|
|
|
|
|
timePoint
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::ranges::reverse(account.transactions);
|
|
|
|
|
|
|
|
|
|
account.SaveTransactions();
|
|
|
|
|
}
|
2026-10-06 15:39:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BunqConnector::RefreshSession() {
|
2026-10-31 20:50:57 +01:00
|
|
|
const std::string body = std::format("{{\"secret\": \"{}\"}}", apiKey);
|
2026-10-06 15:39:17 +02:00
|
|
|
|
|
|
|
|
HTTPResponse response = client.Send(CreateRequestHTTP("POST", "/v1/session-server", "public-api.sandbox.bunq.com", {{"User-Agent", "Nostro Crafter.Network"}, {"Content-Type", "application/json"}, {"Accept", "application/json"}, {"X-Bunq-Client-Authentication", installationToken}, {"X-Bunq-Client-Signature", EscB(SignBody(privateKey, body))}},
|
|
|
|
|
body
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
if(response.status != "200") {
|
|
|
|
|
throw std::runtime_error(std::format("/v1/session-server non 200 status code: {}", response.body));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sessionToken = InResponse(json::parse(response.body), "Token").at("token");
|
|
|
|
|
|
|
|
|
|
json user = GetUser(response.body);
|
|
|
|
|
|
|
|
|
|
name = user.at("name");
|
|
|
|
|
id = user.at("id");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BunqConnector& BunqConnector::Create(std::string&& apiKey, std::string_view allowedIp) {
|
|
|
|
|
BunqConnector& connector = connectors.emplace_back();
|
|
|
|
|
|
|
|
|
|
std::tuple<EVP_PKEY*, std::string> keys = CreateKey();
|
|
|
|
|
|
|
|
|
|
connector.apiKey = std::move(apiKey);
|
|
|
|
|
connector.privateKey = std::get<0>(keys);
|
|
|
|
|
|
|
|
|
|
HTTPResponse response = client.Send(CreateRequestHTTP("POST", "/v1/installation", "public-api.sandbox.bunq.com", {{"User-Agent", "Nostro Crafter.Network"}, {"Content-Type", "application/json"}, {"Accept", "application/json"}},
|
|
|
|
|
std::format("{{\"client_public_key\": \"{}\"}}", EscB(std::get<1>(keys)))
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
if(response.status != "200") {
|
|
|
|
|
throw std::runtime_error(std::format("/v1/installation non 200 status code: {}", response.body));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connector.installationToken = InResponse(json::parse(response.body), "Token").at("token");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response = client.Send(CreateRequestHTTP("POST", "/v1/device-server", "public-api.sandbox.bunq.com", {{"User-Agent", "Nostro Crafter.Network"}, {"Content-Type", "application/json"}, {"Accept", "application/json"}, {"X-Bunq-Client-Authentication", connector.installationToken}},
|
|
|
|
|
std::format("{{\"description\": \"Nostro\", \"secret\": \"{}\", \"permitted_ips\": [{}]}}", connector.apiKey, allowedIp)
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
if(response.status != "200") {
|
|
|
|
|
throw std::runtime_error(std::format("/v1/device-server non 200 status code: {}", response.body));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connector.RefreshSession();
|
|
|
|
|
|
|
|
|
|
return connector;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BunqConnector::RefreshAccounts() {
|
|
|
|
|
HTTPResponse response = client.Send(CreateRequestHTTP("GET", std::format("/v1/user/{}/monetary-account", id), "public-api.sandbox.bunq.com", {{"User-Agent", "Nostro Crafter.Network"}, {"Content-Type", "application/json"}, {"Accept", "application/json"}, {"X-Bunq-Client-Authentication", sessionToken}}));
|
|
|
|
|
|
|
|
|
|
if(response.status != "200") {
|
|
|
|
|
throw std::runtime_error(std::format("/v1/user/{}/monetary-account non 200 status code: {}", id, response.body));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(const json& item : json::parse(response.body).at("Response")) {
|
|
|
|
|
json::const_iterator accountIt = item.find("MonetaryAccountBank");
|
|
|
|
|
if(accountIt == item.end()) {
|
|
|
|
|
accountIt = item.find("MonetaryAccountSavings");
|
|
|
|
|
}
|
|
|
|
|
if(accountIt == item.end()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const json& accountJ = *accountIt;
|
|
|
|
|
|
|
|
|
|
const std::uint32_t accountId = accountJ.at("id").get<std::uint32_t>();
|
|
|
|
|
const Currency currency = GetCurrency(accountJ.at("currency").get<std::string>());
|
|
|
|
|
const std::int64_t balance = ParseAmount(accountJ.at("balance").at("value").get<std::string>(), currency);
|
|
|
|
|
|
|
|
|
|
const std::vector<BunqAccount>::iterator existing = std::ranges::find(accounts, accountId, &BunqAccount::id);
|
|
|
|
|
if(existing != accounts.end()) {
|
|
|
|
|
existing->balance = balance;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string_view iban;
|
|
|
|
|
for(const json& alias : accountJ.at("alias")) {
|
|
|
|
|
if(alias.at("type").get<std::string>() == "IBAN") {
|
|
|
|
|
iban = alias.at("value").get_ref<const std::string&>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
accounts.emplace_back(accountJ.at("description").get<std::string>(), "", std::string(iban), currency, balance, accountId, this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BunqConnector::TestConnector() {
|
|
|
|
|
HTTPResponse response = client.Send(CreateRequestHTTP("GET", std::format("/v1/user/{}/monetary-account", id), "public-api.sandbox.bunq.com", {{"User-Agent", "Nostro Crafter.Network"}, {"Content-Type", "application/json"}, {"Accept", "application/json"}, {"X-Bunq-Client-Authentication", sessionToken}}));
|
|
|
|
|
|
|
|
|
|
if(response.status == "200") {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
RefreshSession();
|
|
|
|
|
HTTPResponse response = client.Send(CreateRequestHTTP("GET", std::format("/v1/user/{}/monetary-account", id), "public-api.sandbox.bunq.com", {{"User-Agent", "Nostro Crafter.Network"}, {"Content-Type", "application/json"}, {"Accept", "application/json"}, {"X-Bunq-Client-Authentication", sessionToken}}));
|
|
|
|
|
if(response.status == "200") {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Nostro::to_json(json& j, const BunqConnector& connector) {
|
|
|
|
|
BIO* bio = ThrowOnOpenssl(BIO_new(BIO_s_mem()));
|
|
|
|
|
ThrowOnOpenssl(PEM_write_bio_PrivateKey(bio, connector.privateKey, nullptr, nullptr, 0, nullptr, nullptr));
|
|
|
|
|
|
|
|
|
|
char* data;
|
|
|
|
|
const long len = ThrowOnOpenssl(BIO_get_mem_data(bio, &data));
|
|
|
|
|
std::string privateKey(data, len);
|
|
|
|
|
BIO_free(bio);
|
|
|
|
|
|
|
|
|
|
j = json{
|
|
|
|
|
{"sessionToken", connector.sessionToken},
|
|
|
|
|
{"installationToken", connector.installationToken},
|
|
|
|
|
{"privateKey", std::move(privateKey)},
|
|
|
|
|
{"apiKey", connector.apiKey},
|
|
|
|
|
{"name", connector.name},
|
|
|
|
|
{"id", connector.id},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Nostro::from_json(const json& j, BunqConnector& connector) {
|
|
|
|
|
j.at("sessionToken").get_to(connector.sessionToken);
|
|
|
|
|
j.at("installationToken").get_to(connector.installationToken);
|
|
|
|
|
j.at("apiKey").get_to(connector.apiKey);
|
|
|
|
|
j.at("name").get_to(connector.name);
|
|
|
|
|
j.at("id").get_to(connector.id);
|
|
|
|
|
|
|
|
|
|
const std::string& pem = j.at("privateKey").get_ref<const std::string&>();
|
|
|
|
|
|
|
|
|
|
BIO* bio = ThrowOnOpenssl(BIO_new_mem_buf(pem.data(), static_cast<int>(pem.size())));
|
|
|
|
|
EVP_PKEY* pkey = PEM_read_bio_PrivateKey(bio, nullptr, nullptr, nullptr);
|
|
|
|
|
BIO_free(bio);
|
|
|
|
|
|
|
|
|
|
connector.privateKey = ThrowOnOpenssl(pkey);
|
|
|
|
|
}
|