From 37877cb5ff6d70fcea1dab2ad397d607bdf38ee0 Mon Sep 17 00:00:00 2001 From: Lito Date: Thu, 22 Mar 2018 17:16:59 +0100 Subject: [PATCH] Added credit/debit cards fields support. Added SHA256 support (not default). --- src/Ceca/Tpv/Tpv.php | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/Ceca/Tpv/Tpv.php b/src/Ceca/Tpv/Tpv.php index 1a802ad..d62b0e0 100644 --- a/src/Ceca/Tpv/Tpv.php +++ b/src/Ceca/Tpv/Tpv.php @@ -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( @@ -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); @@ -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); @@ -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() @@ -206,13 +222,13 @@ 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'); @@ -220,13 +236,13 @@ public function checkTransaction(array $post) foreach ($fields as $field) { if (empty($post[$field])) { - throw new Exception(sprintf('Field %s is empty and is required to verify transaction')); + throw new Exception(sprintf('Field %s 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'])); @@ -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;