RegisterCprController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace Controllers;
  3. use Libs\BashExecutor;
  4. use Libs\ResponseLib;
  5. use Models\CprModel;
  6. use Models\StatusModel;
  7. use Psr\Http\Message\ServerRequestInterface;
  8. use Respect\Validation\Exceptions\ValidationException;
  9. use Respect\Validation\Validator as val;
  10. use Services\PaymentService;
  11. class RegisterCprController
  12. {
  13. private CprModel $cprModel;
  14. private StatusModel $statusModel;
  15. private PaymentService $paymentService;
  16. private const PIX_VALUE = '1000.00';
  17. public function __construct()
  18. {
  19. $this->cprModel = new CprModel();
  20. $this->statusModel = new StatusModel();
  21. $this->paymentService = new PaymentService();
  22. }
  23. public function __invoke(ServerRequestInterface $request)
  24. {
  25. $body = json_decode((string)$request->getBody(), true) ?? [];
  26. try {
  27. val::key('cpr_children_codes', val::arrayType()->notEmpty()->each(val::stringType()->notEmpty()))
  28. ->assert($body);
  29. } catch (ValidationException $e) {
  30. return ResponseLib::sendFail(
  31. 'Validation failed: ' . $e->getFullMessage(),
  32. [],
  33. 'E_VALIDATE'
  34. )->withStatus(400);
  35. }
  36. $userId = (int)($request->getAttribute('api_user_id') ?? 0);
  37. if ($userId <= 0) {
  38. return ResponseLib::sendFail('Authenticated user not found', [], 'E_VALIDATE')->withStatus(401);
  39. }
  40. $statusId = $this->statusModel->getIdByStatus('pending');
  41. if ($statusId === null) {
  42. return ResponseLib::sendFail('Pending status not found', [], 'E_DATABASE')->withStatus(500);
  43. }
  44. try {
  45. $pixData = $this->generateDynamicQrcode();
  46. } catch (\Throwable $e) {
  47. return ResponseLib::sendFail('Failed to generate PIX QR Code: ' . $e->getMessage(), [], 'E_INTERNAL')->withStatus(500);
  48. }
  49. try {
  50. $paymentId = $this->paymentService->createPendingPayment($pixData['item_id'], $statusId, $userId);
  51. } catch (\Throwable $e) {
  52. return ResponseLib::sendFail('Failed to create payment record: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  53. }
  54. try {
  55. $record = $this->cprModel->create($body, $statusId, $paymentId);
  56. } catch (\InvalidArgumentException $e) {
  57. return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
  58. } catch (\Throwable $e) {
  59. return ResponseLib::sendFail('Failed to create CPR: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  60. }
  61. return ResponseLib::sendOk([
  62. 'cpr' => $record,
  63. 'pix' => [
  64. 'qrcode_url' => $pixData['qrcode_url'],
  65. ]
  66. ], 'S_CREATED');
  67. }
  68. private function generateDynamicQrcode(): array
  69. {
  70. $cliPath = dirname(__DIR__) . '/bin/genial-cli';
  71. if (!is_file($cliPath) || !is_executable($cliPath)) {
  72. throw new \RuntimeException('genial-cli executable not found or not executable');
  73. }
  74. $amount = self::PIX_VALUE;
  75. $command = sprintf('%s qrcodedynamic %s', escapeshellarg($cliPath), escapeshellarg($amount));
  76. $result = BashExecutor::run($command, 60);
  77. if (($result['exitCode'] ?? 1) !== 0) {
  78. $this->logCliResult($result, 'genial-cli non-zero exit');
  79. $message = $result['error'] ?: $result['output'] ?: 'Unknown error';
  80. throw new \RuntimeException($message);
  81. }
  82. $output = $result['output'] ?? '';
  83. try {
  84. $parsed = $this->decodeCliOutput($output);
  85. } catch (\Throwable $e) {
  86. $this->logCliResult($result, 'genial-cli parse failure');
  87. throw $e;
  88. }
  89. $items = $parsed['data']['items'] ?? null;
  90. if (!is_array($items) || empty($items) || !is_array($items[0])) {
  91. throw new \RuntimeException('genial-cli output is missing items array');
  92. }
  93. $firstItem = $items[0];
  94. $itemId = $firstItem['itemId'] ?? null;
  95. $qrcodeUrl = $firstItem['data']['qrcodeURL'] ?? null;
  96. if (!$itemId || !$qrcodeUrl) {
  97. throw new \RuntimeException('Unable to parse itemId or qrcodeURL from genial-cli output');
  98. }
  99. return [
  100. 'item_id' => $itemId,
  101. 'qrcode_url' => $qrcodeUrl,
  102. ];
  103. }
  104. private function decodeCliOutput(string $content): array
  105. {
  106. $clean = trim($content);
  107. if ($clean === '') {
  108. throw new \RuntimeException('genial-cli returned empty output');
  109. }
  110. $decoded = json_decode($clean, true);
  111. if (json_last_error() === JSON_ERROR_NONE) {
  112. return $decoded;
  113. }
  114. $normalized = $this->normalizeCliOutput($clean);
  115. $decoded = json_decode($normalized, true);
  116. if (json_last_error() !== JSON_ERROR_NONE) {
  117. throw new \RuntimeException('Failed to decode genial-cli output: ' . json_last_error_msg());
  118. }
  119. return $decoded;
  120. }
  121. private function normalizeCliOutput(string $content): string
  122. {
  123. // Strip ANSI escape codes, just in case
  124. $normalized = preg_replace('/\e\[[\d;]*m/', '', $content);
  125. // Convert single-quoted strings to JSON-compatible double-quoted ones
  126. $normalized = preg_replace_callback(
  127. "/'([^'\\\\]*(?:\\\\.[^'\\\\]*)*)'/",
  128. static function (array $matches): string {
  129. $inner = str_replace(['\\', '"'], ['\\\\', '\\"'], $matches[1]);
  130. return '"' . $inner . '"';
  131. },
  132. $normalized
  133. );
  134. // Quote object keys so json_decode can understand them
  135. $normalized = preg_replace(
  136. '/(?<=\{|\[|,|\n)\s*([A-Za-z_][A-Za-z0-9_]*)\s*:/',
  137. '"$1":',
  138. $normalized
  139. );
  140. return $normalized;
  141. }
  142. private function logCliResult(array $result, string $context): void
  143. {
  144. $exitCode = $result['exitCode'] ?? 'null';
  145. $stdout = trim($result['output'] ?? '');
  146. $stderr = trim($result['error'] ?? '');
  147. error_log(sprintf(
  148. '[RegisterCprController] %s | exitCode: %s | stdout: %s | stderr: %s',
  149. $context,
  150. (string)$exitCode,
  151. $stdout === '' ? '<empty>' : $stdout,
  152. $stderr === '' ? '<empty>' : $stderr
  153. ));
  154. }
  155. }