Skip to content

Commit e2b416e

Browse files
author
Ariano Fonseca Ângelo
committed
v3.1
- Support CryptAPI Pro - Minor fixes
1 parent ddfcc60 commit e2b416e

File tree

8 files changed

+92
-64
lines changed

8 files changed

+92
-64
lines changed

Block/Payment.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ public function getTemplateValues()
8585

8686
$callbackUrl = $this->payment->getCallbackUrl();
8787

88-
$api = new CryptAPIHelper($selected, $address, $callbackUrl, $params, true);
88+
$apiKey = $this->scopeConfig->getValue('payment/cryptapi/api_key', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
89+
90+
$api = new CryptAPIHelper($selected, $address, $apiKey, $callbackUrl, $params, true);
8991
$addressIn = $api->get_address();
9092

9193
$qrCode = $api->get_qrcode('', $qrCodeSize);

Controller/Index/Callback.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function execute()
6464

6565
$history[$data['uuid']] = [
6666
'timestamp' => time(),
67-
'value_paid' => $paid,
67+
'value_paid' => CryptAPIHelper::sig_fig($paid, 6),
6868
'value_paid_fiat' => $fiat_conversion,
6969
'pending' => $data['pending']
7070
];

Cron/CryptapiCronjob.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function execute()
5858

5959
$remaining = $calc['remaining'];
6060
$remaining_pending = $calc['remaining_pending'];
61-
$remaining_fiat = $calc['remaining_fiat'];
61+
$already_paid = $calc['already_paid'];
6262

6363
if (!empty($metaData['cryptapi_address']) && $value_refresh !== 0 && $metaData['cryptapi_cancelled'] !== '1' && (int)$metaData['cryptapi_last_price_update'] + $value_refresh <= time() && $remaining_pending > 0) {
6464

@@ -84,7 +84,7 @@ public function execute()
8484
$this->helper->updatePaymentData($orderQuoteId, 'cryptapi_last_price_update', time());
8585
}
8686

87-
if ($order_timeout !== 0 && ((int)strtotime($order->getCreatedAt()) + $order_timeout) <= time() && empty($metaData['cryptapi_pending']) && $remaining_fiat <= $order->getGrandTotal() && (string)$metaData['cryptapi_cancelled'] === '0') {
87+
if ($order_timeout !== 0 && ((int)strtotime($order->getCreatedAt()) + $order_timeout) <= time() && empty($metaData['cryptapi_pending']) && $already_paid <= 0 && (string)$metaData['cryptapi_cancelled'] === '0') {
8888
$state = \Magento\Sales\Model\Order::STATE_CANCELED;
8989
$status = \Magento\Sales\Model\Order::STATE_CANCELED;
9090
$order->setState($state);

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,9 @@ The easiest and fastest way is via our live chat on our [website](https://crypta
144144
#### 3.0.3
145145
* Minor fixes
146146

147+
#### 3.1
148+
* Support CryptAPI Pro
149+
* Minor fixes
150+
147151
### Upgrade Notice
148152
* No breaking changes.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "cryptapi/cryptapi",
33
"description": "CryptAPI's Magento extension",
44
"type": "magento2-module",
5-
"version": "3.0.3",
5+
"version": "3.1",
66
"require": {
77
"magento/module-payment": "100.1.*",
88
"magento/module-checkout": "100.1.*",

etc/adminhtml/system.xml

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,72 +12,75 @@
1212
<field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
1313
<label>Title</label>
1414
</field>
15-
<field id="show_branding" translate="label" type="select" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
15+
<field id="api_key" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
16+
<label>API Key</label>
17+
</field>
18+
<field id="show_branding" translate="label" type="select" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
1619
<label>Show CryptAPI branding</label>
1720
<comment>Show CryptAPI Logo</comment>
1821
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
1922
</field>
20-
<field id="add_blockchain_fee" translate="label" type="select" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
23+
<field id="add_blockchain_fee" translate="label" type="select" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
2124
<label>Add the blockchain fee to the order</label>
2225
<comment>This will add an estimation of the blockchain fee to the order value</comment>
2326
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
2427
</field>
25-
<field id="fee_order_percentage" translate="label" type="select" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
28+
<field id="fee_order_percentage" translate="label" type="select" sortOrder="6" showInDefault="1" showInWebsite="1" showInStore="1">
2629
<label>Service fee manager</label>
2730
<comment>Set the CryptAPI service fee you want to charge the costumer. Note: Fee you want to charge your costumers (to cover CryptAPI\'s fees fully or partially)</comment>
2831
<source_model>Cryptapi\Cryptapi\Model\Config\Source\FeesList</source_model>
2932
</field>
30-
<field id="qrcode_default" translate="label" type="select" sortOrder="6" showInDefault="1" showInWebsite="1" showInStore="1">
33+
<field id="qrcode_default" translate="label" type="select" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="1">
3134
<label>QR Code by default</label>
3235
<comment>Show the QR Code by default</comment>
3336
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
3437
</field>
35-
<field id="qrcode_size" translate="label" type="text" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="1">
38+
<field id="qrcode_size" translate="label" type="text" sortOrder="8" showInDefault="1" showInWebsite="1" showInStore="1">
3639
<label>QR Code size</label>
3740
</field>
38-
<field id="qrcode_setting" translate="label" type="select" sortOrder="8" showInDefault="1" showInWebsite="1" showInStore="1">
41+
<field id="qrcode_setting" translate="label" type="select" sortOrder="9" showInDefault="1" showInWebsite="1" showInStore="1">
3942
<label>Color Scheme</label>
4043
<comment>Selects the color scheme of the plugin to match your website (Light, Dark and Auto to automatically detect it).</comment>
4144
<source_model>Cryptapi\Cryptapi\Model\Config\Source\QrcodeOptions</source_model>
4245
</field>
43-
<field id="color_scheme" translate="label" type="select" sortOrder="9" showInDefault="1" showInWebsite="1" showInStore="1">
46+
<field id="color_scheme" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
4447
<label>QR Code to show</label>
4548
<comment>Select how you want to show the QR Code to the user. Either select a default to show first, or hide one of them.</comment>
4649
<source_model>Cryptapi\Cryptapi\Model\Config\Source\SchemeList</source_model>
4750
</field>
48-
<field id="refresh_value_interval" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
51+
<field id="refresh_value_interval" translate="label" type="select" sortOrder="11" showInDefault="1" showInWebsite="1" showInStore="1">
4952
<label>Refresh converted value</label>
5053
<comment>The system will automatically update the conversion value of the invoices (with real-time data), every X minutes. This feature is helpful whenever a customer takes long time to pay a generated invoice and the selected crypto a volatile coin/token (not stable coin). Warning: Setting this setting to none might create conversion issues, as we advise you to keep it at 5 minutes.</comment>
5154
<source_model>Cryptapi\Cryptapi\Model\Config\Source\RefreshList</source_model>
5255
</field>
53-
<field id="order_cancelation_timeout" translate="label" type="select" sortOrder="11" showInDefault="1" showInWebsite="1" showInStore="1">
56+
<field id="order_cancelation_timeout" translate="label" type="select" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1">
5457
<label>Order cancelation timeout</label>
5558
<comment>Selects the ammount of time the user has to pay for the order. When this time is over, order will be marked as 'Cancelled' and every paid value will be ignored. Notice: If the user still sends money to the generated address, value will still be redirected to you. Warning: We do not advice more than 1 Hour.</comment>
5659
<source_model>Cryptapi\Cryptapi\Model\Config\Source\CancellationList</source_model>
5760
</field>
58-
<group id="supported_cryptocurrencies" translate="label" type="text" sortOrder="12" showInDefault="1" showInWebsite="1"
61+
<group id="supported_cryptocurrencies" translate="label" type="text" sortOrder="13" showInDefault="1" showInWebsite="1"
5962
showInStore="1">
6063
<label>Cryptocurrencies</label>
61-
<field id="cryptocurrencies" translate="label" sortOrder="13" showInDefault="1" showInWebsite="1"
64+
<field id="cryptocurrencies" translate="label" sortOrder="14" showInDefault="1" showInWebsite="1"
6265
showInStore="1">
6366
<label>Add you address here:</label>
6467
<frontend_model>Cryptapi\Cryptapi\Block\Adminhtml\Cryptocurrencies</frontend_model>
6568
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
6669
<comment>Important: Add only 1 address per cryptocurrency!</comment>
6770
</field>
6871
</group>
69-
<field id="allowspecific" translate="label" type="allowspecific" sortOrder="14" showInDefault="1" showInWebsite="1" showInStore="1">
72+
<field id="allowspecific" translate="label" type="allowspecific" sortOrder="15" showInDefault="1" showInWebsite="1" showInStore="1">
7073
<label>Countries</label>
7174
<source_model>Magento\Payment\Model\Config\Source\Allspecificcountries</source_model>
7275
</field>
73-
<field id="specificcountry" translate="label" type="multiselect" sortOrder="15" showInDefault="1" showInWebsite="1" showInStore="1">
76+
<field id="specificcountry" translate="label" type="multiselect" sortOrder="16" showInDefault="1" showInWebsite="1" showInStore="1">
7477
<label>Specific Countries</label>
7578
<source_model>Magento\Directory\Model\Config\Source\Country</source_model>
7679
</field>
77-
<field id="sort_order" translate="label" type="text" sortOrder="16" showInDefault="1" showInWebsite="1" showInStore="1">
80+
<field id="sort_order" translate="label" type="text" sortOrder="17" showInDefault="1" showInWebsite="1" showInStore="1">
7881
<label>Sort Order</label>
7982
</field>
80-
<field id="disable_conversion" translate="label" type="select" sortOrder="17" showInDefault="1" showInWebsite="1" showInStore="1">
83+
<field id="disable_conversion" translate="label" type="select" sortOrder="18" showInDefault="1" showInWebsite="1" showInStore="1">
8184
<label>Disable price conversion</label>
8285
<comment>Attention: This option will disable the price conversion for ALL cryptocurrencies! If you check this, pricing will not be converted from the currency of your shop to the cryptocurrency selected by the user, and users will be requested to pay the same value as shown on your shop, regardless of the cryptocurrency selected</comment>
8386
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>

etc/config.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
<color_scheme>auto</color_scheme>
1717
<refresh_value_interval>300</refresh_value_interval>
1818
<order_cancelation_timeout>3600</order_cancelation_timeout>
19-
<fee_order_percentage>none</fee_order_percentage>
19+
<add_blockchain_fee>1</add_blockchain_fee>
20+
<fee_order_percentage>0.01</fee_order_percentage>
2021
<can_use_checkout>1</can_use_checkout>
2122
</cryptapi>
2223
</payment>

lib/CryptAPIHelper.php

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@
55
class CryptAPIHelper
66
{
77
private static $base_url = "https://api.cryptapi.io";
8+
private static $pro_url = "https://pro-api.cryptapi.io";
89
private $own_address = null;
910
private $payment_address = null;
1011
private $callback_url = null;
1112
private $coin = null;
1213
private $pending = false;
1314
private $parameters = [];
15+
private $api_key = null;
1416

15-
16-
public function __construct(
17-
$coin,
18-
$own_address,
19-
$callback_url,
20-
$parameters = [],
21-
$pending = false)
17+
public function __construct($coin, $own_address, $api_key, $callback_url, $parameters = [], $pending = false)
2218
{
2319
$this->own_address = $own_address;
2420
$this->callback_url = $callback_url;
21+
$this->api_key = $api_key;
2522
$this->coin = $coin;
2623
$this->pending = $pending ? 1 : 0;
2724
$this->parameters = $parameters;
@@ -32,22 +29,34 @@ public function get_address()
3229

3330
if (empty($this->own_address) || empty($this->coin) || empty($this->callback_url)) return null;
3431

32+
$api_key = $this->api_key;
33+
3534
$callback_url = $this->callback_url;
3635
if (!empty($this->parameters)) {
3736
$req_parameters = http_build_query($this->parameters);
3837
$callback_url = "{$this->callback_url}?{$req_parameters}";
3938
}
4039

41-
$ca_params = [
42-
'callback' => $callback_url,
43-
'address' => $this->own_address,
44-
'pending' => $this->pending,
45-
];
40+
if (empty($api_key)) {
41+
$ca_params = [
42+
'callback' => $callback_url,
43+
'address' => $this->own_address,
44+
'pending' => $this->pending,
45+
];
46+
} else {
47+
$ca_params = [
48+
'apikey' => $api_key,
49+
'callback' => $callback_url,
50+
'address' => $this->own_address,
51+
'pending' => $this->pending,
52+
];
53+
}
4654

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

4957
if ($response->status == 'success') {
5058
$this->payment_address = $response->address_in;
59+
5160
return $response->address_in;
5261
}
5362

@@ -76,7 +85,7 @@ public function get_qrcode($value, $size)
7685
{
7786
if (empty($this->coin)) return null;
7887

79-
if(empty($value)) {
88+
if (empty($value)) {
8089
$params = [
8190
'address' => $this->payment_address,
8291
'size' => $size,
@@ -98,6 +107,34 @@ public function get_qrcode($value, $size)
98107
return null;
99108
}
100109

110+
public static function get_static_qrcode($address, $coin, $value, $size = 300)
111+
{
112+
if (empty($address)) {
113+
return null;
114+
}
115+
116+
if (!empty($value)) {
117+
$params = [
118+
'address' => $address,
119+
'value' => $value,
120+
'size' => $size,
121+
];
122+
} else {
123+
$params = [
124+
'address' => $address,
125+
'size' => $size,
126+
];
127+
}
128+
129+
$response = CryptAPIHelper::_request($coin, 'qrcode', $params);
130+
131+
if ($response->status == 'success') {
132+
return ['qr_code' => $response->qr_code, 'uri' => $response->payment_uri];
133+
}
134+
135+
return null;
136+
}
137+
101138
public static function get_supported_coins()
102139
{
103140
$info = CryptAPIHelper::get_info(null, true);
@@ -173,34 +210,6 @@ public static function process_callback($_get)
173210
return $params;
174211
}
175212

176-
public static function get_static_qrcode($address, $coin, $value, $size = 300)
177-
{
178-
if (empty($address)) {
179-
return null;
180-
}
181-
182-
if (!empty($value)) {
183-
$params = [
184-
'address' => $address,
185-
'value' => $value,
186-
'size' => $size,
187-
];
188-
} else {
189-
$params = [
190-
'address' => $address,
191-
'size' => $size,
192-
];
193-
}
194-
195-
$response = CryptAPIHelper::_request($coin, 'qrcode', $params);
196-
197-
if ($response->status == 'success') {
198-
return ['qr_code' => $response->qr_code, 'uri' => $response->payment_uri];
199-
}
200-
201-
return null;
202-
}
203-
204213
public static function get_conversion($from, $to, $value, $disable_conversion)
205214
{
206215

@@ -241,6 +250,7 @@ public static function get_estimate($coin)
241250
return null;
242251
}
243252

253+
244254
public static function sig_fig($value, $digits)
245255
{
246256
if ($value == 0) {
@@ -261,7 +271,13 @@ private static function _request($coin, $endpoint, $params = [], $assoc = false)
261271

262272
$base_url = CryptAPIHelper::$base_url;
263273

264-
if (!empty($params)) $data = http_build_query($params);
274+
if (!empty($params['apikey']) && $endpoint !== 'info') {
275+
$base_url = CryptAPIHelper::$pro_url;
276+
}
277+
278+
if (!empty($params)) {
279+
$data = http_build_query($params);
280+
}
265281

266282
if (!empty($coin)) {
267283
$coin = str_replace('_', '/', $coin);
@@ -270,7 +286,9 @@ private static function _request($coin, $endpoint, $params = [], $assoc = false)
270286
$url = "{$base_url}/{$endpoint}/";
271287
}
272288

273-
if (!empty($data)) $url .= "?{$data}";
289+
if (!empty($data)) {
290+
$url .= "?{$data}";
291+
}
274292

275293
$curl = curl_init($url);
276294
curl_setopt($curl, CURLOPT_HEADER, 0);

0 commit comments

Comments
 (0)