Skip to content

Commit

Permalink
Merge pull request #18 from eusonlito/develop
Browse files Browse the repository at this point in the history
Added support to OpenSSL. Allow custom input keys and values.
  • Loading branch information
eusonlito authored Apr 18, 2017
2 parents cf5bb56 + 822bedc commit ec4baa8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
17 changes: 17 additions & 0 deletions src/Redsys/Tpv/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ private static function calculate($prefix, array $fields, array $values, $key)
}

private static function encrypt3DES($message, $key)
{
if (function_exists('openssl_encrypt')) {
return self::encrypt3DESOpenSSL($message, $key);
}

return self::encrypt3DESMcrypt($message, $key);
}

private 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)
{
$iv = implode(array_map('chr', array(0, 0, 0, 0, 0, 0, 0, 0)));

Expand Down
38 changes: 24 additions & 14 deletions src/Redsys/Tpv/Tpv.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,7 @@ public function setFormHiddens(array $options)
$this->setValueDefault($options, 'PayMethods');
$this->setValueDefault($options, 'Identifier');

$this->setValue($options, 'MerchantData');
$this->setValue($options, 'Order');
$this->setValue($options, 'ProductDescription');
$this->setValue($options, 'Amount');
$this->setValue($options, 'SumTotal');
$this->setValue($options, 'DateFrecuency');
$this->setValue($options, 'ChargeExpiryDate');
$this->setValues($options);

return $this;
}
Expand All @@ -139,18 +133,23 @@ public function getFormHiddens()
}

return $this->getInputHidden('SignatureVersion', $this->options['SignatureVersion'])
.$this->getInputHidden('MerchantParameters', $this->getMerchantParameters())
.$this->getInputHidden('MerchantParameters', $this->getMerchantParametersEncoded())
.$this->getInputHidden('Signature', $this->getValuesSignature());
}

private function getInputHidden($name, $value)
public function getInputHidden($name, $value)
{
return "\n".'<input type="hidden" name="Ds_'.$name.'" value="'.$value.'" />';
}

private function getMerchantParameters()
public function getMerchantParameters()
{
return base64_encode(json_encode($this->values));
return $this->values;
}

public function getMerchantParametersEncoded()
{
return base64_encode(json_encode($this->getMerchantParameters()));
}

public function sendXml(array $options)
Expand All @@ -167,9 +166,7 @@ public function sendXml(array $options)
$this->setValueDefault($options, 'Identifier');
$this->setValueDefault($options, 'DirectPayment');

$this->setValue($options, 'TransactionType');
$this->setValue($options, 'Order');
$this->setValue($options, 'Amount');
$this->setValues($options);

$Curl = new Curl(array(
'base' => $this->getPath('')
Expand Down Expand Up @@ -274,6 +271,19 @@ private function setValue(array $options, $option)
return $this;
}

private function setValues(array $options)
{
foreach ($options as $key => $value) {
$key = $this->option_prefix.$key;

if (!isset($this->values[$key])) {
$this->values[$key] = $value;
}
}

return $this;
}

public function getOrder($order)
{
if (preg_match('/^[0-9]+$/', $order)) {
Expand Down

0 comments on commit ec4baa8

Please sign in to comment.