-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwcp-crypto-blockchain-apis.php
102 lines (85 loc) · 2.46 KB
/
wcp-crypto-blockchain-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
<?php
// Exit if accessed directly
defined('ABSPATH') || die('Access Restricted!');
// Include everything.
//include( dirname( __FILE__ ) . '/wcp-include-all.php' );
// APIs to retrieve data from the blockchain
// such as balance
// All APIs should extend from BlockchainAPI
// Should implement the following:
// * get_supported_cryptos
// * get_funds_received
// If the API needs an API key, then the
// following should also be overriden:
// * __construct
// * is_active
abstract class BlockchainAPI
{
protected $crypto_in_use;
public function __construct($crypto_in_use)
{
$this->crypto_in_use = strtolower($crypto_in_use);
$this->api_timeout = wcp__get_settings()['blockchain_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;
}
abstract public function get_funds_received($address, $check_confirmation);
}
class BlockchainInfoAPI extends BlockchainAPI
{
protected function get_supported_cryptos()
{
return array('btc');
}
public function get_funds_received($address, $check_confirmation)
{
if ($check_confirmation)
$funds_received = WCP__file_get_contents('https://blockchain.info/q/addressbalance/' . $address['btc_address'] . '?confirmations=' . esc_attr(get_option(WCP_BTC_SETTINGS)['confs_num']), $this->api_timeout);
else
$funds_received = WCP__file_get_contents('https://blockchain.info/q/addressbalance/' . $address['btc_address'], $this->api_timeout);
if (!is_numeric($funds_received)) {
return false;
}
return $funds_received;
}
}
class FairExplorer extends BlockchainAPI
{
protected function get_supported_cryptos()
{
return array('fair');
}
private function extract_funds_received($json_val, $check_confirmation)
{
if (!$json_val) {
return false;
}
if ($check_confirmation)
return $json_val['confirmed'];
else
return $json_val['confirmed'] + $json_val['unconfirmed'];
}
public function get_funds_received($address, $check_confirmation)
{
$funds_received = $this->extract_funds_received(@json_decode(trim(
@WCP__file_get_contents(
'https://faircoin.co/api/balance.php?address=' . $address['fair_address'],
$this->api_timeout
)
), true),$check_confirmation);
if (!is_numeric($funds_received)) {
return false;
}
return $funds_received;
}
}