Skip to content

Commit

Permalink
Added HEADER_SIGNATURE, $response, $logger. Implemented setResponse, …
Browse files Browse the repository at this point in the history
…returnResponse. Fixed returnResponse. Added OrderInfo::returnFlag to return when webapi.
  • Loading branch information
Returnless committed Nov 10, 2020
1 parent c343f5b commit a121122
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 3 deletions.
169 changes: 169 additions & 0 deletions Controller/Order/Info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

namespace Returnless\Connector\Controller\Order;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Returnless\Connector\Model\Api\OrderInfo;
use Returnless\Connector\Model\Config;
use Psr\Log\LoggerInterface;

/**
* Class Index
*
* How to check logs: grep -rn 'returnless' var/log/system.log
*/
class Info extends Action
{
/**
* const HEADER_SIGNATURE
*/
const HEADER_SIGNATURE = 'Returnless-Signature';

/**
* @var Config
*/
protected $config;

/**
* @var OrderInfo
*/
protected $orderInfo;

/**
* @var array
*/
protected $response = [];

/**
* @var LoggerInterface
*
*/
protected $logger;

/**
* Info constructor.
*
* @param OrderInfo $orderInfo
* @param Config $config
* @param LoggerInterface $logger
* @param Context $context
*/
public function __construct(
OrderInfo $orderInfo,
Config $config,
LoggerInterface $logger,
Context $context
) {
$this->orderInfo = $orderInfo;
$this->config = $config;
$this->logger = $logger->withName('returnless');

return parent::__construct($context);
}

/**
* Execution method
*
* @return void
*/
public function execute()
{
$response = [];

// validate if Service is enabled
$enabled = $this->config->getEnabled();
if (empty($enabled)) {
$this->setResponse("Service is disabled!", 423, true)
->returnResponse();
}

// get Signature from Header
$returnlessSignature = $this->getSignatureFromHeader();
if (empty($returnlessSignature)) {
$this->setResponse("Can't find header: '" . self::HEADER_SIGNATURE . "'", 401, true)
->returnResponse();
}

// validate Signature
$incrementId = $this->getRequest()->getParam('increment_id');
$integrationApiPassword = $this->config->getApiPassword();
$hashedSignature = hash_hmac("sha256" , $incrementId , $integrationApiPassword);
if ($returnlessSignature != $hashedSignature) {
$this->setResponse("Signature is not valid!", 403, true)
->returnResponse();
}

// get Order Info
$response = $this->orderInfo
->setReturnFlag()
->getOrderInfoReturnless($incrementId);

// set Response and Return
$this->setResponse("Success!", 200)
->returnResponse($response);
}

/**
* Set Response message
*
* @param string $message
* @param int $code
* @param bool $debug
* @return $this
*/
protected function setResponse($message = '', $code = 0, $debug = false)
{
if ($debug) {
$this->logger->notice($message);
}

$this->response['return_code'] = $code;
$this->response['return_message'] = $message;

return $this;
}

/**
* Get Signature form Header
*
* @return bool|mixed
*/
protected function getSignatureFromHeader()
{
$returnlessSignature = false;

if (empty($returnlessSignature)) {
$allheaders = getallheaders();
}

if (isset($allheaders[self::HEADER_SIGNATURE])) {
$returnlessSignature = $allheaders[self::HEADER_SIGNATURE];
}

return $returnlessSignature;
}

/**
* Apply Response Array
*
* @param $result
*/
protected function returnResponse($result = null)
{
header("Content-Type: application/json; charset=utf-8");

if (isset($result['result']) && !empty($result['result'])) {
$this->response['result'] = $result['result'];
}

if (isset($result['return_message']) && !empty($result['return_message'])) {
$this->response['return_message'] = $result['return_message'];
}

$response = json_encode($this->response);
print_r($response,false);

die();
}
}
23 changes: 22 additions & 1 deletion Model/Api/OrderInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
/**
* Interface OrderInfo
*
* How to check logs: grep -rn 'returnless' var/log/debug.log
* How to check logs: grep -rn 'returnless' var/log/system.log
*/
class OrderInfo implements OrderInfoInterface
{
/**
* @var bool
*/
protected $returnFlag = false;

/**
* @var LoggerInterface
*
Expand Down Expand Up @@ -129,9 +134,25 @@ public function getOrderInfoReturnless($incrementId)
$this->logger->debug($e->getMessage());
}

if ($this->returnFlag) {
return $response;
}

$this->returnResult($response);
}

/**
* This method provides an ability to return Response Data
*
* @return $this
*/
public function setReturnFlag()
{
$this->returnFlag = true;

return $this;
}

/**
* @param $id
* @return \Magento\Catalog\Api\Data\ProductInterface|mixed
Expand Down
68 changes: 68 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Returnless\Connector\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

/**
* Class Config
*/
class Config
{
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* const CONFIG_PATH_API_ENABLED
*/
const CONFIG_PATH_API_ENABLED = 'returnless_connector/general/enabled';

/**
* const CONFIG_PATH_API_PASSWORD
*/
const CONFIG_PATH_API_PASSWORD = 'returnless_connector/general/integration_api_password';

/**
* Config constructor.
*
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
ScopeConfigInterface $scopeConfig
) {
$this->scopeConfig = $scopeConfig;
}

/**
* @param null $store
* @return string
*/
public function getEnabled($store = null)
{
$enabled = (string)$this->scopeConfig->getValue(
self::CONFIG_PATH_API_ENABLED,
ScopeInterface::SCOPE_STORE,
$store
);

return $enabled;
}

/**
* @param null $store
* @return string
*/
public function getApiPassword($store = null)
{
$apiPassword = (string)$this->scopeConfig->getValue(
self::CONFIG_PATH_API_PASSWORD,
ScopeInterface::SCOPE_STORE,
$store
);

return $apiPassword;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"OSL-3.0",
"AFL-3.0"
],
"version": "1.0.1"
"version": "1.0.2"
}
26 changes: 26 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="returnless" translate="label" sortOrder="314">
<label>Returnless</label>
</tab>
<section id="returnless_connector" translate="label" type="text" sortOrder="3145" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Connector Api</label>
<tab>returnless</tab>
<resource>Returnless_Connector::returnless_connector_config</resource>
<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>General Config</label>
<field id="enabled" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="integration_api_password" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Integration Api Password</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
<comment><![CDATA[The Integration Api Password of Returnless Connector]]></comment>
</field>
</group>
</section>
</system>
</config>
12 changes: 12 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<returnless_connector>
<general>
<enabled>1</enabled>
<integration_api_password backend_model="Magento\Config\Model\Config\Backend\Encrypted" ><![CDATA[0:3:GYPxbV49zVR1PoaSzMfZ36rYjrnGQIrSrKI066/AQMlTRoiL7Xzdaoqp99CQpWqwxzkhj1DHrh4eJR0v]]></integration_api_password>
</general>
</returnless_connector>
</default>
</config>
9 changes: 9 additions & 0 deletions etc/frontend/routes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="returnless_connector" frontName="returnless_connector">
<module name="Returnless_Connector"/>
</route>
</router>
</config>
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Returnless_Connector" setup_version="1.0.1">
<module name="Returnless_Connector" setup_version="1.0.2">
<sequence>
<module name="Magento_Sales"/>
</sequence>
Expand Down

0 comments on commit a121122

Please sign in to comment.