55
66class CryptAPI {
77 private static $ base_url = "https://api.cryptapi.io " ;
8+ private static $ pro_url = "https://pro-api.cryptapi.io " ;
89 private $ valid_coins = [];
910 private $ own_address = null ;
1011 private $ payment_address = null ;
1112 private $ callback_url = null ;
1213 private $ coin = null ;
1314 private $ ca_params = [];
1415 private $ parameters = [];
16+ private $ api_key = null ;
1517
1618 public static $ COIN_MULTIPLIERS = [
1719 'btc ' => 10 **8 ,
@@ -23,7 +25,7 @@ class CryptAPI {
2325 'trx ' => 10 **6 ,
2426 ];
2527
26- public function __construct ($ coin , $ own_address , $ callback_url , $ parameters =[], $ ca_params =[]) {
28+ public function __construct ($ coin , $ own_address , $ callback_url , $ parameters =[], $ ca_params =[], $ api_key = null ) {
2729 $ this ->valid_coins = CryptAPI::get_supported_coins ();
2830
2931 if (!in_array ($ coin , $ this ->valid_coins )) {
@@ -36,6 +38,7 @@ public function __construct($coin, $own_address, $callback_url, $parameters=[],
3638 $ this ->coin = $ coin ;
3739 $ this ->ca_params = $ ca_params ;
3840 $ this ->parameters = $ parameters ;
41+ $ this ->api_key = $ api_key ;
3942 }
4043
4144 public static function get_supported_coins () {
@@ -68,21 +71,31 @@ public static function get_supported_coins() {
6871 public function get_address () {
6972 if (empty ($ this ->own_address ) || empty ($ this ->coin ) || empty ($ this ->callback_url )) return null ;
7073
74+ $ api_key = $ this ->api_key ;
75+
7176 $ callback_url = $ this ->callback_url ;
7277 if (!empty ($ this ->parameters )) {
7378 $ req_parameters = http_build_query ($ this ->parameters );
7479 $ callback_url = "{$ this ->callback_url }? {$ req_parameters }" ;
7580 }
7681
77- $ ca_params = array_merge ([
78- 'callback ' => $ callback_url ,
79- 'address ' => $ this ->own_address ,
80- ], $ this ->ca_params );
82+ if (empty ($ api_key )) {
83+ $ ca_params = array_merge ([
84+ 'callback ' => $ callback_url ,
85+ 'address ' => $ this ->own_address ,
86+ ], $ this ->ca_params );
87+ } else {
88+ $ ca_params = array_merge ([
89+ 'apikey ' => $ api_key ,
90+ 'callback ' => $ callback_url ,
91+ 'address ' => $ this ->own_address ,
92+ ], $ this ->ca_params );
93+ }
8194
8295 $ response = CryptAPI::_request ($ this ->coin , 'create ' , $ ca_params );
8396
8497 if ($ response ->status == 'success ' ) {
85- assert ($ response ->address_out == $ this ->own_address , 'Output address mismatch ' );
98+ // assert($response->address_out == $this->own_address, 'Output address mismatch');
8699 $ this ->payment_address = $ response ->address_in ;
87100 return $ response ->address_in ;
88101 }
@@ -200,6 +213,10 @@ private static function _request($coin, $endpoint, $params = [], $assoc = false)
200213 $ base_url = Cryptapi::$ base_url ;
201214 $ coin = str_replace ('_ ' , '/ ' , $ coin );
202215
216+ if (!empty ($ params ['apikey ' ]) && $ endpoint !== 'info ' ) {
217+ $ base_url = CryptAPI::$ pro_url ;
218+ }
219+
203220 if (!empty ($ params )) $ data = http_build_query ($ params );
204221
205222 if (!empty ($ coin )) {
0 commit comments