22
33namespace BlockBee ;
44
5+ use BlockBee \Exceptions \ApiException ;
56use Exception ;
67
78class BlockBee
89{
9- private static $ base_url = 'https://api.blockbee.io ' ;
1010 private $ valid_coins = [];
1111 private $ own_address = null ;
1212 private $ payment_address = null ;
@@ -33,17 +33,9 @@ public function __construct($coin, $own_address, $callback_url, $parameters = []
3333 $ this ->api_key = $ api_key ;
3434 }
3535
36- public static function get_supported_coins ($ api_key )
36+ public static function get_supported_coins ($ api_key = '' )
3737 {
38- if (empty ($ api_key )) {
39- throw new Exception ('API Key is Empty ' );
40- }
41-
42- $ info = BlockBee::get_info (null , true , $ api_key );
43-
44- if (empty ($ info )) {
45- return null ;
46- }
38+ $ info = BlockBee::get_info (null , true );
4739
4840 unset($ info ['fee_tiers ' ]);
4941
@@ -65,6 +57,9 @@ public static function get_supported_coins($api_key)
6557 return $ coins ;
6658 }
6759
60+ /**
61+ * @throws Exception
62+ */
6863 public function get_address ()
6964 {
7065 if (empty ($ this ->coin ) || empty ($ this ->callback_url )) {
@@ -80,21 +75,19 @@ public function get_address()
8075
8176 $ bb_params = array_merge ([
8277 'callback ' => $ callback_url ,
83- 'address ' => $ this ->own_address
78+ 'address ' => $ this ->own_address ,
79+ 'apikey ' => $ this ->api_key ,
8480 ], $ this ->bb_params );
8581
8682 if (empty ($ this ->own_address )) {
8783 unset($ bb_params ['address ' ]);
8884 }
8985
90- $ response = BlockBee ::_request_get ($ this ->coin , 'create ' , $ this -> api_key , $ bb_params );
86+ $ response = Requests ::_request_get ($ this ->coin , 'create ' , $ bb_params );
9187
92- if ($ response ->status === 'success ' ) {
93- $ this ->payment_address = $ response ->address_in ;
94- return $ response ->address_in ;
95- }
88+ $ this ->payment_address = $ response ->address_in ;
9689
97- return null ;
90+ return $ response -> address_in ;
9891 }
9992
10093 public function check_logs ()
@@ -110,10 +103,11 @@ public function check_logs()
110103 }
111104
112105 $ params = [
113- 'callback ' => $ callback_url
106+ 'callback ' => $ callback_url ,
107+ 'apikey ' => $ this ->api_key ,
114108 ];
115109
116- $ response = BlockBee ::_request_get ($ this ->coin , 'logs ' , $ this -> api_key , $ params );
110+ $ response = Requests ::_request_get ($ this ->coin , 'logs ' , $ params );
117111
118112 if ($ response ->status === 'success ' ) {
119113 return $ response ;
@@ -122,6 +116,9 @@ public function check_logs()
122116 return null ;
123117 }
124118
119+ /**
120+ * @throws ApiException
121+ */
125122 public function get_qrcode ($ value = false , $ size = false )
126123 {
127124 if (empty ($ this ->coin )) {
@@ -141,11 +138,12 @@ public function get_qrcode($value = false, $size = false)
141138 if ($ value ) {
142139 $ params ['value ' ] = $ value ;
143140 }
141+
144142 if ($ size ) {
145143 $ params ['size ' ] = $ size ;
146144 }
147145
148- $ response = BlockBee ::_request_get ($ this ->coin , 'qrcode ' , $ this -> api_key , $ params );
146+ $ response = Requests ::_request_get ($ this ->coin , 'qrcode ' , $ params );
149147
150148 if ($ response ->status === 'success ' ) {
151149 return $ response ;
@@ -154,6 +152,9 @@ public function get_qrcode($value = false, $size = false)
154152 return null ;
155153 }
156154
155+ /**
156+ * @throws ApiException
157+ */
157158 public static function get_info ($ coin = null , $ assoc = false , $ api_key = '' )
158159 {
159160 $ params = [];
@@ -162,43 +163,37 @@ public static function get_info($coin = null, $assoc = false, $api_key = '')
162163 $ params ['prices ' ] = '0 ' ;
163164 }
164165
165- $ response = BlockBee::_request_get ($ coin , 'info ' , $ api_key , $ params , $ assoc );
166-
167- if (empty ($ coin ) || $ response ->status === 'success ' ) {
168- return $ response ;
169- }
170-
171- return null ;
166+ return Requests::_request_get ($ coin , 'info ' , $ params , $ assoc );
172167 }
173168
169+ /**
170+ * @throws ApiException
171+ */
174172 public static function get_estimate ($ coin , $ addresses = 1 , $ priority = 'default ' , $ api_key = '' )
175173 {
176- $ response = BlockBee:: _request_get ( $ coin , ' estimate ' , $ api_key , [
174+ $ params = [
177175 'addresses ' => $ addresses ,
178- 'priority ' => $ priority ,
179- ]);
180-
181- if ($ response ->status === 'success ' ) {
182- return $ response ;
183- }
176+ 'priority ' => $ priority
177+ ];
184178
185- return null ;
179+ return Requests:: _request_get ( $ coin , ' estimate ' , $ params ) ;
186180 }
187181
188- public static function get_convert ($ coin , $ value , $ from , $ api_key )
182+ /**
183+ * @throws ApiException
184+ */
185+ public static function get_convert ($ coin , $ value , $ from , $ api_key = '' )
189186 {
190- $ response = BlockBee ::_request_get ($ coin ,'convert ' , $ api_key , [
187+ return Requests ::_request_get ($ coin ,'convert ' , [
191188 'value ' => $ value ,
192189 'from ' => $ from
193190 ]);
194-
195- if ($ response ->status === 'success ' ) {
196- return $ response ;
197- }
198-
199- return null ;
200191 }
201192
193+ /**
194+ * @throws ApiException
195+ * @throws Exception
196+ */
202197 public static function create_payout ($ coin , $ requests , $ api_key , $ process = false ) {
203198 if (empty ($ requests )) {
204199 throw new Exception ('No requests provided ' );
@@ -212,17 +207,16 @@ public static function create_payout($coin, $requests, $api_key, $process = fals
212207 $ endpoint .= '/process ' ;
213208 }
214209
215- $ response = BlockBee::_request_post ($ coin , $ endpoint , $ api_key , $ body , true );
216-
217- if ($ response ->status === 'success ' ) {
218- return $ response ;
219- }
220-
221- return null ;
210+ return Requests::_request_post ($ coin , $ endpoint , $ api_key , $ body , true );
222211 }
223212
213+ /**
214+ * @throws ApiException
215+ */
224216 public static function list_payouts ($ coin , $ status , $ page , $ api_key , $ requests = false ) {
225- $ params = [];
217+ $ params = [
218+ 'apikey ' => $ api_key ,
219+ ];
226220
227221 if ($ status ) {
228222 $ params ['status ' ] = $ status ;
@@ -238,83 +232,73 @@ public static function list_payouts ($coin, $status, $page, $api_key, $requests
238232 $ endpoint = 'payout/request/list ' ;
239233 }
240234
241- $ response = BlockBee::_request_get ($ coin , $ endpoint , $ api_key , $ params );
242-
243- if ($ response ->status === 'success ' ) {
244- return $ response ;
245- }
246-
247- return null ;
235+ return Requests::_request_get ($ coin , $ endpoint , $ params );
248236 }
249237
238+ /**
239+ * @throws ApiException
240+ */
250241 public static function get_payout_wallet ($ coin , $ api_key , $ balance = false ) {
251- $ wallet = BlockBee::_request_get ($ coin , 'payout/address ' , $ api_key );
242+ $ params = [
243+ 'apikey ' => $ api_key ,
244+ ];
245+
246+ $ wallet = Requests::_request_get ($ coin , 'payout/address ' , $ params );
252247
253248 $ output = [];
254249
255250 if ($ wallet ->status === 'success ' ) {
256251 $ output ['address ' ] = $ wallet ->address ;
257252
258253 if ($ balance ) {
259- $ wallet = BlockBee ::_request_get ($ coin , 'payout/balance ' , $ api_key );
254+ $ wallet = Requests ::_request_get ($ coin , 'payout/balance ' , $ params );
260255
261256 if ($ wallet ->status === 'success ' ) {
262257 $ output ['balance ' ] = $ wallet ->balance ;
263258 }
264259 }
265-
266- return (object ) $ output ;
267260 }
268261
269- return null ;
262+ return ( object ) $ output ;
270263 }
271264
265+ /**
266+ * @throws ApiException
267+ */
272268 public static function create_payout_by_ids ($ api_key , $ ids = []) {
273269 if (empty ($ ids )) {
274270 throw new Exception ('Please provide the Payout Request(s) ID(s) ' );
275271 }
276272
277- $ response = BlockBee ::_request_post ('' , 'payout/create ' , $ api_key , [
273+ return Requests ::_request_post ('' , 'payout/create ' , $ api_key , [
278274 'request_ids ' => implode (', ' , $ ids )
279275 ]);
280-
281- if ($ response ->status === 'success ' ) {
282- return $ response ;
283- }
284-
285- return null ;
286276 }
287277
278+ /**
279+ * @throws ApiException
280+ */
288281 public static function process_payout ($ api_key , $ id = '' ) {
289282 if (empty ($ id )) {
290283 throw new Exception ('Please provide the Payout ID ' );
291284 }
292285
293- $ response = BlockBee ::_request_post ('' , 'payout/process ' , $ api_key , [
286+ return Requests ::_request_post ('' , 'payout/process ' , $ api_key , [
294287 'payout_id ' => $ id
295288 ]);
296-
297- if ($ response ->status === 'success ' ) {
298- return $ response ;
299- }
300-
301- return null ;
302289 }
303290
291+ /**
292+ * @throws ApiException
293+ */
304294 public static function check_payout_status ($ api_key , $ id ) {
305295 if (empty ($ id )) {
306296 throw new Exception ('Please provide the Payout ID ' );
307297 }
308298
309- $ response = BlockBee ::_request_post ('' , 'payout/status ' , $ api_key , [
299+ return Requests ::_request_post ('' , 'payout/status ' , $ api_key , [
310300 'payout_id ' => $ id
311301 ]);
312-
313- if ($ response ->status === 'success ' ) {
314- return $ response ;
315- }
316-
317- return null ;
318302 }
319303
320304 public static function process_callback ($ _get )
@@ -342,71 +326,4 @@ public static function process_callback($_get)
342326
343327 return $ params ;
344328 }
345-
346- private static function _request_get ($ coin , $ endpoint , $ api_key , $ params = [], $ assoc = false )
347- {
348- $ base_url = BlockBee::$ base_url ;
349- $ coin = str_replace ('_ ' , '/ ' , (string ) $ coin );
350-
351- if (empty ($ api_key ) && $ endpoint !== 'info ' && !$ coin ) {
352- throw new Exception ('API Key is Empty ' );
353- }
354-
355- $ params ['apikey ' ] = $ api_key ;
356-
357- if (!empty ($ params )) {
358- $ data = http_build_query ($ params );
359- }
360-
361- $ url = !empty ($ coin ) ? "{$ base_url }/ {$ coin }/ {$ endpoint }/ " : "{$ base_url }/ {$ endpoint }/ " ;
362-
363- if (!empty ($ data )) {
364- $ url .= "? {$ data }" ;
365- }
366-
367- $ ch = curl_init ();
368- curl_setopt ($ ch , CURLOPT_URL , $ url );
369- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
370- $ response = curl_exec ($ ch );
371- curl_close ($ ch );
372-
373- return json_decode ($ response , $ assoc );
374- }
375-
376- private static function _request_post ($ coin , $ endpoint , $ api_key , $ body = [], $ isJson = false , $ assoc = false )
377- {
378- $ base_url = BlockBee::$ base_url ;
379- $ coin = str_replace ('_ ' , '/ ' , (string )$ coin );
380- $ url = !empty ($ coin ) ? "{$ base_url }/ {$ coin }/ {$ endpoint }/ " : "{$ base_url }/ {$ endpoint }/ " ;
381-
382- if (empty ($ api_key )) {
383- throw new Exception ('API Key is Empty ' );
384- }
385-
386- $ url .= '?apikey= ' . $ api_key ;
387-
388- $ ch = curl_init ();
389- curl_setopt ($ ch , CURLOPT_URL , $ url );
390- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
391- curl_setopt ($ ch , CURLOPT_POST , true );
392-
393- if ($ isJson ) {
394- $ data = json_encode ($ body );
395- $ headers [] = 'Content-Type: application/json ' ;
396- } else {
397- $ data = http_build_query ($ body );
398- $ headers [] = 'Content-Type: application/x-www-form-urlencoded ' ;
399- }
400-
401- curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ data );
402-
403- if (!empty ($ headers )) {
404- curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
405- }
406-
407- $ response = curl_exec ($ ch );
408- curl_close ($ ch );
409-
410- return json_decode ($ response , $ assoc );
411- }
412329}
0 commit comments