1+ <?php
2+
3+ class Cryptapi {
4+ private $ base_url = "https://cryptapi.io/api " ;
5+ private $ valid_coins = ['btc ' , 'bch ' , 'eth ' , 'ltc ' , 'xmr ' , 'iota ' ];
6+ private $ own_address = null ;
7+ private $ callback_url = null ;
8+ private $ coin = null ;
9+ private $ pending = false ;
10+ private $ parameters = [];
11+
12+ public static $ COIN_MULTIPLIERS = [
13+ 'btc ' => 100000000 ,
14+ 'bch ' => 100000000 ,
15+ 'ltc ' => 100000000 ,
16+ 'eth ' => 1000000000000000000 ,
17+ 'iota ' => 1000000 ,
18+ 'xmr ' => 1000000000000 ,
19+ ];
20+
21+ public function __construct ($ coin , $ own_address , $ callback_url , $ parameters =[], $ pending =false ) {
22+
23+ if (!in_array ($ coin , $ this ->valid_coins )) {
24+ $ vc = print_r ($ this ->valid_coins , true );
25+ throw new Exception ("Unsupported Coin: {$ coin }, Valid options are: {$ vc }" );
26+ }
27+
28+ $ this ->own_address = $ own_address ;
29+ $ this ->callback_url = $ callback_url ;
30+ $ this ->coin = $ coin ;
31+ $ this ->pending = $ pending ? 1 : 0 ;
32+ $ this ->parameters = $ parameters ;
33+
34+ }
35+
36+ public function get_address () {
37+
38+ if (empty ($ this ->own_address ) || empty ($ this ->coin ) || empty ($ this ->callback_url )) return null ;
39+
40+ $ ca_params = [
41+ 'callback ' => $ this ->callback_url ,
42+ 'address ' => $ this ->own_address ,
43+ 'pending ' => $ this ->pending ,
44+ ];
45+
46+ $ response = $ this ->_request ('create ' , array_merge ($ ca_params , $ this ->parameters ));
47+
48+ if ($ response ->status == 'success ' ) {
49+ return $ response ->address_in ;
50+ }
51+
52+ return null ;
53+ }
54+
55+ public function check_logs () {
56+
57+ if (empty ($ this ->coin ) || empty ($ this ->callback_url )) return null ;
58+
59+ $ params = [
60+ 'callback ' => $ this ->callback_url ,
61+ ];
62+
63+ $ response = $ this ->_request ('logs ' , $ params );
64+
65+ if ($ response ->status == 'success ' ) {
66+ return $ response ;
67+ }
68+
69+ return null ;
70+ }
71+
72+ public static function process_callback ($ _get , $ convert =false ) {
73+ $ params = [
74+ 'address_in ' => $ _get ['address_in ' ],
75+ 'address_out ' => $ _get ['address_out ' ],
76+ 'txid_in ' => $ _get ['txid_in ' ],
77+ 'txid_out ' => isset ($ _get ['txid_out ' ]) ? $ _get ['txid_out ' ] : null ,
78+ 'confirmations ' => $ _get ['confirmations ' ],
79+ 'value ' => $ convert ? Cryptapi::convert ($ _get ['value ' ], $ _get ['coin ' ]) : $ _get ['value ' ],
80+ 'value_forwarded ' => isset ($ _get ['value_forwarded ' ]) ? ($ convert ? Cryptapi::convert ($ _get ['value_forwarded ' ], $ _get ['coin ' ]) : $ _get ['value_forwarded ' ]) : null ,
81+ 'coin ' => $ _get ['coin ' ],
82+ 'pending ' => isset ($ _get ['pending ' ]) ? $ _get ['pending ' ] : false ,
83+ ];
84+
85+ foreach ($ _get as $ k => $ v ) {
86+ if (isset ($ params [$ k ])) continue ;
87+ $ params [$ k ] = $ _get [$ k ];
88+ }
89+
90+ return $ params ;
91+ }
92+
93+ public static function convert ($ val , $ coin ) {
94+ return $ val / Cryptapi::$ COIN_MULTIPLIERS [$ coin ];
95+ }
96+
97+ private function _request ($ endpoint , $ params ) {
98+
99+ $ data = http_build_query ($ params );
100+ $ url = "{$ this ->base_url }/ {$ this ->coin }/ {$ endpoint }/? {$ data }" ;
101+
102+ $ ch = curl_init ();
103+ curl_setopt ($ ch , CURLOPT_URL , $ url );
104+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , TRUE );
105+ $ response = curl_exec ($ ch );
106+ curl_close ($ ch );
107+
108+ return json_decode ($ response );
109+ }
110+ }
0 commit comments