From eb595446682e26540c2e40b9472568107d077fe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ariano=20Fonseca=20=C3=82ngelo?= Date: Mon, 22 Apr 2024 16:13:06 +0100 Subject: [PATCH] v1.7.0 * Minor bugfixes * Improve error handling --- .gitignore | 6 +- CryptAPI/CryptAPI.php | 111 ++++++++++++++------------- CryptAPI/Exceptions/ApiException.php | 34 ++++++++ README.md | 5 +- composer.json | 6 +- test.php | 40 ++++++++++ 6 files changed, 142 insertions(+), 60 deletions(-) create mode 100644 CryptAPI/Exceptions/ApiException.php create mode 100644 test.php diff --git a/.gitignore b/.gitignore index f157629..e0b054f 100644 --- a/.gitignore +++ b/.gitignore @@ -65,4 +65,8 @@ venv *.sqlite3 # MacOS -.DS_STORE \ No newline at end of file +.DS_STORE + +composer.lock + +vendor diff --git a/CryptAPI/CryptAPI.php b/CryptAPI/CryptAPI.php index 90901da..f2a6162 100644 --- a/CryptAPI/CryptAPI.php +++ b/CryptAPI/CryptAPI.php @@ -2,6 +2,7 @@ namespace CryptAPI; +use CryptAPI\Exceptions\ApiException; use Exception; class CryptAPI @@ -17,8 +18,23 @@ class CryptAPI private $parameters = []; private $api_key = null; + /** + * @throws Exception + */ public function __construct($coin, $own_address, $callback_url, $parameters = [], $ca_params = [], $api_key = null) { + if (empty($own_address)) { + throw new Exception('Please provide your cryptocurrency wallet address.'); + } + + if (empty($coin)) { + throw new Exception('Please provide a valid coin/ticker.'); + } + + if (empty($callback_url)) { + throw new Exception('Please provide a valid callback url.'); + } + $this->valid_coins = CryptAPI::get_supported_coins(); if (!in_array($coin, $this->valid_coins)) { @@ -34,6 +50,9 @@ public function __construct($coin, $own_address, $callback_url, $parameters = [] $this->api_key = $api_key; } + /** + * @throws ApiException + */ public static function get_supported_coins() { $info = CryptAPI::get_info(null, true); @@ -62,12 +81,11 @@ public static function get_supported_coins() return $coins; } + /** + * @throws ApiException + */ public function get_address() { - if (empty($this->own_address) || empty($this->coin) || empty($this->callback_url)) { - return null; - } - $api_key = $this->api_key; $callback_url = $this->callback_url; @@ -91,20 +109,16 @@ public function get_address() $response = CryptAPI::_request($this->coin, 'create', $ca_params); - if ($response->status == 'success') { - $this->payment_address = $response->address_in; - return $response->address_in; - } + $this->payment_address = $response->address_in; - return null; + return $response->address_in; } + /** + * @throws ApiException + */ public function check_logs() { - if (empty($this->coin) || empty($this->callback_url)) { - return null; - } - $callback_url = $this->callback_url; if (!empty($this->parameters)) { $req_parameters = http_build_query($this->parameters); @@ -115,21 +129,14 @@ public function check_logs() 'callback' => $callback_url, ]; - $response = CryptAPI::_request($this->coin, 'logs', $params); - - if ($response->status == 'success') { - return $response; - } - - return null; + return CryptAPI::_request($this->coin, 'logs', $params); } + /** + * @throws ApiException + */ public function get_qrcode($value = false, $size = false) { - if (empty($this->coin)) { - return null; - } - $address = $this->payment_address; if (empty($address)) { @@ -147,15 +154,12 @@ public function get_qrcode($value = false, $size = false) $params['size'] = $size; } - $response = CryptAPI::_request($this->coin, 'qrcode', $params); - - if ($response->status == 'success') { - return $response; - } - - return null; + return CryptAPI::_request($this->coin, 'qrcode', $params); } + /** + * @throws ApiException + */ public static function get_info($coin = null, $assoc = false) { $params = []; @@ -164,41 +168,29 @@ public static function get_info($coin = null, $assoc = false) $params['prices'] = '0'; } - $response = CryptAPI::_request($coin, 'info', $params, $assoc); - - if (empty($coin) || $response->status == 'success') { - return $response; - } - - return null; + return CryptAPI::_request($coin, 'info', $params, $assoc); } + /** + * @throws ApiException + */ public static function get_estimate($coin, $addresses = 1, $priority = 'default') { - $response = CryptAPI::_request($coin, 'estimate', [ + return CryptAPI::_request($coin, 'estimate', [ 'addresses' => $addresses, 'priority' => $priority ]); - - if ($response->status == 'success') { - return $response; - } - - return null; } + /** + * @throws ApiException + */ public static function get_convert($coin, $value, $from) { - $response = CryptAPI::_request($coin, 'convert', [ + return CryptAPI::_request($coin, 'convert', [ 'value' => $value, 'from' => $from ]); - - if ($response->status == 'success') { - return $response; - } - - return null; } public static function process_callback($_get) @@ -227,6 +219,9 @@ public static function process_callback($_get) return $params; } + /** + * @throws ApiException + */ private static function _request($coin, $endpoint, $params = [], $assoc = false) { $base_url = Cryptapi::$base_url; @@ -256,6 +251,16 @@ private static function _request($coin, $endpoint, $params = [], $assoc = false) $response = curl_exec($ch); curl_close($ch); - return json_decode($response, $assoc); + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $response_object = json_decode($response, $assoc); + + 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')) { + $statusCode = $http_code; + $apiError = $response_object->error ?? null; + + throw ApiException::withStatus($statusCode, $apiError); + } + + return $response_object; } } diff --git a/CryptAPI/Exceptions/ApiException.php b/CryptAPI/Exceptions/ApiException.php new file mode 100644 index 0000000..77ce7c6 --- /dev/null +++ b/CryptAPI/Exceptions/ApiException.php @@ -0,0 +1,34 @@ +statusCode = $statusCode; + } + + public static function withStatus($statusCode, $apiError = null, Exception $previous = null): ApiException + { + $message = $apiError ?? self::getDefaultMessageForStatusCode($statusCode); + + return new self($message, $statusCode, $previous); + } + + // Method to get a default message based on the status code + private static function getDefaultMessageForStatusCode($statusCode): string + { + switch ($statusCode) { + case 400: return "Bad Request"; + case 401: return "Unauthorized"; + case 403: return "Forbidden"; + case 404: return "Not Found"; + case 500: return "Internal Server Error"; + default: return "An unexpected error occurred"; + } + } +} diff --git a/README.md b/README.md index dedcb19..8d4abd8 100644 --- a/README.md +++ b/README.md @@ -248,9 +248,8 @@ array(65) { [1]=>string(3) "bch" [2]=>string(3) "ltc" [3]=>string(4) "doge" - [4]=>string(3) "xmr" # deprecated - [5]=>string(11) "bep20_1inch" - [6]=>string(9) "bep20_ada" + [4]=>string(11) "bep20_1inch" + [5]=>string(9) "bep20_ada" # ...remaining supported cryptocurrencies or tokens } ``` diff --git a/composer.json b/composer.json index c808263..0aa76fe 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "cryptapi/php-cryptapi", "type": "library", "description": "CryptAPI's PHP library", - "keywords": ["cryptapi", "crypto", "bitcoin", "litecoin", "ethereum", "bitcoin cash", "monero", "iota", "payments", "cryptocurrencies", "cryptapi pro"], + "keywords": ["cryptapi", "crypto", "bitcoin", "litecoin", "ethereum", "bitcoin cash", "polygon", "base", "avalanche-c", "payments", "cryptocurrencies"], "homepage": "https://github.com/cryptapi/php-cryptapi", "license": "MIT", "authors": [ @@ -16,7 +16,7 @@ "support": { "email": "info@cryptapi.io", "chat": "https://cryptapi.io", - "docs": "https://cryptapi.io/docs/" + "docs": "https://docs.cryptapi.io/" }, "require": { "php": ">=7.2.0", @@ -29,5 +29,5 @@ "CryptAPI\\": "CryptAPI/" } }, - "version": "1.6.2" + "version": "1.7.0" } diff --git a/test.php b/test.php new file mode 100644 index 0000000..daf4613 --- /dev/null +++ b/test.php @@ -0,0 +1,40 @@ + 12345, +]; + +$cryptapi_params = [ + 'pending' => 1, +]; + +try { + $ca = new CryptAPI\CryptAPI($coin, '0xA6B78B56ee062185E405a1DDDD18cE8fcBC4395d', $callback_url, $parameters, $cryptapi_params); + + # var_dump($ca->get_address()) . PHP_EOL; + + # var_dump($ca->check_logs()) . PHP_EOL; + + # var_dump($ca->get_qrcode()) . PHP_EOL; + + # var_dump($ca->get_qrcode(2, 500)) . PHP_EOL; + + # var_dump(\CryptAPI\CryptAPI::get_info('btc', true)) . PHP_EOL; + + # var_dump(\CryptAPI\CryptAPI::get_info($coin, false)) . PHP_EOL; + + # var_dump(\CryptAPI\CryptAPI::get_supported_coins()) . PHP_EOL; + + # var_dump(\CryptAPI\CryptAPI::get_estimate($coin, 1, '')) . PHP_EOL; + + # var_dump(\CryptAPI\CryptAPI::get_convert($coin, 3, 'usd')) . PHP_EOL; +} catch (Exception $e) { + var_dump($e); +}