okchanger.dev.paysystems.content
PHP
<?php
/**
*
* API-call related functions
*
*/
class OkChangerAPI {
protected $public_api = 'http://www.okchanger.com/API/';
protected $api_id;
protected $api_key;
public $PaymentSystems = array();
public function __construct($api_id, $api_key) {
$this->api_id = $api_id;
$this->api_key = $api_key;
// Init payment systems
$this->RefreshPaymentSystems();
}
/**
* Call the API
*/
public function Query($method = '', $args = array()) {
// Prepare request before send
$this->Prepare($args);
print_r($args);
$url = $this->public_api.$method;
$ch = curl_init();
if(strtolower((substr($url,0,5))=='https')) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POST, 0);
#curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/4.0 (Windows; U; Windows NT 5.0; En; rv:1.8.0.2) Gecko/20070306 Firefox/1.0.0.4");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
curl_close($ch);
print_r($result.PHP_EOL);
return json_decode($result);
}
/**
* Prepare request
*/
private function Prepare(&$Params) {
// Sort array by key (ASC)
if(isset($Params['signature'])) {
unset($Params['signature']);
}
$Params = array_reverse($Params, true);
$Params['APIID'] = $this->api_id;
$Params = array_reverse($Params, true);
$SortedParams = array();
$a1 = array_keys($Params);
foreach($a1 as $key) {
$SortedParams[strtolower($key)] = $Params[$key];
}
ksort($SortedParams);
// Implode values and append API key
$Imploded = implode(":", $SortedParams).":".$this->api_key;
$SortedParams['signature'] = md5($Imploded);
$Params = $SortedParams;
}
/**
* Refresh payment systems collection
*/
public function RefreshPaymentSystems() {
$PaySystemsResult = $this->Query("Rating_PaySystems");
if($PaySystemsResult->IsSuccess) {
$this->PaymentSystems = array();
foreach($PaySystemsResult->Result as $PaySystem) {
$this->PaymentSystems[$PaySystem->ID] = $PaySystem;
}
} else {
throw new OkChangerAPIFailureException('File: '.__FILE__.' Line: '.__LINE__.' API Error: '.$PaySystemsResult->ErrorMessage);
}
}
/**
* Get payment systems list
*/
public function Rating_PaySystems() {
$PaySystemsResult = $this->Query("Rating_PaySystems");
if($PaySystemsResult->IsSuccess) {
return $PaySystemsResult->Result;
} else {
throw new OkChangerAPIFailureException('File: '.__FILE__.' Line: '.__LINE__.' API Error: '.$CurrencyResult->ErrorMessage);
}
}
/**
* Get exchangers list
*/
public function Rating_Exchangers($PaySystemID) {
if(!array_key_exists($PaySystemID, $this->PaymentSystems)) {
throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Payment system '.$PaySystemID.' not found');
}
$ExchangersResult = $this->Query("Rating_Exchangers", array('paySystemID' => $PaySystemID));
if($ExchangersResult->IsSuccess) {
return $ExchangersResult->Result;
} else {
throw new OkChangerAPIFailureException('File: '.__FILE__.' Line: '.__LINE__.' API Error: '.$ExchangersResult->ErrorMessage);
}
}
/**
* Get available directions
*/
public function Rating_Exchangers_Directions($FromPaySystemID, $FromCurrencyName, $ToPaySystemID, $ToCurrencyName) {
if(!array_key_exists($FromPaySystemID, $this->PaymentSystems)) {
throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Payment system '.$PaySystemID.' not found');
}
if(!array_key_exists($ToPaySystemID, $this->PaymentSystems)) {
throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Payment system '.$ToPaySystemID.' not found');
}
if(!in_array($FromCurrencyName, $this->PaymentSystems[$FromPaySystemID]->Currencies)) {
throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Currency '.$FromCurrencyName.' not found in '.$this->PaymentSystems[$FromPaySystemID]->Name.' payment system');
}
if(!in_array($ToCurrencyName, $this->PaymentSystems[$ToPaySystemID]->Currencies)) {
throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Currency '.$ToCurrencyName.' not found in '.$this->PaymentSystems[$FromPaySystemID]->Name.' payment system');
}
$DirectionsResult =
$this->Query(
"Rating_Exchangers_Directions",
array(
'fromPaySystemID' => $FromPaySystemID,
'fromCurrencyName' => $FromCurrencyName,
'toPaySystemID' => $ToPaySystemID,
'toCurrencyName' => $ToCurrencyName
)
);
if($DirectionsResult->IsSuccess) {
return $DirectionsResult->Result;
} else {
throw new OkChangerAPIFailureException('File: '.__FILE__.' Line: '.__LINE__.' API Error: '.$DirectionsResult->ErrorMessage);
}
}
/**
* Get available directions by payment system and currency code (FROM)
*/
public function Rating_Directions_From($FromPaySystemID, $FromCurrencyName) {
if(!array_key_exists($FromPaySystemID, $this->PaymentSystems)) {
throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Payment system '.$FromPaySystemID.' not found');
}
if(!in_array($FromCurrencyName, $this->PaymentSystems[$FromPaySystemID]->Currencies)) {
throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Currency '.$FromCurrencyName.' not found in '.$this->PaymentSystems[$FromPaySystemID]->Name.' payment system');
}
$DirectionsResult =
$this->Query(
"Rating_Directions_From",
array(
'fromPaySystemID' => $FromPaySystemID,
'fromCurrencyName' => $FromCurrencyName
)
);
if($DirectionsResult->IsSuccess) {
return $DirectionsResult->Result;
} else {
throw new OkChangerAPIFailureException('File: '.__FILE__.' Line: '.__LINE__.' API Error: '.$DirectionsResult->ErrorMessage);
}
}
/**
* Get available directions by payment system and currency code (TO)
*/
public function Rating_Directions_To($ToPaySystemID, $ToCurrencyName) {
if(!array_key_exists($ToPaySystemID, $this->PaymentSystems)) {
throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Payment system '.$ToPaySystemID.' not found');
}
if(!in_array($ToCurrencyName, $this->PaymentSystems[$ToPaySystemID]->Currencies)) {
throw new OkChangerAPIInvalidParameterException('File: '.__FILE__.' Line: '.__LINE__.' Error: Currency '.$ToCurrencyName.' not found in '.$this->PaymentSystems[$FromPaySystemID]->Name.' payment system');
}
$DirectionsResult =
$this->Query(
"Rating_Directions_To",
array(
'toPaySystemID' => $ToPaySystemID,
'toCurrencyName' => $ToCurrencyName
)
);
if($DirectionsResult->IsSuccess) {
return $DirectionsResult->Result;
} else {
throw new OkChangerAPIFailureException('File: '.__FILE__.' Line: '.__LINE__.' API Error: '.$DirectionsResult->ErrorMessage);
}
}
}
/**
* Exceptions
*/
class OkChangerAPIException extends Exception {}
class OkChangerAPIFailureException extends OkChangerAPIException {}
class OkChangerAPIInvalidParameterException extends OkChangerAPIException {}
?>
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using Newtonsoft.Json;
using System.Web; //Add reference to System.Web
using Newtonsoft.Json.Linq; //"Manage NuGet packages" -> Search for "newtonsoft json". -> click "install".
namespace OkChanger
{
/*
{
var method = new OkChanger.API.MethodCall(OkChanger.API.API_Methods.Rating_PaySystems, 21241, "API_KEY_HERE");
method.Execute();
//Console.WriteLine(method.Result.ToString());
}
{
var method = new OkChanger.API.MethodCall(OkChanger.API.API_Methods.Rating_Exchangers, 21241, "API_KEY_HERE");
//method.AddParameter("paySystemID", 10);//OKPAY
method.AddParameter("paySystemID", 0);//ALL Exhangers
method.Execute();
//Console.WriteLine(method.Result.ToString());
}
{
var method = new OkChanger.API.MethodCall(OkChanger.API.API_Methods.Rating_Exchangers_Directions, 21241, "API_KEY_HERE");
method.AddParameter("fromPaySystemID", 10);//OKPAY
method.AddParameter("fromCurrencyName", "USD");
method.AddParameter("toPaySystemID", 10);//OKPAY
method.AddParameter("toCurrencyName", "EUR");
method.Execute();
//Console.WriteLine(method.Result.ToString());
}
{
var method = new OkChanger.API.MethodCall(OkChanger.API.API_Methods.Rating_Directions_From, 21241, "API_KEY_HERE");
method.AddParameter("fromPaySystemID", 10);//OKPAY
method.AddParameter("fromCurrencyName", "USD");
method.Execute();
//Console.WriteLine(method.Result.ToString());
}
{
var method = new OkChanger.API.MethodCall(OkChanger.API.API_Methods.Rating_Directions_To, 21241, "API_KEY_HERE");
method.AddParameter("toPaySystemID", 10);//OKPAY
method.AddParameter("toCurrencyName", "USD");
method.Execute();
//Console.WriteLine(method.Result.ToString());
}
*/
public class API
{
public enum API_Methods
{
Rating_PaySystems,
Rating_Exchangers_Directions,
Rating_Exchangers,
Rating_Directions_From,
Rating_Directions_To,
}
public class MethodCall
{
const string APIURL = @"https://www.OkChanger.com/API/";
public long? APIID { get; set; }
public string APIKey { get; set; }
public string MethodName { get; set; }
public JToken Result { get; private set; }
private Dictionary<string, string> Parameters = new Dictionary<string, string>();
public MethodCall(API_Methods method, long? APIID = null, string APIKey = null) : this(method.ToString(), APIID, APIKey) { }
public MethodCall(string methodName, long? APIID = null, string APIKey = null)
{
this.MethodName = methodName;
this.APIID = APIID;
this.APIKey = APIKey;
if (this.APIID.HasValue)
{
this.AddParameter("APIID", APIID);
}
}
public void AddParameter(string name, object value)
{
Parameters.Add(name, value.ToString());
}
public void AddParameter(string name, long value)
{
Parameters.Add(name, value.ToString());
}
public void AddParameter(string name, int value)
{
Parameters.Add(name, value.ToString());
}
public void AddParameter(string name, short value)
{
Parameters.Add(name, value.ToString());
}
public void AddParameter(string name, byte value)
{
Parameters.Add(name, value.ToString());
}
public void AddParameter(string name, float value)
{
Parameters.Add(name, value.ToString("0.00000000", System.Globalization.CultureInfo.InvariantCulture.NumberFormat));
}
public void AddParameter(string name, decimal value)
{
Parameters.Add(name, value.ToString("0.00000000", System.Globalization.CultureInfo.InvariantCulture.NumberFormat));
}
public void Execute()
{
var tmpParameters = this.Parameters.ToDictionary(c => c.Key, c => c.Value);
var values = tmpParameters.OrderBy(c => c.Key).Select(c => c.Value).ToList();
if (!string.IsNullOrEmpty(this.APIKey))
{
values.Add(this.APIKey);
var original = string.Join(":", values);
tmpParameters.Add("signature", API.GetMd5Hash(original));
}
string url = APIURL + MethodName + "?" + string.Join("&", tmpParameters.Select(c => HttpUtility.UrlEncode(c.Key) + "=" + HttpUtility.UrlEncode(c.Value)));
string result = null;
try
{
using (WebClient wc = new WebClient())
{
wc.Encoding = System.Text.UTF8Encoding.UTF8;
#if DEBUG
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
#endif
result = wc.DownloadString(url);
#if DEBUG
sw.Stop();
Console.WriteLine("API Call Method: {0}, {1} ms", MethodName, sw.ElapsedMilliseconds);
#endif
}
}
catch
{
throw new OkChangerAPIConnectionException();
}
JObject jObj;
string errorMessage;
try
{
jObj = JObject.Parse(result);
errorMessage = (string)jObj["ErrorMessage"];
}
catch
{
throw new OkChangerAPIInvalidAnswerException();
}
if (!string.IsNullOrEmpty(errorMessage))
{
throw new OkChangerAPIException(errorMessage);
}
this.Result = jObj["Result"];
}
}
internal static string GetMd5Hash(string input)
{
using (var md5 = System.Security.Cryptography.MD5.Create())
{
var arr = System.Text.Encoding.ASCII.GetBytes(input);
arr = md5.ComputeHash(arr);
return BitConverter.ToString(arr).Replace("-", "");
}
}
}
public class OkChangerAPIConnectionException : Exception
{
}
public class OkChangerAPIInvalidAnswerException : Exception
{
}
public class OkChangerAPIException : Exception
{
public OkChangerAPIException(string message)
: base(message)
{
}
}
}