Files
oficial/app/src/Controller/API/Ventas/MediosPago/Toku.php

150 lines
5.8 KiB
PHP
Raw Normal View History

2025-05-09 18:06:45 -04:00
<?php
namespace Incoviba\Controller\API\Ventas\MediosPago;
2025-05-27 19:30:08 -04:00
use Exception;
2025-06-03 12:04:53 -04:00
use Psr\Container\ContainerInterface;
2025-05-09 18:06:45 -04:00
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Ideal\Controller;
use Incoviba\Controller\API\withJson;
use Incoviba\Exception\InvalidResult;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Service;
2025-05-13 20:03:20 -04:00
use Incoviba\Model;
2025-05-09 18:06:45 -04:00
class Toku extends Controller
{
use withJson;
2025-05-10 12:38:14 -04:00
public function cuotas(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\MediosPago\Toku $tokuService, Service\Venta $ventaService, int $venta_id): ResponseInterface
2025-05-09 18:06:45 -04:00
{
$input = $request->getParsedBody();
2025-05-13 20:03:20 -04:00
if ($input === null) {
$input = json_decode($request->getBody()->getContents(), true);
}
2025-05-09 18:06:45 -04:00
$output = [
'input' => $input,
'venta_id' => $venta_id,
'cuotas' => [],
'success' => false,
'partial' => false,
];
try {
$venta = $ventaService->getById($venta_id);
2025-05-27 19:24:23 -04:00
$cuotas = $tokuService->sendCuotas($venta, $input['cuotas'] ?? []);
2025-05-09 18:06:45 -04:00
$output['cuotas'] = array_map(function($row) { return $row['cuota_id']; }, $cuotas);
2025-05-27 19:24:23 -04:00
if (count($cuotas) < count($input['cuotas'] ?? [])) {
2025-05-09 18:06:45 -04:00
$output['partial'] = true;
} else {
$output['success'] = true;
}
} catch (Read | InvalidResult $exception) {
$this->logger->debug($exception);
}
2025-05-09 18:06:45 -04:00
return $this->withJson($response, $output);
}
2025-05-10 12:38:14 -04:00
public function success(ServerRequestInterface $request, ResponseInterface $response,
ResponseFactoryInterface $responseFactory,
Service\Venta\MediosPago\Toku $tokuService): ResponseInterface
2025-05-09 18:06:45 -04:00
{
2025-06-09 12:44:42 -04:00
$input = $request->getParsedBody();
if ($input === null) {
$body = $request->getBody()->getContents();
$input = json_decode($body, true);
}
2025-05-29 19:15:17 -04:00
$this->logger->info('Toku payment success', ['input' => $input]);
2025-05-09 18:06:45 -04:00
try {
2025-05-12 19:46:09 -04:00
if ($tokuService->successEvent($input)) {
2025-05-15 16:05:03 -04:00
return $responseFactory->createResponse(200);
2025-05-09 18:06:45 -04:00
}
2025-05-19 14:02:55 -04:00
$this->logger->warning("Could not update payment", ['input' => $input]);
2025-05-09 18:06:45 -04:00
return $responseFactory->createResponse(409, 'Payment could not be updated');
} catch (InvalidResult $exception) {
2025-05-19 14:02:55 -04:00
$this->logger->warning($exception, [
'uri' => $request->getUri()->getPath(),
'body' => $body,
'input' => $input
]);
2025-05-09 18:06:45 -04:00
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);
}
}
2025-05-13 20:03:20 -04:00
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);
}
2025-05-27 18:17:56 -04:00
public function reset(ServerRequestInterface $request, ResponseInterface $response,
2025-06-03 12:04:53 -04:00
Service\Venta\MediosPago\Toku $tokuService, ContainerInterface $container): ResponseInterface
2025-05-27 18:17:56 -04:00
{
2025-06-03 12:04:53 -04:00
if (!$container->has('TOKU_ENV') or strtolower($container->get('TOKU_ENV')) !== 'sandbox') {
2025-06-03 19:58:36 -04:00
return $this->withJson($response, ['success' => false], 409);
2025-05-27 18:17:56 -04:00
}
2025-06-09 12:44:42 -04:00
$this->logger->info('Toku reset');
2025-05-27 19:30:08 -04:00
$input = $request->getParsedBody();
$output = [
'input' => $input,
'success' => false
];
try {
$tokuService->reset($input['skips'] ?? []);
$output['success'] = true;
2025-06-09 12:44:42 -04:00
} catch (Exception $exception) {
$this->logger->error($exception);
}
2025-05-27 18:17:56 -04:00
return $this->withJson($response, $output);
}
2025-05-27 19:24:23 -04:00
public function enqueue(ServerRequestInterface $request, ResponseInterface $response,
Service\Queue $queueService, Service\Venta\MediosPago\Toku $tokuService): ResponseInterface
{
2025-05-29 17:44:30 -04:00
$body = $request->getBody()->getContents();
2025-05-29 19:06:54 -04:00
$input = json_decode($body, true);
2025-05-27 19:24:23 -04:00
$output = [
'input' => $input,
'success' => false
];
$queue = $tokuService->queue($input['venta_ids'] ?? []);
if (count($queue) > 0) {
foreach ($queue as $job) {
$queueService->enqueue($job);
}
$output['success'] = true;
}
return $this->withJson($response, $output);
}
2025-05-09 18:06:45 -04:00
}