Skip to content

Commit 978ea17

Browse files
authored
Merge pull request #4 from arianoangelo/master
CryptAPI Pro support
2 parents 0228d76 + 8ad9842 commit 978ea17

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

CryptAPI/CryptAPI.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
class 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)) {

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ composer require cryptapi/php-cryptapi
3030
<?php
3131
require 'vendor/autoload.php'; // Where your vendor directory is
3232

33-
$ca = new CryptAPI\CryptAPI($coin, $my_address, $callback_url, $parameters, $cryptapi_params);
33+
$ca = new CryptAPI\CryptAPI($coin, $my_address, $callback_url, $parameters, $cryptapi_params, $api_key);
3434
$payment_address = $ca->get_address();
3535
```
3636

@@ -48,6 +48,8 @@ Where:
4848

4949
``$payment_address`` is the newly generated address, that you will show your users
5050

51+
``$api_key`` is the API Key provided by [CryptAPI Pro](https://pro.cryptapi.io/). This field can be empty if you don't wish to use it.
52+
5153

5254
### Getting notified when the user pays
5355

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "cryptapi/php-cryptapi",
33
"type": "library",
44
"description": "CryptAPI's PHP library",
5-
"keywords": ["cryptapi", "crypto", "bitcoin", "litecoin", "ethereum", "bitcoin cash", "monero", "iota", "payments", "cryptocurrencies"],
5+
"keywords": ["cryptapi", "crypto", "bitcoin", "litecoin", "ethereum", "bitcoin cash", "monero", "iota", "payments", "cryptocurrencies", "cryptapi pro"],
66
"homepage": "https://github.com/cryptapi/php-cryptapi",
77
"license": "MIT",
88
"authors": [
@@ -28,5 +28,5 @@
2828
"CryptAPI\\": "CryptAPI/"
2929
}
3030
},
31-
"version": "1.5.0"
31+
"version": "1.6.0"
3232
}

0 commit comments

Comments
 (0)