Skip to content

Commit c3cc303

Browse files
author
CryptAPI
committed
Initial Commit
0 parents  commit c3cc303

File tree

5 files changed

+309
-0
lines changed

5 files changed

+309
-0
lines changed

.gitignore

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
5+
# C extensions
6+
*.so
7+
8+
# Distribution / packaging
9+
.Python
10+
env/
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
lib/
17+
lib64/
18+
parts/
19+
sdist/
20+
var/
21+
*.egg-info/
22+
.installed.cfg
23+
*.egg
24+
25+
# PyInstaller
26+
# Usually these files are written by a python script from a template
27+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
28+
*.manifest
29+
*.spec
30+
31+
# Installer logs
32+
pip-log.txt
33+
pip-delete-this-directory.txt
34+
35+
# Unit test / coverage reports
36+
htmlcov/
37+
.tox/
38+
.coverage
39+
.cache
40+
nosetests.xml
41+
coverage.xml
42+
43+
# Translations
44+
*.mo
45+
*.pot
46+
47+
# Django stuff:
48+
*.log
49+
50+
# Sphinx documentation
51+
docs/_build/
52+
53+
# PyBuilder
54+
target/
55+
README.rst
56+
venv
57+
58+
59+
# IDE stuff
60+
61+
.idea
62+
63+
# Test DB
64+
65+
*.sqlite3

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 CryptAPI
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

MANIFEST.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include README.md
2+
recursive-include cryptapi *.html
3+

README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# CryptAPI's PHP Library
2+
Official PHP library of CryptAPI
3+
4+
## Requirements:
5+
6+
```
7+
PHP >= 5.5
8+
PHP Curl
9+
```
10+
11+
12+
13+
## Install
14+
15+
16+
```
17+
git clone https://github.com/cryptapi/php-cryptapi
18+
```
19+
20+
[on GitHub](https://github.com/cryptapi/php-cryptapi)
21+
22+
## Usage
23+
24+
### Generating a new address
25+
26+
```php
27+
<?php
28+
require ('cryptapi.php');
29+
30+
$ca = new Cryptapi($coin, $my_address, $callback_url, $parameters, $pending);
31+
$payment_address = $ca->get_address();
32+
```
33+
34+
Where:
35+
36+
``$coin`` is the coin you wish to use, can be one of: ``['btc', 'eth', 'bch', 'ltc', 'iota', 'xmr']``
37+
38+
``$my_address`` is your own crypto address, where your funds will be sent to
39+
40+
``$callback_url`` is the URL that will be called upon payment
41+
42+
``$parameters`` is any parameter you wish to send to identify the payment, such as ``['order_id' => 1234]``
43+
44+
``$pending`` if you want to be notified of pending transactions.
45+
46+
``$payment_address`` is the newly generated address, that you will show your users
47+
48+
49+
### Getting notified when the user pays
50+
51+
The URL you provided earlier will be called when a user pays, for easier processing of the request we've added the ``process_callback`` helper
52+
53+
```php
54+
<?php
55+
require ('cryptapi.php');
56+
57+
$payment_data = Cryptapi::process_callback($_GET, $convert);
58+
```
59+
60+
Where:
61+
62+
`$convert` is a boolean to whether to convert to the main coin denomination.
63+
64+
&nbsp;
65+
66+
The `$payment_data` will be an array with the following keys:
67+
68+
`address_in` - the address generated by our service, where the funds were received
69+
70+
`address_out` - your address, where funds were sent
71+
72+
`txid_in` - the received TXID
73+
74+
`txid_out` - the sent TXID or null, in the case of a pending TX
75+
76+
`confirmations` - number of confirmations, or 0 in case of pending TX
77+
78+
`value` - the value that your customer paid
79+
80+
`value_forwarded` - the value we forwarded to you, after our fee
81+
82+
`coin` - the coin the payment was made in, one of: ``['btc', 'eth', 'bch', 'ltc', 'iota', 'xmr']``
83+
84+
`pending` - whether the transaction is pending, if `false` means it's confirmed
85+
86+
plus, any values set on `$params` when requesting the address, like the order ID.
87+
88+
&nbsp;
89+
90+
From here you just need to check if the value matches your order's value.
91+
92+
93+
### Checking the logs of a request
94+
95+
```php
96+
<?php
97+
require ('cryptapi.php');
98+
99+
$ca = new Cryptapi($coin, $my_address, $callback_url, $parameters);
100+
$data = $ca->check_logs();
101+
```
102+
103+
Same parameters as before, the `$data` returned can be checked here: https://cryptapi.io/docs/#/Bitcoin/btclogs
104+
105+
106+
## Help
107+
108+
Need help?
109+
Contact us @ https://cryptapi.io/contact/

cryptapi.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

Comments
 (0)