From 2a1317d393845b4d8c2aebe83482addf91da2948 Mon Sep 17 00:00:00 2001 From: yawaflua Date: Thu, 18 Apr 2024 18:34:27 +0300 Subject: [PATCH 1/3] New update --- gitignore => .gitignore | 0 obj/Debug/net6.0/spworlds.AssemblyInfo.cs | 3 ++- .../net6.0/spworlds.AssemblyInfoInputs.cache | 2 +- ...ds.GeneratedMSBuildEditorConfig.editorconfig | 4 +++- obj/Debug/net6.0/spworlds.assets.cache | Bin 228 -> 228 bytes 5 files changed, 6 insertions(+), 3 deletions(-) rename gitignore => .gitignore (100%) diff --git a/gitignore b/.gitignore similarity index 100% rename from gitignore rename to .gitignore diff --git a/obj/Debug/net6.0/spworlds.AssemblyInfo.cs b/obj/Debug/net6.0/spworlds.AssemblyInfo.cs index 5af4006..676a2aa 100644 --- a/obj/Debug/net6.0/spworlds.AssemblyInfo.cs +++ b/obj/Debug/net6.0/spworlds.AssemblyInfo.cs @@ -1,6 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -15,7 +16,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyDescriptionAttribute("данная библиотека предназначена для работы с сайтом spworlds. Ознакомиться с доку" + "ментацией можно в гитхабе проекта")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.5.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.5")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.5+c0805a7cd8abbd2deb3f3887f26df3ea891694a8")] [assembly: System.Reflection.AssemblyProductAttribute("spworlds")] [assembly: System.Reflection.AssemblyTitleAttribute("spworlds")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.5.0")] diff --git a/obj/Debug/net6.0/spworlds.AssemblyInfoInputs.cache b/obj/Debug/net6.0/spworlds.AssemblyInfoInputs.cache index 14e30a0..23c87c7 100644 --- a/obj/Debug/net6.0/spworlds.AssemblyInfoInputs.cache +++ b/obj/Debug/net6.0/spworlds.AssemblyInfoInputs.cache @@ -1 +1 @@ -16a07a97f6c3ac88ffd8da0fdc03f5c5f3aadf69 +1d5e88e75f835d7d540fe5df58387336023a0ed6689730e9ed5a2c7e28829bec diff --git a/obj/Debug/net6.0/spworlds.GeneratedMSBuildEditorConfig.editorconfig b/obj/Debug/net6.0/spworlds.GeneratedMSBuildEditorConfig.editorconfig index ad8855c..a5a88ec 100644 --- a/obj/Debug/net6.0/spworlds.GeneratedMSBuildEditorConfig.editorconfig +++ b/obj/Debug/net6.0/spworlds.GeneratedMSBuildEditorConfig.editorconfig @@ -8,4 +8,6 @@ build_property.PlatformNeutralAssembly = build_property.EnforceExtendedAnalyzerRules = build_property._SupportedPlatformList = Linux,macOS,Windows build_property.RootNamespace = spworlds -build_property.ProjectDir = C:\Users\losev\OneDrive\Рабочий стол\spworlds\spworlds-csharp-library\ +build_property.ProjectDir = C:\Users\yawaflua\Source\Repos\spworlds-csharp-library\ +build_property.EnableComHosting = +build_property.EnableGeneratedComInterfaceComImportInterop = diff --git a/obj/Debug/net6.0/spworlds.assets.cache b/obj/Debug/net6.0/spworlds.assets.cache index b6f7e026f3c5621e89a68c329191f2bf663f7b78..ed852395567b6be4ec62a895b47879069ce9c218 100644 GIT binary patch delta 55 zcmV-70LcI30ptNGP)kQa3;+NCNETR+U~h&3a`LGT%dLkrEtc|ii#!RM_yCmo4LAJU N;E^#Skw6HMRFF3f7F7TM delta 55 zcmV-70LcI30ptNGP)kQa3;+NCkSLw_+2Rd9=Ji0iW*N8+B@B6VG>d>@bfrBRZwIH# N;E^#Skw6HMRFF Date: Thu, 18 Apr 2024 18:38:00 +0300 Subject: [PATCH 2/3] update --- Deserialize.cs | 22 +++ Types/Card.cs | 14 ++ Types/PaymentData.cs | 9 +- Types/UserAccount.cs | 41 +++++ Types/UserCard.cs | 14 ++ Types/WebhookResponse.cs | 14 ++ obj/Debug/net6.0/spworlds.AssemblyInfo.cs | 2 +- .../net6.0/spworlds.AssemblyInfoInputs.cache | 2 +- obj/project.assets.json | 78 +++++++-- obj/project.nuget.cache | 8 +- obj/spworlds.csproj.nuget.dgspec.json | 27 ++- obj/spworlds.csproj.nuget.g.props | 6 +- spworlds.cs | 163 ++++++++++++------ spworlds.csproj | 7 +- 14 files changed, 324 insertions(+), 83 deletions(-) create mode 100644 Deserialize.cs create mode 100644 Types/Card.cs create mode 100644 Types/UserAccount.cs create mode 100644 Types/UserCard.cs create mode 100644 Types/WebhookResponse.cs diff --git a/Deserialize.cs b/Deserialize.cs new file mode 100644 index 0000000..85022c1 --- /dev/null +++ b/Deserialize.cs @@ -0,0 +1,22 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace spworlds +{ + internal class Deserialize + { + public static T DeserializeObject(string data) where T : class + { + T objectToReturn = JsonConvert.DeserializeObject(data); + if (objectToReturn == null) + throw new Exception("User not player."); + else + return objectToReturn; + + } + } +} diff --git a/Types/Card.cs b/Types/Card.cs new file mode 100644 index 0000000..835de8a --- /dev/null +++ b/Types/Card.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace spworlds.Types +{ + public class Card + { + public int Balance { get; set; } + public string Webhook { get; set; } + } +} diff --git a/Types/PaymentData.cs b/Types/PaymentData.cs index 8185f8f..4021ea6 100644 --- a/Types/PaymentData.cs +++ b/Types/PaymentData.cs @@ -1,8 +1,15 @@ namespace spworlds.Types; +public class Item +{ + public string Name { get; set; } + public int Count { get; set; } + public int Price { get; set; } + public string? Comment { get; set; } = null; +} public class PaymentData { - public int Amount; + public Item[] Items; public string RedirectUrl; public string WebHookUrl; public string Data; diff --git a/Types/UserAccount.cs b/Types/UserAccount.cs new file mode 100644 index 0000000..34bd5c5 --- /dev/null +++ b/Types/UserAccount.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace spworlds.Types +{ + // Root myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse); + public class UserCard + { + public string id { get; set; } + public string name { get; set; } + public string number { get; set; } + public int color { get; set; } + } + + public class City + { + public string id { get; set; } + public string name { get; set; } + public string description { get; set; } + public int x { get; set; } + public int z { get; set; } + public bool isMayor { get; set; } + } + + public class UserAccount + { + public string id { get; set; } + public string username { get; set; } + public string minecraftUUID { get; set; } + public string status { get; set; } + public List roles { get; set; } + public City city { get; set; } + public List cards { get; set; } + public DateTime createdAt { get; set; } + } + + +} diff --git a/Types/UserCard.cs b/Types/UserCard.cs new file mode 100644 index 0000000..02bdf4a --- /dev/null +++ b/Types/UserCard.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace spworlds.Types +{ + public class UserCard + { + public string Name { get; set; } + public int Number { get; set; } + } +} diff --git a/Types/WebhookResponse.cs b/Types/WebhookResponse.cs new file mode 100644 index 0000000..35f6f21 --- /dev/null +++ b/Types/WebhookResponse.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace spworlds.Types +{ + public class WebhookResponse + { + public int Id { get; set; } + public string Webhook { get; set; } + } +} diff --git a/obj/Debug/net6.0/spworlds.AssemblyInfo.cs b/obj/Debug/net6.0/spworlds.AssemblyInfo.cs index 676a2aa..bbb8a8d 100644 --- a/obj/Debug/net6.0/spworlds.AssemblyInfo.cs +++ b/obj/Debug/net6.0/spworlds.AssemblyInfo.cs @@ -16,7 +16,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyDescriptionAttribute("данная библиотека предназначена для работы с сайтом spworlds. Ознакомиться с доку" + "ментацией можно в гитхабе проекта")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.5.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.5+c0805a7cd8abbd2deb3f3887f26df3ea891694a8")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.5+2a1317d393845b4d8c2aebe83482addf91da2948")] [assembly: System.Reflection.AssemblyProductAttribute("spworlds")] [assembly: System.Reflection.AssemblyTitleAttribute("spworlds")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.5.0")] diff --git a/obj/Debug/net6.0/spworlds.AssemblyInfoInputs.cache b/obj/Debug/net6.0/spworlds.AssemblyInfoInputs.cache index 23c87c7..c33cbdd 100644 --- a/obj/Debug/net6.0/spworlds.AssemblyInfoInputs.cache +++ b/obj/Debug/net6.0/spworlds.AssemblyInfoInputs.cache @@ -1 +1 @@ -1d5e88e75f835d7d540fe5df58387336023a0ed6689730e9ed5a2c7e28829bec +408e46d4b0c697d197fb7b372e95e1f96ce6875650810a4a0ad66dc759971202 diff --git a/obj/project.assets.json b/obj/project.assets.json index 86a2ca5..e2c80f2 100644 --- a/obj/project.assets.json +++ b/obj/project.assets.json @@ -1,30 +1,77 @@ { "version": 3, "targets": { - "net6.0": {} + "net6.0": { + "Newtonsoft.Json/13.0.3": { + "type": "package", + "compile": { + "lib/net6.0/Newtonsoft.Json.dll": { + "related": ".xml" + } + }, + "runtime": { + "lib/net6.0/Newtonsoft.Json.dll": { + "related": ".xml" + } + } + } + } + }, + "libraries": { + "Newtonsoft.Json/13.0.3": { + "sha512": "HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ==", + "type": "package", + "path": "newtonsoft.json/13.0.3", + "files": [ + ".nupkg.metadata", + ".signature.p7s", + "LICENSE.md", + "README.md", + "lib/net20/Newtonsoft.Json.dll", + "lib/net20/Newtonsoft.Json.xml", + "lib/net35/Newtonsoft.Json.dll", + "lib/net35/Newtonsoft.Json.xml", + "lib/net40/Newtonsoft.Json.dll", + "lib/net40/Newtonsoft.Json.xml", + "lib/net45/Newtonsoft.Json.dll", + "lib/net45/Newtonsoft.Json.xml", + "lib/net6.0/Newtonsoft.Json.dll", + "lib/net6.0/Newtonsoft.Json.xml", + "lib/netstandard1.0/Newtonsoft.Json.dll", + "lib/netstandard1.0/Newtonsoft.Json.xml", + "lib/netstandard1.3/Newtonsoft.Json.dll", + "lib/netstandard1.3/Newtonsoft.Json.xml", + "lib/netstandard2.0/Newtonsoft.Json.dll", + "lib/netstandard2.0/Newtonsoft.Json.xml", + "newtonsoft.json.13.0.3.nupkg.sha512", + "newtonsoft.json.nuspec", + "packageIcon.png" + ] + } }, - "libraries": {}, "projectFileDependencyGroups": { - "net6.0": [] + "net6.0": [ + "Newtonsoft.Json >= 13.0.3" + ] }, "packageFolders": { - "C:\\Users\\losev\\.nuget\\packages\\": {}, + "C:\\Users\\yawaflua\\.nuget\\packages\\": {}, "C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages": {} }, "project": { "version": "1.0.5", "restore": { - "projectUniqueName": "C:\\Users\\losev\\OneDrive\\Рабочий стол\\spworlds\\spworlds-csharp-library\\spworlds.csproj", + "projectUniqueName": "C:\\Users\\yawaflua\\Source\\Repos\\spworlds-csharp-library\\spworlds.csproj", "projectName": "spworlds", - "projectPath": "C:\\Users\\losev\\OneDrive\\Рабочий стол\\spworlds\\spworlds-csharp-library\\spworlds.csproj", - "packagesPath": "C:\\Users\\losev\\.nuget\\packages\\", - "outputPath": "C:\\Users\\losev\\OneDrive\\Рабочий стол\\spworlds\\spworlds-csharp-library\\obj\\", + "projectPath": "C:\\Users\\yawaflua\\Source\\Repos\\spworlds-csharp-library\\spworlds.csproj", + "packagesPath": "C:\\Users\\yawaflua\\.nuget\\packages\\", + "outputPath": "C:\\Users\\yawaflua\\Source\\Repos\\spworlds-csharp-library\\obj\\", "projectStyle": "PackageReference", "fallbackFolders": [ "C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages" ], "configFilePaths": [ - "C:\\Users\\losev\\AppData\\Roaming\\NuGet\\NuGet.Config", + "C:\\Users\\yawaflua\\AppData\\Roaming\\NuGet\\NuGet.Config", "C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config", "C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config" ], @@ -45,11 +92,22 @@ "warnAsError": [ "NU1605" ] + }, + "restoreAuditProperties": { + "enableAudit": "true", + "auditLevel": "low", + "auditMode": "direct" } }, "frameworks": { "net6.0": { "targetAlias": "net6.0", + "dependencies": { + "Newtonsoft.Json": { + "target": "Package", + "version": "[13.0.3, )" + } + }, "imports": [ "net461", "net462", @@ -66,7 +124,7 @@ "privateAssets": "all" } }, - "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\7.0.401\\RuntimeIdentifierGraph.json" + "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.204\\RuntimeIdentifierGraph.json" } } } diff --git a/obj/project.nuget.cache b/obj/project.nuget.cache index ba1db74..be9e906 100644 --- a/obj/project.nuget.cache +++ b/obj/project.nuget.cache @@ -1,8 +1,10 @@ { "version": 2, - "dgSpecHash": "p7cKQ/JzbW/pFxNjOXo5Xhfj5ODn4c+I/3V85sntBWgUuc5uosay2iPDLl5YmDVFkvR7Suoevt+A+2w9fpQxjw==", + "dgSpecHash": "wn8UpI/92drj7Y9CQl+s7VBYfOMeRffRtWpQCB7W3D+sZ2Noiue7K9viVJxtJpvymvDsYPRnQ4JHorfg+z+nsQ==", "success": true, - "projectFilePath": "C:\\Users\\losev\\OneDrive\\Рабочий стол\\spworlds\\spworlds-csharp-library\\spworlds.csproj", - "expectedPackageFiles": [], + "projectFilePath": "C:\\Users\\yawaflua\\Source\\Repos\\spworlds-csharp-library\\spworlds.csproj", + "expectedPackageFiles": [ + "C:\\Users\\yawaflua\\.nuget\\packages\\newtonsoft.json\\13.0.3\\newtonsoft.json.13.0.3.nupkg.sha512" + ], "logs": [] } \ No newline at end of file diff --git a/obj/spworlds.csproj.nuget.dgspec.json b/obj/spworlds.csproj.nuget.dgspec.json index 0b1b577..d766e22 100644 --- a/obj/spworlds.csproj.nuget.dgspec.json +++ b/obj/spworlds.csproj.nuget.dgspec.json @@ -1,23 +1,23 @@ { "format": 1, "restore": { - "C:\\Users\\losev\\OneDrive\\Рабочий стол\\spworlds\\spworlds-csharp-library\\spworlds.csproj": {} + "C:\\Users\\yawaflua\\Source\\Repos\\spworlds-csharp-library\\spworlds.csproj": {} }, "projects": { - "C:\\Users\\losev\\OneDrive\\Рабочий стол\\spworlds\\spworlds-csharp-library\\spworlds.csproj": { + "C:\\Users\\yawaflua\\Source\\Repos\\spworlds-csharp-library\\spworlds.csproj": { "version": "1.0.5", "restore": { - "projectUniqueName": "C:\\Users\\losev\\OneDrive\\Рабочий стол\\spworlds\\spworlds-csharp-library\\spworlds.csproj", + "projectUniqueName": "C:\\Users\\yawaflua\\Source\\Repos\\spworlds-csharp-library\\spworlds.csproj", "projectName": "spworlds", - "projectPath": "C:\\Users\\losev\\OneDrive\\Рабочий стол\\spworlds\\spworlds-csharp-library\\spworlds.csproj", - "packagesPath": "C:\\Users\\losev\\.nuget\\packages\\", - "outputPath": "C:\\Users\\losev\\OneDrive\\Рабочий стол\\spworlds\\spworlds-csharp-library\\obj\\", + "projectPath": "C:\\Users\\yawaflua\\Source\\Repos\\spworlds-csharp-library\\spworlds.csproj", + "packagesPath": "C:\\Users\\yawaflua\\.nuget\\packages\\", + "outputPath": "C:\\Users\\yawaflua\\Source\\Repos\\spworlds-csharp-library\\obj\\", "projectStyle": "PackageReference", "fallbackFolders": [ "C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages" ], "configFilePaths": [ - "C:\\Users\\losev\\AppData\\Roaming\\NuGet\\NuGet.Config", + "C:\\Users\\yawaflua\\AppData\\Roaming\\NuGet\\NuGet.Config", "C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config", "C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config" ], @@ -38,11 +38,22 @@ "warnAsError": [ "NU1605" ] + }, + "restoreAuditProperties": { + "enableAudit": "true", + "auditLevel": "low", + "auditMode": "direct" } }, "frameworks": { "net6.0": { "targetAlias": "net6.0", + "dependencies": { + "Newtonsoft.Json": { + "target": "Package", + "version": "[13.0.3, )" + } + }, "imports": [ "net461", "net462", @@ -59,7 +70,7 @@ "privateAssets": "all" } }, - "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\7.0.401\\RuntimeIdentifierGraph.json" + "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.204\\RuntimeIdentifierGraph.json" } } } diff --git a/obj/spworlds.csproj.nuget.g.props b/obj/spworlds.csproj.nuget.g.props index 631dc36..0432187 100644 --- a/obj/spworlds.csproj.nuget.g.props +++ b/obj/spworlds.csproj.nuget.g.props @@ -5,12 +5,12 @@ NuGet $(MSBuildThisFileDirectory)project.assets.json $(UserProfile)\.nuget\packages\ - C:\Users\losev\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages + C:\Users\yawaflua\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages PackageReference - 6.7.0 + 6.9.2 - + \ No newline at end of file diff --git a/spworlds.cs b/spworlds.cs index d242a76..a825671 100644 --- a/spworlds.cs +++ b/spworlds.cs @@ -6,8 +6,11 @@ using System.Security.Cryptography; using System.Runtime.InteropServices; using spworlds.Types; +using System.Text.Json.Serialization; +using Newtonsoft.Json.Converters; namespace spworlds; + public class SPWorlds { private readonly HttpClient client; @@ -24,49 +27,65 @@ public class SPWorlds client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Base64BearerToken); } - private async Task ValidateWebHook(string webHook, string bodyHash) + /// + /// Validating wenhook from site + /// + /// Body of request + /// X-Body-Hash + /// + public bool ValidateWebhook(string requestBody, string base64Hash) { - byte[] body = Encoding.UTF8.GetBytes(bodyHash); - byte[] webhook = Encoding.UTF8.GetBytes(webHook); - var key = new HMACSHA256(Encoding.UTF8.GetBytes(token)); - string webhook64 = Convert.ToBase64String(key.ComputeHash(webhook)); - return webhook64.Equals(body); - } - - private async Task SendRequest(string endpoint, Boolean getResult = true, Dictionary? body = null) - { - string respond; - string jsonBody; - - if (body == null) + byte[] requestData = Encoding.UTF8.GetBytes(requestBody); + + using (HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(token))) { - return respond = client.GetAsync(endpoint).Result.Content.ReadAsStringAsync().Result; + byte[] hashBytes = hmac.ComputeHash(requestData); + + string computedHash = Convert.ToBase64String(hashBytes); + + return base64Hash.Equals(computedHash); } + } + + private async Task SendRequest(string endpoint, bool getResult = true, HttpMethod method = null, object body = null) + { + method ??= body == null ? HttpMethod.Get : HttpMethod.Post; + HttpResponseMessage message; + + using (var requestMessage = new HttpRequestMessage(method, client.BaseAddress + endpoint)) + { + requestMessage.Content = new StringContent( + JsonSerializer.Serialize(body), + Encoding.UTF8, "application/json" + ); + requestMessage.Headers.Authorization = client.DefaultRequestHeaders.Authorization; + + message = await client.SendAsync(requestMessage); + } + + if (getResult) + return await message.Content.ReadAsStringAsync(); else - { - jsonBody = JsonSerializer.Serialize(body); - var payload = new StringContent(jsonBody, Encoding.UTF8, "application/json"); + return null; - if (getResult) - return respond = client.PostAsync(endpoint, payload).Result.Content.ReadAsStringAsync().Result; - else - await client.PostAsync(endpoint, payload); - } - - return null; } - public async Task GetBalance() - { - string respond = await SendRequest("card"); - var card = JsonObject.Parse(respond); - var balance = card["balance"]; + /// + /// Get card from spworlds + /// + /// + public async Task GetCard() + => Deserialize.DeserializeObject(await SendRequest("card")); - return (int)balance; - } - - public async Task CreateTransaction(string receiver, int amount, string comment) + /// + /// Create transaction + /// + /// receiver card + /// amount of AR + /// comment to transaction + /// balance of card + public async Task CreateTransaction(string receiver, int amount, string comment) { var transitionInfo = new Dictionary { @@ -75,21 +94,41 @@ public class SPWorlds { "comment", comment } }; - await SendRequest(endpoint: "transactions", body: transitionInfo); + var response = JsonObject.Parse(await SendRequest(endpoint: "transactions", body: transitionInfo, getResult: true)); + return (int)response["balance"]; } + /// + /// Get user cards by nickname + /// + /// Username of player + /// Array of cards + public async Task GetUserCardsAsync(string username) + => Deserialize.DeserializeObject(await SendRequest($"accounts/{username}/cards")); + + + + /// + /// Get user info from site + /// + /// Discord id of user + /// public async Task GetUser(string discordId) - { - string userName = (string)JsonObject.Parse(await SendRequest($"users/{discordId}"))["username"]; - User user = await User.CreateUser(userName); - return (User)user; - } - - public async Task InitPayment(int amount, string redirectUrl, string webhookUrl, string data) + => Deserialize.DeserializeObject(await SendRequest($"users/{discordId}")); + + /// + /// Create payment url + /// + /// List of items + /// User will be redirected to this url + /// Webhook will be sended to this url + /// Data, returned with webhook + /// + public async Task InitPayment(Item[] items, string redirectUrl, string webhookUrl, string data) { var paymentInfo = new Dictionary { - { "amount", amount }, + { "items", JsonSerializer.Serialize(items)}, { "redirectUrl", redirectUrl }, { "webhookUrl", webhookUrl }, { "data", data } @@ -101,20 +140,34 @@ public class SPWorlds return (string)url; } + /// + /// Create payment url + /// + /// + /// public async Task InitPayment(PaymentData paymentData) { - var paymentInfo = new Dictionary - { - { "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; + var payment = JsonObject.Parse(await SendRequest(endpoint: $"payment", body: JsonSerializer.Serialize(paymentData))); + return (string)payment["url"]; } + /// + /// Setting up a webhook to card + /// + /// Url of webhook + /// + public async Task SetWebhook(string webhookUrl) + => Deserialize.DeserializeObject( + await SendRequest( + "card/webhook", + true, + HttpMethod.Put, + new Dictionary() + { + { "url", webhookUrl } + } + ) + ); + public async Task GetMeAsync() + => Deserialize.DeserializeObject(await SendRequest("accounts/me")); } diff --git a/spworlds.csproj b/spworlds.csproj index 1fd6258..c99dbab 100644 --- a/spworlds.csproj +++ b/spworlds.csproj @@ -16,6 +16,11 @@ - + + + + + + From 3c72d2b5cb2375b71e27a9a8556b18690eeff6ba Mon Sep 17 00:00:00 2001 From: Dima yawaflua Andreev Date: Thu, 18 Apr 2024 18:43:10 +0300 Subject: [PATCH 3/3] Update UserAccount.cs --- Types/UserAccount.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Types/UserAccount.cs b/Types/UserAccount.cs index 34bd5c5..0cb2d9a 100644 --- a/Types/UserAccount.cs +++ b/Types/UserAccount.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace spworlds.Types { // Root myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse); - public class UserCard + public class MyCard { public string id { get; set; } public string name { get; set; }