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 { $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)) { 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, ContainerInterface $container): ResponseInterface { 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, 'success' => false ]; try { $tokuService->reset($input['skips'] ?? []); $output['success'] = true; } catch (Exception $exception) { $this->logger->error($exception); } return $this->withJson($response, $output); } public function enqueue(ServerRequestInterface $request, ResponseInterface $response, Service\Queue $queueService, Service\Venta\MediosPago\Toku $tokuService): ResponseInterface { $body = $request->getBody()->getContents(); $input = json_decode($body, true); $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); } }