132 lines
5.1 KiB
PHP
132 lines
5.1 KiB
PHP
<?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\Ideal\Controller;
|
|
use Incoviba\Controller\API\withJson;
|
|
use Incoviba\Exception\InvalidResult;
|
|
use Incoviba\Exception\ServiceAction\Read;
|
|
use Incoviba\Service;
|
|
use Incoviba\Model;
|
|
|
|
class Toku extends Controller
|
|
{
|
|
use withJson;
|
|
|
|
public function cuotas(ServerRequestInterface $request, ResponseInterface $response,
|
|
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,
|
|
'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 $exception) {
|
|
$this->logger->debug($exception);
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function success(ServerRequestInterface $request, ResponseInterface $response,
|
|
ResponseFactoryInterface $responseFactory,
|
|
Service\Venta\MediosPago\Toku $tokuService): ResponseInterface
|
|
{
|
|
$body = $request->getBody()->getContents();
|
|
$input = json_decode($body, true);
|
|
try {
|
|
if ($tokuService->successEvent($input)) {
|
|
return $responseFactory->createResponse(200);
|
|
}
|
|
$this->logger->warning("Could not update payment", ['input' => $input]);
|
|
return $responseFactory->createResponse(409, 'Payment could not be updated');
|
|
} catch (InvalidResult $exception) {
|
|
$this->logger->warning($exception, [
|
|
'uri' => $request->getUri()->getPath(),
|
|
'body' => $body,
|
|
'input' => $input
|
|
]);
|
|
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);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
public function reset(ServerRequestInterface $request, ResponseInterface $response,
|
|
Service\Venta\MediosPago\Toku $tokuService): ResponseInterface
|
|
{
|
|
if (!isset($_ENV['TOKU_ENV']) or strtolower($_ENV['TOKU_ENV']) !== 'sandbox') {
|
|
return $this->withJson($response);
|
|
}
|
|
$output = $tokuService->reset();
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function enqueue(ServerRequestInterface $request, ResponseInterface $response,
|
|
Service\Queue $queueService, Service\Venta\MediosPago\Toku $tokuService): ResponseInterface
|
|
{
|
|
$input = $request->getParsedBody();
|
|
$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);
|
|
}
|
|
}
|