Controlador Toku

This commit is contained in:
Juan Pablo Vial
2025-05-09 18:06:45 -04:00
parent 59ecb6cc79
commit c95e74d574

View File

@ -0,0 +1,64 @@
<?php
namespace Incoviba\Controller\API\Ventas\MediosPago;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement\Exception\EmptyResponse;
use Incoviba\Common\Ideal\Controller;
use Incoviba\Controller\API\withJson;
use Incoviba\Exception\InvalidResult;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Service;
class Toku extends Controller
{
use withJson;
public function cuotas(ServerRequestInterface $request, ResponseInterface $response,
Service\MediosPago\Toku $tokuService, Service\Venta $ventaService, int $venta_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => $input,
'venta_id' => $venta_id,
'cuotas' => [],
'success' => false,
'partial' => false,
];
try {
$venta = $ventaService->getById($venta_id);
$cuotas = $tokuService->sendCuotas($venta, $input['cuotas']);
$output['cuotas'] = array_map(function($row) { return $row['cuota_id']; }, $cuotas);
if (count($cuotas) < count($input['cuotas'])) {
$output['partial'] = true;
} else {
$output['success'] = true;
}
} catch (Read | InvalidResult) {}
return $this->withJson($response, $output);
}
public function success(ServerRequestInterface $request, ResponseInterface $response,
ResponseFactoryInterface $responseFactory,
Service\MediosPago\Toku $tokuService): ResponseInterface
{
$body = $request->getBody();
$input = json_decode($body->getContents(), true);
try {
if ($tokuService->updatePago($input['payment_intent'])) {
return $responseFactory->createResponse(204);
}
return $responseFactory->createResponse(409, 'Payment could not be updated');
} catch (InvalidResult $exception) {
$this->logger->warning($exception);
if (str_contains($exception->getMessage(), 'Customer')) {
$message = 'Customer not found';
} elseif (str_contains($exception->getMessage(), 'Invoice')) {
$message = 'Invoice not found';
} else {
$message = $exception->getMessage();
}
return $responseFactory->createResponse($exception->getCode(), $message);
}
}
}