Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore

This file was deleted.

71 changes: 71 additions & 0 deletions Block/Adminhtml/Sales/Order/Fee.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Cryptapi\Cryptapi\Block\Adminhtml\Sales\Order;

use Magento\Framework\View\Element\Template;

class Fee extends Template
{
protected $config;

protected $order;

protected $source;

public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Tax\Model\Config $taxConfig,
array $data = []
)
{
$this->_config = $taxConfig;
parent::__construct($context, $data);
}

public function displayFullSummary()
{
return true;
}

public function getSource()
{
return $this->source;
}

public function getStore()
{
return $this->order->getStore();
}

public function getOrder()
{
return $this->order;
}

public function getLabelProperties()
{
return $this->getParentBlock()->getLabelProperties();
}

public function getValueProperties()
{
return $this->getParentBlock()->getValueProperties();
}

public function initTotals()
{
$parent = $this->getParentBlock();
$this->order = $parent->getOrder();
$this->source = $parent->getSource();
$fee = new \Magento\Framework\DataObject(
[
'code' => 'cryptapi_fee',
'strong' => false,
'value' => $this->order->getData('cryptapi_fee'),
'label' => __('Service Fee'),
]
);
$parent->addTotal($fee, 'cryptapi_fee');
return $this;
}
}
4 changes: 3 additions & 1 deletion Block/Adminhtml/Sales/Order/View.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace Cryptapi\Cryptapi\Block\Adminhtml\Sales\Order;

class View extends \Magento\Backend\Block\Template
use Magento\Backend\Block\Template;

class View extends Template
{
protected $helper;
protected $request;
Expand Down
69 changes: 46 additions & 23 deletions Block/Success.php → Block/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,69 @@
namespace Cryptapi\Cryptapi\Block;

use Cryptapi\Cryptapi\lib\CryptAPIHelper;
use Magento\Framework\View\Element\Template;

class Success extends \Magento\Framework\View\Element\Template
class Payment extends Template
{
protected $helper;
protected $payment;

public function __construct(
\Cryptapi\Cryptapi\Helper\Data $helper,
\Cryptapi\Cryptapi\Model\Pay $payment,
\Cryptapi\Cryptapi\Model\Method\CryptapiPayment $payment,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Catalog\Block\Product\Context $context,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
\Magento\Framework\App\Request\Http $request,
\Magento\Framework\App\ProductMetadataInterface $productMetadata,
array $data = []
)
{
parent::__construct($context, $data);
$this->helper = $helper;
$this->payment = $payment;
$this->scopeConfig = $scopeConfig;
$this->logger = $logger;
$this->request = $request;
$this->orderRepository = $orderRepository;
$this->productMetadata = $productMetadata;
}

public function getTemplateValues()
{
$order = $this->payment->getOrder();
if ($this->productMetadata->getVersion() >= 2.3 && $this->productMetadata->getVersion() < 2.4) {
$order = $this->payment->getOrder();
} else {
$order_id = (int)$this->request->getParam('order_id');
$nonce = (string)$this->request->getParam('nonce');
$order = $this->orderRepository->get($order_id);
}

$total = $order->getGrandTotal();
$currencySymbol = $order->getOrderCurrencyCode();

$metaData = $this->helper->getPaymentResponse($order->getQuoteId());

if (empty($metaData)) {
return false;
}

$qrCodeSize = $this->scopeConfig->getValue('payment/cryptapi/qrcode_size', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

$branding = $this->scopeConfig->getValue('payment/cryptapi/show_branding', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

$metaData = json_decode($metaData, true);

if (!$this->productMetadata->getVersion() >= 2.3 && $this->productMetadata->getVersion() < 2.4) {
if ($nonce != $metaData['cryptapi_nonce']) {
return false;
}
}

$cryptoValue = $metaData['cryptapi_total'];
$cryptoCoin = $metaData['cryptapi_currency'];

if (isset($metaData['cryptapi_address']) && !empty($metaData['cryptapi_address'])) {
$addressIn = $metaData['cryptapi_address'];
} else {

$selected = $cryptoCoin;

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

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

$this->helper->updatePaymentData($order->getQuoteId(), 'cryptapi_address', $addressIn);
$this->helper->updatePaymentData($order->getQuoteId(), 'cryptapi_qr_code_value', $qrCodeValue['qr_code']);
$this->helper->updatePaymentData($order->getQuoteId(), 'cryptapi_qr_code', $qrCode['qr_code']);
}

$ajaxParams = [
'order_id' => $order->getId()
'order_id' => $order->getId(),
];

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

$qrCodeSize = $this->scopeConfig->getValue('payment/cryptapi/qrcode_size', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

$branding = $this->scopeConfig->getValue('payment/cryptapi/show_branding', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

$qrCode = $api->get_qrcode('', $qrCodeSize);
$qrCodeValue = $api->get_qrcode($cryptoValue, $qrCodeSize);
$metaData = $this->helper->getPaymentResponse($order->getQuoteId());
$metaData = json_decode($metaData, true);

$values = [
'crypto_value' => $cryptoValue,
return [
'crypto_value' => floatval($cryptoValue),
'currency_symbol' => $currencySymbol,
'total' => $total,
'address_in' => $addressIn,
'crypto_coin' => $cryptoCoin,
'ajax_url' => $ajaxUrl,
'qrcode_size' => $qrCodeSize,
'qrcode' => $qrCode['qr_code'],
'qrcode_value' => $qrCodeValue['qr_code'],
'qrcode' => $metaData['cryptapi_qr_code'],
'qrcode_value' => $metaData['cryptapi_qr_code_value'],
'qrcode_default' => $this->scopeConfig->getValue('payment/cryptapi/qrcode_default', \Magento\Store\Model\ScopeInterface::SCOPE_STORE),
'payment_uri' => $qrCode['uri'],
'show_branding' => $branding
'show_branding' => $branding,
'qr_code_setting' => $this->scopeConfig->getValue('payment/cryptapi/qrcode_setting', \Magento\Store\Model\ScopeInterface::SCOPE_STORE),
'order_timestamp' => strtotime($order->getCreatedAt()),
'order_cancelation_timeout' => $this->scopeConfig->getValue('payment/cryptapi/order_cancelation_timeout', \Magento\Store\Model\ScopeInterface::SCOPE_STORE),
'refresh_value_interval' => $this->scopeConfig->getValue('payment/cryptapi/refresh_value_interval', \Magento\Store\Model\ScopeInterface::SCOPE_STORE),
'last_price_update' => $metaData['cryptapi_last_price_update'],
'min_tx' => $metaData['cryptapi_min'],
];

return $values;
}
}
87 changes: 58 additions & 29 deletions Controller/Index/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,104 @@
namespace Cryptapi\Cryptapi\Controller\Index;

use Cryptapi\Cryptapi\lib\CryptAPIHelper;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\HttpGetActionInterface;

class Callback extends \Magento\Framework\App\Action\Action
class Callback implements HttpGetActionInterface
{
protected $helper;
protected $payment;
protected $orderFactory;

public function __construct(
\Cryptapi\Cryptapi\Helper\Data $helper,
\Cryptapi\Cryptapi\Model\Pay $payment,
\Magento\Sales\Model\OrderFactory $orderFactory,
\Cryptapi\Cryptapi\Model\Method\CryptapiPayment $payment,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\App\Action\Context $context
\Magento\Framework\App\Request\Http $request,
\Magento\Framework\App\Response\Http $response
)
{
$this->helper = $helper;
$this->payment = $payment;
$this->orderFactory = $orderFactory;
$this->orderRepository = $orderRepository;
$this->scopeConfig = $scopeConfig;
parent::__construct($context);
$this->request = $request;
$this->response = $response;
}

public function execute()
{
$params = $this->getRequest()->getParams();
$params = $this->request->getParams();

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

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

$metaData = $this->helper->getPaymentResponse($order->getQuoteId());
$currencySymbol = $order->getOrderCurrencyCode();

if (!empty($metaData)) {
$metaData = json_decode($metaData, true);
}
$metaData = json_decode($this->helper->getPaymentResponse($orderId), true);

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

$alreadyPaid = 0;
$paid = floatval($data['value_coin']);

if (isset($metaData['cryptapi_paid'])) {
$alreadyPaid = $metaData['cryptapi_paid'];
}
$min_tx = floatval($metaData['cryptapi_min']);

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

if (!$data['pending']) {
$this->helper->updatePaymentData($order->getQuoteId(), 'cryptapi_paid', $paid);
$update_history = true;

foreach ($history as $uuid => $item) {
if ($uuid === $data['uuid']) {
$update_history = false;
}
}

if ($paid >= $metaData['cryptapi_total']) {
if ($data['pending']) {
$this->helper->updatePaymentData($order->getQuoteId(), 'cryptapi_pending', "1");
} else {
$this->helper->deletePaymentData($order->getQuoteId(), 'cryptapi_pending');
if ($update_history) {
$fiat_conversion = CryptAPIHelper::get_conversion($metaData['cryptapi_currency'], $currencySymbol, $paid, $this->scopeConfig->getValue('payment/cryptapi/disable_conversion', \Magento\Store\Model\ScopeInterface::SCOPE_STORE));

$history[$data['uuid']] = [
'timestamp' => time(),
'value_paid' => $paid,
'value_paid_fiat' => $fiat_conversion,
'pending' => $data['pending']
];
} else {
$history[$data['uuid']]['pending'] = $data['pending'];
}

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

$metaData = json_decode($this->helper->getPaymentResponse($orderId), true);

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

$calc = $this->payment::calcOrder($history, $metaData);

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

if ($remaining_pending <= 0) {
if ($remaining <= 0) {
$state = \Magento\Sales\Model\Order::STATE_PROCESSING;
$status = \Magento\Sales\Model\Order::STATE_PROCESSING;
$order->setState($state);
$order->setStatus($status);
$order->setTotalPaid($order->getGrandTotal());
$order->save();

$this->helper->updatePaymentData($order->getQuoteId(), 'cryptapi_txid', $data['txid_in']);
}
return $this->response->setBody("*ok*");
}
return $this->getResponse()->setBody("*ok*");

if ($remaining_pending <= $min_tx) {
$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']);
} else {
$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']);
}

return $this->response->setBody("*ok*");
}
}
38 changes: 38 additions & 0 deletions Controller/Index/CartQuote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Cryptapi\Cryptapi\Controller\Index;

use Magento\Framework\App\Action\HttpGetActionInterface;

class CartQuote implements HttpGetActionInterface
{
protected $helper;
protected $payment;
protected $orderFactory;
protected $quoteRepository;

public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Framework\App\Request\Http $request,
\Magento\Framework\App\Response\Http $response
)
{
$this->checkoutSession = $checkoutSession;
$this->request = $request;
$this->response = $response;
}

public function execute()
{
$selected = (string)$this->request->getParam('selected');

$this->checkoutSession->setCurrency($selected);

$data = [
'status' => 'done'
];

$response = json_encode($data);
return $this->response->setBody($response);
}
}
Loading