Skip to content

Commit

Permalink
Simple API wrapper in .NET for CryptAPI (unofficial)
Browse files Browse the repository at this point in the history
  • Loading branch information
Biitez committed Apr 30, 2021
1 parent 5d1cbfd commit a39f754
Show file tree
Hide file tree
Showing 21 changed files with 231 additions and 466 deletions.
18 changes: 18 additions & 0 deletions CryptAPI/CryptAPI.Test/CryptAPI.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<ItemGroup>
<Reference Include="CryptAPI">
<HintPath>..\CryptAPI\bin\Debug\netstandard2.1\CryptAPI.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
47 changes: 47 additions & 0 deletions CryptAPI/CryptAPI.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
6 changes: 6 additions & 0 deletions CryptAPI/CryptAPI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
95 changes: 57 additions & 38 deletions CryptAPI/CryptAPI/CryptAPI.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)

/// <summary>
/// Library initialization
/// </summary>
/// <param name="CryptoCurrency">The cryptocurrency you will use</param>
/// <param name="CustomHttpClient">Your own configured HttpClient (optional)</param>
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()
/// <summary>
/// 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.
/// </summary>
/// <param name="CallBack">Website where CryptAPI will make a GET request when the user sends the payment and the order reaches 1 confirmation</param>
/// <returns></returns>
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;
}

/// <summary>
/// Get up-to-date information of the crypto currency
/// </summary>
/// <returns></returns>
public CryptoCurrencyInfo CurrencyInfo()
{
return JsonConvert.DeserializeObject<CryptoCurrencyInfo>(RequestAsync($"/{_CryptoCurrency.ToString().ToLower()}/info/").Result);
}

private async Task<string> Request(NameValueCollection Params = null)
/// <summary>
/// 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.
/// </summary>
/// <param name="Callback">The previously entered callback url</param>
/// <returns></returns>
public CallbackLogs CallBackLogs(string Callback)
{
return JsonConvert.DeserializeObject<CallbackLogs>(RequestAsync($"/{_CryptoCurrency.ToString().ToLower()}/logs/?callback={Callback}").Result);
}

string data = null;
private async Task<string> 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;
}
}
}
16 changes: 0 additions & 16 deletions CryptAPI/CryptAPI/Enums/Coins.cs

This file was deleted.

17 changes: 17 additions & 0 deletions CryptAPI/CryptAPI/Enums/CryptoCurrency.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
13 changes: 0 additions & 13 deletions CryptAPI/CryptAPI/Enums/EndPoint.cs

This file was deleted.

21 changes: 0 additions & 21 deletions CryptAPI/CryptAPI/Enums/Tokens.cs

This file was deleted.

23 changes: 0 additions & 23 deletions CryptAPI/CryptAPI/Extensions/StringExtensions.cs

This file was deleted.

46 changes: 46 additions & 0 deletions CryptAPI/CryptAPI/Models/CallbackLogs.cs
Original file line number Diff line number Diff line change
@@ -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<Log> 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<Callback> callbacks { get; set; }
}

}
Loading

0 comments on commit a39f754

Please sign in to comment.