Skip to content

Commit

Permalink
PHP8 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
birkof authored and birkof committed Apr 18, 2022
1 parent dbf136e commit 3c3b955
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 36 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# netopia-mobilpay
# NETOPIA Payments API

Mirroring [MobilePay](https://github.com/mobilpay/PHP_CARD) library with composer PSR-0 autoloading capability.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"email": "birkof@gmail.com"
}
],
"require": {},
"require": {
"ext-dom": "*",
"ext-openssl": "*"
},
"autoload": {
"psr-4" : {
"Mobilpay\\Payment\\" : "src/Mobilpay/Payment/"
Expand Down
2 changes: 1 addition & 1 deletion src/Mobilpay/Global.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static function isValidMsisdn($param_msisdn)
{
$pattern = '/^(4)?07[0-9]{8,8}$/';

return (preg_match($pattern, $param_msisdn) != 0 ? true : false);
return preg_match($pattern, $param_msisdn) != 0;
}

public static function unreferrencedVariable($variable)
Expand Down
7 changes: 4 additions & 3 deletions src/Mobilpay/Payment/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Request
*/
public $m_params = [];

public function builParametersList()
public function buildParametersList()
{
if (is_null($this->m_signature) || /*is_null($this->m_service) || */
is_null($this->m_tran_id) || is_null($this->m_timestamp)) {
Expand Down Expand Up @@ -164,14 +164,15 @@ public function builParametersList()
*/
public function buildAccessParameters($public_key, &$env_key, &$enc_data)
{
$params = $this->builParametersList();
$params = $this->buildParametersList();
if (is_null($params)) {
return false;
}
$src_data = MobilpayGlobal::buildQueryString($params);
$enc_data = '';
$env_keys = [];
$result = openssl_seal($src_data, $enc_data, $env_keys, [$public_key]);
$cipher_algo = 'RC4';
$result = openssl_seal($src_data, $enc_data, $env_keys, [$public_key], $cipher_algo);
if ($result === false) {
$env_key = null;
$enc_data = null;
Expand Down
17 changes: 13 additions & 4 deletions src/Mobilpay/Payment/Request/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Mobilpay\Payment\Request;

use DOMDocument;
use DOMElement;
use Exception;
use Mobilpay\Payment\Invoice;

/**
Expand All @@ -24,14 +27,17 @@ function __construct()
$this->type = self::PAYMENT_TYPE_CARD;
}

protected function _loadFromXml(\DOMElement $elem)
/**
* @throws Exception
*/
protected function _loadFromXml(DOMElement $elem)
{
parent::_parseFromXml($elem);

//card request specific data
$elems = $elem->getElementsByTagName('invoice');
if ($elems->length != 1) {
throw new \Exception(
throw new Exception(
'Mobilpay\Payment\Request\Card::loadFromXml failed; invoice element is missing',
self::ERROR_LOAD_FROM_XML_ORDER_INVOICE_ELEM_MISSING
);
Expand All @@ -42,18 +48,21 @@ protected function _loadFromXml(\DOMElement $elem)
return $this;
}

/**
* @throws Exception
*/
protected function _prepare()
{
if (is_null($this->signature) || is_null(
$this->orderId
) || !($this->invoice instanceof Invoice)) {
throw new \Exception(
throw new Exception(
'One or more mandatory properties are invalid!',
self::ERROR_PREPARE_MANDATORY_PROPERTIES_UNSET
);
}

$this->_xmlDoc = new \DOMDocument('1.0', 'utf-8');
$this->_xmlDoc = new DOMDocument('1.0', 'utf-8');
$rootElem = $this->_xmlDoc->createElement('order');

//set payment type attribute
Expand Down
52 changes: 35 additions & 17 deletions src/Mobilpay/Payment/Request/RequestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Mobilpay\Payment\Request;

use DOMDocument;
use DOMNode;
use Exception;

/**
* Class RequestAbstract
* This class can be used for accessing mobilpay.ro payment interface for your configured online services
Expand Down Expand Up @@ -128,7 +132,7 @@ abstract protected function _loadFromXml(\DOMElement $elem);
static public function factory($data)
{
$objPmReq = null;
$xmlDoc = new \DOMDocument();
$xmlDoc = new DOMDocument();
if (@$xmlDoc->loadXML($data) === true) {
//try to create payment request from xml
$objPmReq = self::_factoryFromXml($xmlDoc);
Expand All @@ -142,6 +146,9 @@ static public function factory($data)
return $objPmReq;
}

/**
* @throws Exception
*/
static public function factoryFromEncrypted($envKey, $encData, $privateKeyFilePath, $privateKeyPassword = null)
{
$privateKey = null;
Expand All @@ -160,43 +167,47 @@ static public function factoryFromEncrypted($envKey, $encData, $privateKeyFilePa
);
}
if ($privateKey === false) {
throw new \Exception('Error loading private key', self::ERROR_CONFIRM_LOAD_PRIVATE_KEY);
throw new Exception('Error loading private key', self::ERROR_CONFIRM_LOAD_PRIVATE_KEY);
}

$srcData = base64_decode($encData);

if ($srcData === false) {
@openssl_free_key($privateKey);
throw new \Exception('Failed decoding data', self::ERROR_CONFIRM_FAILED_DECODING_DATA);
throw new Exception('Failed decoding data', self::ERROR_CONFIRM_FAILED_DECODING_DATA);
}

$srcEnvKey = base64_decode($envKey);
if ($srcEnvKey === false) {
throw new \Exception('Failed decoding envelope key', self::ERROR_CONFIRM_FAILED_DECODING_ENVELOPE_KEY);
throw new Exception('Failed decoding envelope key', self::ERROR_CONFIRM_FAILED_DECODING_ENVELOPE_KEY);
}

$data = null;
$result = @openssl_open($srcData, $data, $srcEnvKey, $privateKey);
$cipher_algo = 'RC4';
$result = @openssl_open($srcData, $data, $srcEnvKey, $privateKey, $cipher_algo);
if ($result === false) {
throw new \Exception('Failed decrypting data', self::ERROR_CONFIRM_FAILED_DECRYPT_DATA);
throw new Exception('Failed decrypting data', self::ERROR_CONFIRM_FAILED_DECRYPT_DATA);
}

return self::factory($data);
}

static protected function _factoryFromXml(\DOMDocument $xmlDoc)
/**
* @throws Exception
*/
static protected function _factoryFromXml(DOMDocument $xmlDoc)
{
$elems = $xmlDoc->getElementsByTagName('order');
if ($elems->length != 1) {
throw new \Exception(
throw new Exception(
'factoryFromXml order element not found', self::ERROR_FACTORY_BY_XML_ORDER_ELEM_NOT_FOUND
);
}
$orderElem = $elems->item(0);

$attr = $orderElem->attributes->getNamedItem('type');
if ($attr == null || strlen($attr->nodeValue) == 0) {
throw new \Exception(
throw new Exception(
'factoryFromXml invalid payment request type='.$attr->nodeValue,
self::ERROR_FACTORY_BY_XML_ORDER_TYPE_ATTR_NOT_FOUND
);
Expand All @@ -209,11 +220,10 @@ static protected function _factoryFromXml(\DOMDocument $xmlDoc)
$objPmReq = new Sms();
break;
default:
throw new \Exception(
throw new Exception(
'factoryFromXml invalid payment request type='.$attr->nodeValue,
self::ERROR_FACTORY_BY_XML_INVALID_TYPE
);
break;
}
$objPmReq->_loadFromXml($orderElem);

Expand All @@ -240,11 +250,14 @@ public function getRequestInfo()
return $this->_objRequestInfo;
}

protected function _parseFromXml(\DOMNode $elem)
/**
* @throws Exception
*/
protected function _parseFromXml(DOMNode $elem)
{
$xmlAttr = $elem->attributes->getNamedItem('id');
if ($xmlAttr == null || strlen((string)$xmlAttr->nodeValue) == 0) {
throw new \Exception(
throw new Exception(
'Mobilpay\Payment\Request\Sms::_parseFromXml failed: empty order id',
self::ERROR_LOAD_FROM_XML_ORDER_ID_ATTR_MISSING
);
Expand All @@ -253,7 +266,7 @@ protected function _parseFromXml(\DOMNode $elem)

$elems = $elem->getElementsByTagName('signature');
if ($elems->length != 1) {
throw new \Exception(
throw new Exception(
'Mobilpay\Payment\Request\Sms::loadFromXml failed: signature is missing',
self::ERROR_LOAD_FROM_XML_SIGNATURE_ELEM_MISSING
);
Expand Down Expand Up @@ -304,6 +317,9 @@ protected function _parseFromXml(\DOMNode $elem)
}
}

/**
* @throws Exception
*/
public function encrypt($x509FilePath)
{
$this->_prepare();
Expand All @@ -318,21 +334,23 @@ public function encrypt($x509FilePath)
while (($errorString = openssl_error_string())) {
$errorMessage .= $errorString."\n";
}
throw new \Exception($errorMessage, self::ERROR_LOAD_X509_CERTIFICATE);
throw new Exception($errorMessage, self::ERROR_LOAD_X509_CERTIFICATE);
}
$srcData = $this->_xmlDoc->saveXML();
$publicKeys = [$publicKey];
$encData = null;
$envKeys = null;
$result = openssl_seal($srcData, $encData, $envKeys, $publicKeys);
$cipher_algo = 'RC4';
$result = openssl_seal($srcData, $encData, $envKeys, $publicKeys, $cipher_algo);

if ($result === false) {
$this->outEncData = null;
$this->outEnvKey = null;
$errorMessage = "Error while encrypting data! Reason:";
while (($errorString = openssl_error_string())) {
$errorMessage .= $errorString."\n";
}
throw new \Exception($errorMessage, self::ERROR_ENCRYPT_DATA);
throw new Exception($errorMessage, self::ERROR_ENCRYPT_DATA);
}

$this->outEncData = base64_encode($encData);
Expand Down
26 changes: 17 additions & 9 deletions src/Mobilpay/Payment/Request/Sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Mobilpay\Payment\Request;

use DOMDocument;
use DOMElement;
use Exception;

/**
* Class Sms
* This class can be used for accessing mobilpay.ro payment interface for your configured online services
Expand All @@ -27,14 +31,17 @@ function __construct()
$this->type = self::PAYMENT_TYPE_SMS;
}

protected function _loadFromXml(\DOMElement $elem)
/**
* @throws Exception
*/
protected function _loadFromXml(DOMElement $elem)
{
parent::_parseFromXml($elem);

//SMS request specific data
$elems = $elem->getElementsByTagName('service');
if ($elems->length != 1) {
throw new \Exception(
throw new Exception(
'Mobilpay\Payment\Request\Sms::loadFromXml failed: service is missing',
self::ERROR_LOAD_FROM_XML_SERVICE_ELEM_MISSING
);
Expand All @@ -47,11 +54,12 @@ protected function _loadFromXml(\DOMElement $elem)
$this->msisdn = $elems->item(0)->nodeValue;
}

$elem = $elem;

return $this;
}

/**
* @throws Exception
*/
protected function _loadFromQueryString($queryString)
{
$parameters = explode('&', $queryString);
Expand All @@ -62,21 +70,21 @@ protected function _loadFromQueryString($queryString)
}

if (!isset($reqParams['signature'])) {
throw new \Exception(
throw new Exception(
'Mobilpay\Payment\Request\Sms::loadFromQueryString failed: signature is missing',
self::ERROR_LOAD_FROM_XML_SIGNATURE_ELEM_MISSING
);
}
$this->signature = $reqParams['signature'];
if (!isset($reqParams['service'])) {
throw new \Exception(
throw new Exception(
'Mobilpay\Payment\Request\Sms::loadFromQueryString failed: service is missing',
self::ERROR_LOAD_FROM_XML_SERVICE_ELEM_MISSING
);
}
$this->service = $reqParams['service'];
if (!isset($reqParams['tran_id'])) {
throw new \Exception(
throw new Exception(
'Mobilpay\Payment\Request\Sms::loadFromQueryString failed: empty order id',
self::ERROR_LOAD_FROM_XML_ORDER_ID_ATTR_MISSING
);
Expand Down Expand Up @@ -107,13 +115,13 @@ protected function _loadFromQueryString($queryString)
protected function _prepare()
{
if (is_null($this->signature) || is_null($this->service) || is_null($this->orderId)) {
throw new \Exception(
throw new Exception(
'One or more mandatory properties are invalid!',
self::ERROR_PREPARE_MANDATORY_PROPERTIES_UNSET
);
}

$this->_xmlDoc = new \DOMDocument('1.0', 'utf-8');
$this->_xmlDoc = new DOMDocument('1.0', 'utf-8');
$rootElem = $this->_xmlDoc->createElement('order');

//set payment type attribute
Expand Down

0 comments on commit 3c3b955

Please sign in to comment.