Mas logging

This commit is contained in:
Juan Pablo Vial
2025-06-09 12:44:42 -04:00
parent f1ed9668fc
commit abb1ce7299
8 changed files with 140 additions and 30 deletions

View File

@ -49,8 +49,11 @@ class Toku extends Controller
ResponseFactoryInterface $responseFactory, ResponseFactoryInterface $responseFactory,
Service\Venta\MediosPago\Toku $tokuService): ResponseInterface Service\Venta\MediosPago\Toku $tokuService): ResponseInterface
{ {
$input = $request->getParsedBody();
if ($input === null) {
$body = $request->getBody()->getContents(); $body = $request->getBody()->getContents();
$input = json_decode($body, true); $input = json_decode($body, true);
}
$this->logger->info('Toku payment success', ['input' => $input]); $this->logger->info('Toku payment success', ['input' => $input]);
try { try {
if ($tokuService->successEvent($input)) { if ($tokuService->successEvent($input)) {
@ -111,6 +114,7 @@ class Toku extends Controller
if (!$container->has('TOKU_ENV') or strtolower($container->get('TOKU_ENV')) !== 'sandbox') { if (!$container->has('TOKU_ENV') or strtolower($container->get('TOKU_ENV')) !== 'sandbox') {
return $this->withJson($response, ['success' => false], 409); return $this->withJson($response, ['success' => false], 409);
} }
$this->logger->info('Toku reset');
$input = $request->getParsedBody(); $input = $request->getParsedBody();
$output = [ $output = [
'input' => $input, 'input' => $input,
@ -119,7 +123,9 @@ class Toku extends Controller
try { try {
$tokuService->reset($input['skips'] ?? []); $tokuService->reset($input['skips'] ?? []);
$output['success'] = true; $output['success'] = true;
} catch (Exception $exception) {} } catch (Exception $exception) {
$this->logger->error($exception);
}
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
public function enqueue(ServerRequestInterface $request, ResponseInterface $response, public function enqueue(ServerRequestInterface $request, ResponseInterface $response,

View File

@ -2,6 +2,8 @@
namespace Incoviba\Repository\Venta\MediosPago\Toku; namespace Incoviba\Repository\Venta\MediosPago\Toku;
use DateTimeImmutable; use DateTimeImmutable;
use PDO;
use PDOException;
use Incoviba\Common\Define; use Incoviba\Common\Define;
use Incoviba\Common\Ideal; use Incoviba\Common\Ideal;
use Incoviba\Common\Implement; use Incoviba\Common\Implement;
@ -79,4 +81,32 @@ class Customer extends Ideal\Repository
->where('toku_id = :toku_id'); ->where('toku_id = :toku_id');
return $this->fetchOne($query, compact('toku_id')); return $this->fetchOne($query, compact('toku_id'));
} }
/**
* @return array
* @throws Implement\Exception\EmptyResult
*/
public function fetchAllTokuIds(): array
{
$query = $this->connection->getQueryBuilder()
->select('toku_id')
->from($this->getTable());
try {
$statement = $this->connection->query($query);
} catch (PDOException $exception) {
throw new Implement\Exception\EmptyResult($query, $exception);
}
if ($statement->rowCount() === 0) {
throw new Implement\Exception\EmptyResult($query);
}
return $statement->fetchAll(PDO::FETCH_COLUMN);
}
public function removeByTokuId(string $toku_id): void
{
$query = $this->connection->getQueryBuilder()
->delete()
->from($this->getTable())
->where('toku_id = :toku_id');
$this->connection->execute($query, compact('toku_id'));
}
} }

View File

@ -2,6 +2,8 @@
namespace Incoviba\Repository\Venta\MediosPago\Toku; namespace Incoviba\Repository\Venta\MediosPago\Toku;
use DateTimeImmutable; use DateTimeImmutable;
use PDO;
use PDOException;
use Incoviba\Common\Define; use Incoviba\Common\Define;
use Incoviba\Common\Ideal; use Incoviba\Common\Ideal;
use Incoviba\Common\Implement; use Incoviba\Common\Implement;
@ -69,4 +71,38 @@ class Invoice extends Ideal\Repository
->where('toku_id = :toku_id'); ->where('toku_id = :toku_id');
return $this->fetchOne($query, compact('toku_id')); return $this->fetchOne($query, compact('toku_id'));
} }
/**
* @return array
* @throws Implement\Exception\EmptyResult
*/
public function fetchAllTokuIds(): array
{
$query = $this->connection->getQueryBuilder()
->select('toku_id')
->from($this->getTable());
try {
$statement = $this->connection->query($query);
} catch (PDOException $exception) {
throw new Implement\Exception\EmptyResult($query, $exception);
}
if ($statement->rowCount() === 0) {
throw new Implement\Exception\EmptyResult($query);
}
return $statement->fetchAll(PDO::FETCH_COLUMN);
}
/**
* @param string $toku_id
* @return void
* @throws PDOException
*/
public function removeByTokuId(string $toku_id): void
{
$query = $this->connection->getQueryBuilder()
->delete()
->from($this->getTable())
->where('toku_id = :toku_id');
$this->connection->execute($query, compact('toku_id'));
}
} }

View File

@ -2,6 +2,8 @@
namespace Incoviba\Repository\Venta\MediosPago\Toku; namespace Incoviba\Repository\Venta\MediosPago\Toku;
use DateTimeImmutable; use DateTimeImmutable;
use PDO;
use PDOException;
use Incoviba\Common\Define; use Incoviba\Common\Define;
use Incoviba\Common\Ideal; use Incoviba\Common\Ideal;
use Incoviba\Common\Implement; use Incoviba\Common\Implement;
@ -85,4 +87,38 @@ class Subscription extends Ideal\Repository
->where("venta_id IN ({$idsQuery})"); ->where("venta_id IN ({$idsQuery})");
return $this->fetchMany($query, $ventas_ids); return $this->fetchMany($query, $ventas_ids);
} }
/**
* @return array
* @throws Implement\Exception\EmptyResult
*/
public function fetchAllTokuIds(): array
{
$query = $this->connection->getQueryBuilder()
->select('toku_id')
->from($this->getTable());
try {
$statement = $this->connection->query($query);
} catch (PDOException $exception) {
throw new Implement\Exception\EmptyResult($query, $exception);
}
if ($statement->rowCount() === 0) {
throw new Implement\Exception\EmptyResult($query);
}
return $statement->fetchAll(PDO::FETCH_COLUMN);
}
/**
* @param string $toku_id
* @return void
* @throws PDOException
*/
public function removeByTokuId(string $toku_id): void
{
$query = $this->connection->getQueryBuilder()
->delete()
->from($this->getTable())
->where('toku_id = :toku_id');
$this->connection->execute($query, compact('toku_id'));
}
} }

View File

@ -140,6 +140,7 @@ abstract class AbstractEndPoint extends LoggerEnabled implements EndPoint
*/ */
protected function sendDelete(string $request_uri, array $validStatus, array $invalidStatus): void protected function sendDelete(string $request_uri, array $validStatus, array $invalidStatus): void
{ {
$this->logger->info('Send Delete', ['uri' => $request_uri]);
try { try {
$response = $this->client->delete($request_uri); $response = $this->client->delete($request_uri);
} catch (ClientExceptionInterface $exception) { } catch (ClientExceptionInterface $exception) {
@ -147,6 +148,7 @@ abstract class AbstractEndPoint extends LoggerEnabled implements EndPoint
} }
$this->validateResponse($response, $request_uri, $validStatus, $invalidStatus); $this->validateResponse($response, $request_uri, $validStatus, $invalidStatus);
$this->logger->info('Delete Response', ['request_uri' => $request_uri]);
} }
protected function doSave(Repository $repository, array $data): bool protected function doSave(Repository $repository, array $data): bool
{ {

View File

@ -1,10 +1,10 @@
<?php <?php
namespace Incoviba\Service\Venta\MediosPago\Toku; namespace Incoviba\Service\Venta\MediosPago\Toku;
use PDOException;
use Psr\Http\Client\ClientInterface; use Psr\Http\Client\ClientInterface;
use Incoviba\Common\Implement\Exception\EmptyResponse; use Incoviba\Common\Implement\Exception\EmptyResponse;
use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Model;
use Incoviba\Repository; use Incoviba\Repository;
use Incoviba\Service\Venta\MediosPago\AbstractEndPoint; use Incoviba\Service\Venta\MediosPago\AbstractEndPoint;
@ -47,24 +47,24 @@ class Customer extends AbstractEndPoint
public function reset(array $skip = []): array public function reset(array $skip = []): array
{ {
try { try {
$customers = $this->customerRepository->fetchAll(); $tokuIds = $this->customerRepository->fetchAllTokuIds();
$customers = array_filter($customers, function (Model\Venta\MediosPago\Toku\Customer $customer) use ($skip) { $tokuIds = array_filter($tokuIds, function (string $tokuId) use ($skip) {
return !in_array($customer->toku_id, $skip); return !in_array($tokuId, $skip);
}); });
} catch (EmptyResult $exception) { } catch (EmptyResult $exception) {
$this->logger->warning($exception); $this->logger->warning($exception);
return []; return [];
} }
$this->logger->info('Resetando ' . count($customers) . ' clientes'); $this->logger->info('Resetando ' . count($tokuIds) . ' clientes');
foreach ($customers as $customer) { foreach ($tokuIds as $tokuId) {
try { try {
$this->delete($customer->toku_id); $this->delete($tokuId);
$this->customerRepository->remove($customer); $this->customerRepository->removeByTokuId($tokuId);
} catch (EmptyResponse $exception) { } catch (EmptyResponse | PDOException $exception) {
$this->logger->warning($exception, ['customer' => $customer]); $this->logger->warning($exception, ['customer->toku_id' => $tokuId]);
} }
} }
return $customers; return $tokuIds;
} }
public function save(array $data): bool public function save(array $data): bool

View File

@ -55,23 +55,23 @@ class Invoice extends AbstractEndPoint
public function reset(array $skip = []): array public function reset(array $skip = []): array
{ {
try { try {
$invoices = $this->invoiceRepository->fetchAll(); $tokuIds = $this->invoiceRepository->fetchAllTokuIds();
$invoices = array_filter($invoices, function (Model\Venta\MediosPago\Toku\Invoice $invoice) use ($skip) { $tokuIds = array_filter($tokuIds, function (string $tokuId) use ($skip) {
return !in_array($invoice->toku_id, $skip); return !in_array($tokuId, $skip);
}); });
} catch (EmptyResult $exception) { } catch (EmptyResult $exception) {
$this->logger->warning($exception); $this->logger->warning($exception);
return []; return [];
} }
foreach ($invoices as $invoice) { foreach ($tokuIds as $tokuId) {
try { try {
$this->delete($invoice->toku_id); $this->delete($tokuId);
$this->invoiceRepository->remove($invoice); $this->invoiceRepository->removeByTokuId($tokuId);
} catch (EmptyResponse $exception) { } catch (EmptyResponse $exception) {
$this->logger->warning($exception, ['invoice' => $invoice]); $this->logger->warning($exception, ['invoice->toku_id' => $tokuId]);
} }
} }
return $invoices; return $tokuIds;
} }
/** /**

View File

@ -51,23 +51,23 @@ class Subscription extends AbstractEndPoint
public function reset(array $skip = []): array public function reset(array $skip = []): array
{ {
try { try {
$subscriptions = $this->subscriptionRepsitory->fetchAll(); $tokuIds = $this->subscriptionRepsitory->fetchAllTokuIds();
$subscriptions = array_filter($subscriptions, function (Venta\MediosPago\Toku\Subscription $subscription) use ($skip) { $tokuIds = array_filter($tokuIds, function (string $tokuId) use ($skip) {
return !in_array($subscription->toku_id, $skip); return !in_array($tokuId, $skip);
}); });
} catch (EmptyResult $exception) { } catch (EmptyResult $exception) {
$this->logger->warning($exception); $this->logger->warning($exception);
return []; return [];
} }
foreach ($subscriptions as $subscription) { foreach ($tokuIds as $tokuId) {
try { try {
$this->delete($subscription->toku_id); $this->delete($tokuId);
$this->subscriptionRepsitory->remove($subscription); $this->subscriptionRepsitory->removeByTokuId($tokuId);
} catch (EmptyResponse $exception) { } catch (EmptyResponse $exception) {
$this->logger->warning($exception, ['subscription' => $subscription]); $this->logger->warning($exception, ['subscription->toku_id' => $tokuId]);
} }
} }
return $subscriptions; return $tokuIds;
} }
/** /**