Validate with service

This commit is contained in:
Juan Pablo Vial
2025-05-29 19:43:06 -04:00
parent b4159d1417
commit f399eb8d47
3 changed files with 12 additions and 1 deletions

View File

@ -30,7 +30,7 @@ return [
'externalPaths' => [
'/api/external' => [
'/toku/success' => [
'header' => 'x-api-key',
'validator' => Incoviba\Service\Venta\MediosPago\Toku::class,
'token' => $_ENV['TOKU_TOKEN']
]
],

View File

@ -86,6 +86,10 @@ class API
protected function validateExternalKey(ServerRequestInterface $request, $basePath, $subPath): bool
{
$data = $this->externalPaths[$basePath][$subPath];
if (isset($data['validator'])) {
$method = [$data['validator'], 'validateToken'];
return $method($request, $data['token']);
}
if (isset($data['header']) and $request->hasHeader($data['header'])) {
$token = $request->getHeaderLine($data['header']);
if ($token === $this->externalPaths[$basePath][$subPath]['token']) {

View File

@ -2,6 +2,7 @@
namespace Incoviba\Service\Venta\MediosPago;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Service\Venta\MediosPago\Toku\{Customer,Subscription,Invoice};
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement\Exception\EmptyResponse;
@ -393,4 +394,10 @@ class Toku extends Ideal\Service
$data['date'] = $data['transaction_date'];
return $data;
}
public static function validateToken(ServerRequestInterface $request, string $token): bool
{
$tokenHeader = json_decode($request->getHeaderLine('token'));
return strtolower($tokenHeader->header) === 'x-api-key' and $tokenHeader->token === $token;
}
}