Skip to content

Commit bb5aeba

Browse files
authored
Merge pull request #1 from arianoangelo/master
v3.0.3
2 parents b225d11 + ddfcc60 commit bb5aeba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2210
-482
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Cryptapi\Cryptapi\Block\Adminhtml\Sales\Order;
4+
5+
use Magento\Framework\View\Element\Template;
6+
7+
class Fee extends Template
8+
{
9+
protected $config;
10+
11+
protected $order;
12+
13+
protected $source;
14+
15+
public function __construct(
16+
\Magento\Framework\View\Element\Template\Context $context,
17+
\Magento\Tax\Model\Config $taxConfig,
18+
array $data = []
19+
)
20+
{
21+
$this->_config = $taxConfig;
22+
parent::__construct($context, $data);
23+
}
24+
25+
public function displayFullSummary()
26+
{
27+
return true;
28+
}
29+
30+
public function getSource()
31+
{
32+
return $this->source;
33+
}
34+
35+
public function getStore()
36+
{
37+
return $this->order->getStore();
38+
}
39+
40+
public function getOrder()
41+
{
42+
return $this->order;
43+
}
44+
45+
public function getLabelProperties()
46+
{
47+
return $this->getParentBlock()->getLabelProperties();
48+
}
49+
50+
public function getValueProperties()
51+
{
52+
return $this->getParentBlock()->getValueProperties();
53+
}
54+
55+
public function initTotals()
56+
{
57+
$parent = $this->getParentBlock();
58+
$this->order = $parent->getOrder();
59+
$this->source = $parent->getSource();
60+
$fee = new \Magento\Framework\DataObject(
61+
[
62+
'code' => 'cryptapi_fee',
63+
'strong' => false,
64+
'value' => $this->order->getData('cryptapi_fee'),
65+
'label' => __('Service Fee'),
66+
]
67+
);
68+
$parent->addTotal($fee, 'cryptapi_fee');
69+
return $this;
70+
}
71+
}

Block/Adminhtml/Sales/Order/View.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
namespace Cryptapi\Cryptapi\Block\Adminhtml\Sales\Order;
33

4-
class View extends \Magento\Backend\Block\Template
4+
use Magento\Backend\Block\Template;
5+
6+
class View extends Template
57
{
68
protected $helper;
79
protected $request;
Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,69 @@
33
namespace Cryptapi\Cryptapi\Block;
44

55
use Cryptapi\Cryptapi\lib\CryptAPIHelper;
6+
use Magento\Framework\View\Element\Template;
67

7-
class Success extends \Magento\Framework\View\Element\Template
8+
class Payment extends Template
89
{
910
protected $helper;
1011
protected $payment;
1112

1213
public function __construct(
1314
\Cryptapi\Cryptapi\Helper\Data $helper,
14-
\Cryptapi\Cryptapi\Model\Pay $payment,
15+
\Cryptapi\Cryptapi\Model\Method\CryptapiPayment $payment,
1516
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
16-
\Magento\Catalog\Block\Product\Context $context,
17-
\Psr\Log\LoggerInterface $logger,
17+
\Magento\Framework\View\Element\Template\Context $context,
18+
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
19+
\Magento\Framework\App\Request\Http $request,
20+
\Magento\Framework\App\ProductMetadataInterface $productMetadata,
1821
array $data = []
1922
)
2023
{
2124
parent::__construct($context, $data);
2225
$this->helper = $helper;
2326
$this->payment = $payment;
2427
$this->scopeConfig = $scopeConfig;
25-
$this->logger = $logger;
28+
$this->request = $request;
29+
$this->orderRepository = $orderRepository;
30+
$this->productMetadata = $productMetadata;
2631
}
2732

2833
public function getTemplateValues()
2934
{
30-
$order = $this->payment->getOrder();
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+
}
3142

3243
$total = $order->getGrandTotal();
3344
$currencySymbol = $order->getOrderCurrencyCode();
34-
3545
$metaData = $this->helper->getPaymentResponse($order->getQuoteId());
3646

3747
if (empty($metaData)) {
3848
return false;
3949
}
4050

51+
$qrCodeSize = $this->scopeConfig->getValue('payment/cryptapi/qrcode_size', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
52+
53+
$branding = $this->scopeConfig->getValue('payment/cryptapi/show_branding', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
54+
4155
$metaData = json_decode($metaData, true);
4256

57+
if (!$this->productMetadata->getVersion() >= 2.3 && $this->productMetadata->getVersion() < 2.4) {
58+
if ($nonce != $metaData['cryptapi_nonce']) {
59+
return false;
60+
}
61+
}
62+
4363
$cryptoValue = $metaData['cryptapi_total'];
4464
$cryptoCoin = $metaData['cryptapi_currency'];
4565

4666
if (isset($metaData['cryptapi_address']) && !empty($metaData['cryptapi_address'])) {
4767
$addressIn = $metaData['cryptapi_address'];
4868
} else {
49-
5069
$selected = $cryptoCoin;
5170

5271
$address = '';
@@ -69,37 +88,41 @@ public function getTemplateValues()
6988
$api = new CryptAPIHelper($selected, $address, $callbackUrl, $params, true);
7089
$addressIn = $api->get_address();
7190

91+
$qrCode = $api->get_qrcode('', $qrCodeSize);
92+
$qrCodeValue = $api->get_qrcode($cryptoValue, $qrCodeSize);
93+
7294
$this->helper->updatePaymentData($order->getQuoteId(), 'cryptapi_address', $addressIn);
95+
$this->helper->updatePaymentData($order->getQuoteId(), 'cryptapi_qr_code_value', $qrCodeValue['qr_code']);
96+
$this->helper->updatePaymentData($order->getQuoteId(), 'cryptapi_qr_code', $qrCode['qr_code']);
7397
}
7498

7599
$ajaxParams = [
76-
'order_id' => $order->getId()
100+
'order_id' => $order->getId(),
77101
];
78102

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

81-
$qrCodeSize = $this->scopeConfig->getValue('payment/cryptapi/qrcode_size', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
82-
83-
$branding = $this->scopeConfig->getValue('payment/cryptapi/show_branding', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
84-
85-
$qrCode = $api->get_qrcode('', $qrCodeSize);
86-
$qrCodeValue = $api->get_qrcode($cryptoValue, $qrCodeSize);
105+
$metaData = $this->helper->getPaymentResponse($order->getQuoteId());
106+
$metaData = json_decode($metaData, true);
87107

88-
$values = [
89-
'crypto_value' => $cryptoValue,
108+
return [
109+
'crypto_value' => floatval($cryptoValue),
90110
'currency_symbol' => $currencySymbol,
91111
'total' => $total,
92112
'address_in' => $addressIn,
93113
'crypto_coin' => $cryptoCoin,
94114
'ajax_url' => $ajaxUrl,
95115
'qrcode_size' => $qrCodeSize,
96-
'qrcode' => $qrCode['qr_code'],
97-
'qrcode_value' => $qrCodeValue['qr_code'],
116+
'qrcode' => $metaData['cryptapi_qr_code'],
117+
'qrcode_value' => $metaData['cryptapi_qr_code_value'],
98118
'qrcode_default' => $this->scopeConfig->getValue('payment/cryptapi/qrcode_default', \Magento\Store\Model\ScopeInterface::SCOPE_STORE),
99-
'payment_uri' => $qrCode['uri'],
100-
'show_branding' => $branding
119+
'show_branding' => $branding,
120+
'qr_code_setting' => $this->scopeConfig->getValue('payment/cryptapi/qrcode_setting', \Magento\Store\Model\ScopeInterface::SCOPE_STORE),
121+
'order_timestamp' => strtotime($order->getCreatedAt()),
122+
'order_cancelation_timeout' => $this->scopeConfig->getValue('payment/cryptapi/order_cancelation_timeout', \Magento\Store\Model\ScopeInterface::SCOPE_STORE),
123+
'refresh_value_interval' => $this->scopeConfig->getValue('payment/cryptapi/refresh_value_interval', \Magento\Store\Model\ScopeInterface::SCOPE_STORE),
124+
'last_price_update' => $metaData['cryptapi_last_price_update'],
125+
'min_tx' => $metaData['cryptapi_min'],
101126
];
102-
103-
return $values;
104127
}
105128
}

Controller/Index/Callback.php

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,104 @@
33
namespace Cryptapi\Cryptapi\Controller\Index;
44

55
use Cryptapi\Cryptapi\lib\CryptAPIHelper;
6-
use Magento\Framework\Controller\ResultFactory;
6+
use Magento\Framework\App\Action\HttpGetActionInterface;
77

8-
class Callback extends \Magento\Framework\App\Action\Action
8+
class Callback implements HttpGetActionInterface
99
{
1010
protected $helper;
1111
protected $payment;
1212
protected $orderFactory;
1313

1414
public function __construct(
1515
\Cryptapi\Cryptapi\Helper\Data $helper,
16-
\Cryptapi\Cryptapi\Model\Pay $payment,
17-
\Magento\Sales\Model\OrderFactory $orderFactory,
16+
\Cryptapi\Cryptapi\Model\Method\CryptapiPayment $payment,
17+
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
1818
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
19-
\Magento\Framework\App\Action\Context $context
19+
\Magento\Framework\App\Request\Http $request,
20+
\Magento\Framework\App\Response\Http $response
2021
)
2122
{
2223
$this->helper = $helper;
2324
$this->payment = $payment;
24-
$this->orderFactory = $orderFactory;
25+
$this->orderRepository = $orderRepository;
2526
$this->scopeConfig = $scopeConfig;
26-
parent::__construct($context);
27+
$this->request = $request;
28+
$this->response = $response;
2729
}
2830

2931
public function execute()
3032
{
31-
$params = $this->getRequest()->getParams();
33+
$params = $this->request->getParams();
3234

3335
$data = CryptAPIHelper::process_callback($params);
3436

35-
$order = $this->orderFactory->create()->load($data['order_id']);
37+
$order = $this->orderRepository->get($data['order_id']);
38+
$orderId = $order->getQuoteId();
3639

37-
$metaData = $this->helper->getPaymentResponse($order->getQuoteId());
40+
$currencySymbol = $order->getOrderCurrencyCode();
3841

39-
if (!empty($metaData)) {
40-
$metaData = json_decode($metaData, true);
41-
}
42+
$metaData = json_decode($this->helper->getPaymentResponse($orderId), true);
4243

4344
if ($this->payment->hasBeenPaid($order) || $data['nonce'] != $metaData['cryptapi_nonce']) {
44-
return $this->getResponse()->setBody("*ok*");
45+
return $this->response->setBody("*ok*");
4546
}
4647

47-
$alreadyPaid = 0;
48+
$paid = floatval($data['value_coin']);
4849

49-
if (isset($metaData['cryptapi_paid'])) {
50-
$alreadyPaid = $metaData['cryptapi_paid'];
51-
}
50+
$min_tx = floatval($metaData['cryptapi_min']);
5251

53-
$paid = floatval($alreadyPaid) + floatval($data['value_coin']);
52+
$history = json_decode($metaData['cryptapi_history'], true);
5453

55-
if (!$data['pending']) {
56-
$this->helper->updatePaymentData($order->getQuoteId(), 'cryptapi_paid', $paid);
54+
$update_history = true;
55+
56+
foreach ($history as $uuid => $item) {
57+
if ($uuid === $data['uuid']) {
58+
$update_history = false;
59+
}
5760
}
5861

59-
if ($paid >= $metaData['cryptapi_total']) {
60-
if ($data['pending']) {
61-
$this->helper->updatePaymentData($order->getQuoteId(), 'cryptapi_pending', "1");
62-
} else {
63-
$this->helper->deletePaymentData($order->getQuoteId(), 'cryptapi_pending');
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));
64+
65+
$history[$data['uuid']] = [
66+
'timestamp' => time(),
67+
'value_paid' => $paid,
68+
'value_paid_fiat' => $fiat_conversion,
69+
'pending' => $data['pending']
70+
];
71+
} else {
72+
$history[$data['uuid']]['pending'] = $data['pending'];
73+
}
74+
75+
$this->helper->updatePaymentData($orderId, 'cryptapi_history', json_encode($history));
76+
77+
$metaData = json_decode($this->helper->getPaymentResponse($orderId), true);
6478

79+
$history = json_decode($metaData['cryptapi_history'], true);
80+
81+
$calc = $this->payment::calcOrder($history, $metaData);
82+
83+
$remaining = $calc['remaining'];
84+
$remaining_pending = $calc['remaining_pending'];
85+
86+
if ($remaining_pending <= 0) {
87+
if ($remaining <= 0) {
6588
$state = \Magento\Sales\Model\Order::STATE_PROCESSING;
6689
$status = \Magento\Sales\Model\Order::STATE_PROCESSING;
6790
$order->setState($state);
6891
$order->setStatus($status);
6992
$order->setTotalPaid($order->getGrandTotal());
7093
$order->save();
71-
72-
$this->helper->updatePaymentData($order->getQuoteId(), 'cryptapi_txid', $data['txid_in']);
7394
}
95+
return $this->response->setBody("*ok*");
7496
}
75-
return $this->getResponse()->setBody("*ok*");
97+
98+
if ($remaining_pending <= $min_tx) {
99+
$this->helper->updatePaymentData($orderId, 'cryptapi_qr_code_value', CryptAPIHelper::get_static_qrcode($metaData['cryptapi_address'], $metaData['cryptapi_currency'], $min_tx, $this->scopeConfig->getValue('payment/cryptapi/qrcode_size', \Magento\Store\Model\ScopeInterface::SCOPE_STORE))['qr_code']);
100+
} else {
101+
$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']);
102+
}
103+
104+
return $this->response->setBody("*ok*");
76105
}
77106
}

Controller/Index/CartQuote.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Cryptapi\Cryptapi\Controller\Index;
4+
5+
use Magento\Framework\App\Action\HttpGetActionInterface;
6+
7+
class CartQuote implements HttpGetActionInterface
8+
{
9+
protected $helper;
10+
protected $payment;
11+
protected $orderFactory;
12+
protected $quoteRepository;
13+
14+
public function __construct(
15+
\Magento\Checkout\Model\Session $checkoutSession,
16+
\Magento\Framework\App\Request\Http $request,
17+
\Magento\Framework\App\Response\Http $response
18+
)
19+
{
20+
$this->checkoutSession = $checkoutSession;
21+
$this->request = $request;
22+
$this->response = $response;
23+
}
24+
25+
public function execute()
26+
{
27+
$selected = (string)$this->request->getParam('selected');
28+
29+
$this->checkoutSession->setCurrency($selected);
30+
31+
$data = [
32+
'status' => 'done'
33+
];
34+
35+
$response = json_encode($data);
36+
return $this->response->setBody($response);
37+
}
38+
}

0 commit comments

Comments
 (0)