Skip to content

Commit 4070588

Browse files
author
Ariano Ângelo
committed
v3.0
- New settings and color schemes to fit dark mode - New settings to add CryptAPI's services fees to the checkout - New settings to add blockchain fees to the checkout - Upgrade the settings - Added a history of transactions to the order payment page - Better handling of partial payments - Disable QR Code with value in certain currencies due to some wallets not supporting it - Minor fixes - UI Improvements
1 parent bbd0c3c commit 4070588

File tree

9 files changed

+57
-121
lines changed

9 files changed

+57
-121
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

Controller/Index/Callback.php

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct(
1717
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
1818
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
1919
\Magento\Framework\App\Request\Http $request,
20-
\Magento\Framework\App\Response\Http\Interceptor $response
20+
\Magento\Framework\App\Response\Http $response
2121
)
2222
{
2323
$this->helper = $helper;
@@ -37,30 +37,20 @@ public function execute()
3737
$order = $this->orderRepository->get($data['order_id']);
3838
$orderId = $order->getQuoteId();
3939

40-
$currencySymbol = $order->getOrderCurrencyCode();
41-
42-
$metaData = json_decode($this->helper->getPaymentResponse($orderId), true);
40+
$metaData = json_decode($this->helper->getPaymentResponse($orderId), true);
4341

4442
if ($this->payment->hasBeenPaid($order) || $data['nonce'] != $metaData['cryptapi_nonce']) {
4543
return $this->response->setBody("*ok*");
4644
}
4745

48-
$paid = floatval($data['value_coin']);
46+
$paid = $data['value_coin'];
4947

5048
$min_tx = floatval($metaData['cryptapi_min']);
5149

5250
$history = json_decode($metaData['cryptapi_history'], true);
5351

54-
$update_history = true;
55-
56-
foreach ($history as $uuid => $item) {
57-
if ($uuid === $data['uuid']) {
58-
$update_history = false;
59-
}
60-
}
61-
62-
if ($update_history) {
63-
$fiat_conversion = CryptAPIHelper::get_conversion($metaData['cryptapi_currency'], $currencySymbol, $paid, $this->scopeConfig->getValue('payment/cryptapi/disable_conversion', \Magento\Store\Model\ScopeInterface::SCOPE_STORE));
52+
if (empty($history[$data['uuid']])) {
53+
$fiat_conversion = CryptAPIHelper::get_conversion($metaData['cryptapi_currency'], $order->getOrderCurrencyCode(), $paid, $this->scopeConfig->getValue('payment/cryptapi/disable_conversion', \Magento\Store\Model\ScopeInterface::SCOPE_STORE));
6454

6555
$history[$data['uuid']] = [
6656
'timestamp' => time(),
@@ -74,24 +64,25 @@ public function execute()
7464

7565
$this->helper->updatePaymentData($orderId, 'cryptapi_history', json_encode($history));
7666

77-
$metaData = json_decode($this->helper->getPaymentResponse($orderId), true);
67+
$metaData = json_decode($this->helper->getPaymentResponse($orderId), true);
7868

7969
$history = json_decode($metaData['cryptapi_history'], true);
8070

81-
$calc = $this->payment::calcOrder($history, $metaData);
71+
$calc = $this->payment::calcOrder($history, $metaData['cryptapi_total'], $metaData['cryptapi_total_fiat']);
8272

83-
$remaining = $calc['remaining']->result();
84-
$remaining_pending = $calc['remaining_pending']->result();
73+
$remaining = $calc['remaining'];
74+
$remaining_pending = $calc['remaining_pending'];
8575

86-
if ($remaining <= 0) {
87-
if ($data['pending'] === '0') {
76+
if ($remaining_pending <= 0) {
77+
if ($remaining <= 0) {
8878
$state = \Magento\Sales\Model\Order::STATE_PROCESSING;
8979
$status = \Magento\Sales\Model\Order::STATE_PROCESSING;
9080
$order->setState($state);
9181
$order->setStatus($status);
9282
$order->setTotalPaid($order->getGrandTotal());
9383
$order->save();
9484
}
85+
return $this->response->setBody("*ok*");
9586
}
9687

9788
if ($remaining_pending <= $min_tx) {
@@ -100,8 +91,6 @@ public function execute()
10091
$this->helper->updatePaymentData($orderId, 'cryptapi_qr_code_value', CryptAPIHelper::get_static_qrcode($metaData['cryptapi_address'], $metaData['cryptapi_currency'], $remaining_pending, $this->scopeConfig->getValue('payment/cryptapi/qrcode_size', \Magento\Store\Model\ScopeInterface::SCOPE_STORE))['qr_code']);
10192
}
10293

103-
104-
10594
return $this->response->setBody("*ok*");
10695
}
10796
}

Controller/Index/CartQuote.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ class CartQuote implements HttpGetActionInterface
1212
protected $quoteRepository;
1313

1414
public function __construct(
15-
\Magento\Checkout\Model\Session $checkoutSession,
16-
\Magento\Framework\App\Request\Http $request,
17-
\Magento\Framework\App\Response\Http\Interceptor $response,
18-
\Psr\Log\LoggerInterface $logger
15+
\Magento\Checkout\Model\Session $checkoutSession,
16+
\Magento\Framework\App\Request\Http $request,
17+
\Magento\Framework\App\Response\Http $response,
18+
\Psr\Log\LoggerInterface $logger
1919
)
2020
{
2121
$this->checkoutSession = $checkoutSession;

Controller/Index/Status.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __construct(
1818
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
1919
\Cryptapi\Cryptapi\Cron\CryptapiCronjob $cronjob,
2020
\Magento\Framework\App\Request\Http $request,
21-
\Magento\Framework\App\Response\Http\Interceptor $response,
21+
\Magento\Framework\App\Response\Http $response,
2222
\Magento\Framework\Pricing\Helper\Data $priceHelper,
2323
\Psr\Log\LoggerInterface $logger
2424
)
@@ -49,42 +49,42 @@ public function execute()
4949

5050
$history = json_decode($metaData['cryptapi_history'], true);
5151

52-
$calc = $this->payment::calcOrder($history, $metaData);
52+
$calc = $this->payment::calcOrder($history, $metaData['cryptapi_total'], $metaData['cryptapi_total_fiat']);
5353

54-
$already_paid = $calc['already_paid']->result();
55-
$already_paid_fiat = $calc['already_paid_fiat']->result() <= 0 ? 0 : $calc['already_paid_fiat']->result();
54+
$already_paid = $calc['already_paid'];
55+
$already_paid_fiat = $calc['already_paid_fiat'] <= 0 ? 0 : $calc['already_paid_fiat'];
5656

5757
$min_tx = floatval($metaData['cryptapi_min']);
5858

59-
// $remaining = $calc['remaining'];
60-
$remaining_pending = $calc['remaining_pending']->result();
61-
$remaining_fiat = $calc['remaining_fiat']->result();
59+
$remaining = $calc['remaining'];
60+
$remaining_pending = $calc['remaining_pending'];
61+
$remaining_fiat = $calc['remaining_fiat'];
6262

6363
$cryptapi_pending = '0';
6464
if ($remaining_pending <= 0 && !$this->payment->hasBeenPaid($order)) {
65-
$cryptapi_pending = '1';
65+
$cryptapi_pending = 1;
6666
}
6767

6868
$counter_calc = (int)$metaData['cryptapi_last_price_update'] + (int)$this->scopeConfig->getValue('payment/cryptapi/refresh_value_interval', \Magento\Store\Model\ScopeInterface::SCOPE_STORE) - time();
6969
if (!$this->payment->hasBeenPaid($order) && $counter_calc <= 0) {
7070
$this->cronjob->execute();
7171
}
7272

73-
if ($remaining_pending <= $min_tx && $remaining_fiat > 0) {
73+
if ($remaining_pending <= $min_tx && $remaining_pending > 0) {
7474
$remaining_pending = $min_tx;
7575
$showMinFee = '1';
7676
}
7777

7878
$data = [
7979
'is_paid' => $this->payment->hasBeenPaid($order),
80-
'is_pending' => (int)($cryptapi_pending),
80+
'is_pending' => (int)$cryptapi_pending,
8181
'crypto_total' => floatval($metaData['cryptapi_total']),
8282
'qr_code_value' => $metaData['cryptapi_qr_code_value'],
8383
'cancelled' => $metaData['cryptapi_cancelled'],
84-
'remaining' => $remaining_pending <= 0 ? 0 : $remaining_pending,
84+
'remaining' => $remaining_pending < 0 ? 0 : $remaining_pending,
8585
'fiat_remaining' => $this->priceHelper->currency($remaining_fiat, true, false),
8686
'coin' => strtoupper($metaData['cryptapi_currency']),
87-
'show_min_fee' => $showMinFee,
87+
'show_min_fee' => (int)$showMinFee,
8888
'order_history' => $history,
8989
'already_paid' => $already_paid,
9090
'already_paid_fiat' => $this->priceHelper->currency(floatval($already_paid_fiat) <= 0 ? 0 : floatval($already_paid_fiat), true, false),

Cron/CryptapiCronjob.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ public function execute()
5454

5555
$min_tx = floatval($metaData['cryptapi_min']);
5656

57-
$calc = $this->payment::calcOrder($history, $metaData);
57+
$calc = $this->payment::calcOrder($history, $metaData['cryptapi_total'], $metaData['cryptapi_total_fiat']);
5858

59-
$remaining = $calc['remaining']->result();
60-
$remaining_pending = $calc['remaining_pending']->result();
61-
$remaining_fiat = $calc['remaining_fiat']->result();
59+
$remaining = $calc['remaining'];
60+
$remaining_pending = $calc['remaining_pending'];
61+
$remaining_fiat = $calc['remaining_fiat'];
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

@@ -68,8 +68,8 @@ public function execute()
6868
$crypto_total = CryptAPIHelper::get_conversion($order->getOrderCurrencyCode(), $cryptapi_coin, $metaData['cryptapi_total_fiat'], $disable_conversion);
6969
$this->helper->updatePaymentData($orderQuoteId, 'cryptapi_total', $crypto_total);
7070

71-
$calc_cron = $this->payment::calcOrder($history, $metaData);
72-
$crypto_remaining_total = $calc_cron['remaining_pending']->result();
71+
$calc_cron = $this->payment::calcOrder($history, $metaData['cryptapi_total'], $metaData['cryptapi_total_fiat']);
72+
$crypto_remaining_total = $calc_cron['remaining_pending'];
7373

7474
if ($remaining_pending <= $min_tx && !$remaining_pending <= 0) {
7575
$qr_code_data_value = CryptAPIHelper::get_static_qrcode($metaData['cryptapi_address'], $cryptapi_coin, $min_tx, $this->scopeConfig->getValue('payment/cryptapi/qrcode_size'));

Helper/Decimal.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

Model/Method/CryptapiPayment.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Magento\Framework\DataObject;
88
use Cryptapi\Cryptapi\lib\CryptAPIHelper;
99
use Magento\Payment\Model\Method\AbstractMethod;
10-
use Cryptapi\Cryptapi\Helper\Decimal;
10+
1111

1212
class CryptapiPayment extends AbstractMethod
1313
{
@@ -205,34 +205,34 @@ public function hasBeenPaid($order)
205205
}
206206
}
207207

208-
public static function calcOrder($history, $meta)
208+
public static function calcOrder($history, $total, $total_fiat)
209209
{
210-
$already_paid = new Decimal(0);
211-
$already_paid_fiat = new Decimal(0);
212-
$remaining = new Decimal($meta['cryptapi_total']);
213-
$remaining_pending = new Decimal($meta['cryptapi_total']);
214-
$remaining_fiat = new Decimal($meta['cryptapi_total_fiat']);
210+
$already_paid = 0;
211+
$already_paid_fiat = 0;
212+
$remaining = $total;
213+
$remaining_pending = $total;
214+
$remaining_fiat = $total_fiat;
215215

216-
if (count($history) > 0) {
216+
if (!empty($history)) {
217217
foreach ($history as $uuid => $item) {
218218
if ((int)$item['pending'] === 0) {
219-
$remaining = $remaining->sub($item['value_paid']);
219+
$remaining = bcsub($remaining, $item['value_paid'], 18);
220220
}
221221

222-
$remaining_pending = $remaining_pending->sub($item['value_paid']);
223-
$remaining_fiat = $remaining_fiat->sub($item['value_paid_fiat']);
222+
$remaining_pending = bcsub($remaining_pending, $item['value_paid'], 18);
223+
$remaining_fiat = bcsub($remaining_fiat, $item['value_paid_fiat'], 18);
224224

225-
$already_paid = $already_paid->sum($item['value_paid']);
226-
$already_paid_fiat = $already_paid_fiat->sum($item['value_paid_fiat']);
225+
$already_paid = bcadd($already_paid, $item['value_paid'], 18);
226+
$already_paid_fiat = bcadd($already_paid_fiat, $item['value_paid_fiat'], 18);
227227
}
228228
}
229229

230230
return [
231-
'already_paid' => $already_paid,
232-
'already_paid_fiat' => $already_paid_fiat,
233-
'remaining' => $remaining,
234-
'remaining_pending' => $remaining_pending,
235-
'remaining_fiat' => $remaining_fiat
231+
'already_paid' => floatval($already_paid),
232+
'already_paid_fiat' => floatval($already_paid_fiat),
233+
'remaining' => floatval($remaining),
234+
'remaining_pending' => floatval($remaining_pending),
235+
'remaining_fiat' => floatval($remaining_fiat)
236236
];
237237
}
238238

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
"name": "cryptapi/cryptapi",
33
"description": "CryptAPI's Magento extension",
44
"type": "magento2-module",
5-
"version": "3.0",
5+
"version": "3.0.0",
66
"require": {
77
"magento/module-payment": "100.1.*",
88
"magento/module-checkout": "100.1.*",
99
"magento/module-sales": "100.1.*",
1010
"ext-curl": "*",
11-
"ext-json": "*"
11+
"ext-json": "*",
12+
"ext-bcmath": "*"
1213
},
1314
"autoload": {
1415
"files": [ "registration.php" ],

view/frontend/templates/payment.phtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ if ($values) {
4747
is_paid = true;
4848
}
4949

50-
if (data.is_pending) {
50+
if (data.is_pending === 1) {
5151
waiting_payment.addClass('done');
5252
waiting_network.addClass('done');
5353
jQuery('.ca_loader').remove();
@@ -81,7 +81,7 @@ if ($values) {
8181
jQuery('.ca_qrcode.value').attr("src", "data:image/png;base64," + data.qr_code_value);
8282
}
8383

84-
if (data.show_min_fee === '1') {
84+
if (data.show_min_fee === 1) {
8585
jQuery('.ca_notification_remaining').show();
8686
} else {
8787
jQuery('.ca_notification_remaining').hide();

0 commit comments

Comments
 (0)