<?php
namespace Aviatur\PaymentBundle\Services;
use Aviatur\GeneralBundle\Entity\Parameter;
use Aviatur\GeneralBundle\Services\AviaturLogSave;
use Doctrine\Persistence\ManagerRegistry;
class TokenizerService
{
private \Doctrine\Persistence\ManagerRegistry $managerRegistry;
private \Aviatur\GeneralBundle\Services\AviaturLogSave $aviaturLogSave;
private $env;
private $tokenizerService;
private $em;
public function __construct(ManagerRegistry $managerRegistry, AviaturLogSave $aviaturLogSave, $tokenizerService, $env)
{
$this->managerRegistry = $managerRegistry;
$this->aviaturLogSave = $aviaturLogSave;
$this->env = $env;
$this->tokenizerService = $tokenizerService;
$this->tokenizerIsActive = true;
$environment = $this->env;
$this->em = $this->managerRegistry->getManager();
$aviatur_service_token = $this->em->getRepository(\Aviatur\GeneralBundle\Entity\Parameter::class)->findOneByName('aviatur_service_token');
$this->retryService = 0;
if (empty($aviatur_service_token) || !$this->tokenizerService || false == $this->tokenizerService) {
$this->serviceToken = 0;
} else {
$this->serviceToken = $aviatur_service_token->getValue();
$description = $aviatur_service_token->getDescription();
$this->url = json_decode($description, true)['endPoints'][$environment];
$this->parameters = json_decode($description, true)['parameters'];
}
}
public function getToken($cardNumber)
{
if (!$this->serviceToken) {
return $cardNumber;
}
$data = ['card_number' => $cardNumber];
$token = $this->TokenApi($this->url['authenticate'], $data, 'authenticate');
return (empty($token) || isset($token['error'])) ? $cardNumber : $token;
}
private function TokenApi($url, $data, $method)
{
$headersArray = null;
$postFields = null;
$response = null;
if ($this->retryService > 1) {
$response['error'] = 'El servicio de token no responde';
} else {
$curl = curl_init();
switch ($method) {
case 'authenticate':
$headersArray = ['Content-Type: application/x-www-form-urlencoded'];
$postFields = 'Username='.$this->parameters['username'].'&Password='.$this->parameters['password'].'';
break;
case 'TokenCard':
$headersArray = [
'Content-Type: application/json',
'Authorization: Bearer '.$data['token'],
];
$postFields = json_encode($data);
break;
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headersArray);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$execute = curl_exec($curl);
$response = $execute;
if (curl_error($curl)) {
$error_msg = curl_error($curl);
$response['error'] = $error_msg;
++$this->retryService;
}
//$this->aviaturLogSave->logSave(print_r($response, true), 'Tokenizer', $method.'_RS');
switch ($method) {
case 'authenticate':
$dataToken = [
'strSecHashVal' => $this->parameters['strSecHashVal'],
'strMODULECDK_ext' => $this->parameters['strMODULECDK_ext'],
'strFININSTCK' => $this->parameters['strFININSTCK'],
'strUSERCODEK' => $this->parameters['strUSERCODEK'],
'strUSRPASSW1' => $this->parameters['strUSRPASSW1'],
'strTOKMOTCOD' => $this->parameters['strTOKMOTCOD'],
'strUALTEXTVF' => $this->parameters['strUALTEXTVF'],
'strCCARDNBRK_clr' => $data['card_number'],
'strCCARDNBRK_cip' => '',
'card_number' => $data['card_number'],
];
$dataToken['token'] = str_replace('"', '', $execute);
$response = $this->TokenApi($this->url['TokenCard'], $dataToken, 'TokenCard');
break;
case 'TokenCard':
if (isset($response['error'])) {
$response = $this->TokenApi($this->url['authenticate'], $data, 'authenticate');
} else {
$response = $this->json_validate($execute);
if (isset($response['value'])) {
$response = $response['value'];
} else {
$response = $data['card_number'];
}
}
break;
}
curl_close($curl);
}
return $response;
}
public function getTokenBridge()
{
$aviatur_bridge_service_token = $this->em->getRepository(\Aviatur\GeneralBundle\Entity\Parameter::class)->findOneByName('aviatur_bridge_service_token');
$url = $aviatur_bridge_service_token->getValue();
$data = [
'Username' => 'OpenTrip.COM',
'Password' => '0x5AF1EE58F7625C483336B868E3599FA794A63687',
'AppName' => 'PASARELA',
];
// $data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
curl_close($ch);
// $response = file_get_contents($url, null, stream_context_create(array(
// 'http' => array(
// 'method' => 'POST',
// 'header' => 'Content-Type: application/json' . "\r\n"
// . 'Content-Length: ' . strlen($data_string) . "\r\n",
// 'content' => $data_string,
// ),
// )));
$this->aviaturLogSave->logSave(print_r($response, true), 'TokenBridge', 'info', '');
return json_decode($response, true);
}
public function json_validate($string)
{
// decode the JSON data
$result = json_decode($string, true);
// switch and check possible JSON errors
switch (json_last_error()) {
case JSON_ERROR_NONE:
$error = ''; // JSON is valid // No error has occurred
break;
case JSON_ERROR_DEPTH:
$error = 'The maximum stack depth has been exceeded.';
break;
case JSON_ERROR_STATE_MISMATCH:
$error = 'Invalid or malformed JSON.';
break;
case JSON_ERROR_CTRL_CHAR:
$error = 'Control character error, possibly incorrectly encoded.';
break;
case JSON_ERROR_SYNTAX:
$error = 'Syntax error, malformed JSON.';
break;
// PHP >= 5.3.3
case JSON_ERROR_UTF8:
$error = 'Malformed UTF-8 characters, possibly incorrectly encoded.';
break;
// PHP >= 5.5.0
case JSON_ERROR_RECURSION:
$error = 'One or more recursive references in the value to be encoded.';
break;
// PHP >= 5.5.0
case JSON_ERROR_INF_OR_NAN:
$error = 'One or more NAN or INF values in the value to be encoded.';
break;
case JSON_ERROR_UNSUPPORTED_TYPE:
$error = 'A value of a type that cannot be encoded was given.';
break;
default:
$error = 'Unknown JSON error occured.';
break;
}
if ('' !== $error) {
$result['error'] = $error;
}
// everything is OK
return $result;
}
}