Toku: Reset payments
This commit is contained in:
91
app/common/Implement/Log/Processor/ArrayBuilder.php
Normal file
91
app/common/Implement/Log/Processor/ArrayBuilder.php
Normal file
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Log\Processor;
|
||||
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Monolog\Formatter;
|
||||
use Monolog\Handler;
|
||||
use Monolog\Level;
|
||||
use Predis;
|
||||
use Incoviba;
|
||||
use Incoviba\Common\Ideal;
|
||||
|
||||
class ArrayBuilder extends Ideal\Service
|
||||
{
|
||||
public function __construct(LoggerInterface $logger, protected ContainerInterface $container)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
}
|
||||
|
||||
public function build(array $data): array
|
||||
{
|
||||
$handlers = [];
|
||||
foreach ($data as $handlerData) {
|
||||
if (in_array($handlerData['handler'], [Handler\StreamHandler::class, Handler\RotatingFileHandler::class,])) {
|
||||
$params = [
|
||||
"/logs/{$handlerData['filename']}",
|
||||
];
|
||||
if ($handlerData['handler'] === Handler\RotatingFileHandler::class) {
|
||||
$params []= 10;
|
||||
}
|
||||
try {
|
||||
$formatter = Formatter\LineFormatter::class;
|
||||
if (array_key_exists('formatter', $handlerData)) {
|
||||
$formatter = $handlerData['formatter'];
|
||||
}
|
||||
$handler = new $handlerData['handler'](...$params)
|
||||
->setFormatter($this->container->get($formatter));
|
||||
} catch (NotFoundExceptionInterface | ContainerExceptionInterface $exception) {
|
||||
$this->logger->error($exception->getMessage(), ['exception' => $exception, 'handlerData' => $handlerData]);
|
||||
continue;
|
||||
}
|
||||
} elseif ($handlerData['handler'] === Incoviba\Common\Implement\Log\Handler\MySQL::class) {
|
||||
try {
|
||||
$params = [
|
||||
$this->container->get(Incoviba\Common\Define\Connection::class)
|
||||
];
|
||||
$formatter = Incoviba\Common\Implement\Log\Formatter\PDO::class;
|
||||
if (array_key_exists('formatter', $handlerData)) {
|
||||
$formatter = $handlerData['formatter'];
|
||||
}
|
||||
$handler = new $handlerData['handler'](...$params)
|
||||
->setFormatter($this->container->get($formatter));
|
||||
} catch (NotFoundExceptionInterface | ContainerExceptionInterface $exception) {
|
||||
$this->logger->error($exception->getMessage(), ['exception' => $exception, 'handlerData' => $handlerData]);
|
||||
continue;
|
||||
}
|
||||
} elseif ($handlerData['handler'] === Handler\RedisHandler::class) {
|
||||
try {
|
||||
$params = [
|
||||
$this->container->get(Predis\ClientInterface::class),
|
||||
"logs:{$handlerData['name']}"
|
||||
];
|
||||
} catch (NotFoundExceptionInterface | ContainerExceptionInterface $exception) {
|
||||
$this->logger->error($exception->getMessage(), ['exception' => $exception, 'handlerData' => $handlerData]);
|
||||
continue;
|
||||
}
|
||||
$handler = new $handlerData['handler'](...$params);
|
||||
}
|
||||
if (!isset($handler)) {
|
||||
$this->logger->error("Invalid handler", ['handlerData' => $handlerData]);
|
||||
continue;
|
||||
}
|
||||
$params = [
|
||||
$handler,
|
||||
];
|
||||
if (is_array($handlerData['levels'])) {
|
||||
foreach ($handlerData['levels'] as $level) {
|
||||
$params []= $level;
|
||||
}
|
||||
} else {
|
||||
$params []= $handlerData['levels'];
|
||||
$params []= Level::Emergency;
|
||||
}
|
||||
$params []= false;
|
||||
$handlers []= new Handler\FilterHandler(...$params);
|
||||
}
|
||||
return $handlers;
|
||||
}
|
||||
}
|
@ -27,8 +27,8 @@ return [
|
||||
$container->get(Monolog\Processor\UidProcessor::class),
|
||||
];
|
||||
},
|
||||
'defaultMonologHandlers' => function(ContainerInterface $container) {
|
||||
$baseHandlers = [
|
||||
'baseDefaultHandlers' => function(ContainerInterface $container) {
|
||||
return [
|
||||
'critical' => [
|
||||
'handler' => Monolog\Handler\RotatingFileHandler::class,
|
||||
'filename' => 'critical.log',
|
||||
@ -49,56 +49,28 @@ return [
|
||||
'levels' => [Monolog\Level::Debug, Monolog\Level::Info]
|
||||
],
|
||||
];
|
||||
if ($container->has('ENVIRONMENT') and $container->get('ENVIRONMENT') === 'development') {
|
||||
},
|
||||
'developmentHandlers' => function(ContainerInterface $container) {
|
||||
$baseHandlers = $container->get('baseDefaultHandlers');
|
||||
$baseHandlers['critical']['handler'] = Monolog\Handler\StreamHandler::class;
|
||||
$baseHandlers['error']['handler'] = Monolog\Handler\StreamHandler::class;
|
||||
$baseHandlers['notices']['handler'] = Monolog\Handler\StreamHandler::class;
|
||||
$baseHandlers['notices']['filename'] = 'notices.log';
|
||||
$baseHandlers['debug']['handler'] = Monolog\Handler\StreamHandler::class;
|
||||
$baseHandlers['debug']['filename'] = 'debug.log';
|
||||
return $baseHandlers;
|
||||
},
|
||||
'defaultMonologHandlers' => function(ContainerInterface $container) {
|
||||
$key = 'baseDefault';
|
||||
if ($container->has('ENVIRONMENT') and $container->get('ENVIRONMENT') === 'development') {
|
||||
$key = 'development';
|
||||
if (!$container->has("{$key}Handlers")) {
|
||||
$key = 'baseDefault';
|
||||
}
|
||||
$handlers = [];
|
||||
foreach ($baseHandlers as $handlerData) {
|
||||
if (in_array($handlerData['handler'], [
|
||||
Monolog\Handler\StreamHandler::class,
|
||||
Monolog\Handler\RotatingFileHandler::class,
|
||||
])) {
|
||||
$params = [
|
||||
"/logs/{$handlerData['filename']}",
|
||||
];
|
||||
if ($handlerData['handler'] === Monolog\Handler\RotatingFileHandler::class) {
|
||||
$params []= 10;
|
||||
}
|
||||
$handler = new $handlerData['handler'](...$params)
|
||||
->setFormatter($container->get(Monolog\Formatter\LineFormatter::class));
|
||||
} elseif ($handlerData['handler'] === Incoviba\Common\Implement\Log\Handler\MySQL::class) {
|
||||
$params = [
|
||||
$container->get(Incoviba\Common\Define\Connection::class)
|
||||
];
|
||||
$handler = new $handlerData['handler'](...$params)
|
||||
->setFormatter(new \Incoviba\Common\Implement\Log\Formatter\PDO());
|
||||
} elseif ($handlerData['handler'] === Monolog\Handler\RedisHandler::class) {
|
||||
$params = [
|
||||
$container->get(Predis\ClientInterface::class),
|
||||
"logs:{$handlerData['name']}"
|
||||
];
|
||||
$handler = new $handlerData['handler'](...$params);
|
||||
}
|
||||
$params = [
|
||||
$handler,
|
||||
];
|
||||
if (is_array($handlerData['levels'])) {
|
||||
foreach ($handlerData['levels'] as $level) {
|
||||
$params []= $level;
|
||||
}
|
||||
} else {
|
||||
$params []= $handlerData['levels'];
|
||||
$params []= Monolog\Level::Emergency;
|
||||
}
|
||||
$params []= false;
|
||||
$handlers []= new Monolog\Handler\FilterHandler(...$params);
|
||||
}
|
||||
return $handlers;
|
||||
$baseHandlers = $container->get("{$key}Handlers");
|
||||
$builder = $container->get(Incoviba\Common\Implement\Log\Processor\ArrayBuilder::class);
|
||||
return $builder->build($baseHandlers);
|
||||
},
|
||||
Psr\Log\LoggerInterface::class => function(ContainerInterface $container) {
|
||||
return new Monolog\Logger('incoviba',
|
||||
@ -108,6 +80,26 @@ return [
|
||||
$container->get(DateTimeZone::class)
|
||||
);
|
||||
},
|
||||
'jsonHandlers' => function(ContainerInterface $container) {
|
||||
$baseHandlers = $container->get('baseDefaultHandlers');
|
||||
$baseHandlers['debug']['handler'] = Monolog\Handler\RotatingFileHandler::class;
|
||||
$baseHandlers['debug']['filename'] = 'info.json';
|
||||
$baseHandlers['debug']['formatter'] = Monolog\Formatter\JsonFormatter::class;
|
||||
$baseHandlers['notices']['handler'] = Monolog\Handler\RotatingFileHandler::class;
|
||||
$baseHandlers['notices']['filename'] = 'notices.json';
|
||||
$baseHandlers['notices']['formatter'] = Monolog\Formatter\JsonFormatter::class;
|
||||
return $baseHandlers;
|
||||
},
|
||||
'jsonLogger' => function(ContainerInterface $container) {
|
||||
$builder = $container->get(Incoviba\Common\Implement\Log\Processor\ArrayBuilder::class);
|
||||
$handlers = $builder->build($container->get('jsonHandlers'));
|
||||
return new Monolog\Logger('json',
|
||||
$handlers,
|
||||
[$container->get(Incoviba\Common\Implement\Log\Processor\User::class)]
|
||||
+ $container->get('baseMonologProcessors'),
|
||||
$container->get(DateTimeZone::class)
|
||||
);
|
||||
},
|
||||
'loginLogger' => function(ContainerInterface $container) {
|
||||
return new Monolog\Logger('login',
|
||||
[
|
||||
|
@ -149,6 +149,7 @@ return [
|
||||
$container->get(Incoviba\Service\UF::class)
|
||||
);
|
||||
$service->setLogger($container->get('externalLogger'));
|
||||
$service->setAltLogger($container->get('jsonLogger'));
|
||||
return $service;
|
||||
},
|
||||
Incoviba\Service\Venta\MediosPago\Toku::class => function(ContainerInterface $container) {
|
||||
|
@ -135,14 +135,19 @@ abstract class AbstractEndPoint extends LoggerEnabled implements EndPoint
|
||||
* @param string $request_uri
|
||||
* @param array $validStatus
|
||||
* @param array $invalidStatus
|
||||
* @param array|null $data
|
||||
* @return void
|
||||
* @throws EmptyResponse
|
||||
*/
|
||||
protected function sendDelete(string $request_uri, array $validStatus, array $invalidStatus): void
|
||||
protected function sendDelete(string $request_uri, array $validStatus, array $invalidStatus, ?array $data = null): void
|
||||
{
|
||||
$this->logger->info('Send Delete', ['uri' => $request_uri]);
|
||||
try {
|
||||
$response = $this->client->delete($request_uri);
|
||||
$options = [];
|
||||
if ($data !== null) {
|
||||
$options = ['json' => $data];
|
||||
}
|
||||
$response = $this->client->delete($request_uri, $options);
|
||||
} catch (ClientExceptionInterface $exception) {
|
||||
throw new EmptyResponse($request_uri, $exception);
|
||||
}
|
||||
|
@ -267,6 +267,7 @@ class Toku extends Ideal\Service
|
||||
$output['customer'] = $this->customer->reset($skips['customer'] ?? []);
|
||||
$output['subscription'] = $this->subscription->reset($skips['subscription'] ?? []);
|
||||
$output['invoice'] = $this->invoice->reset($skips['invoice'] ?? []);
|
||||
$output['payments'] = $this->invoice->resetPayments();
|
||||
} catch (InvalidResult $exception) {
|
||||
$this->logger->warning($exception);
|
||||
return [];
|
||||
|
@ -13,6 +13,7 @@ use Incoviba\Repository;
|
||||
use Incoviba\Service\UF;
|
||||
use Incoviba\Service\Venta\MediosPago\AbstractEndPoint;
|
||||
use Incoviba\Service\Venta\Pago;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Invoice extends AbstractEndPoint
|
||||
{
|
||||
@ -73,6 +74,57 @@ class Invoice extends AbstractEndPoint
|
||||
}
|
||||
return $tokuIds;
|
||||
}
|
||||
public function resetPayments(): array
|
||||
{
|
||||
try {
|
||||
$request_uri = "/payments";
|
||||
$payments = $this->sendGet($request_uri, [200], [404, 422]);
|
||||
} catch (EmptyResponse $exception) {
|
||||
$this->logger->warning($exception, ['request_uri' => $request_uri]);
|
||||
return [];
|
||||
}
|
||||
$this->altLogger->info('Reset Payments', ['count' => count($payments), 'payments' => $payments]);
|
||||
$query = [];
|
||||
/*
|
||||
"id": "pay_79zh1OU1pVV5g0V0I6kShf5AF-I24cUn",
|
||||
"created_at": "2025-06-07T07:08:51+0000",
|
||||
"deleted_at": null,
|
||||
"invoice": "in_IhbKbT21x0ADlnKRCbV57sn2DDI8neq0",
|
||||
"customer": "cus_bTXPBVopZxKOqTOWzRZkhvDEM9XXtvWh",
|
||||
"government_id": "175181431",
|
||||
"name": "Augusto Felipe Schilfferli Rojas",
|
||||
"product_id": "1304-d1749582981383358",
|
||||
"due_date": "2024-11-01",
|
||||
"transaction_date": "2025-06-07T07:08:51+0000",
|
||||
"payment_amount": 14.4822,
|
||||
"buy_order": null,
|
||||
"processed_by_toku": false,
|
||||
"payment_method": null,
|
||||
"card_type": null,
|
||||
"card_number": null,
|
||||
"payment_type": null,
|
||||
"authorization_code": null,
|
||||
"mc_order_id": null,
|
||||
"amount_paid": 14.4822
|
||||
*/
|
||||
foreach ($payments as $payment) {
|
||||
$query[] = [
|
||||
'payment_amount' => $payment['payment_amount'],
|
||||
'payment_date' => $payment['transaction_date'],
|
||||
'payment_method' => $payment['payment_method'],
|
||||
'product_id' => $payment['product_id'],
|
||||
'due_date' => $payment['due_date'],
|
||||
];
|
||||
}
|
||||
try {
|
||||
$this->sendDelete($request_uri, [204], [404, 422], $query);
|
||||
} catch (EmptyResponse $exception) {
|
||||
$this->logger->warning($exception, ['request_uri' => $request_uri]);
|
||||
return [];
|
||||
}
|
||||
$this->altLogger->info('Reset Payments Payload', ['count' => count($payments), 'payments' => $query]);
|
||||
return $payments;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $customer_id
|
||||
@ -133,6 +185,14 @@ class Invoice extends AbstractEndPoint
|
||||
{
|
||||
return $this->doSave($this->invoiceRepository, $data);
|
||||
}
|
||||
|
||||
protected LoggerInterface $altLogger;
|
||||
public function setAltLogger(LoggerInterface $altLogger): self
|
||||
{
|
||||
$this->altLogger = $altLogger;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function mapParams(array $data): array
|
||||
{
|
||||
$paramsMap = [
|
||||
|
Reference in New Issue
Block a user