Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
eusonlito committed Aug 31, 2018
2 parents bed7822 + 781cc32 commit 3dd57bc
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 48 deletions.
39 changes: 21 additions & 18 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/Redsys/Tpv/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -129,6 +129,7 @@ public function setBase()
}

$base = parse_url($this->info['url']);

$this->settings['base'] = $base['scheme'].'://'.$base['host'];
}

Expand Down
12 changes: 6 additions & 6 deletions src/Redsys/Tpv/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand All @@ -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);
Expand All @@ -57,27 +57,27 @@ 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));

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));
}
Expand Down
49 changes: 31 additions & 18 deletions src/Redsys/Tpv/Tpv.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,32 @@

class Tpv
{
private $options = array(
protected $options = array(
'Version' => '0.1',
'Environment' => 'test',
'Currency' => '978',
'Terminal' => '1',
'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)
{
Expand Down Expand Up @@ -179,7 +185,7 @@ public function sendXml(array $options)
));
}

private function setXmlValues()
protected function setXmlValues()
{
$xml = array('DS_Version' => $this->options['Version']);

Expand All @@ -190,7 +196,7 @@ private function setXmlValues()
return $xml;
}

private function xmlArray2string($xml)
protected function xmlArray2string($xml)
{
$doc = new DOMDocument();

Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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);
}
}
3 changes: 1 addition & 2 deletions tests/Redsys/Tpv/TpvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3dd57bc

Please sign in to comment.