Skip to content

Commit

Permalink
Updated with new parameters and new tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptapi committed Aug 10, 2020
1 parent 328b3fb commit d5f68da
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Official PHP library of CryptAPI
## Requirements:

```
PHP >= 5.5
PHP >= 5.6
ext-curl
```

Expand All @@ -30,21 +30,21 @@ composer require cryptapi/php-cryptapi
<?php
require 'vendor/autoload.php'; // Where your vendor directory is

$ca = new CryptAPI\CryptAPI($coin, $my_address, $callback_url, $parameters, $pending);
$ca = new CryptAPI\CryptAPI($coin, $my_address, $callback_url, $parameters, $cryptapi_params);
$payment_address = $ca->get_address();
```

Where:

``$coin`` is the coin you wish to use, can be one of: ``['btc', 'eth', 'bch', 'ltc', 'iota', 'xmr']``
``$coin`` is the coin you wish to use, from CryptAPI's supported currencies (e.g `'btc', 'eth', 'erc20_usdt', ...`)

``$my_address`` is your own crypto address, where your funds will be sent to

``$callback_url`` is the URL that will be called upon payment

``$parameters`` is any parameter you wish to send to identify the payment, such as ``['order_id' => 1234]``
``$parameters`` is any parameter you wish to send to identify the payment, such as `['order_id' => 1234]`

``$pending`` if you want to be notified of pending transactions.
``$cryptapi_params`` parameters that will be passed to CryptAPI _(check which extra parameters are available here: https://cryptapi.io/docs/#/Bitcoin/btccreate)_

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

Expand All @@ -58,15 +58,9 @@ The URL you provided earlier will be called when a user pays, for easier process

require 'vendor/autoload.php'; // Where your vendor directory is

$payment_data = CryptAPI\CryptAPI::process_callback($_GET, $convert);
$payment_data = CryptAPI\CryptAPI::process_callback($_GET);
```

Where:

`$convert` is a boolean to whether to convert to the main coin denomination.

&nbsp;

The `$payment_data` will be an array with the following keys:

`address_in` - the address generated by our service, where the funds were received
Expand All @@ -81,9 +75,13 @@ The `$payment_data` will be an array with the following keys:

`value` - the value that your customer paid

`value_coin` - the value that your customer paid, in the main coin denomination (e.g `BTC`)

`value_forwarded` - the value we forwarded to you, after our fee

`coin` - the coin the payment was made in, one of: ``['btc', 'eth', 'bch', 'ltc', 'iota', 'xmr']``
`value_forwarded_coin` - the value we forwarded to you, after our fee, in the main coin denomination (e.g `BTC`)

`coin` - the coin the payment was made in (e.g: `'btc', 'eth', 'erc20_usdt', ...`)

`pending` - whether the transaction is pending, if `false` means it's confirmed

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
"authors": [
{
"name": "CryptAPI",
"email": "[email protected]",
"email": "[email protected]",
"homepage": "https://cryptapi.io",
"role": "Developer"
}
],
"support": {
"email": "[email protected]",
"email": "[email protected]",
"chat": "https://cryptapi.io",
"docs": "https://cryptapi.io/docs/"
},
"require": {
"php": ">=5.5.0",
"php": ">=5.6.0",
"ext-curl": "*",
"ext-json": "*"
},
Expand All @@ -28,5 +28,5 @@
"CryptAPI\\": "cryptapi/"
}
},
"version": "1.1.0"
"version": "1.2.0"
}
43 changes: 23 additions & 20 deletions cryptapi/cryptapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@

class CryptAPI {
private static $base_url = "https://cryptapi.io/api";
private $valid_coins = ['btc', 'bch', 'eth', 'ltc', 'xmr', 'iota'];
private $valid_tokens = ['becaz', 'bnb', 'busd', 'cro', 'link', 'mkr', 'nexo', 'pax', 'tusd', 'usdc', 'usdt', ];
private $valid_coins = ['btc', 'bch', 'eth', 'ltc', 'xmr', 'iota', ];
private $own_address = null;
private $callback_url = null;
private $coin = null;
private $pending = false;
private $ca_params = [];
private $parameters = [];

public static $COIN_MULTIPLIERS = [
'btc' => 100000000,
'bch' => 100000000,
'ltc' => 100000000,
'eth' => 1000000000000000000,
'iota' => 1000000,
'xmr' => 1000000000000,
'btc' => 10**8,
'bch' => 10**8,
'ltc' => 10**8,
'eth' => 10**18,
'iota' => 10**6,
'xmr' => 10**12,
];

public function __construct($coin, $own_address, $callback_url, $parameters=[], $pending=false) {
public function __construct($coin, $own_address, $callback_url, $parameters=[], $ca_params=[]) {

foreach ($this->valid_tokens as $token) {
$this->valid_coins[] = 'erc20_' . $token;
}

if (!in_array($coin, $this->valid_coins)) {
$vc = print_r($this->valid_coins, true);
Expand All @@ -31,7 +36,7 @@ public function __construct($coin, $own_address, $callback_url, $parameters=[],
$this->own_address = $own_address;
$this->callback_url = $callback_url;
$this->coin = $coin;
$this->pending = $pending ? 1 : 0;
$this->ca_params = $ca_params;
$this->parameters = $parameters;

}
Expand All @@ -46,11 +51,10 @@ public function get_address() {
$callback_url = "{$this->callback_url}?{$req_parameters}";
}

$ca_params = [
$ca_params = array_merge([
'callback' => $callback_url,
'address' => $this->own_address,
'pending' => $this->pending,
];
], $this->ca_params);

$response = CryptAPI::_request($this->coin, 'create', $ca_params);

Expand Down Expand Up @@ -88,15 +92,17 @@ public static function get_info($coin) {
return null;
}

public static function process_callback($_get, $convert=false) {
public static function process_callback($_get) {
$params = [
'address_in' => $_get['address_in'],
'address_out' => $_get['address_out'],
'txid_in' => $_get['txid_in'],
'txid_out' => isset($_get['txid_out']) ? $_get['txid_out'] : null,
'confirmations' => $_get['confirmations'],
'value' => $convert ? Cryptapi::convert($_get['value'], $_get['coin']) : $_get['value'],
'value_forwarded' => isset($_get['value_forwarded']) ? ($convert ? Cryptapi::convert($_get['value_forwarded'], $_get['coin']) : $_get['value_forwarded']) : null,
'value' => $_get['value'],
'value_coin' => $_get['value_coin'],
'value_forwarded' => isset($_get['value_forwarded']) ? $_get['value_forwarded'] : null,
'value_forwarded_coin' => isset($_get['value_forwarded_coin']) ? $_get['value_forwarded_coin'] : null,
'coin' => $_get['coin'],
'pending' => isset($_get['pending']) ? $_get['pending'] : false,
];
Expand All @@ -109,13 +115,10 @@ public static function process_callback($_get, $convert=false) {
return $params;
}

public static function convert($val, $coin) {
return $val / Cryptapi::$COIN_MULTIPLIERS[$coin];
}

private static function _request($coin, $endpoint, $params=[]) {

$base_url = Cryptapi::$base_url;
$coin = str_replace('_', '/', $coin);

if (!empty($params)) $data = http_build_query($params);

Expand Down

0 comments on commit d5f68da

Please sign in to comment.