From 781cc32a666ba30d81eaf00f2fb88c0f63efa3ca Mon Sep 17 00:00:00 2001 From: Lito Date: Thu, 30 Aug 2018 13:03:28 +0200 Subject: [PATCH] Updated private with protected scope. Fixed messages response as array --- src/Redsys/Tpv/Curl.php | 9 ++++--- src/Redsys/Tpv/Signature.php | 12 ++++----- src/Redsys/Tpv/Tpv.php | 49 +++++++++++++++++++++++------------- tests/Redsys/Tpv/TpvTest.php | 3 +-- 4 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/Redsys/Tpv/Curl.php b/src/Redsys/Tpv/Curl.php index a4f6a01..bd0314f 100644 --- a/src/Redsys/Tpv/Curl.php +++ b/src/Redsys/Tpv/Curl.php @@ -7,11 +7,11 @@ class Curl { - private $settings = array(); + protected $settings = array(); - private $connect; - private $html; - private $info; + protected $connect; + protected $html; + protected $info; public function __construct(array $settings) { @@ -129,6 +129,7 @@ public function setBase() } $base = parse_url($this->info['url']); + $this->settings['base'] = $base['scheme'].'://'.$base['host']; } diff --git a/src/Redsys/Tpv/Signature.php b/src/Redsys/Tpv/Signature.php index e6eefb8..822fdd4 100644 --- a/src/Redsys/Tpv/Signature.php +++ b/src/Redsys/Tpv/Signature.php @@ -35,7 +35,7 @@ public static function fromXML($xml, $key) return self::MAC256($xml, self::encryptKey($order[1], $key)); } - private static function calculate($prefix, array $fields, array $values, $key) + protected static function calculate($prefix, array $fields, array $values, $key) { foreach ($fields as $field) { if (!isset($values[$prefix.$field])) { @@ -48,7 +48,7 @@ private static function calculate($prefix, array $fields, array $values, $key) return self::MAC256(base64_encode(json_encode($values)), $key); } - private static function encrypt3DES($message, $key) + protected static function encrypt3DES($message, $key) { if (function_exists('openssl_encrypt')) { return self::encrypt3DESOpenSSL($message, $key); @@ -57,7 +57,7 @@ private static function encrypt3DES($message, $key) return self::encrypt3DESMcrypt($message, $key); } - private static function encrypt3DESOpenSSL($message, $key) + protected static function encrypt3DESOpenSSL($message, $key) { $l = ceil(strlen($message) / 8) * 8; $message = $message.str_repeat("\0", $l - strlen($message)); @@ -65,19 +65,19 @@ private static function encrypt3DESOpenSSL($message, $key) return substr(openssl_encrypt($message, 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, "\0\0\0\0\0\0\0\0"), 0, $l); } - private static function encrypt3DESMcrypt($message, $key) + protected static function encrypt3DESMcrypt($message, $key) { $iv = implode(array_map('chr', array(0, 0, 0, 0, 0, 0, 0, 0))); return mcrypt_encrypt(MCRYPT_3DES, $key, $message, MCRYPT_MODE_CBC, $iv); } - private static function encryptKey($order, $key) + protected static function encryptKey($order, $key) { return self::encrypt3DES($order, base64_decode($key)); } - private static function MAC256($string, $key) + protected static function MAC256($string, $key) { return base64_encode(hash_hmac('sha256', $string, $key, true)); } diff --git a/src/Redsys/Tpv/Tpv.php b/src/Redsys/Tpv/Tpv.php index b498d6e..fd986f3 100644 --- a/src/Redsys/Tpv/Tpv.php +++ b/src/Redsys/Tpv/Tpv.php @@ -8,7 +8,7 @@ class Tpv { - private $options = array( + protected $options = array( 'Version' => '0.1', 'Environment' => 'test', 'Currency' => '978', @@ -16,18 +16,24 @@ class Tpv 'ConsumerLanguage' => '001' ); - private $option_prefix = 'Ds_Merchant_'; + protected $option_prefix = 'Ds_Merchant_'; - private $o_required = array('Environment', 'Currency', 'Terminal', 'ConsumerLanguage', 'MerchantCode', 'Key', 'SignatureVersion', 'MerchantName', 'Titular'); - private $o_optional = array('UrlOK', 'UrlKO', 'TransactionType', 'MerchantURL', 'PayMethods'); + protected $o_required = array( + 'Environment', 'Currency', 'Terminal', 'ConsumerLanguage', + 'MerchantCode', 'Key', 'SignatureVersion', 'MerchantName', 'Titular' + ); + + protected $o_optional = array( + 'UrlOK', 'UrlKO', 'TransactionType', 'MerchantURL', 'PayMethods' + ); - private $environment = ''; - private $environments = array( + protected $environment = ''; + protected $environments = array( 'test' => 'https://sis-t.redsys.es:25443/sis', 'real' => 'https://sis.redsys.es/sis' ); - private $values = array(); + protected $values = array(); public function __construct(array $options) { @@ -179,7 +185,7 @@ public function sendXml(array $options) )); } - private function setXmlValues() + protected function setXmlValues() { $xml = array('DS_Version' => $this->options['Version']); @@ -190,7 +196,7 @@ private function setXmlValues() return $xml; } - private function xmlArray2string($xml) + protected function xmlArray2string($xml) { $doc = new DOMDocument(); @@ -249,7 +255,7 @@ public function checkTransactionXml(array $post) return array_merge($post, $data); } - private function setValueDefault(array $options, $option) + protected function setValueDefault(array $options, $option) { $code = $this->option_prefix.$option; @@ -262,7 +268,7 @@ private function setValueDefault(array $options, $option) return $this; } - private function setValues(array $options) + protected function setValues(array $options) { foreach ($options as $key => $value) { $key = $this->option_prefix.$key; @@ -338,14 +344,19 @@ public function checkTransaction(array $post) return array_merge($post, array_map('urldecode', $data)); } - private function checkTransactionError(array $data, $prefix) + public function getTransactionParameters(array $post) + { + return json_decode(base64_decode(strtr($post['Ds_MerchantParameters'], '-_', '+/')), true); + } + + protected function checkTransactionError(array $data, $prefix) { if ($error = (isset($data[$prefix.'ErrorCode']) ? $data[$prefix.'ErrorCode'] : false)) { - throw new Exception(Messages::getByCode($error) ?: '', (int)$error); + $this->throwErrorByCode($error); } } - private function checkTransactionResponse(array $data, $prefix) + protected function checkTransactionResponse(array $data, $prefix) { $response = isset($data[$prefix.'Response']) ? $data[$prefix.'Response'] : null; @@ -356,19 +367,21 @@ private function checkTransactionResponse(array $data, $prefix) $value = (int)$response; if (($value < 0) || (($value > 99) && ($value !== 900))) { - throw new Exception(Messages::getByCode($response) ?: '', $value); + $this->throwErrorByCode($response); } } - private function checkTransactionSignature($signature, $postSignature) + protected function checkTransactionSignature($signature, $postSignature) { if ($signature !== strtr($postSignature, '-_', '+/')) { throw new Exception(sprintf('Signature not valid (%s != %s)', $signature, $postSignature)); } } - public function getTransactionParameters(array $post) + protected function throwErrorByCode($code) { - return json_decode(base64_decode(strtr($post['Ds_MerchantParameters'], '-_', '+/')), true); + $message = Messages::getByCode($code); + + throw new Exception($message ? $message['message'] : '', (int)$code); } } diff --git a/tests/Redsys/Tpv/TpvTest.php b/tests/Redsys/Tpv/TpvTest.php index 78d9619..64b95fc 100644 --- a/tests/Redsys/Tpv/TpvTest.php +++ b/tests/Redsys/Tpv/TpvTest.php @@ -11,9 +11,8 @@ class TpvTest extends PHPUnit_Framework_TestCase { public function testInstance() { - $config = require (realpath(__DIR__.'/../../..').'/config.php'); + $tpv = new Tpv(require (__DIR__.'/../../../config.php')); - $tpv = new Tpv($config); $this->assertInstanceOf('Redsys\Tpv\Tpv', $tpv); return $tpv;