From abb1ce72994266e1796c1d595f138a0b6dd30dea Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 9 Jun 2025 12:44:42 -0400 Subject: [PATCH] Mas logging --- .../Controller/API/Ventas/MediosPago/Toku.php | 12 +++++-- .../Venta/MediosPago/Toku/Customer.php | 30 ++++++++++++++++ .../Venta/MediosPago/Toku/Invoice.php | 36 +++++++++++++++++++ .../Venta/MediosPago/Toku/Subscription.php | 36 +++++++++++++++++++ .../Venta/MediosPago/AbstractEndPoint.php | 2 ++ .../Venta/MediosPago/Toku/Customer.php | 22 ++++++------ .../Service/Venta/MediosPago/Toku/Invoice.php | 16 ++++----- .../Venta/MediosPago/Toku/Subscription.php | 16 ++++----- 8 files changed, 140 insertions(+), 30 deletions(-) diff --git a/app/src/Controller/API/Ventas/MediosPago/Toku.php b/app/src/Controller/API/Ventas/MediosPago/Toku.php index dc7594a..d100b6f 100644 --- a/app/src/Controller/API/Ventas/MediosPago/Toku.php +++ b/app/src/Controller/API/Ventas/MediosPago/Toku.php @@ -49,8 +49,11 @@ class Toku extends Controller ResponseFactoryInterface $responseFactory, Service\Venta\MediosPago\Toku $tokuService): ResponseInterface { - $body = $request->getBody()->getContents(); - $input = json_decode($body, true); + $input = $request->getParsedBody(); + if ($input === null) { + $body = $request->getBody()->getContents(); + $input = json_decode($body, true); + } $this->logger->info('Toku payment success', ['input' => $input]); try { if ($tokuService->successEvent($input)) { @@ -111,6 +114,7 @@ class Toku extends Controller if (!$container->has('TOKU_ENV') or strtolower($container->get('TOKU_ENV')) !== 'sandbox') { return $this->withJson($response, ['success' => false], 409); } + $this->logger->info('Toku reset'); $input = $request->getParsedBody(); $output = [ 'input' => $input, @@ -119,7 +123,9 @@ class Toku extends Controller try { $tokuService->reset($input['skips'] ?? []); $output['success'] = true; - } catch (Exception $exception) {} + } catch (Exception $exception) { + $this->logger->error($exception); + } return $this->withJson($response, $output); } public function enqueue(ServerRequestInterface $request, ResponseInterface $response, diff --git a/app/src/Repository/Venta/MediosPago/Toku/Customer.php b/app/src/Repository/Venta/MediosPago/Toku/Customer.php index db7928e..e0ea581 100644 --- a/app/src/Repository/Venta/MediosPago/Toku/Customer.php +++ b/app/src/Repository/Venta/MediosPago/Toku/Customer.php @@ -2,6 +2,8 @@ namespace Incoviba\Repository\Venta\MediosPago\Toku; use DateTimeImmutable; +use PDO; +use PDOException; use Incoviba\Common\Define; use Incoviba\Common\Ideal; use Incoviba\Common\Implement; @@ -79,4 +81,32 @@ class Customer extends Ideal\Repository ->where('toku_id = :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')); + } } diff --git a/app/src/Repository/Venta/MediosPago/Toku/Invoice.php b/app/src/Repository/Venta/MediosPago/Toku/Invoice.php index f9dc07a..69d3a94 100644 --- a/app/src/Repository/Venta/MediosPago/Toku/Invoice.php +++ b/app/src/Repository/Venta/MediosPago/Toku/Invoice.php @@ -2,6 +2,8 @@ namespace Incoviba\Repository\Venta\MediosPago\Toku; use DateTimeImmutable; +use PDO; +use PDOException; use Incoviba\Common\Define; use Incoviba\Common\Ideal; use Incoviba\Common\Implement; @@ -69,4 +71,38 @@ class Invoice extends Ideal\Repository ->where('toku_id = :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')); + } } diff --git a/app/src/Repository/Venta/MediosPago/Toku/Subscription.php b/app/src/Repository/Venta/MediosPago/Toku/Subscription.php index b2d5d25..034d661 100644 --- a/app/src/Repository/Venta/MediosPago/Toku/Subscription.php +++ b/app/src/Repository/Venta/MediosPago/Toku/Subscription.php @@ -2,6 +2,8 @@ namespace Incoviba\Repository\Venta\MediosPago\Toku; use DateTimeImmutable; +use PDO; +use PDOException; use Incoviba\Common\Define; use Incoviba\Common\Ideal; use Incoviba\Common\Implement; @@ -85,4 +87,38 @@ class Subscription extends Ideal\Repository ->where("venta_id IN ({$idsQuery})"); 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')); + } } diff --git a/app/src/Service/Venta/MediosPago/AbstractEndPoint.php b/app/src/Service/Venta/MediosPago/AbstractEndPoint.php index 3ff3c41..ce30f71 100644 --- a/app/src/Service/Venta/MediosPago/AbstractEndPoint.php +++ b/app/src/Service/Venta/MediosPago/AbstractEndPoint.php @@ -140,6 +140,7 @@ abstract class AbstractEndPoint extends LoggerEnabled implements EndPoint */ protected function sendDelete(string $request_uri, array $validStatus, array $invalidStatus): void { + $this->logger->info('Send Delete', ['uri' => $request_uri]); try { $response = $this->client->delete($request_uri); } catch (ClientExceptionInterface $exception) { @@ -147,6 +148,7 @@ abstract class AbstractEndPoint extends LoggerEnabled implements EndPoint } $this->validateResponse($response, $request_uri, $validStatus, $invalidStatus); + $this->logger->info('Delete Response', ['request_uri' => $request_uri]); } protected function doSave(Repository $repository, array $data): bool { diff --git a/app/src/Service/Venta/MediosPago/Toku/Customer.php b/app/src/Service/Venta/MediosPago/Toku/Customer.php index d3ef288..f65c243 100644 --- a/app/src/Service/Venta/MediosPago/Toku/Customer.php +++ b/app/src/Service/Venta/MediosPago/Toku/Customer.php @@ -1,10 +1,10 @@ customerRepository->fetchAll(); - $customers = array_filter($customers, function (Model\Venta\MediosPago\Toku\Customer $customer) use ($skip) { - return !in_array($customer->toku_id, $skip); + $tokuIds = $this->customerRepository->fetchAllTokuIds(); + $tokuIds = array_filter($tokuIds, function (string $tokuId) use ($skip) { + return !in_array($tokuId, $skip); }); } catch (EmptyResult $exception) { $this->logger->warning($exception); return []; } - $this->logger->info('Resetando ' . count($customers) . ' clientes'); - foreach ($customers as $customer) { + $this->logger->info('Resetando ' . count($tokuIds) . ' clientes'); + foreach ($tokuIds as $tokuId) { try { - $this->delete($customer->toku_id); - $this->customerRepository->remove($customer); - } catch (EmptyResponse $exception) { - $this->logger->warning($exception, ['customer' => $customer]); + $this->delete($tokuId); + $this->customerRepository->removeByTokuId($tokuId); + } catch (EmptyResponse | PDOException $exception) { + $this->logger->warning($exception, ['customer->toku_id' => $tokuId]); } } - return $customers; + return $tokuIds; } public function save(array $data): bool diff --git a/app/src/Service/Venta/MediosPago/Toku/Invoice.php b/app/src/Service/Venta/MediosPago/Toku/Invoice.php index c6b3000..0f1a579 100644 --- a/app/src/Service/Venta/MediosPago/Toku/Invoice.php +++ b/app/src/Service/Venta/MediosPago/Toku/Invoice.php @@ -55,23 +55,23 @@ class Invoice extends AbstractEndPoint public function reset(array $skip = []): array { try { - $invoices = $this->invoiceRepository->fetchAll(); - $invoices = array_filter($invoices, function (Model\Venta\MediosPago\Toku\Invoice $invoice) use ($skip) { - return !in_array($invoice->toku_id, $skip); + $tokuIds = $this->invoiceRepository->fetchAllTokuIds(); + $tokuIds = array_filter($tokuIds, function (string $tokuId) use ($skip) { + return !in_array($tokuId, $skip); }); } catch (EmptyResult $exception) { $this->logger->warning($exception); return []; } - foreach ($invoices as $invoice) { + foreach ($tokuIds as $tokuId) { try { - $this->delete($invoice->toku_id); - $this->invoiceRepository->remove($invoice); + $this->delete($tokuId); + $this->invoiceRepository->removeByTokuId($tokuId); } catch (EmptyResponse $exception) { - $this->logger->warning($exception, ['invoice' => $invoice]); + $this->logger->warning($exception, ['invoice->toku_id' => $tokuId]); } } - return $invoices; + return $tokuIds; } /** diff --git a/app/src/Service/Venta/MediosPago/Toku/Subscription.php b/app/src/Service/Venta/MediosPago/Toku/Subscription.php index a7edf8d..d572e73 100644 --- a/app/src/Service/Venta/MediosPago/Toku/Subscription.php +++ b/app/src/Service/Venta/MediosPago/Toku/Subscription.php @@ -51,23 +51,23 @@ class Subscription extends AbstractEndPoint public function reset(array $skip = []): array { try { - $subscriptions = $this->subscriptionRepsitory->fetchAll(); - $subscriptions = array_filter($subscriptions, function (Venta\MediosPago\Toku\Subscription $subscription) use ($skip) { - return !in_array($subscription->toku_id, $skip); + $tokuIds = $this->subscriptionRepsitory->fetchAllTokuIds(); + $tokuIds = array_filter($tokuIds, function (string $tokuId) use ($skip) { + return !in_array($tokuId, $skip); }); } catch (EmptyResult $exception) { $this->logger->warning($exception); return []; } - foreach ($subscriptions as $subscription) { + foreach ($tokuIds as $tokuId) { try { - $this->delete($subscription->toku_id); - $this->subscriptionRepsitory->remove($subscription); + $this->delete($tokuId); + $this->subscriptionRepsitory->removeByTokuId($tokuId); } catch (EmptyResponse $exception) { - $this->logger->warning($exception, ['subscription' => $subscription]); + $this->logger->warning($exception, ['subscription->toku_id' => $tokuId]); } } - return $subscriptions; + return $tokuIds; } /**