Parse body correctly and test fill

This commit is contained in:
Juan Pablo Vial
2025-05-13 20:03:20 -04:00
parent 308a84a448
commit c0024a4a63

View File

@ -9,6 +9,7 @@ use Incoviba\Controller\API\withJson;
use Incoviba\Exception\InvalidResult;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Service;
use Incoviba\Model;
class Toku extends Controller
{
@ -18,6 +19,9 @@ class Toku extends Controller
Service\Venta\MediosPago\Toku $tokuService, Service\Venta $ventaService, int $venta_id): ResponseInterface
{
$input = $request->getParsedBody();
if ($input === null) {
$input = json_decode($request->getBody()->getContents(), true);
}
$output = [
'input' => $input,
'venta_id' => $venta_id,
@ -60,4 +64,35 @@ class Toku extends Controller
return $responseFactory->createResponse($exception->getCode(), $message);
}
}
public function test(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta $ventaService, Service\Queue $queueService): ResponseInterface
{
$output = [
'success' => false,
'queue' => []
];
try {
$ventas = $ventaService->getAllWithCuotaPending();
foreach ($ventas as $venta) {
$cuotas = $venta->formaPago()->pie->cuotas();
if (count($cuotas) === 0) {
continue;
}
$queueData = [
'type' => 'request',
'url' => "/api/external/toku/cuotas/{$venta->id}",
'method' => 'post',
'body' => ['cuotas' => array_map(function(Model\Venta\Cuota $cuota) {return $cuota->id;}, $cuotas)]
];
$output['queue'] []= $queueData;
$queueService->enqueue($queueData);
}
$output['success'] = true;
} catch (Read) {
$response->withStatus(404);
return $response;
}
return $this->withJson($response, $output);
}
}