Skip to content

Commit

Permalink
Added credit/debit cards fields support. Added SHA256 support (not de…
Browse files Browse the repository at this point in the history
…fault).
  • Loading branch information
eusonlito committed Mar 22, 2018
1 parent 41241d6 commit 37877cb
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/Ceca/Tpv/Tpv.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Tpv
);

private $o_required = array('Environment', 'ClaveCifrado', 'MerchantID', 'AcquirerBIN', 'TerminalID', 'TipoMoneda', 'Exponente', 'Cifrado', 'Pago_soportado');
private $o_optional = array('Idioma', 'Descripcion', 'URL_OK', 'URL_NOK', 'Tipo_operacion', 'Datos_operaciones');
private $o_optional = array('Idioma', 'Descripcion', 'URL_OK', 'URL_NOK', 'Tipo_operacion', 'Datos_operaciones', 'PAN', 'Caducidad', 'CVV2', 'Pago_elegido');

private $environment = '';
private $environments = array(
Expand Down Expand Up @@ -120,6 +120,10 @@ public function setFormHiddens(array $options)
$this->setValue($options, 'Tipo_operacion');
$this->setValue($options, 'Datos_operaciones');

if (!empty($options['PAN'])) {
$this->setCreditCardInputs($options);
}

$this->setValueLength('MerchantID', 9);
$this->setValueLength('AcquirerBIN', 10);
$this->setValueLength('TerminalID', 8);
Expand All @@ -133,6 +137,16 @@ public function setFormHiddens(array $options)
return $this;
}

private function setCreditCardInputs(array $options)
{
$options['Pago_elegido'] = 'SSL';

$this->setValue($options, 'PAN');
$this->setValue($options, 'Caducidad');
$this->setValue($options, 'CVV2');
$this->setValue($options, 'Pago_elegido');
}

private function setValueLength($key, $length)
{
$this->values[$key] = str_pad($this->values[$key], $length, '0', STR_PAD_LEFT);
Expand Down Expand Up @@ -186,11 +200,13 @@ public function getAmount($amount)
{
if (empty($amount)) {
return '000';
} elseif (preg_match('/[\.,]/', $amount)) {
}

if (preg_match('/[\.,]/', $amount)) {
return str_replace(array('.', ','), '', $amount);
} else {
return ($amount * 100);
}

return ($amount * 100);
}

public function getSignature()
Expand All @@ -206,27 +222,27 @@ public function getSignature()
$key .= $this->values[$field];
}

return sha1($this->options['ClaveCifrado'].$key);
return $this->makeHash($key);
}

public function checkTransaction(array $post)
{
if (empty($post) || empty($post['Firma'])) {
throw new Exception('_POST data is empty');
throw new Exception('POST data is empty');
}

$fields = array('MerchantID', 'AcquirerBIN', 'TerminalID', 'Num_operacion', 'Importe', 'TipoMoneda', 'Exponente', 'Referencia');
$key = '';

foreach ($fields as $field) {
if (empty($post[$field])) {
throw new Exception(sprintf('Field <strong>%s</strong> is empty and is required to verify transaction'));
throw new Exception(sprintf('Field <strong>%s</strong> is empty and is required to verify transaction', $field));
}

$key .= $post[$field];
}

$signature = sha1($this->options['ClaveCifrado'].$key);
$signature = $this->makeHash($key);

if ($signature !== $post['Firma']) {
throw new Exception(sprintf('Signature not valid (%s != %s)', $signature, $post['Firma']));
Expand All @@ -235,6 +251,17 @@ public function checkTransaction(array $post)
return $post['Firma'];
}

private function makeHash($message)
{
$message = $this->options['ClaveCifrado'].$message;

if ($this->options['Cifrado'] === 'SHA2') {
return hash('sha256', $message);
}

return sha1($message);
}

public function successCode()
{
return $this->success;
Expand Down

0 comments on commit 37877cb

Please sign in to comment.