mirror of
https://github.com/yawaflua/spworlds-csharp-library.git
synced 2025-12-10 04:29:25 +02:00
9
src/Types/PaymentData.cs
Normal file
9
src/Types/PaymentData.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace spworlds.Types;
|
||||||
|
|
||||||
|
public class PaymentData
|
||||||
|
{
|
||||||
|
public int Amount;
|
||||||
|
public string RedirectUrl;
|
||||||
|
public string WebHookUrl;
|
||||||
|
public string Data;
|
||||||
|
}
|
||||||
13
src/Types/SkinPart.cs
Normal file
13
src/Types/SkinPart.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
namespace spworlds.Types;
|
||||||
|
|
||||||
|
public enum SkinPart
|
||||||
|
{
|
||||||
|
face;
|
||||||
|
front;
|
||||||
|
front_full;
|
||||||
|
head;
|
||||||
|
bust;
|
||||||
|
full;
|
||||||
|
skin;
|
||||||
|
|
||||||
|
}
|
||||||
25
src/Types/User.cs
Normal file
25
src/Types/User.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
using spworlds;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
namespace spworlds.Types;
|
||||||
|
|
||||||
|
public class User
|
||||||
|
{
|
||||||
|
public readonly string Name
|
||||||
|
public readonly string Uuid
|
||||||
|
public readonly JsonNode profile
|
||||||
|
|
||||||
|
private HttpClient client = new();
|
||||||
|
|
||||||
|
public bool IsPlayer() => Name != null ? true : false;
|
||||||
|
|
||||||
|
public User(string name)
|
||||||
|
{
|
||||||
|
Uuid = JsonNode.Parse(client.GetStringAsync($"https://api.mojang.com/users/profiles/minecraft/{name}"))["id"];
|
||||||
|
profile = JsonNode.Parse(client.GetStringAsync($"https://sessionserver.mojang.com/session/minecraft/profile/{Uuid}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetSkinPart(SkinPart skinPart, string size = "64")
|
||||||
|
{
|
||||||
|
return (string)$"https://visage.surgeplay.com/{skinPart}/{size}/{this.profile["profileId"]}"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,40 +5,44 @@ using System.Text.Json.Nodes;
|
|||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
using spworlds.Types;
|
||||||
namespace spworlds;
|
namespace spworlds;
|
||||||
|
|
||||||
public class SPWorlds
|
public class SPWorlds
|
||||||
{
|
{
|
||||||
private readonly HttpClient client;
|
private readonly HttpClient client;
|
||||||
|
private string token;
|
||||||
|
|
||||||
public SPWorlds(string id, string token)
|
public SPWorlds(string id, string token)
|
||||||
{
|
{
|
||||||
client = new HttpClient();
|
client = new HttpClient();
|
||||||
var BearerToken = $"{id}:{token}";
|
var BearerToken = $"{id}:{token}";
|
||||||
var token = token
|
this.token = token;
|
||||||
string Base64BearerToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(BearerToken));
|
string Base64BearerToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(BearerToken));
|
||||||
|
|
||||||
client.BaseAddress = new Uri("https://spworlds.ru/api/public/");
|
client.BaseAddress = new Uri("https://spworlds.ru/api/public/");
|
||||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Base64BearerToken);
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Base64BearerToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<bool> ValidateWebhook(string webhook, string body_hash)
|
|
||||||
|
// Полностью бесполезная функция, вебхук, возвращающийся от сайта по факту невозможно валидировать.
|
||||||
|
private async Task<bool> ValidateWebHook(string webHook, string bodyHash)
|
||||||
{
|
{
|
||||||
// Если я правильно все понял, то вот
|
// Если я правильно все понял, то вот
|
||||||
// Конвертим из string в bytes body_hash
|
// Конвертим из string в bytes body_hash
|
||||||
byte[] body = Encoding.UTF8.GetBytes(body_hash);
|
byte[] body = Encoding.UTF8.GetBytes(bodyHash);
|
||||||
// потом конвертим вебхук
|
// потом конвертим вебхук
|
||||||
byte[] webhook = Encoding.UTF8.GetBytes(webhook);
|
byte[] webhook = Encoding.UTF8.GetBytes(webHook);
|
||||||
// создаем объект с токеном(тоже encoded в bytes) для сопостовления
|
// создаем объект с токеном(тоже encoded в bytes) для сопостовления
|
||||||
var key = new HMACSHA256(Encoding.UTF8.GetBytes(token));
|
var key = new HMACSHA256(Encoding.UTF8.GetBytes(token));
|
||||||
// Переводим в Base64
|
// Переводим в Base64
|
||||||
string webhook_64 = Convert.ToBase64String(key.ComputeHash(webhook));
|
string webhook64 = Convert.ToBase64String(key.ComputeHash(webhook));
|
||||||
return webhook_64.Equals(body);
|
return webhook64.Equals(body);
|
||||||
/**
|
/**
|
||||||
* Тот же код, но на Python:
|
* Тот же код, но на Python:
|
||||||
hmac_data = hmac.new(token.encode('utf - 8'), webhook.encode('utf - 8'), sha256).digest()
|
hmacData = hmac.new(token.encode('utf - 8'), webhook.encode('utf - 8'), sha256).digest()
|
||||||
base64_data = b64encode(hmac_data)
|
base64Data = b64encode(hmacData)
|
||||||
return hmac.compare_digest(base64_data, body_hash.encode('utf-8'))
|
return hmac.compare_digest(base64Data, bodyHash.encode('utf-8'))
|
||||||
**/
|
**/
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,12 +91,12 @@ public class SPWorlds
|
|||||||
await SendRequest(endpoint: "transactions", body: transitionInfo);
|
await SendRequest(endpoint: "transactions", body: transitionInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> GetUser(string discordId)
|
public async Task<User> GetUser(string discordId)
|
||||||
{
|
{
|
||||||
var user = JsonObject.Parse(await SendRequest($"users/{discordId}"));
|
var userResponse = JsonObject.Parse(await SendRequest($"users/{discordId}"));
|
||||||
var userName = user["username"];
|
var userName = userResponse["username"];
|
||||||
|
User user = new() { Name = userName}
|
||||||
return (string)userName;
|
return (User)user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> InitPayment(int amount, string redirectUrl, string webhookUrl, string data)
|
public async Task<string> InitPayment(int amount, string redirectUrl, string webhookUrl, string data)
|
||||||
@@ -110,4 +114,21 @@ public class SPWorlds
|
|||||||
|
|
||||||
return (string)url;
|
return (string)url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<string> InitPayment(PaymentData paymentData)
|
||||||
|
{
|
||||||
|
var paymentInfo = new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
{ "amount", paymentData.Amount },
|
||||||
|
{ "redirectUrl", paymentData.RedirectUrl },
|
||||||
|
{ "webhookUrl", paymentData.WebHookUrl },
|
||||||
|
{ "data", paymentData.Data }
|
||||||
|
};
|
||||||
|
|
||||||
|
var payment = JsonObject.Parse(await SendRequest(endpoint: $"payment", body: paymentInfo));
|
||||||
|
var url = payment["url"];
|
||||||
|
|
||||||
|
return (string)url;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user