Skip to content

Commit

Permalink
Fix PHP 8 issue and new metrics system added
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarciajovel committed Jan 30, 2023
1 parent e609d74 commit b218d26
Showing 1 changed file with 67 additions and 11 deletions.
78 changes: 67 additions & 11 deletions Observer/SaveConfigObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Transbank\Webpay\Observer;

use GuzzleHttp\Client;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\App\RequestInterface;
Expand All @@ -25,23 +26,78 @@ public function __construct (

public function execute(EventObserver $observer)
{
$this->_logger->info('Invoice Observer');
try {

$params = $this->request->getParam('groups');
$orderStatus = $params['transbank_webpay']['groups']['general_parameters']['fields']['payment_successful_status']['value'];
$params = $this->request->getParam('groups');
$orderStatus = $params['transbank_webpay']['groups']['general_parameters']['fields']['payment_successful_status']['value'];

if ($orderStatus !== 'processing') {
$value = 'default';
$this->configWriter->save('payment/transbank_webpay/general_parameters/invoice_settings', $value);
$this->sendMetrics($params);

if ($orderStatus !== 'processing') {
$value = 'default';
$this->configWriter->save('payment/transbank_webpay/general_parameters/invoice_settings', $value);
}

$oneclickOrderStatus = isset($params['transbank_oneclick']['groups']['general_parameters']['fields']['payment_successful_status']['value']) ? : 0;

if ($oneclickOrderStatus !== 'processing') {
$value = 'default';
$this->configWriter->save('payment/transbank_oneclick/general_parameters/invoice_settings', $value);
}

return $this;
} catch (\ErrorException $e) {
$this->_logger->critical($e);
}
}

public function sendMetrics($params) {
try {
$this->_logger->info('Sending Metric');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface');
$moduleInfo = $objectManager->get('Magento\Framework\Module\ModuleList')->getOne('Transbank_Webpay'); // SR_Learning is module name
$pluginVersion = $moduleInfo['setup_version'];

$webpayEnviroment = $params['transbank_webpay']['groups']['security']['fields']['environment']['value'];
$webpayCommerceCode = $params['transbank_webpay']['groups']['security']['fields']['commerce_code']['value'];

$oneclickEnviroment = $params['transbank_oneclick']['groups']['security']['fields']['environment']['value'];
$oneclickCommerceCode = $params['transbank_oneclick']['groups']['security']['fields']['commerce_code']['value'];

$webpayPayload = [
'commerceCode' => 1,
'plugin' => 'magento',
'environment' => $webpayEnviroment,
'product' => 'webpay',
'pluginVersion' => $pluginVersion,
'commerceCode' => $webpayCommerceCode,
'phpVersion' => phpversion(),
'metadata' => json_encode(['magentoVersion' => $productMetadata->getVersion()]),
];

$oneclickPayload = [
'commerceCode' => 1,
'plugin' => 'magento',
'environment' => $oneclickEnviroment,
'product' => 'oneclick',
'pluginVersion' => $pluginVersion,
'commerceCode' => $oneclickCommerceCode,
'phpVersion' => phpversion(),
'metadata' => json_encode(['magentoVersion' => $productMetadata->getVersion()]),
];

$client = new Client();

$oneclickOrderStatus = $params['transbank_oneclick']['groups']['general_parameters']['fields']['payment_successful_status']['value'];
$client->request('POST', 'https://tbk-app-y8unz.ondigitalocean.app/records/newRecord', ['form_params' => $webpayPayload]);

$client->request('POST', 'https://tbk-app-y8unz.ondigitalocean.app/records/newRecord', ['form_params' => $oneclickPayload]);

if ($oneclickOrderStatus !== 'processing') {
$value = 'default';
$this->configWriter->save('payment/transbank_oneclick/general_parameters/invoice_settings', $value);
$this->_logger->info('Saved');
} catch (\ErrorException $e) {
$this->_logger->critical($e);
}

return $this;
}
}

0 comments on commit b218d26

Please sign in to comment.