22
33namespace CryptAPI ;
44
5+ use CryptAPI \Exceptions \ApiException ;
56use Exception ;
67
78class 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}
0 commit comments