-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from alma/feature/ecom-1823-magento1-add-hmac-…
…verification-on-ipn feature : Add hmac validation to IPN controller
- Loading branch information
Showing
3 changed files
with
173 additions
and
3 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
src/app/code/community/Alma/Installments/Helper/Payment.php
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,112 @@ | ||
<?php | ||
/** | ||
* 2024 Alma / Nabla SAS | ||
* | ||
* THE MIT LICENSE | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and | ||
* to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the | ||
* Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | ||
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | ||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
* IN THE SOFTWARE. | ||
* | ||
* @author Alma / Nabla SAS <[email protected]> | ||
* @copyright 2024 Alma / Nabla SAS | ||
* @license https://opensource.org/licenses/MIT The MIT License | ||
* | ||
*/ | ||
|
||
/** | ||
* Alma_Installments_Helper_Payment | ||
* Payment helper class | ||
*/ | ||
class Alma_Installments_Helper_Payment extends Mage_Core_Helper_Abstract | ||
{ | ||
const HEADER_SIGNATURE_KEY = 'X-Alma-Signature'; | ||
|
||
/** | ||
* @var AlmaLogger | ||
*/ | ||
private $logger; | ||
|
||
public function __construct() | ||
{ | ||
$this->logger = Mage::helper('alma/Logger')->getLogger(); | ||
} | ||
|
||
/** | ||
* Get header signature | ||
* | ||
* @return string | ||
* @throws Alma_installments_Model_Exceptions_PaymentException | ||
*/ | ||
private function getHeaderSignature() | ||
{ | ||
$signature = null; | ||
|
||
try { | ||
$signature = $this->_getRequest()->getHeader(self::HEADER_SIGNATURE_KEY); | ||
} catch (Zend_Controller_Request_Exception $e) { | ||
$this->logger->error('Error retrieving header signature: ', [$e->getMessage()]); | ||
} finally { | ||
if (!$signature) { | ||
throw new Alma_installments_Model_Exceptions_PaymentException('Header signature not found'); | ||
}; | ||
return $signature; | ||
} | ||
} | ||
|
||
/** | ||
* Check signature with payment id and API key with error handling | ||
* | ||
* @return void | ||
* @throws Alma_installments_Model_Exceptions_PaymentException | ||
*/ | ||
public function checkSignature($almaPaymentId) | ||
{ | ||
if (!$this->isHmacValidated($almaPaymentId, $this->getApiKey(), $this->getHeaderSignature())) { | ||
throw new Alma_installments_Model_Exceptions_PaymentException('Invalid signature'); | ||
} | ||
} | ||
|
||
/** | ||
* Validate hmac signature | ||
* | ||
* @param string $almaPaymentId | ||
* @param string $apiKey | ||
* @param string $signature | ||
* @return bool | ||
*/ | ||
private function isHmacValidated($almaPaymentId, $apiKey, $signature) | ||
{ | ||
return is_string($almaPaymentId) && | ||
is_string($apiKey) && | ||
hash_hmac('sha256', $almaPaymentId, $apiKey) === $signature; | ||
} | ||
|
||
/** | ||
* Get Current API key in config with error handling | ||
* | ||
* @return string | ||
* @throws Alma_installments_Model_Exceptions_PaymentException | ||
*/ | ||
private function getApiKey() | ||
{ | ||
/** @var Alma_Installments_Helper_Config $configHelper */ | ||
$configHelper = Mage::helper('alma/Config'); | ||
$apiKey = $configHelper->getActiveAPIKey(); | ||
if (!$apiKey) { | ||
throw new Alma_installments_Model_Exceptions_PaymentException('API key not found'); | ||
} | ||
return $apiKey; | ||
} | ||
|
||
|
||
} |
6 changes: 6 additions & 0 deletions
6
src/app/code/community/Alma/Installments/Model/Exceptions/PaymentException.php
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,6 @@ | ||
<?php | ||
|
||
class Alma_installments_Model_Exceptions_PaymentException extends \Mage_Core_Exception | ||
{ | ||
|
||
} |
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