-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwcp-crypto-exchange-rate-apis.php
192 lines (139 loc) · 4.93 KB
/
wcp-crypto-exchange-rate-apis.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
// Exit if accessed directly
defined( 'ABSPATH' ) || die( 'Access Restricted!' );
// Include everything.
//include( dirname( __FILE__ ) . '/wcp-include-all.php' );
// APIs to retrieve data for exchange rates
// All APIs should extend from ExchangeRateAPI
// Should implement the following:
// * get_supported_cryptos
// * get_supported_exchange_reference_rates
// According to the get_supported_exchange_reference_rates
// the following should be overriden as needed
// * get_exchange_rate
// If the API needs an API key, then the
// following should also be overriden:
// * __construct
// * is_active
// See Class Coinmarketcap for an example
abstract class ExchangeRateAPI {
protected $crypto_in_use;
protected $exchange_rate_api_timeout_secs;
public function __construct( $crypto_in_use ) {
$this->crypto_in_use = strtolower( $crypto_in_use );
$this->exchange_rate_api_timeout_secs = wcp__get_settings()['exchange_rate_api_timeout_secs'];
}
abstract protected function get_supported_cryptos();
protected function is_crypto_supported() {
return in_array( $this->crypto_in_use, $this->get_supported_cryptos() );
}
public function is_active() {
if ( $this->is_crypto_supported() ) {
return true;
}
return false;
}
}
class Bitpay extends ExchangeRateAPI {
private static $supported_cryptos = array( 'btc' );
protected function get_supported_cryptos() {
return self::$supported_cryptos;
}
public function get_exchange_rate() {
$source_url = 'https://bitpay.com/api/rates/BTC/' . get_woocommerce_currency();
$result = @WCP__file_get_contents( $source_url, $this->exchange_rate_api_timeout_secs );
$rate_obj = @json_decode( trim( $result ), true );
if ( @$rate_obj['code'] == get_woocommerce_currency() && $rate_obj['rate'] ) {
return $rate_obj['rate'];
}
return false;
}
}
class Coingecko extends ExchangeRateAPI {
private static $supported_cryptos = array('btc');
protected function get_supported_cryptos() {
return self::$supported_cryptos;
}
private function get_variant_url_part() {
switch ( $this->crypto_in_use ) {
case 'btc':
return 'bitcoin';
break;
case 'fair':
return 'faircoin';
break;
default:
break;
}
}
public function get_exchange_rate() {
$source_url = 'https://api.coingecko.com/api/v3/simple/price?ids=' . $this->get_variant_url_part() . '&vs_currencies=' . get_woocommerce_currency();
$result = @WCP__file_get_contents( $source_url, $this->exchange_rate_api_timeout_secs );
$rate_obj = @json_decode( trim( $result ), true );
$currency_code_tolower = strtolower( get_woocommerce_currency() );
if ( $rate_obj[ $this->get_variant_url_part() ][ $currency_code_tolower ] ) {
return $rate_obj[ $this->get_variant_url_part() ][ $currency_code_tolower ];
}
return false;
}
}
class FreeVision extends ExchangeRateAPI {
private static $supported_cryptos = array( 'fair' );
protected function get_supported_cryptos() {
return self::$supported_cryptos;
}
public function get_exchange_rate() {
$source_url = 'https://faircoin.co/api/freevision.php';
$result = @WCP__file_get_contents( $source_url, $this->exchange_rate_api_timeout_secs );
$rate_obj = @json_decode( trim( $result ), true );
if ( @$rate_obj[get_woocommerce_currency()] ) {
return $rate_obj[get_woocommerce_currency()];
}
return false;
}
}
class FairCoop extends ExchangeRateAPI {
private static $supported_cryptos = array( 'fair' );
protected function get_supported_cryptos() {
return self::$supported_cryptos;
}
public function get_exchange_rate() {
$source_url = 'https://faircoin.co/api/faircoop.php';
$result = @WCP__file_get_contents( $source_url, $this->exchange_rate_api_timeout_secs );
$rate_obj = @json_decode( trim( $result ), true );
if ( @$rate_obj[get_woocommerce_currency()] ) {
return $rate_obj[get_woocommerce_currency()];
}
return false;
}
}
class Fairo extends ExchangeRateAPI {
private static $supported_cryptos = array( 'fair' );
protected function get_supported_cryptos() {
return self::$supported_cryptos;
}
public function get_exchange_rate() {
$source_url = 'https://faircoin.co/api/fairo.php';
$result = @WCP__file_get_contents( $source_url, $this->exchange_rate_api_timeout_secs );
$rate_obj = @json_decode( trim( $result ), true );
if ( @$rate_obj[get_woocommerce_currency()] ) {
return $rate_obj[get_woocommerce_currency()];
}
return false;
}
}
class Bisq extends ExchangeRateAPI {
private static $supported_cryptos = array( 'fair' );
protected function get_supported_cryptos() {
return self::$supported_cryptos;
}
public function get_exchange_rate() {
$source_url = 'https://faircoin.co/api/bisq.php';
$result = @WCP__file_get_contents( $source_url, $this->exchange_rate_api_timeout_secs );
$rate_obj = @json_decode( trim( $result ), true );
if ( @$rate_obj[get_woocommerce_currency()] ) {
return $rate_obj[get_woocommerce_currency()];
}
return false;
}
}