diff --git a/CryptAPI/CryptAPI.Test/CryptAPI.Test.csproj b/CryptAPI/CryptAPI.Test/CryptAPI.Test.csproj new file mode 100644 index 0000000..4dddf86 --- /dev/null +++ b/CryptAPI/CryptAPI.Test/CryptAPI.Test.csproj @@ -0,0 +1,18 @@ + + + + Exe + netcoreapp3.1 + + + + + + + + + ..\CryptAPI\bin\Debug\netstandard2.1\CryptAPI.dll + + + + diff --git a/CryptAPI/CryptAPI.Test/Program.cs b/CryptAPI/CryptAPI.Test/Program.cs new file mode 100644 index 0000000..cd73459 --- /dev/null +++ b/CryptAPI/CryptAPI.Test/Program.cs @@ -0,0 +1,47 @@ +using System; +using CryptAPI.Enums; +using CryptAPI.Models; + +namespace CryptAPI.Test +{ + class Program + { + static void Main(string[] args) + { + + try + { + var CryptApi = new CryptAPI(CryptoCurrency.BTC); + + var address_in = CryptApi.CreateCharge("13YWjVEctF8DcdtyW1cEpkXPnGMwbznevW", CallBack: "https://biitez.dev/"); + + var callbacklogs = CryptApi.CallBackLogs("https://biitez.dev/"); + + Console.WriteLine(address_in + Environment.NewLine); + + foreach (var i in callbacklogs.callbacks) + { + Console.WriteLine(i.last_update); + Console.WriteLine(i.txid_in); + Console.WriteLine(i.confirmations); + + foreach (var e in i.logs) + { + Console.WriteLine(e.request_url); + Console.WriteLine(e.timestamp); + Console.WriteLine(e.next_try); + Console.WriteLine(e.request_url); + } + } + + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + //Console.WriteLine(ex.InnerException.Message); + } + + Console.ReadLine(); + } + } +} diff --git a/CryptAPI/CryptAPI.sln b/CryptAPI/CryptAPI.sln index 8bf6c76..152d6e3 100644 --- a/CryptAPI/CryptAPI.sln +++ b/CryptAPI/CryptAPI.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.31129.286 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CryptAPI", "CryptAPI\CryptAPI.csproj", "{69DBF6C4-4331-457E-83FC-975345D12AE3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CryptAPI.Test", "CryptAPI.Test\CryptAPI.Test.csproj", "{3F43692C-EB1C-40A0-8172-A2728B249653}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {69DBF6C4-4331-457E-83FC-975345D12AE3}.Debug|Any CPU.Build.0 = Debug|Any CPU {69DBF6C4-4331-457E-83FC-975345D12AE3}.Release|Any CPU.ActiveCfg = Release|Any CPU {69DBF6C4-4331-457E-83FC-975345D12AE3}.Release|Any CPU.Build.0 = Release|Any CPU + {3F43692C-EB1C-40A0-8172-A2728B249653}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3F43692C-EB1C-40A0-8172-A2728B249653}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3F43692C-EB1C-40A0-8172-A2728B249653}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3F43692C-EB1C-40A0-8172-A2728B249653}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CryptAPI/CryptAPI/CryptAPI.cs b/CryptAPI/CryptAPI/CryptAPI.cs index 9212b2e..bd3f9c5 100644 --- a/CryptAPI/CryptAPI/CryptAPI.cs +++ b/CryptAPI/CryptAPI/CryptAPI.cs @@ -1,6 +1,5 @@ -using CryptAPI.Enums; -using CryptAPI.Extensions; -using CryptAPI.Models; +using CryptAPI.Models; +using CryptAPI.Enums; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; @@ -17,58 +16,78 @@ namespace CryptAPI public class CryptAPI { - internal CryptAPISettings CryptAPISettings { get; set; } - internal HttpClient HttpClient { get; set; } + private HttpClient HttpClient { get; set; } + private CryptoCurrency _CryptoCurrency { get; set; } - public CryptAPI(CryptAPISettings CryptAPISettings, HttpClient HttpClient = null) + + /// + /// Library initialization + /// + /// The cryptocurrency you will use + /// Your own configured HttpClient (optional) + public CryptAPI(CryptoCurrency CryptoCurrency, HttpClient CustomHttpClient = null) { - this.HttpClient = HttpClient ?? new HttpClient(); - this.CryptAPISettings = CryptAPISettings; + _CryptoCurrency = CryptoCurrency; + HttpClient = CustomHttpClient ?? new HttpClient(); } - public string Get_Address() + /// + /// When you call this method, a invoice will be created, this method returns the bitcoin address + /// where your customer needs to pay, after the payment is completed and reaches 1 confirmation, + /// CryptAPI will issue a GET request to your callback, so I recommend that you enter parameters + /// that define the user's invoice, for example, create a random ID and that is integrated into + /// your database and you enter it as a parameter in your callback, then when CryptAPI makes the + /// GET request, your website It delivers the product or service to its user, you can enter the + /// parameters you want. I recommend that everything be under HTTPS. + /// + /// Website where CryptAPI will make a GET request when the user sends the payment and the order reaches 1 confirmation + /// + public string CreateCharge(string Address, string CallBack) { + if (string.IsNullOrEmpty(CallBack) || string.IsNullOrEmpty(Address)) + throw new HttpRequestException($"You must specify your {(string.IsNullOrEmpty(CallBack) ? "CallBack URL" : "Crypto address")}"); - string callback_url = CryptAPISettings.Callback; - - if (CryptAPISettings.Parameters != null && CryptAPISettings.Parameters.Count > 0) - callback_url = $"{CryptAPISettings.Callback}{CryptAPISettings.Parameters.HttpBuildQuery()}"; - - var callback_params = new NameValueCollection() - { - { "callback", callback_url }, - { "address", CryptAPISettings.Address } - }; - - if (CryptAPISettings.callback_params != null && CryptAPISettings.callback_params.Count > 0) - callback_params.Add(CryptAPISettings.callback_params); - - var responseString = Request(callback_params).Result; + var ChargeResponse = RequestAsync($"/{_CryptoCurrency.ToString().ToLower()}/create/?address={Address}&callback={CallBack}").Result; - dynamic dyn = JsonConvert.DeserializeObject(responseString); + dynamic crdyn = JObject.Parse(ChargeResponse); - return dyn.status == "success" ? dyn.address_in : null; + return crdyn.status == "success" ? crdyn.address_in : null; + } + /// + /// Get up-to-date information of the crypto currency + /// + /// + public CryptoCurrencyInfo CurrencyInfo() + { + return JsonConvert.DeserializeObject(RequestAsync($"/{_CryptoCurrency.ToString().ToLower()}/info/").Result); } - private async Task Request(NameValueCollection Params = null) + /// + /// With this method you will get all the information about the request to your callback, + /// the payment confirmation, the next re-attempt to make a next request, etc. + /// + /// The previously entered callback url + /// + public CallbackLogs CallBackLogs(string Callback) { + return JsonConvert.DeserializeObject(RequestAsync($"/{_CryptoCurrency.ToString().ToLower()}/logs/?callback={Callback}").Result); + } - string data = null; + private async Task RequestAsync(string EndPoint) + { + var RequestResponse = await HttpClient.GetAsync(Globals.BaseURL + EndPoint); - if (Params != null) - data = Params.HttpBuildQuery(); + var RequestString = await RequestResponse.Content.ReadAsStringAsync(); - try + if (!RequestResponse.IsSuccessStatusCode) { - var ResponseString = await HttpClient.GetStringAsync($"{Globals.BaseURL}/{CryptAPISettings.Coin}/{CryptAPISettings.EndPoint}/{data ?? string.Empty}"); + dynamic crdyn = JObject.Parse(RequestString); - return ResponseString; - } - catch - { - return null; - } + throw new HttpRequestException($"Error : {crdyn.error}"); + } + + return RequestString; } } } diff --git a/CryptAPI/CryptAPI/Enums/Coins.cs b/CryptAPI/CryptAPI/Enums/Coins.cs deleted file mode 100644 index bc7173d..0000000 --- a/CryptAPI/CryptAPI/Enums/Coins.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace CryptAPI.Enums -{ - public enum Coins - { - btc, - bch, - eth, - ltc, - xmr, - iota - } -} diff --git a/CryptAPI/CryptAPI/Enums/CryptoCurrency.cs b/CryptAPI/CryptAPI/Enums/CryptoCurrency.cs new file mode 100644 index 0000000..eeb9ee2 --- /dev/null +++ b/CryptAPI/CryptAPI/Enums/CryptoCurrency.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CryptAPI.Enums +{ + public enum CryptoCurrency + { + BTC, // Bitcoin + BCH, // Bitcoin Cash + LTC, // Litecoin + ETH, // Ethereum + XMR, // Monero + TRX, // Tron + IOTA // Iota + } +} diff --git a/CryptAPI/CryptAPI/Enums/EndPoint.cs b/CryptAPI/CryptAPI/Enums/EndPoint.cs deleted file mode 100644 index 6b8825d..0000000 --- a/CryptAPI/CryptAPI/Enums/EndPoint.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace CryptAPI.Enums -{ - public enum EndPoint - { - create, - info, - logs - } -} diff --git a/CryptAPI/CryptAPI/Enums/Tokens.cs b/CryptAPI/CryptAPI/Enums/Tokens.cs deleted file mode 100644 index c16c20b..0000000 --- a/CryptAPI/CryptAPI/Enums/Tokens.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace CryptAPI.Enums -{ - public enum Tokens - { - becaz, - bnb, - busd, - cro, - link, - mkr, - nexo, - pax, - tusd, - usdc, - usdt - } -} diff --git a/CryptAPI/CryptAPI/Extensions/StringExtensions.cs b/CryptAPI/CryptAPI/Extensions/StringExtensions.cs deleted file mode 100644 index e4c66e0..0000000 --- a/CryptAPI/CryptAPI/Extensions/StringExtensions.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Specialized; -using System.Globalization; -using System.Linq; -using System.Text; -using System.Web; - -namespace CryptAPI.Extensions -{ - internal static class StringExtensions - { - internal static string HttpBuildQuery(this NameValueCollection nameValueCollection) - { - var StringArray = (from key in nameValueCollection.AllKeys - from value in nameValueCollection.GetValues(key) - select string.Format(CultureInfo.InvariantCulture, "{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value))) - .ToArray(); - - return "?" + string.Join("&", StringArray); - } - } -} \ No newline at end of file diff --git a/CryptAPI/CryptAPI/Models/CallbackLogs.cs b/CryptAPI/CryptAPI/Models/CallbackLogs.cs new file mode 100644 index 0000000..421705f --- /dev/null +++ b/CryptAPI/CryptAPI/Models/CallbackLogs.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CryptAPI.Models +{ + public class Log + { + public string request_url { get; set; } + public string response { get; set; } + public string response_status { get; set; } + public string timestamp { get; set; } + public string next_try { get; set; } + public bool pending { get; set; } + public bool success { get; set; } + } + + public class Callback + { + public string last_update { get; set; } + public string result { get; set; } + public int confirmations { get; set; } + public string fee_percent { get; set; } + public int fee { get; set; } + public int value { get; set; } + public string value_coin { get; set; } + public int value_forwarded { get; set; } + public string value_forwarded_coin { get; set; } + public string txid_in { get; set; } + public string txid_out { get; set; } + public List logs { get; set; } + } + + public class CallbackLogs + { + public string status { get; set; } + public string callback_url { get; set; } + public string address_in { get; set; } + public string address_out { get; set; } + public bool notify_pending { get; set; } + public int notify_confirmations { get; set; } + public string priority { get; set; } + public List callbacks { get; set; } + } + +} diff --git a/CryptAPI/CryptAPI/Models/CryptAPISettings.cs b/CryptAPI/CryptAPI/Models/CryptAPISettings.cs deleted file mode 100644 index 9f8c302..0000000 --- a/CryptAPI/CryptAPI/Models/CryptAPISettings.cs +++ /dev/null @@ -1,79 +0,0 @@ -using CryptAPI.Enums; -using System; -using System.Collections.Generic; -using System.Collections.Specialized; -using System.Text; - -namespace CryptAPI.Models -{ - public class CryptAPISettings - { - private string _Address; - private string _Callback; - private NameValueCollection _Parameters; - private NameValueCollection _callback_params; - - public Coins Coin { get; set; } - public EndPoint EndPoint { get; set; } - - public string Address - { - get - { - return _Address; - } - set - { - if (string.IsNullOrEmpty(value)) - throw new ArgumentNullException(); - - _Address = value; - } - } - - public string Callback - { - get - { - return _Callback; - } - set - { - if (string.IsNullOrEmpty(value)) - throw new ArgumentNullException(); - - _Callback = value; - } - } - - public NameValueCollection Parameters - { - get - { - return _Parameters; - } - set - { - if (value == null || value.Count < 1) - throw new ArgumentException(); - - _Parameters = value; - } - } - - public NameValueCollection callback_params - { - get - { - return _callback_params; - } - set - { - if (value == null || value.Count < 1) - throw new ArgumentException(); - - _callback_params = value; - } - } - } -} diff --git a/CryptAPI/CryptAPI/Models/CryptoCurrencyInfo.cs b/CryptAPI/CryptAPI/Models/CryptoCurrencyInfo.cs new file mode 100644 index 0000000..bd6b21f --- /dev/null +++ b/CryptAPI/CryptAPI/Models/CryptoCurrencyInfo.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CryptAPI.Models +{ + public class Prices + { + public string USD { get; set; } + public string EUR { get; set; } + public string GBP { get; set; } + public string CAD { get; set; } + public string JPY { get; set; } + public string AED { get; set; } + public string DKK { get; set; } + public string BRL { get; set; } + public string CNY { get; set; } + public string HKD { get; set; } + public string INR { get; set; } + public string MXN { get; set; } + public string UGX { get; set; } + public string PLN { get; set; } + public string PHP { get; set; } + } + + public class CryptoCurrencyInfo + { + public string coin { get; set; } + public string logo { get; set; } + public string ticker { get; set; } + public long minimum_transaction { get; set; } + public string minimum_transaction_coin { get; set; } + public long minimum_fee { get; set; } + public string minimum_fee_coin { get; set; } + public string fee_percent { get; set; } + public Prices prices { get; set; } + public DateTime prices_updated { get; set; } + public string status { get; set; } + } +} diff --git a/back/CryptAPI.sln b/back/CryptAPI.sln deleted file mode 100644 index 8bf6c76..0000000 --- a/back/CryptAPI.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.31129.286 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CryptAPI", "CryptAPI\CryptAPI.csproj", "{69DBF6C4-4331-457E-83FC-975345D12AE3}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {69DBF6C4-4331-457E-83FC-975345D12AE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {69DBF6C4-4331-457E-83FC-975345D12AE3}.Debug|Any CPU.Build.0 = Debug|Any CPU - {69DBF6C4-4331-457E-83FC-975345D12AE3}.Release|Any CPU.ActiveCfg = Release|Any CPU - {69DBF6C4-4331-457E-83FC-975345D12AE3}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {708671DE-C3C2-4223-B3F1-69A552161EE4} - EndGlobalSection -EndGlobal diff --git a/back/CryptAPI/CryptAPI.cs b/back/CryptAPI/CryptAPI.cs deleted file mode 100644 index 9212b2e..0000000 --- a/back/CryptAPI/CryptAPI.cs +++ /dev/null @@ -1,74 +0,0 @@ -using CryptAPI.Enums; -using CryptAPI.Extensions; -using CryptAPI.Models; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using System; -using System.Collections.Generic; -using System.Collections.Specialized; -using System.Globalization; -using System.Linq; -using System.Net.Http; -using System.Threading.Tasks; -using System.Web; - -namespace CryptAPI -{ - public class CryptAPI - { - - internal CryptAPISettings CryptAPISettings { get; set; } - internal HttpClient HttpClient { get; set; } - - public CryptAPI(CryptAPISettings CryptAPISettings, HttpClient HttpClient = null) - { - this.HttpClient = HttpClient ?? new HttpClient(); - this.CryptAPISettings = CryptAPISettings; - } - - public string Get_Address() - { - - string callback_url = CryptAPISettings.Callback; - - if (CryptAPISettings.Parameters != null && CryptAPISettings.Parameters.Count > 0) - callback_url = $"{CryptAPISettings.Callback}{CryptAPISettings.Parameters.HttpBuildQuery()}"; - - var callback_params = new NameValueCollection() - { - { "callback", callback_url }, - { "address", CryptAPISettings.Address } - }; - - if (CryptAPISettings.callback_params != null && CryptAPISettings.callback_params.Count > 0) - callback_params.Add(CryptAPISettings.callback_params); - - var responseString = Request(callback_params).Result; - - dynamic dyn = JsonConvert.DeserializeObject(responseString); - - return dyn.status == "success" ? dyn.address_in : null; - - } - - private async Task Request(NameValueCollection Params = null) - { - - string data = null; - - if (Params != null) - data = Params.HttpBuildQuery(); - - try - { - var ResponseString = await HttpClient.GetStringAsync($"{Globals.BaseURL}/{CryptAPISettings.Coin}/{CryptAPISettings.EndPoint}/{data ?? string.Empty}"); - - return ResponseString; - } - catch - { - return null; - } - } - } -} diff --git a/back/CryptAPI/CryptAPI.csproj b/back/CryptAPI/CryptAPI.csproj deleted file mode 100644 index d74e4a7..0000000 --- a/back/CryptAPI/CryptAPI.csproj +++ /dev/null @@ -1,12 +0,0 @@ - - - - netstandard2.1 - - - - - - - - diff --git a/back/CryptAPI/Enums/Coins.cs b/back/CryptAPI/Enums/Coins.cs deleted file mode 100644 index bc7173d..0000000 --- a/back/CryptAPI/Enums/Coins.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace CryptAPI.Enums -{ - public enum Coins - { - btc, - bch, - eth, - ltc, - xmr, - iota - } -} diff --git a/back/CryptAPI/Enums/EndPoint.cs b/back/CryptAPI/Enums/EndPoint.cs deleted file mode 100644 index 6b8825d..0000000 --- a/back/CryptAPI/Enums/EndPoint.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace CryptAPI.Enums -{ - public enum EndPoint - { - create, - info, - logs - } -} diff --git a/back/CryptAPI/Enums/Tokens.cs b/back/CryptAPI/Enums/Tokens.cs deleted file mode 100644 index c16c20b..0000000 --- a/back/CryptAPI/Enums/Tokens.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace CryptAPI.Enums -{ - public enum Tokens - { - becaz, - bnb, - busd, - cro, - link, - mkr, - nexo, - pax, - tusd, - usdc, - usdt - } -} diff --git a/back/CryptAPI/Extensions/StringExtensions.cs b/back/CryptAPI/Extensions/StringExtensions.cs deleted file mode 100644 index e4c66e0..0000000 --- a/back/CryptAPI/Extensions/StringExtensions.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Specialized; -using System.Globalization; -using System.Linq; -using System.Text; -using System.Web; - -namespace CryptAPI.Extensions -{ - internal static class StringExtensions - { - internal static string HttpBuildQuery(this NameValueCollection nameValueCollection) - { - var StringArray = (from key in nameValueCollection.AllKeys - from value in nameValueCollection.GetValues(key) - select string.Format(CultureInfo.InvariantCulture, "{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value))) - .ToArray(); - - return "?" + string.Join("&", StringArray); - } - } -} \ No newline at end of file diff --git a/back/CryptAPI/Globals.cs b/back/CryptAPI/Globals.cs deleted file mode 100644 index e49b67e..0000000 --- a/back/CryptAPI/Globals.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace CryptAPI -{ - public class Globals - { - - public const string BaseURL = "https://api.cryptapi.io"; - - } -} diff --git a/back/CryptAPI/Models/CryptAPISettings.cs b/back/CryptAPI/Models/CryptAPISettings.cs deleted file mode 100644 index 9f8c302..0000000 --- a/back/CryptAPI/Models/CryptAPISettings.cs +++ /dev/null @@ -1,79 +0,0 @@ -using CryptAPI.Enums; -using System; -using System.Collections.Generic; -using System.Collections.Specialized; -using System.Text; - -namespace CryptAPI.Models -{ - public class CryptAPISettings - { - private string _Address; - private string _Callback; - private NameValueCollection _Parameters; - private NameValueCollection _callback_params; - - public Coins Coin { get; set; } - public EndPoint EndPoint { get; set; } - - public string Address - { - get - { - return _Address; - } - set - { - if (string.IsNullOrEmpty(value)) - throw new ArgumentNullException(); - - _Address = value; - } - } - - public string Callback - { - get - { - return _Callback; - } - set - { - if (string.IsNullOrEmpty(value)) - throw new ArgumentNullException(); - - _Callback = value; - } - } - - public NameValueCollection Parameters - { - get - { - return _Parameters; - } - set - { - if (value == null || value.Count < 1) - throw new ArgumentException(); - - _Parameters = value; - } - } - - public NameValueCollection callback_params - { - get - { - return _callback_params; - } - set - { - if (value == null || value.Count < 1) - throw new ArgumentException(); - - _callback_params = value; - } - } - } -}