Skip to content

Commit

Permalink
v1.7.0
Browse files Browse the repository at this point in the history
* Minor bugfixes
* Improve error handling
  • Loading branch information
arianoangelo committed Apr 22, 2024
1 parent 8dc02be commit eb59544
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 60 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ venv
*.sqlite3

# MacOS
.DS_STORE
.DS_STORE

composer.lock

vendor
111 changes: 58 additions & 53 deletions CryptAPI/CryptAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CryptAPI;

use CryptAPI\Exceptions\ApiException;
use Exception;

class CryptAPI
Expand All @@ -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)) {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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)) {
Expand All @@ -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 = [];
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
34 changes: 34 additions & 0 deletions CryptAPI/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace CryptAPI\Exceptions;

use Exception;

class ApiException extends Exception {
protected $statusCode;

public function __construct($message = "", $statusCode = 0, ?\Exception $previous = null) {
parent::__construct($message, $statusCode, $previous);
$this->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";
}
}
}
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -16,7 +16,7 @@
"support": {
"email": "[email protected]",
"chat": "https://cryptapi.io",
"docs": "https://cryptapi.io/docs/"
"docs": "https://docs.cryptapi.io/"
},
"require": {
"php": ">=7.2.0",
Expand All @@ -29,5 +29,5 @@
"CryptAPI\\": "CryptAPI/"
}
},
"version": "1.6.2"
"version": "1.7.0"
}
40 changes: 40 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Testing BlockBee Library...
*/

require __DIR__ . '/vendor/autoload.php';

$coin = "bep20_usdt";
$callback_url = "https://example.com";
$parameters = [
'payment_id' => 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);
}

0 comments on commit eb59544

Please sign in to comment.