-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added HEADER_SIGNATURE, $response, $logger. Implemented setResponse, …
…returnResponse. Fixed returnResponse. Added OrderInfo::returnFlag to return when webapi.
- Loading branch information
Returnless
committed
Nov 10, 2020
1 parent
c343f5b
commit a121122
Showing
8 changed files
with
308 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,5 @@ | |
"OSL-3.0", | ||
"AFL-3.0" | ||
], | ||
"version": "1.0.1" | ||
"version": "1.0.2" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters