Skip to content

Commit

Permalink
v2.1.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 b446182 commit 4e5e1f1
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 231 deletions.
221 changes: 69 additions & 152 deletions BlockBee/BlockBee.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace BlockBee;

use BlockBee\Exceptions\ApiException;
use Exception;

class BlockBee
{
private static $base_url = 'https://api.blockbee.io';
private $valid_coins = [];
private $own_address = null;
private $payment_address = null;
Expand All @@ -33,17 +33,9 @@ public function __construct($coin, $own_address, $callback_url, $parameters = []
$this->api_key = $api_key;
}

public static function get_supported_coins($api_key)
public static function get_supported_coins($api_key = '')
{
if (empty($api_key)) {
throw new Exception('API Key is Empty');
}

$info = BlockBee::get_info(null, true, $api_key);

if (empty($info)) {
return null;
}
$info = BlockBee::get_info(null, true);

unset($info['fee_tiers']);

Expand All @@ -65,6 +57,9 @@ public static function get_supported_coins($api_key)
return $coins;
}

/**
* @throws Exception
*/
public function get_address()
{
if (empty($this->coin) || empty($this->callback_url)) {
Expand All @@ -80,21 +75,19 @@ public function get_address()

$bb_params = array_merge([
'callback' => $callback_url,
'address' => $this->own_address
'address' => $this->own_address,
'apikey' => $this->api_key,
], $this->bb_params);

if (empty($this->own_address)) {
unset($bb_params['address']);
}

$response = BlockBee::_request_get($this->coin, 'create', $this->api_key, $bb_params);
$response = Requests::_request_get($this->coin, 'create', $bb_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;
}

public function check_logs()
Expand All @@ -110,10 +103,11 @@ public function check_logs()
}

$params = [
'callback' => $callback_url
'callback' => $callback_url,
'apikey' => $this->api_key,
];

$response = BlockBee::_request_get($this->coin, 'logs', $this->api_key, $params);
$response = Requests::_request_get($this->coin, 'logs', $params);

if ($response->status === 'success') {
return $response;
Expand All @@ -122,6 +116,9 @@ public function check_logs()
return null;
}

/**
* @throws ApiException
*/
public function get_qrcode($value = false, $size = false)
{
if (empty($this->coin)) {
Expand All @@ -141,11 +138,12 @@ public function get_qrcode($value = false, $size = false)
if ($value) {
$params['value'] = $value;
}

if ($size) {
$params['size'] = $size;
}

$response = BlockBee::_request_get($this->coin, 'qrcode', $this->api_key, $params);
$response = Requests::_request_get($this->coin, 'qrcode', $params);

if ($response->status === 'success') {
return $response;
Expand All @@ -154,6 +152,9 @@ public function get_qrcode($value = false, $size = false)
return null;
}

/**
* @throws ApiException
*/
public static function get_info($coin = null, $assoc = false, $api_key = '')
{
$params = [];
Expand All @@ -162,43 +163,37 @@ public static function get_info($coin = null, $assoc = false, $api_key = '')
$params['prices'] = '0';
}

$response = BlockBee::_request_get($coin, 'info', $api_key, $params, $assoc);

if (empty($coin) || $response->status === 'success') {
return $response;
}

return null;
return Requests::_request_get($coin, 'info', $params, $assoc);
}

/**
* @throws ApiException
*/
public static function get_estimate($coin, $addresses = 1, $priority = 'default', $api_key = '')
{
$response = BlockBee::_request_get($coin, 'estimate', $api_key, [
$params = [
'addresses' => $addresses,
'priority' => $priority,
]);

if ($response->status === 'success') {
return $response;
}
'priority' => $priority
];

return null;
return Requests::_request_get($coin, 'estimate', $params);
}

public static function get_convert($coin, $value, $from, $api_key)
/**
* @throws ApiException
*/
public static function get_convert($coin, $value, $from, $api_key = '')
{
$response = BlockBee::_request_get($coin,'convert', $api_key, [
return Requests::_request_get($coin,'convert', [
'value' => $value,
'from' => $from
]);

if ($response->status === 'success') {
return $response;
}

return null;
}

/**
* @throws ApiException
* @throws Exception
*/
public static function create_payout($coin, $requests, $api_key, $process = false) {
if (empty($requests)) {
throw new Exception('No requests provided');
Expand All @@ -212,17 +207,16 @@ public static function create_payout($coin, $requests, $api_key, $process = fals
$endpoint .= '/process';
}

$response = BlockBee::_request_post($coin, $endpoint, $api_key, $body, true);

if ($response->status === 'success') {
return $response;
}

return null;
return Requests::_request_post($coin, $endpoint, $api_key, $body, true);
}

/**
* @throws ApiException
*/
public static function list_payouts ($coin, $status, $page, $api_key, $requests = false) {
$params = [];
$params = [
'apikey' => $api_key,
];

if ($status) {
$params['status'] = $status;
Expand All @@ -238,83 +232,73 @@ public static function list_payouts ($coin, $status, $page, $api_key, $requests
$endpoint = 'payout/request/list';
}

$response = BlockBee::_request_get($coin, $endpoint, $api_key, $params);

if ($response->status === 'success') {
return $response;
}

return null;
return Requests::_request_get($coin, $endpoint, $params);
}

/**
* @throws ApiException
*/
public static function get_payout_wallet($coin, $api_key, $balance = false) {
$wallet = BlockBee::_request_get($coin, 'payout/address', $api_key);
$params = [
'apikey' => $api_key,
];

$wallet = Requests::_request_get($coin, 'payout/address', $params);

$output = [];

if ($wallet->status === 'success') {
$output['address'] = $wallet->address;

if ($balance) {
$wallet = BlockBee::_request_get($coin, 'payout/balance', $api_key);
$wallet = Requests::_request_get($coin, 'payout/balance', $params);

if ($wallet->status === 'success') {
$output['balance'] = $wallet->balance;
}
}

return (object) $output;
}

return null;
return (object) $output;
}

/**
* @throws ApiException
*/
public static function create_payout_by_ids($api_key, $ids = []) {
if (empty($ids)) {
throw new Exception('Please provide the Payout Request(s) ID(s)');
}

$response = BlockBee::_request_post('', 'payout/create', $api_key, [
return Requests::_request_post('', 'payout/create', $api_key, [
'request_ids' => implode(',', $ids)
]);

if ($response->status === 'success') {
return $response;
}

return null;
}

/**
* @throws ApiException
*/
public static function process_payout($api_key, $id = '') {
if (empty($id)) {
throw new Exception('Please provide the Payout ID');
}

$response = BlockBee::_request_post('', 'payout/process', $api_key, [
return Requests::_request_post('', 'payout/process', $api_key, [
'payout_id' => $id
]);

if ($response->status === 'success') {
return $response;
}

return null;
}

/**
* @throws ApiException
*/
public static function check_payout_status($api_key, $id) {
if (empty($id)) {
throw new Exception('Please provide the Payout ID');
}

$response = BlockBee::_request_post('', 'payout/status', $api_key, [
return Requests::_request_post('', 'payout/status', $api_key, [
'payout_id' => $id
]);

if ($response->status === 'success') {
return $response;
}

return null;
}

public static function process_callback($_get)
Expand Down Expand Up @@ -342,71 +326,4 @@ public static function process_callback($_get)

return $params;
}

private static function _request_get($coin, $endpoint, $api_key, $params = [], $assoc = false)
{
$base_url = BlockBee::$base_url;
$coin = str_replace('_', '/', (string) $coin);

if (empty($api_key) && $endpoint !== 'info' && !$coin) {
throw new Exception('API Key is Empty');
}

$params['apikey'] = $api_key;

if (!empty($params)) {
$data = http_build_query($params);
}

$url = !empty($coin) ? "{$base_url}/{$coin}/{$endpoint}/" : "{$base_url}/{$endpoint}/";

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

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

return json_decode($response, $assoc);
}

private static function _request_post($coin, $endpoint, $api_key, $body = [], $isJson = false, $assoc = false )
{
$base_url = BlockBee::$base_url;
$coin = str_replace('_', '/', (string)$coin);
$url = !empty($coin) ? "{$base_url}/{$coin}/{$endpoint}/" : "{$base_url}/{$endpoint}/";

if (empty($api_key)) {
throw new Exception('API Key is Empty');
}

$url .= '?apikey=' . $api_key;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);

if ($isJson) {
$data = json_encode($body);
$headers[] = 'Content-Type: application/json';
} else {
$data = http_build_query($body);
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
}

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}

$response = curl_exec($ch);
curl_close($ch);

return json_decode($response, $assoc);
}
}
Loading

0 comments on commit 4e5e1f1

Please sign in to comment.