Skip to content

Commit eb59544

Browse files
committed
v1.7.0
* Minor bugfixes * Improve error handling
1 parent 8dc02be commit eb59544

File tree

6 files changed

+142
-60
lines changed

6 files changed

+142
-60
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@ venv
6565
*.sqlite3
6666

6767
# MacOS
68-
.DS_STORE
68+
.DS_STORE
69+
70+
composer.lock
71+
72+
vendor

CryptAPI/CryptAPI.php

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CryptAPI;
44

5+
use CryptAPI\Exceptions\ApiException;
56
use Exception;
67

78
class CryptAPI
@@ -17,8 +18,23 @@ class CryptAPI
1718
private $parameters = [];
1819
private $api_key = null;
1920

21+
/**
22+
* @throws Exception
23+
*/
2024
public function __construct($coin, $own_address, $callback_url, $parameters = [], $ca_params = [], $api_key = null)
2125
{
26+
if (empty($own_address)) {
27+
throw new Exception('Please provide your cryptocurrency wallet address.');
28+
}
29+
30+
if (empty($coin)) {
31+
throw new Exception('Please provide a valid coin/ticker.');
32+
}
33+
34+
if (empty($callback_url)) {
35+
throw new Exception('Please provide a valid callback url.');
36+
}
37+
2238
$this->valid_coins = CryptAPI::get_supported_coins();
2339

2440
if (!in_array($coin, $this->valid_coins)) {
@@ -34,6 +50,9 @@ public function __construct($coin, $own_address, $callback_url, $parameters = []
3450
$this->api_key = $api_key;
3551
}
3652

53+
/**
54+
* @throws ApiException
55+
*/
3756
public static function get_supported_coins()
3857
{
3958
$info = CryptAPI::get_info(null, true);
@@ -62,12 +81,11 @@ public static function get_supported_coins()
6281
return $coins;
6382
}
6483

84+
/**
85+
* @throws ApiException
86+
*/
6587
public function get_address()
6688
{
67-
if (empty($this->own_address) || empty($this->coin) || empty($this->callback_url)) {
68-
return null;
69-
}
70-
7189
$api_key = $this->api_key;
7290

7391
$callback_url = $this->callback_url;
@@ -91,20 +109,16 @@ public function get_address()
91109

92110
$response = CryptAPI::_request($this->coin, 'create', $ca_params);
93111

94-
if ($response->status == 'success') {
95-
$this->payment_address = $response->address_in;
96-
return $response->address_in;
97-
}
112+
$this->payment_address = $response->address_in;
98113

99-
return null;
114+
return $response->address_in;
100115
}
101116

117+
/**
118+
* @throws ApiException
119+
*/
102120
public function check_logs()
103121
{
104-
if (empty($this->coin) || empty($this->callback_url)) {
105-
return null;
106-
}
107-
108122
$callback_url = $this->callback_url;
109123
if (!empty($this->parameters)) {
110124
$req_parameters = http_build_query($this->parameters);
@@ -115,21 +129,14 @@ public function check_logs()
115129
'callback' => $callback_url,
116130
];
117131

118-
$response = CryptAPI::_request($this->coin, 'logs', $params);
119-
120-
if ($response->status == 'success') {
121-
return $response;
122-
}
123-
124-
return null;
132+
return CryptAPI::_request($this->coin, 'logs', $params);
125133
}
126134

135+
/**
136+
* @throws ApiException
137+
*/
127138
public function get_qrcode($value = false, $size = false)
128139
{
129-
if (empty($this->coin)) {
130-
return null;
131-
}
132-
133140
$address = $this->payment_address;
134141

135142
if (empty($address)) {
@@ -147,15 +154,12 @@ public function get_qrcode($value = false, $size = false)
147154
$params['size'] = $size;
148155
}
149156

150-
$response = CryptAPI::_request($this->coin, 'qrcode', $params);
151-
152-
if ($response->status == 'success') {
153-
return $response;
154-
}
155-
156-
return null;
157+
return CryptAPI::_request($this->coin, 'qrcode', $params);
157158
}
158159

160+
/**
161+
* @throws ApiException
162+
*/
159163
public static function get_info($coin = null, $assoc = false)
160164
{
161165
$params = [];
@@ -164,41 +168,29 @@ public static function get_info($coin = null, $assoc = false)
164168
$params['prices'] = '0';
165169
}
166170

167-
$response = CryptAPI::_request($coin, 'info', $params, $assoc);
168-
169-
if (empty($coin) || $response->status == 'success') {
170-
return $response;
171-
}
172-
173-
return null;
171+
return CryptAPI::_request($coin, 'info', $params, $assoc);
174172
}
175173

174+
/**
175+
* @throws ApiException
176+
*/
176177
public static function get_estimate($coin, $addresses = 1, $priority = 'default')
177178
{
178-
$response = CryptAPI::_request($coin, 'estimate', [
179+
return CryptAPI::_request($coin, 'estimate', [
179180
'addresses' => $addresses,
180181
'priority' => $priority
181182
]);
182-
183-
if ($response->status == 'success') {
184-
return $response;
185-
}
186-
187-
return null;
188183
}
189184

185+
/**
186+
* @throws ApiException
187+
*/
190188
public static function get_convert($coin, $value, $from)
191189
{
192-
$response = CryptAPI::_request($coin, 'convert', [
190+
return CryptAPI::_request($coin, 'convert', [
193191
'value' => $value,
194192
'from' => $from
195193
]);
196-
197-
if ($response->status == 'success') {
198-
return $response;
199-
}
200-
201-
return null;
202194
}
203195

204196
public static function process_callback($_get)
@@ -227,6 +219,9 @@ public static function process_callback($_get)
227219
return $params;
228220
}
229221

222+
/**
223+
* @throws ApiException
224+
*/
230225
private static function _request($coin, $endpoint, $params = [], $assoc = false)
231226
{
232227
$base_url = Cryptapi::$base_url;
@@ -256,6 +251,16 @@ private static function _request($coin, $endpoint, $params = [], $assoc = false)
256251
$response = curl_exec($ch);
257252
curl_close($ch);
258253

259-
return json_decode($response, $assoc);
254+
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
255+
$response_object = json_decode($response, $assoc);
256+
257+
if ((is_object($response_object) && isset($response_object->status) && $response_object->status === 'error') || (is_array($response_object) && isset($response_object['status']) && $response_object['status'] === 'error')) {
258+
$statusCode = $http_code;
259+
$apiError = $response_object->error ?? null;
260+
261+
throw ApiException::withStatus($statusCode, $apiError);
262+
}
263+
264+
return $response_object;
260265
}
261266
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace CryptAPI\Exceptions;
4+
5+
use Exception;
6+
7+
class ApiException extends Exception {
8+
protected $statusCode;
9+
10+
public function __construct($message = "", $statusCode = 0, ?\Exception $previous = null) {
11+
parent::__construct($message, $statusCode, $previous);
12+
$this->statusCode = $statusCode;
13+
}
14+
15+
public static function withStatus($statusCode, $apiError = null, Exception $previous = null): ApiException
16+
{
17+
$message = $apiError ?? self::getDefaultMessageForStatusCode($statusCode);
18+
19+
return new self($message, $statusCode, $previous);
20+
}
21+
22+
// Method to get a default message based on the status code
23+
private static function getDefaultMessageForStatusCode($statusCode): string
24+
{
25+
switch ($statusCode) {
26+
case 400: return "Bad Request";
27+
case 401: return "Unauthorized";
28+
case 403: return "Forbidden";
29+
case 404: return "Not Found";
30+
case 500: return "Internal Server Error";
31+
default: return "An unexpected error occurred";
32+
}
33+
}
34+
}

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,8 @@ array(65) {
248248
[1]=>string(3) "bch"
249249
[2]=>string(3) "ltc"
250250
[3]=>string(4) "doge"
251-
[4]=>string(3) "xmr" # deprecated
252-
[5]=>string(11) "bep20_1inch"
253-
[6]=>string(9) "bep20_ada"
251+
[4]=>string(11) "bep20_1inch"
252+
[5]=>string(9) "bep20_ada"
254253
# ...remaining supported cryptocurrencies or tokens
255254
}
256255
```

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "cryptapi/php-cryptapi",
33
"type": "library",
44
"description": "CryptAPI's PHP library",
5-
"keywords": ["cryptapi", "crypto", "bitcoin", "litecoin", "ethereum", "bitcoin cash", "monero", "iota", "payments", "cryptocurrencies", "cryptapi pro"],
5+
"keywords": ["cryptapi", "crypto", "bitcoin", "litecoin", "ethereum", "bitcoin cash", "polygon", "base", "avalanche-c", "payments", "cryptocurrencies"],
66
"homepage": "https://github.com/cryptapi/php-cryptapi",
77
"license": "MIT",
88
"authors": [
@@ -16,7 +16,7 @@
1616
"support": {
1717
"email": "info@cryptapi.io",
1818
"chat": "https://cryptapi.io",
19-
"docs": "https://cryptapi.io/docs/"
19+
"docs": "https://docs.cryptapi.io/"
2020
},
2121
"require": {
2222
"php": ">=7.2.0",
@@ -29,5 +29,5 @@
2929
"CryptAPI\\": "CryptAPI/"
3030
}
3131
},
32-
"version": "1.6.2"
32+
"version": "1.7.0"
3333
}

test.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Testing BlockBee Library...
4+
*/
5+
6+
require __DIR__ . '/vendor/autoload.php';
7+
8+
$coin = "bep20_usdt";
9+
$callback_url = "https://example.com";
10+
$parameters = [
11+
'payment_id' => 12345,
12+
];
13+
14+
$cryptapi_params = [
15+
'pending' => 1,
16+
];
17+
18+
try {
19+
$ca = new CryptAPI\CryptAPI($coin, '0xA6B78B56ee062185E405a1DDDD18cE8fcBC4395d', $callback_url, $parameters, $cryptapi_params);
20+
21+
# var_dump($ca->get_address()) . PHP_EOL;
22+
23+
# var_dump($ca->check_logs()) . PHP_EOL;
24+
25+
# var_dump($ca->get_qrcode()) . PHP_EOL;
26+
27+
# var_dump($ca->get_qrcode(2, 500)) . PHP_EOL;
28+
29+
# var_dump(\CryptAPI\CryptAPI::get_info('btc', true)) . PHP_EOL;
30+
31+
# var_dump(\CryptAPI\CryptAPI::get_info($coin, false)) . PHP_EOL;
32+
33+
# var_dump(\CryptAPI\CryptAPI::get_supported_coins()) . PHP_EOL;
34+
35+
# var_dump(\CryptAPI\CryptAPI::get_estimate($coin, 1, '')) . PHP_EOL;
36+
37+
# var_dump(\CryptAPI\CryptAPI::get_convert($coin, 3, 'usd')) . PHP_EOL;
38+
} catch (Exception $e) {
39+
var_dump($e);
40+
}

0 commit comments

Comments
 (0)