Skip to content

Commit c13729c

Browse files
author
Ariano Fonseca Ângelo
committed
v3.0.2
- Minor bugfix
1 parent 4070588 commit c13729c

File tree

24 files changed

+624
-499
lines changed

24 files changed

+624
-499
lines changed

Block/Payment.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function __construct(
1717
\Magento\Framework\View\Element\Template\Context $context,
1818
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
1919
\Magento\Framework\App\Request\Http $request,
20+
\Magento\Framework\App\ProductMetadataInterface $productMetadata,
2021
array $data = []
2122
)
2223
{
@@ -26,13 +27,18 @@ public function __construct(
2627
$this->scopeConfig = $scopeConfig;
2728
$this->request = $request;
2829
$this->orderRepository = $orderRepository;
30+
$this->productMetadata = $productMetadata;
2931
}
3032

3133
public function getTemplateValues()
3234
{
33-
$order_id = (int)$this->request->getParam('order_id');
34-
$nonce = (string)$this->request->getParam('nonce');
35-
$order = $this->orderRepository->get($order_id);
35+
if ($this->productMetadata->getVersion() >= 2.3 && $this->productMetadata->getVersion() < 2.4) {
36+
$order = $this->payment->getOrder();
37+
} else {
38+
$order_id = (int)$this->request->getParam('order_id');
39+
$nonce = (string)$this->request->getParam('nonce');
40+
$order = $this->orderRepository->get($order_id);
41+
}
3642

3743
$total = $order->getGrandTotal();
3844
$currencySymbol = $order->getOrderCurrencyCode();
@@ -48,8 +54,10 @@ public function getTemplateValues()
4854

4955
$metaData = json_decode($metaData, true);
5056

51-
if ($nonce != $metaData['cryptapi_nonce']) {
52-
return false;
57+
if (!$this->productMetadata->getVersion() >= 2.3 && $this->productMetadata->getVersion() < 2.4) {
58+
if ($nonce != $metaData['cryptapi_nonce']) {
59+
return false;
60+
}
5361
}
5462

5563
$cryptoValue = $metaData['cryptapi_total'];
@@ -89,7 +97,7 @@ public function getTemplateValues()
8997
}
9098

9199
$ajaxParams = [
92-
'order_id' => $order_id,
100+
'order_id' => $order->getId(),
93101
];
94102

95103
$ajaxUrl = $this->payment->getAjaxStatusUrl($ajaxParams);

Controller/Index/Callback.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,30 @@ public function execute()
3737
$order = $this->orderRepository->get($data['order_id']);
3838
$orderId = $order->getQuoteId();
3939

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

4244
if ($this->payment->hasBeenPaid($order) || $data['nonce'] != $metaData['cryptapi_nonce']) {
4345
return $this->response->setBody("*ok*");
4446
}
4547

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

4850
$min_tx = floatval($metaData['cryptapi_min']);
4951

5052
$history = json_decode($metaData['cryptapi_history'], true);
5153

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));
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));
5464

5565
$history[$data['uuid']] = [
5666
'timestamp' => time(),
@@ -68,7 +78,7 @@ public function execute()
6878

6979
$history = json_decode($metaData['cryptapi_history'], true);
7080

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

7383
$remaining = $calc['remaining'];
7484
$remaining_pending = $calc['remaining_pending'];

Controller/Index/CartQuote.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ class CartQuote implements HttpGetActionInterface
1414
public function __construct(
1515
\Magento\Checkout\Model\Session $checkoutSession,
1616
\Magento\Framework\App\Request\Http $request,
17-
\Magento\Framework\App\Response\Http $response,
18-
\Psr\Log\LoggerInterface $logger
17+
\Magento\Framework\App\Response\Http $response
1918
)
2019
{
2120
$this->checkoutSession = $checkoutSession;
2221
$this->request = $request;
2322
$this->response = $response;
24-
$this->logger = $logger;
2523
}
2624

2725
public function execute()

Controller/Index/Status.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,20 @@ public function execute()
4949

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

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

5454
$already_paid = $calc['already_paid'];
5555
$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'];
59+
// $remaining = $calc['remaining'];
6060
$remaining_pending = $calc['remaining_pending'];
6161
$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();
@@ -77,17 +77,17 @@ public function execute()
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' => (int)$showMinFee,
87+
'show_min_fee' => $showMinFee,
8888
'order_history' => $history,
8989
'already_paid' => $already_paid,
90-
'already_paid_fiat' => $this->priceHelper->currency(floatval($already_paid_fiat) <= 0 ? 0 : floatval($already_paid_fiat), true, false),
90+
'already_paid_fiat' => $this->priceHelper->currency($remaining_pending <= 0 ? 0 : floatval($already_paid_fiat), true, false),
9191
'counter' => (string)$counter_calc,
9292
'fiat_symbol' => $order->getOrderCurrencyCode()
9393
];

Cron/CryptapiCronjob.php

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

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

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

5959
$remaining = $calc['remaining'];
6060
$remaining_pending = $calc['remaining_pending'];
@@ -68,7 +68,7 @@ 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['cryptapi_total'], $metaData['cryptapi_total_fiat']);
71+
$calc_cron = $this->payment::calcOrder($history, $metaData);
7272
$crypto_remaining_total = $calc_cron['remaining_pending'];
7373

7474
if ($remaining_pending <= $min_tx && !$remaining_pending <= 0) {

Helper/Decimal.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Cryptapi\Cryptapi\Helper;
4+
5+
class Decimal {
6+
private $val = 0;
7+
private $precision = 18;
8+
9+
private function to_int($float_val) {
10+
return $float_val * 10 ** $this->precision;
11+
}
12+
13+
private function maybe_reduce_precision($new_val) {
14+
while ($new_val >= PHP_INT_MAX) {
15+
$new_val = ($new_val / 10 ** $this->precision) * 10 ** ($this->precision - 1);
16+
$this->precision -= 1;
17+
}
18+
19+
$this->val = intval($new_val);
20+
}
21+
22+
function __construct($float_val) {
23+
$this->sum($float_val);
24+
}
25+
26+
function mult($float_val) {
27+
$new_val = $this->val * $this->to_int($float_val);
28+
$this->maybe_reduce_precision($new_val);
29+
return $this;
30+
}
31+
32+
function div($float_val) {
33+
$new_val = $this->val / $this->to_int($float_val);
34+
$this->maybe_reduce_precision($new_val);
35+
return $this;
36+
}
37+
38+
function sum($float_val) {
39+
$new_val = $this->val + $this->to_int($float_val);
40+
$this->maybe_reduce_precision($new_val);
41+
return $this;
42+
}
43+
44+
function sub($float_val) {
45+
$new_val = $this->val - $this->to_int($float_val);
46+
$this->maybe_reduce_precision($new_val);
47+
return $this;
48+
}
49+
50+
function result() {
51+
return $this->val / 10 ** $this->precision;
52+
}
53+
}

Model/Method/CryptapiPayment.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
namespace Cryptapi\Cryptapi\Model\Method;
44

5-
65
use Magento\Quote\Api\Data\PaymentInterface;
76
use Magento\Framework\DataObject;
87
use Cryptapi\Cryptapi\lib\CryptAPIHelper;
98
use Magento\Payment\Model\Method\AbstractMethod;
10-
9+
use Cryptapi\Cryptapi\Helper\Decimal;
1110

1211
class CryptapiPayment extends AbstractMethod
1312
{
@@ -205,25 +204,25 @@ public function hasBeenPaid($order)
205204
}
206205
}
207206

208-
public static function calcOrder($history, $total, $total_fiat)
207+
public static function calcOrder($history, $meta)
209208
{
210209
$already_paid = 0;
211210
$already_paid_fiat = 0;
212-
$remaining = $total;
213-
$remaining_pending = $total;
214-
$remaining_fiat = $total_fiat;
211+
$remaining = $meta['cryptapi_total'];
212+
$remaining_pending = $meta['cryptapi_total'];
213+
$remaining_fiat = $meta['cryptapi_total_fiat'];
215214

216215
if (!empty($history)) {
217216
foreach ($history as $uuid => $item) {
218217
if ((int)$item['pending'] === 0) {
219-
$remaining = bcsub($remaining, $item['value_paid'], 18);
218+
$remaining = bcsub(CryptAPIHelper::sig_fig($remaining, 6), $item['value_paid'], 8);
220219
}
221220

222-
$remaining_pending = bcsub($remaining_pending, $item['value_paid'], 18);
223-
$remaining_fiat = bcsub($remaining_fiat, $item['value_paid_fiat'], 18);
221+
$remaining_pending = bcsub(CryptAPIHelper::sig_fig($remaining_pending, 6), $item['value_paid'], 8);
222+
$remaining_fiat = bcsub(CryptAPIHelper::sig_fig($remaining_fiat, 6), $item['value_paid_fiat'], 8);
224223

225-
$already_paid = bcadd($already_paid, $item['value_paid'], 18);
226-
$already_paid_fiat = bcadd($already_paid_fiat, $item['value_paid_fiat'], 18);
224+
$already_paid = bcadd(CryptAPIHelper::sig_fig($already_paid, 6), $item['value_paid'], 8);
225+
$already_paid_fiat = bcadd(CryptAPIHelper::sig_fig($already_paid_fiat, 6), $item['value_paid_fiat'], 8);
227226
}
228227
}
229228

Model/Total/Fee.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public function collect(
5555
$total->setGrandTotal($total->getGrandTotal());
5656
$total->setBaseGrandTotal($total->getBaseGrandTotal());
5757

58-
5958
$quote->setFee($fee);
6059

6160
return $this;

Observer/AfterSuccess.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ public function __construct(
1313
\Cryptapi\Cryptapi\Model\Method\CryptapiPayment $payment,
1414
\Magento\Framework\App\ResponseFactory $responseFactory,
1515
\Magento\Framework\UrlInterface $url,
16-
\Psr\Log\LoggerInterface $logger
16+
\Psr\Log\LoggerInterface $logger,
17+
\Magento\Framework\App\ProductMetadataInterface $productMetadata,
18+
\Magento\Framework\App\Response\Http $redirect
1719

1820
)
1921
{
@@ -22,10 +24,22 @@ public function __construct(
2224
$this->url = $url;
2325
$this->responseFactory = $responseFactory;
2426
$this->logger = $logger;
27+
$this->productMetadata = $productMetadata;
28+
$this->redirect = $redirect;
2529
}
2630

2731
public function execute(Observer $observer)
2832
{
33+
$version_check = 1;
34+
35+
if ($this->productMetadata->getVersion() >= 2.3 && $this->productMetadata->getVersion() < 2.4) {
36+
$version_check = 0;
37+
}
38+
39+
if (empty($version_check)) {
40+
return false;
41+
}
42+
2943
$order = $this->payment->getOrder();
3044
$paymentMethod = $order->getPayment()->getMethodInstance()->getCode();
3145

@@ -39,7 +53,7 @@ public function execute(Observer $observer)
3953

4054
$redirectOrder = $this->url->getUrl('cryptapi/index/payment', $params);
4155
$this->responseFactory->create()->setRedirect($redirectOrder)->sendResponse();
42-
die();
56+
return $this->redirect->setRedirect($redirectOrder);
4357
}
4458
}
4559
}

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Accept cryptocurrency payments on your Magento store
66
### Requirements:
77

88
```
9-
Magento >= 2.3
9+
Magento >= 2.4
1010
```
1111

1212
### Description
@@ -134,5 +134,11 @@ The easiest and fastest way is via our live chat on our [website](https://crypta
134134
* Minor fixes
135135
* UI Improvements
136136

137+
#### 3.0.1
138+
* Minor fixes
139+
140+
#### 3.0.2
141+
* Minor fixes
142+
137143
### Upgrade Notice
138144
* No breaking changes.

0 commit comments

Comments
 (0)