feat(g1): integrate authorized manual payouts into complete SYS.API and ROBUST economy

This commit is contained in:
OpenGenesis (EULA) committed 2026-11-25 21:00:56 +01:00
1 parent 7a8422dfbd
commit f1f6fd10d9
1 file changed
+94 -1
+94 -1
View File
@@ -32,6 +32,7 @@ public sealed class SysApiHost : IDisposable
private readonly IAuthenticationService _auth;
private readonly ISysApiReadModel? _readModel;
private readonly ISysApiAlertPublisher? _alertPublisher;
private readonly SysApiNvMoney? _nvMoney;
private readonly SemaphoreSlim _alertGate = new(1, 1);
private DateTimeOffset _lastAlertAt = DateTimeOffset.MinValue;
private readonly ISysApiAccessPolicyStore? _accessPolicy;
@@ -50,7 +51,8 @@ public sealed class SysApiHost : IDisposable
ISysApiAccessPolicyStore? accessPolicy = null, string policyReadToken = "",
SysApiResidentGateway? residents = null,
ISysApiAdminData? adminData = null,
ISysApiAlertPublisher? alertPublisher = null)
ISysApiAlertPublisher? alertPublisher = null,
SysApiNvMoney? nvMoney = null)
{
if (localPort is < 1025 or > 65535) throw new ArgumentOutOfRangeException(nameof(localPort));
_accounts = accounts ?? throw new ArgumentNullException(nameof(accounts));
@@ -60,6 +62,7 @@ public sealed class SysApiHost : IDisposable
_residents = residents;
_adminData = adminData;
_alertPublisher = alertPublisher;
_nvMoney = nvMoney;
_policyReadToken = policyReadToken.Length >= 32 && policyReadToken.Length <= 128 &&
policyReadToken.All(Uri.IsHexDigit) ? Encoding.ASCII.GetBytes(policyReadToken) : Array.Empty<byte>();
if (portalToken.Length != 128 || !portalToken.All(Uri.IsHexDigit))
@@ -295,6 +298,7 @@ public sealed class SysApiHost : IDisposable
await AssetAsync(response, "index.html", "text/html; charset=utf-8");
return;
}
if (isGet && path == "/money.js") { await AssetAsync(response, "money.js", "text/javascript; charset=utf-8"); return; }
if (isGet && path == "/app.js") { await AssetAsync(response, "app.js", "text/javascript; charset=utf-8"); return; }
if (isGet && path == "/style.css") { await AssetAsync(response, "style.css", "text/css; charset=utf-8"); return; }
if (isGet && path == "/ogl-mascot.webp") { await AssetAsync(response, "ogl-mascot.webp", "image/webp"); return; }
@@ -749,6 +753,35 @@ public sealed class SysApiHost : IDisposable
return;
}
}
// Money payouts use a DIFFERENT credential from the general portal token.
// No client-provided "Stripe paid" or "PayPal completed" claim is trusted.
if (path is "/api/v1/portal/money/payouts" or "/api/v1/portal/money/rate")
{
if (_nvMoney is null || !_nvMoney.PortalEnabled)
{
await ReplyAsync(response, 503, new { error = "Portal NV$ payouts are not configured" });
return;
}
if (!IsTrustedProxy(req) || !_nvMoney.ValidPortalCredential(req))
{
await ReplyAsync(response, 401, new { error = "Dedicated payout authorization required" });
return;
}
if (isGet && path.EndsWith("/rate", StringComparison.Ordinal))
{
await ReplyAsync(response, 200, new {
nvPerEuro = SysApiNvMoney.NvPerEuro, euroCurrency = "EUR",
currency = "NV$", paymentSettlementEnabled = false
});
return;
}
if (req.HttpMethod != "POST" || path.EndsWith("/rate", StringComparison.Ordinal))
{
await ReplyAsync(response, 405, new { error = "Method not allowed" }); return;
}
await PayNvAsync(req, response, "portal", true);
return;
}
if (!TryAdmin(req, out AdminSession admin))
{
await ReplyAsync(response, 401, new { error = "Access Denied – Keine Berechtigung" }); return;
@@ -758,6 +791,27 @@ public sealed class SysApiHost : IDisposable
{
await ReplyAsync(response, 403, new { error = "CSRF protection denied" }); return;
}
if (path == "/api/admin/money/status")
{
if (!isGet)
{
await ReplyAsync(response, 405, new { error = "GET required" }); return;
}
await ReplyAsync(response, 200, _nvMoney?.Status() ??
new { enabled = false, portalEnabled = false,
nvPerEuro = SysApiNvMoney.NvPerEuro,
paymentSettlementEnabled = false });
return;
}
if (path == "/api/admin/money/payouts")
{
if (req.HttpMethod != "POST")
{
await ReplyAsync(response, 405, new { error = "POST required" }); return;
}
await PayNvAsync(req, response, admin.Avatar.ToString(), false);
return;
}
if (path == "/api/admin/alerts")
{
if (req.HttpMethod != "POST")
@@ -843,6 +897,7 @@ public sealed class SysApiHost : IDisposable
minAdminLevel = 200,
oldWorldApiUnchanged = true,
alertsAvailable = _alertPublisher is not null,
moneyAvailable = _nvMoney?.AdminEnabled == true,
endpoints = new[] { "/v1/health", "/api/v1/portal/ping", "/api/v1/portal/capabilities" }
});
return;
@@ -858,6 +913,44 @@ public sealed class SysApiHost : IDisposable
finally { try { context.Response.Close(); } catch { } }
}
private async Task PayNvAsync(HttpListenerRequest request, HttpListenerResponse response,
string actor, bool portal)
{
if (_nvMoney is null || (portal ? !_nvMoney.PortalEnabled : !_nvMoney.AdminEnabled))
{
await ReplyAsync(response, 503, new { error = "NV$ payout feature is disabled" });
return;
}
var input = await ReadInput<NvPayoutInput>(request);
try
{
NvPayoutReceipt receipt = _nvMoney.Pay(input, actor, portal);
Log.InfoFormat("[SYS.API MONEY] Credit {0} NV$ to {1}, transaction {2}, duplicated={3}",
receipt.AmountNv, receipt.AvatarId, receipt.TransactionId, receipt.Duplicate);
await ReplyAsync(response, receipt.Duplicate ? 200 : 201, new {
receipt.TransactionId, receipt.AvatarId, receipt.AmountNv,
receipt.BalanceNv, receipt.Duplicate, currency = "NV$"
});
}
catch (ArgumentException ex)
{
await ReplyAsync(response, 400, new { error = ex.Message });
}
catch (NexVerse.Core.Economy.NexLedgerConflictException)
{
await ReplyAsync(response, 409, new { error = "Idempotency request conflicts with existing ledger transaction" });
}
catch (InvalidOperationException)
{
await ReplyAsync(response, 503, new { error = "NV$ ledger or payout service unavailable" });
}
catch (Exception ex)
{
Log.Error("[SYS.API MONEY] Financial transaction failed", ex);
await ReplyAsync(response, 503, new { error = "NV$ payout could not be completed" });
}
}
public void Dispose()
{
if (Interlocked.Exchange(ref _disposed, 1) != 0) return;