diff --git a/app/src/Controller/API/Ventas/MediosPago/Toku.php b/app/src/Controller/API/Ventas/MediosPago/Toku.php index 78a190a..4c7fcad 100644 --- a/app/src/Controller/API/Ventas/MediosPago/Toku.php +++ b/app/src/Controller/API/Ventas/MediosPago/Toku.php @@ -102,4 +102,13 @@ class Toku extends Controller } 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); + } } diff --git a/app/src/Service/Venta/MediosPago/EndPoint.php b/app/src/Service/Venta/MediosPago/EndPoint.php index e7b7b74..0b49eca 100644 --- a/app/src/Service/Venta/MediosPago/EndPoint.php +++ b/app/src/Service/Venta/MediosPago/EndPoint.php @@ -47,4 +47,11 @@ interface EndPoint * @throws EmptyResponse */ public function delete(string $id): void; + + /** + * @param array $skip + * @return array + * @throws InvalidResult + */ + public function reset(array $skip = []): array; } diff --git a/app/src/Service/Venta/MediosPago/Toku.php b/app/src/Service/Venta/MediosPago/Toku.php index e8ec6b9..af30beb 100644 --- a/app/src/Service/Venta/MediosPago/Toku.php +++ b/app/src/Service/Venta/MediosPago/Toku.php @@ -248,6 +248,19 @@ class Toku extends Ideal\Service } return $queues; } + public function reset(array $skips = []): array + { + $output = []; + try { + $output['invoice'] = $this->invoice->reset($skips['invoice'] ?? []); + $output['subscription'] = $this->subscription->reset($skips['subscription'] ?? []); + $output['customer'] = $this->customer->reset($skips['customer'] ?? []); + } catch (InvalidResult $exception) { + $this->logger->warning($exception); + return []; + } + return $output; + } /** * @param array $request diff --git a/app/src/Service/Venta/MediosPago/Toku/Customer.php b/app/src/Service/Venta/MediosPago/Toku/Customer.php index 8d17133..3bd5749 100644 --- a/app/src/Service/Venta/MediosPago/Toku/Customer.php +++ b/app/src/Service/Venta/MediosPago/Toku/Customer.php @@ -2,6 +2,9 @@ namespace Incoviba\Service\Venta\MediosPago\Toku; use Psr\Http\Client\ClientInterface; +use Incoviba\Common\Implement\Exception\EmptyResponse; +use Incoviba\Common\Implement\Exception\EmptyResult; +use Incoviba\Model; use Incoviba\Repository; use Incoviba\Service\Venta\MediosPago\AbstractEndPoint; @@ -41,6 +44,26 @@ class Customer extends AbstractEndPoint $request_uri = "/customer/{$id}"; $this->sendDelete($request_uri, [204], [404, 409]); } + public function reset(array $skip = []): array + { + try { + $customers = $this->customerRepository->fetchAll(); + $customers = array_filter($customers, function (Model\Venta\MediosPago\Toku\Customer $customer) use ($skip) { + return !in_array($customer->toku_id, $skip); + }); + } catch (EmptyResult $exception) { + $this->logger->warning($exception); + return []; + } + foreach ($customers as $customer) { + try { + $this->delete($customer->toku_id); + } catch (EmptyResponse $exception) { + $this->logger->warning($exception, ['customer' => $customer]); + } + } + return $customers; + } 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 daa98d2..c60005e 100644 --- a/app/src/Service/Venta/MediosPago/Toku/Invoice.php +++ b/app/src/Service/Venta/MediosPago/Toku/Invoice.php @@ -51,6 +51,26 @@ class Invoice extends AbstractEndPoint $request_uri = "/invoices/{$id}"; $this->sendDelete($request_uri, [204], [404, 409]); } + 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); + }); + } catch (EmptyResult $exception) { + $this->logger->warning($exception); + return []; + } + foreach ($invoices as $invoice) { + try { + $this->delete($invoice->toku_id); + } catch (EmptyResponse $exception) { + $this->logger->warning($exception, ['invoice' => $invoice]); + } + } + return $invoices; + } /** * @param string $customer_id diff --git a/app/src/Service/Venta/MediosPago/Toku/Subscription.php b/app/src/Service/Venta/MediosPago/Toku/Subscription.php index 2589a7c..d33fcaa 100644 --- a/app/src/Service/Venta/MediosPago/Toku/Subscription.php +++ b/app/src/Service/Venta/MediosPago/Toku/Subscription.php @@ -2,6 +2,7 @@ namespace Incoviba\Service\Venta\MediosPago\Toku; use Psr\Http\Client\ClientInterface; +use Incoviba\Common\Implement\Exception\EmptyResponse; use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Exception\ServiceAction\Read; use Incoviba\Model\Venta; @@ -47,6 +48,26 @@ class Subscription extends AbstractEndPoint $request_uri = "/subscriptions/{$id}"; $this->sendDelete($request_uri, [204], [404, 409]); } + 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); + }); + } catch (EmptyResult $exception) { + $this->logger->warning($exception); + return []; + } + foreach ($subscriptions as $subscription) { + try { + $this->delete($subscription->toku_id); + } catch (EmptyResponse $exception) { + $this->logger->warning($exception, ['subscription' => $subscription]); + } + } + return $subscriptions; + } /** * @return array diff --git a/cli/setup/settings/commands.php b/cli/setup/settings/commands.php index a7aa8e1..944e416 100644 --- a/cli/setup/settings/commands.php +++ b/cli/setup/settings/commands.php @@ -16,6 +16,7 @@ return [ 'ventas:cuotas:vencer' => Incoviba\Command\Ventas\Cuotas\PorVencer::class, 'queue' => Incoviba\Command\Queue::class, 'external:services' => Incoviba\Command\ExternalServices::class, + 'external:toku:reset' => Incoviba\Command\Ventas\MedioPagos\Toku\Reset::class, ]; } ]; diff --git a/cli/src/Command/Ventas/MedioPagos/Toku/Reset.php b/cli/src/Command/Ventas/MedioPagos/Toku/Reset.php new file mode 100644 index 0000000..e2572c0 --- /dev/null +++ b/cli/src/Command/Ventas/MedioPagos/Toku/Reset.php @@ -0,0 +1,20 @@ +logger->debug("Running {$this->getName()}"); + $uri = '/api/external/toku/reset'; + $output->writeln("GET {$uri}"); + $response = $this->client->get($uri); + $output->writeln("Response Code: {$response->getStatusCode()}"); + + return self::SUCCESS; + } +}