Skip to content

Commit 1ded79b

Browse files
author
CryptAPI
committed
Composer spec and PSR-4 autoload style
1 parent c3cc303 commit 1ded79b

File tree

3 files changed

+145
-112
lines changed

3 files changed

+145
-112
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ git clone https://github.com/cryptapi/php-cryptapi
2525

2626
```php
2727
<?php
28-
require ('cryptapi.php');
28+
use CryptAPI\CryptAPI;
2929

3030
$ca = new Cryptapi($coin, $my_address, $callback_url, $parameters, $pending);
3131
$payment_address = $ca->get_address();
@@ -52,7 +52,7 @@ The URL you provided earlier will be called when a user pays, for easier process
5252

5353
```php
5454
<?php
55-
require ('cryptapi.php');
55+
use CryptAPI\CryptAPI;
5656

5757
$payment_data = Cryptapi::process_callback($_GET, $convert);
5858
```
@@ -94,7 +94,7 @@ From here you just need to check if the value matches your order's value.
9494

9595
```php
9696
<?php
97-
require ('cryptapi.php');
97+
use CryptAPI\CryptAPI;
9898

9999
$ca = new Cryptapi($coin, $my_address, $callback_url, $parameters);
100100
$data = $ca->check_logs();

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "cryptapi/php-cryptapi",
3+
"type": "library",
4+
"description": "CryptAPI's PHP library",
5+
"keywords": ["cryptapi", "crypto", "bitcoin", "litecoin", "ethereum", "bitcoin cash", "monero", "iota", "payments", "cryptocurrencies"],
6+
"homepage": "https://github.com/cryptapi/php-cryptapi",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "CryptAPI",
11+
"email": "cryptapi@protonmail.com",
12+
"homepage": "https://cryptapi.io",
13+
"role": "Developer"
14+
}
15+
],
16+
"support": {
17+
"email": "cryptapi@protonmail.com",
18+
"chat": "https://cryptapi.io",
19+
"docs": "https://cryptapi.io/docs/"
20+
},
21+
"require": {
22+
"php": ">=5.5.0",
23+
"ext-curl": "*"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"CryptAPI\\": "cryptapi/"
28+
}
29+
}
30+
}
Lines changed: 112 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,113 @@
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-
}
1+
<?php
2+
3+
namespace CryptAPI;
4+
use Exception;
5+
6+
class CryptAPI {
7+
private $base_url = "https://cryptapi.io/api";
8+
private $valid_coins = ['btc', 'bch', 'eth', 'ltc', 'xmr', 'iota'];
9+
private $own_address = null;
10+
private $callback_url = null;
11+
private $coin = null;
12+
private $pending = false;
13+
private $parameters = [];
14+
15+
public static $COIN_MULTIPLIERS = [
16+
'btc' => 100000000,
17+
'bch' => 100000000,
18+
'ltc' => 100000000,
19+
'eth' => 1000000000000000000,
20+
'iota' => 1000000,
21+
'xmr' => 1000000000000,
22+
];
23+
24+
public function __construct($coin, $own_address, $callback_url, $parameters=[], $pending=false) {
25+
26+
if (!in_array($coin, $this->valid_coins)) {
27+
$vc = print_r($this->valid_coins, true);
28+
throw new Exception("Unsupported Coin: {$coin}, Valid options are: {$vc}");
29+
}
30+
31+
$this->own_address = $own_address;
32+
$this->callback_url = $callback_url;
33+
$this->coin = $coin;
34+
$this->pending = $pending ? 1 : 0;
35+
$this->parameters = $parameters;
36+
37+
}
38+
39+
public function get_address() {
40+
41+
if (empty($this->own_address) || empty($this->coin) || empty($this->callback_url)) return null;
42+
43+
$ca_params = [
44+
'callback' => $this->callback_url,
45+
'address' => $this->own_address,
46+
'pending' => $this->pending,
47+
];
48+
49+
$response = $this->_request('create', array_merge($ca_params, $this->parameters));
50+
51+
if ($response->status == 'success') {
52+
return $response->address_in;
53+
}
54+
55+
return null;
56+
}
57+
58+
public function check_logs() {
59+
60+
if (empty($this->coin) || empty($this->callback_url)) return null;
61+
62+
$params = [
63+
'callback' => $this->callback_url,
64+
];
65+
66+
$response = $this->_request('logs', $params);
67+
68+
if ($response->status == 'success') {
69+
return $response;
70+
}
71+
72+
return null;
73+
}
74+
75+
public static function process_callback($_get, $convert=false) {
76+
$params = [
77+
'address_in' => $_get['address_in'],
78+
'address_out' => $_get['address_out'],
79+
'txid_in' => $_get['txid_in'],
80+
'txid_out' => isset($_get['txid_out']) ? $_get['txid_out'] : null,
81+
'confirmations' => $_get['confirmations'],
82+
'value' => $convert ? Cryptapi::convert($_get['value'], $_get['coin']) : $_get['value'],
83+
'value_forwarded' => isset($_get['value_forwarded']) ? ($convert ? Cryptapi::convert($_get['value_forwarded'], $_get['coin']) : $_get['value_forwarded']) : null,
84+
'coin' => $_get['coin'],
85+
'pending' => isset($_get['pending']) ? $_get['pending'] : false,
86+
];
87+
88+
foreach ($_get as $k => $v) {
89+
if (isset($params[$k])) continue;
90+
$params[$k] = $_get[$k];
91+
}
92+
93+
return $params;
94+
}
95+
96+
public static function convert($val, $coin) {
97+
return $val / Cryptapi::$COIN_MULTIPLIERS[$coin];
98+
}
99+
100+
private function _request($endpoint, $params) {
101+
102+
$data = http_build_query($params);
103+
$url = "{$this->base_url}/{$this->coin}/{$endpoint}/?{$data}";
104+
105+
$ch = curl_init();
106+
curl_setopt($ch, CURLOPT_URL, $url);
107+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
108+
$response = curl_exec($ch);
109+
curl_close($ch);
110+
111+
return json_decode($response);
112+
}
110113
}

0 commit comments

Comments
 (0)