pre-ai
This commit is contained in:
parent
be13205ffd
commit
367c19386c
17 changed files with 561 additions and 166 deletions
|
|
@ -14,7 +14,9 @@ module Nostro:Bunq_impl;
|
|||
|
||||
import std;
|
||||
import :Bunq;
|
||||
import :Connector;
|
||||
import :Crypto;
|
||||
import :Utils;
|
||||
import Crafter.Network;
|
||||
|
||||
using namespace Nostro;
|
||||
|
|
@ -24,27 +26,6 @@ using namespace Crafter;
|
|||
|
||||
std::vector<BunqConnector> BunqConnector::connectors;
|
||||
|
||||
std::string EscB(std::string_view s) {
|
||||
std::string out;
|
||||
out.reserve(s.size() + 8);
|
||||
for (const char c : s) {
|
||||
switch (c) {
|
||||
case '"': out += "\\\""; break;
|
||||
case '\\': out += "\\\\"; break;
|
||||
case '\n': out += "\\n"; break;
|
||||
case '\r': out += "\\r"; break;
|
||||
case '\t': out += "\\t"; break;
|
||||
default:
|
||||
if (static_cast<unsigned char>(c) < 0x20) {
|
||||
out += std::format("\\u{:04x}", static_cast<unsigned char>(c));
|
||||
} else {
|
||||
out += c;
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
json InResponse(const json& doc, std::string_view key) {
|
||||
for (const json& item : doc.at("Response")) {
|
||||
if (item.is_object()) {
|
||||
|
|
@ -64,13 +45,21 @@ json GetUser(std::string_view body) {
|
|||
}
|
||||
}
|
||||
|
||||
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>();
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
void BunqAccount::SendPayment(std::string body) const {
|
||||
std::string route = std::format("v1/user/{}/monetary-account/{}/payment", connector->id, id);
|
||||
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);
|
||||
|
||||
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
|
||||
|
|
@ -81,18 +70,70 @@ void BunqAccount::SendPayment(std::string body) const {
|
|||
}
|
||||
}
|
||||
|
||||
void BunqAccount::Send(std::string_view iban, std::string_view name, std::int64_t amount, Currency currency) const {
|
||||
std::string body = std::format("{{\"amount\": {{\"value\":{}, \"currency\":\"{}\"}},\"counterparty_alias\":{{\"type\":\"IBAN\",\"value\":{}, \"name\":{}}}}}", amount, GetCurrencyCode(currency), iban, name);
|
||||
SendPayment(body);
|
||||
}
|
||||
void BunqConnector::GetNewTransactions() {
|
||||
for(BunqAccount& account : accounts) {
|
||||
account.transactions.clear();
|
||||
|
||||
void BunqAccount::Send(std::string_view iban, std::string_view name, std::int64_t amount, Currency currency, std::string_view description) const {
|
||||
std::string body = std::format("{{\"amount\": {{\"value\":{}, \"currency\":\"{}\"}},\"counterparty_alias\":{{\"type\":\"IBAN\",\"value\":{}, \"name\":{}}}, \"description\":\"{}\"}}", amount, GetCurrencyCode(currency), iban, name, description);
|
||||
SendPayment(body);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
void BunqConnector::RefreshSession() {
|
||||
std::string body = std::format("{{\"secret\": \"{}\"}}", apiKey);
|
||||
const std::string body = std::format("{{\"secret\": \"{}\"}}", apiKey);
|
||||
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue