Skip to content

Commit eb1a192

Browse files
authored
Merge pull request #2 from benphelps/pull-request
dynamic supported, qrcode, estimate, conversion
2 parents da40883 + 276867a commit eb1a192

File tree

3 files changed

+167
-20
lines changed

3 files changed

+167
-20
lines changed
Lines changed: 91 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
class CryptAPI {
77
private static $base_url = "https://api.cryptapi.io";
8-
private $valid_erc20_tokens = ['becaz', 'bnb', 'busd', 'cro', 'link', 'mkr', 'nexo', 'pax', 'tusd', 'usdc', 'usdt', ];
9-
private $valid_trc20_tokens = ['usdt', 'btc', 'eth', ];
10-
private $valid_coins = ['btc', 'bch', 'eth', 'ltc', 'xmr', 'iota', 'trx', ];
8+
private $valid_coins = [];
119
private $own_address = null;
10+
private $payment_address = null;
1211
private $callback_url = null;
1312
private $coin = null;
1413
private $ca_params = [];
@@ -25,14 +24,7 @@ class CryptAPI {
2524
];
2625

2726
public function __construct($coin, $own_address, $callback_url, $parameters=[], $ca_params=[]) {
28-
29-
foreach ($this->valid_erc20_tokens as $token) {
30-
$this->valid_coins[] = 'erc20_' . $token;
31-
}
32-
33-
foreach ($this->valid_trc20_tokens as $token) {
34-
$this->valid_coins[] = 'trc20_' . $token;
35-
}
27+
$this->valid_coins = CryptAPI::get_supported_coins();
3628

3729
if (!in_array($coin, $this->valid_coins)) {
3830
$vc = print_r($this->valid_coins, true);
@@ -44,11 +36,36 @@ public function __construct($coin, $own_address, $callback_url, $parameters=[],
4436
$this->coin = $coin;
4537
$this->ca_params = $ca_params;
4638
$this->parameters = $parameters;
39+
}
40+
41+
public static function get_supported_coins() {
42+
$info = CryptAPI::get_info(null, true);
4743

44+
if (empty($info)) {
45+
return null;
46+
}
47+
48+
unset($info['fee_tiers']);
49+
50+
$coins = [];
51+
52+
foreach ($info as $chain => $data) {
53+
$is_base_coin = in_array('ticker', array_keys($data));
54+
if ($is_base_coin) {
55+
$coins[] = $chain;
56+
continue;
57+
}
58+
59+
$base_ticker = "{$chain}_";
60+
foreach ($data as $token => $subdata) {
61+
$coins[] = $base_ticker . $token;
62+
}
63+
}
64+
65+
return $coins;
4866
}
4967

5068
public function get_address() {
51-
5269
if (empty($this->own_address) || empty($this->coin) || empty($this->callback_url)) return null;
5370

5471
$callback_url = $this->callback_url;
@@ -65,14 +82,14 @@ public function get_address() {
6582
$response = CryptAPI::_request($this->coin, 'create', $ca_params);
6683

6784
if ($response->status == 'success') {
85+
$this->payment_address = $response->address_in;
6886
return $response->address_in;
6987
}
7088

7189
return null;
7290
}
7391

7492
public function check_logs() {
75-
7693
if (empty($this->coin) || empty($this->callback_url)) return null;
7794

7895
$params = [
@@ -88,8 +105,59 @@ public function check_logs() {
88105
return null;
89106
}
90107

91-
public static function get_info($coin) {
92-
$response = CryptAPI::_request($coin, 'info');
108+
public function get_qrcode($value = false, $size = false) {
109+
if (empty($this->coin)) return null;
110+
111+
$params = [
112+
'address' => $this->payment_address,
113+
];
114+
115+
if ($value) $params['value'] = $value;
116+
if ($size) $params['size'] = $size;
117+
118+
$response = CryptAPI::_request($this->coin, 'qrcode', $params);
119+
120+
if ($response->status == 'success') {
121+
return $response;
122+
}
123+
124+
return null;
125+
}
126+
127+
public static function get_info($coin = null, $assoc = false) {
128+
$params = [];
129+
130+
if (empty($coin)) {
131+
$params['prices'] = '0';
132+
}
133+
134+
$response = CryptAPI::_request($coin, 'info', $params, $assoc);
135+
136+
if (empty($coin) || $response->status == 'success') {
137+
return $response;
138+
}
139+
140+
return null;
141+
}
142+
143+
public static function get_estimate($coin, $addresses = 1, $priority = 'default') {
144+
$response = CryptAPI::_request($coin, 'estimate', [
145+
'addresses' => $addresses,
146+
'priority' => $priority
147+
]);
148+
149+
if ($response->status == 'success') {
150+
return $response;
151+
}
152+
153+
return null;
154+
}
155+
156+
public static function get_convert($coin, $value, $from) {
157+
$response = CryptAPI::_request($coin, 'convert', [
158+
'value' => $value,
159+
'from' => $from
160+
]);
93161

94162
if ($response->status == 'success') {
95163
return $response;
@@ -121,14 +189,18 @@ public static function process_callback($_get) {
121189
return $params;
122190
}
123191

124-
private static function _request($coin, $endpoint, $params=[]) {
125-
192+
private static function _request($coin, $endpoint, $params = [], $assoc = false) {
126193
$base_url = Cryptapi::$base_url;
127194
$coin = str_replace('_', '/', $coin);
128195

129196
if (!empty($params)) $data = http_build_query($params);
130197

131-
$url = "{$base_url}/{$coin}/{$endpoint}/";
198+
if (!empty($coin)) {
199+
$coin = str_replace('_', '/', $coin);
200+
$url = "{$base_url}/{$coin}/{$endpoint}/";
201+
} else {
202+
$url = "{$base_url}/{$endpoint}/";
203+
}
132204

133205
if (!empty($data)) $url .= "?{$data}";
134206

@@ -138,6 +210,6 @@ private static function _request($coin, $endpoint, $params=[]) {
138210
$response = curl_exec($ch);
139211
curl_close($ch);
140212

141-
return json_decode($response);
213+
return json_decode($response, $assoc);
142214
}
143215
}

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,81 @@ $data = $ca->check_logs();
106106
Same parameters as before, the `$data` returned can be checked here: https://cryptapi.io/docs/#/Bitcoin/btclogs
107107

108108

109+
### Generating a QR code
110+
111+
```php
112+
<?php
113+
require 'vendor/autoload.php'; // Where your vendor directory is
114+
115+
$ca = new CryptAPI\CryptAPI($coin, $my_address, $callback_url, $parameters, $cryptapi_params);
116+
$payment_address = $ca->get_address();
117+
118+
$qrcode = $ca->get_qrcode($value, $size);
119+
```
120+
121+
For object creation, same parameters as before. You must first call `get_address` as this method requires the payment address to have been created.
122+
123+
For QR code generation:
124+
125+
``$value`` Value to request the user, in the main coin (BTC, ETH, etc). Optional, pass `false` to not add a value to the QR.
126+
127+
``$size`` Size of the QR Code image in pixels. Optional, pass `false` to use the default size of 512.
128+
129+
Response is an object with `qr_code` (base64 encoded image data) and `payment_uri` (the value encoded in the QR), see https://cryptapi.io/docs/#operation/btcqrcode for more information.
130+
131+
132+
### Estimating transaction fees
133+
134+
```php
135+
<?php
136+
require 'vendor/autoload.php'; // Where your vendor directory is
137+
138+
$fees = CryptAPI\CryptAPI::get_estimate($coin, $addresses, $priority);
139+
```
140+
141+
Where:
142+
143+
``$coin`` is the coin you wish to check, from CryptAPI's supported currencies (e.g `'btc', 'eth', 'erc20_usdt', ...`)
144+
145+
``$addresses`` The number of addresses to forward the funds to. Optional, defaults to 1.
146+
147+
``$priority`` Confirmation priority, needs to be one of `['fast', 'default', 'economic']`. Optional, defaults to `default`.
148+
149+
Response is an object with `estimated_cost` and `estimated_cost_usd`, see https://cryptapi.io/docs/#operation/btcestimate for more information.
150+
151+
152+
### Converting between coins and fiat
153+
154+
```php
155+
<?php
156+
require 'vendor/autoload.php'; // Where your vendor directory is
157+
158+
$conversion = CryptAPI\CryptAPI::get_convert($coin, $value, $from);
159+
```
160+
161+
Where:
162+
163+
``$coin`` the target currency to convert to, from CryptAPI's supported currencies (e.g `'btc', 'eth', 'erc20_usdt', ...`)
164+
165+
``$value`` Value to convert in `from`.
166+
167+
``$from`` Currency to convert from, FIAT or crypto.
168+
169+
Response is an object with `value_coin` and `exchange_rate`, see https://cryptapi.io/docs/#operation/btcconvert for more information.
170+
171+
172+
### Getting supported coins
173+
174+
```php
175+
<?php
176+
require 'vendor/autoload.php'; // Where your vendor directory is
177+
178+
$coins = CryptAPI\CryptAPI::get_supported_coins();
179+
```
180+
181+
Response is an array with all support coins.
182+
183+
109184
## Help
110185

111186
Need help?

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"autoload": {
2727
"psr-4": {
28-
"CryptAPI\\": "cryptapi/"
28+
"CryptAPI\\": "CryptAPI/"
2929
}
3030
},
3131
"version": "1.3.0"

0 commit comments

Comments
 (0)