Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: plugin unification support #88

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
98 changes: 98 additions & 0 deletions Block/Adminhtml/Iframe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Culqi\Pago\Block\Adminhtml;

use Magento\Backend\Block\Template;
use Magento\Backend\Block\Template\Context;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use Culqi\Pago\Helper\Data;

class Iframe extends Template
{
protected $storeManager;
protected $scopeConfig;
protected $helper;

public function __construct(
Context $context,
StoreManagerInterface $storeManager,
Data $helper,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
array $data = []
) {
parent::__construct($context, $data);
$this->storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
$this->helper = $helper;
}

public function getIframeUrl()
{
// Reinitialize config to ensure updated values
$config = ObjectManager::getInstance()->get(ReinitableConfigInterface::class);
$config->reinit();

list($public_key, $merchant, $payment_methods, $plugin_status) = $this->getIframeParameters();
$token = $this->helper->generate_token(true);

if($token === null) {
$token = '';
}

$shopUrl = $this->storeManager->getStore()->getBaseUrl();

return CULQI_CONFIG_URL . '?platform=' . PLATFORM . '&status=' . urlencode( $plugin_status ) . '&pk=' . urlencode( $public_key ) . '&merchant=' . urlencode( $merchant ) . '&activePaymentMethods=' . urlencode($payment_methods) . '&shop=' . urlencode($shopUrl) . '&token=' . urlencode($token);
}

private function getIframeParameters()
{
$payment_methods = $this->scopeConfig->getValue(
"payment/culqi/payment_methods",
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) ?? '';

if (empty($payment_methods)) {
$resource = ObjectManager::getInstance()->get('Magento\\Framework\\App\\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('core_config_data');
$query = "SELECT value FROM $tableName WHERE path = 'payment/culqi/payment_methods' LIMIT 1";
$payment_methods = $connection->fetchOne($query) ?: '';
}

$publicKey = $this->scopeConfig->getValue(
'payment/culqi/pk',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) ?? '';

if (empty($publicKey)) {
$resource = ObjectManager::getInstance()->get('Magento\\Framework\\App\\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('core_config_data');
$query = "SELECT value FROM $tableName WHERE path = 'payment/culqi/pk' LIMIT 1";
$publicKey = $connection->fetchOne($query) ?: '';
}

$merchant = $this->scopeConfig->getValue(
"payment/culqi/merchant",
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) ?? '';

if (empty($merchant)) {
$resource = ObjectManager::getInstance()->get('Magento\\Framework\\App\\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('core_config_data');
$query = "SELECT value FROM $tableName WHERE path = 'payment/culqi/merchant' LIMIT 1";
$merchant = $connection->fetchOne($query) ?: '';
}

$resource = ObjectManager::getInstance()->get('Magento\\Framework\\App\\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('core_config_data');
$query = "SELECT value FROM $tableName WHERE path = 'payment/culqi/active' LIMIT 1";
$plugin_status = $connection->fetchOne($query) ?: 0;

return [$publicKey, $merchant, $payment_methods, $plugin_status];
}
}
97 changes: 97 additions & 0 deletions Controller/Adminhtml/Payment/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Culqi\Pago\Controller\Adminhtml\Payment;

use Magento\Backend\App\Action;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Payment\Model\Config as Test;


class Config extends Action
{
protected $configWriter;
protected $resultJsonFactory;
protected $_scopeConfig;
protected $_paymentConfig;

public function __construct(
Action\Context $context,
WriterInterface $configWriter,
JsonFactory $resultJsonFactory,
ScopeConfigInterface $scopeConfig,
Test $paymentConfig,
) {
parent::__construct($context);
$this->configWriter = $configWriter;
$this->resultJsonFactory = $resultJsonFactory;
$this->_scopeConfig = $scopeConfig;
$this->_paymentConfig = $paymentConfig;
}

public function execute()
{
// Return the available payment methods as JSON
$resultJson = $this->resultJsonFactory->create();

try {
$methodCode = $this->getRequest()->getParam('method_code');
$status = $this->getRequest()->getParam('status');
$publicKey = $this->getRequest()->getParam('publicKey');
$merchant = $this->getRequest()->getParam('merchant');
$rsa_pk_culqi = $this->getRequest()->getParam('rsa_pk_culqi');
$plugin_status = $this->getRequest()->getParam('pluginStatus');
$plugin_status = ($plugin_status === 'true');
$rsa_sk_plugin = $this->getRequest()->getParam('rsa_sk_plugin');
$payment_methods = $this->getRequest()->getParam('payment_methods');

if (!$methodCode) {
throw new \Exception(__('Payment method code is missing.'));
}

// Save the configuration
$this->configWriter->save("payment/culqi/active", $plugin_status, ScopeInterface::SCOPE_STORE);

// Clear configuration and full-page caches
$this->_objectManager->get(\Magento\Framework\App\Cache\Manager::class)->flush(['config', 'full_page']);

// Reload configuration to apply changes immediately
$this->_objectManager->create(\Magento\Framework\App\Config\ReinitableConfigInterface::class)->reinit();

$paymentMethods = $this->_paymentConfig->getActiveMethods();

$activeMethods = [];
foreach ($paymentMethods as $methodCode => $method) {
// Check if the method is enabled in configuration
$isActive = $this->_scopeConfig->isSetFlag(
"payment/{$methodCode}/active",
ScopeInterface::SCOPE_STORE
);

if ($isActive) {
$activeMethods[] = [
'method_code' => $methodCode,
'method_title' => $method->getTitle(),
'method_icon' => $method->getConfigData('icon')
];
}
}

$this->configWriter->save("payment/culqi/pk", $publicKey, ScopeInterface::SCOPE_STORE);
$this->configWriter->save("payment/culqi/merchant", $merchant, ScopeInterface::SCOPE_STORE);
$this->configWriter->save("payment/culqi/rsa_pk_culqi", $rsa_pk_culqi, ScopeInterface::SCOPE_STORE);
$this->configWriter->save("payment/culqi/plugin_status", $plugin_status, ScopeInterface::SCOPE_STORE);
$this->configWriter->save("payment/culqi/rsa_sk_plugin", $rsa_sk_plugin, ScopeInterface::SCOPE_STORE);
$this->configWriter->save("payment/culqi/payment_methods", $payment_methods, ScopeInterface::SCOPE_STORE);

return $resultJson->setData([
'success' => true,
'message' => __('Payment method %1 has been %2.', $methodCode, $status ? 'enabled' : 'disabled')
]);
} catch (\Exception $e) {
return $resultJson->setData(['success' => false, 'message' => $e->getMessage()]);
}
}
}
28 changes: 28 additions & 0 deletions Controller/Adminhtml/Payment/View.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Culqi\Pago\Controller\Adminhtml\Payment;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class View extends Action
{
const ADMIN_RESOURCE = 'Culqi_Pago::payment_view';

protected $resultPageFactory;

public function __construct(Context $context, PageFactory $resultPageFactory)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}

public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('Culqi_Pago::payment');
$resultPage->getConfig()->getTitle()->prepend(__('Payment Method Full Page View'));
return $resultPage;
}
}
31 changes: 24 additions & 7 deletions Controller/Payment/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function execute()

public function responseAction()
{
if ($this->getRequest()->get("orderId") && $this->getRequest()->get("statusOrder") == 'complete_payment') {
$this->logger->info("inicio de llamada");
if ($this->getRequest()->get("orderId") && $this->getRequest()->get("status") == 'complete_payment') {
$orderId = $this->getRequest()->get("orderId");
$card_number = $this->getRequest()->get("card_number");
$card_brand = $this->getRequest()->get("card_brand");
Expand All @@ -82,7 +83,11 @@ public function responseAction()
$this->_checkoutSession->setSuccess(true);
$resultRedirect->setUrl($this->url->getUrl('pago/payment/success'));

return $resultRedirect;
//return $resultRedirect;
die(json_encode([
'success' => true,
'data' => $resultRedirect,
]));
} elseif ($this->getRequest()->get("orderId") && $this->getRequest()->get("statusOrder") == 'order'){
$orderId = $this->getRequest()->get("orderId");
$culqi_order_id = $this->getRequest()->get("order_id");
Expand All @@ -98,7 +103,10 @@ public function responseAction()
$this->_checkoutSession->setSuccess(true);
$resultRedirect->setUrl($this->url->getUrl('pago/payment/success'));

return $resultRedirect;
die(json_encode([
'success' => true,
'data' => $resultRedirect,
]));
} elseif ($this->getRequest()->get("orderId") && $this->getRequest()->get("statusOrder") == 'fail') {
$orderId = $this->getRequest()->get("orderId");

Expand All @@ -119,8 +127,11 @@ public function responseAction()
}
$resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->url->getUrl('checkout/cart/'));
return $resultRedirect;
} elseif ($this->getRequest()->get("orderId") && $this->getRequest()->get("statusOrder") == 'pending_payment') {
die(json_encode([
'success' => false,
'data' => $resultRedirect,
]));
} elseif ($this->getRequest()->get("orderId") && $this->getRequest()->get("status") == 'pending') {

$orderId = $this->getRequest()->get("orderId");

Expand All @@ -136,7 +147,10 @@ public function responseAction()
$this->_checkoutSession->setSuccess(true);
$resultRedirect->setUrl($this->url->getUrl('pago/payment/success'));

return $resultRedirect;
die(json_encode([
'success' => true,
'data' => $resultRedirect,
]));
} elseif ($this->getRequest()->get("orderId") && $this->getRequest()->get("statusOrder") == 'cancelado_por_usuario') {

$orderId = $this->getRequest()->get("orderId");
Expand All @@ -157,7 +171,10 @@ public function responseAction()
}
$resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->url->getUrl('checkout/cart/'));
return $resultRedirect;
die(json_encode([
'success' => false,
'data' => $resultRedirect,
]));
} else {
$resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->url->getUrl('checkout/onepage/failure'));
Expand Down
Loading