RegisterCprController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
  41. if ($companyId <= 0) {
  42. return ResponseLib::sendFail('Authenticated company not found', [], 'E_VALIDATE')->withStatus(401);
  43. }
  44. $statusId = $this->statusModel->getIdByStatus('pending');
  45. if ($statusId === null) {
  46. return ResponseLib::sendFail('Pending status not found', [], 'E_DATABASE')->withStatus(500);
  47. }
  48. try {
  49. $pixData = $this->generateDynamicQrcode();
  50. } catch (\Throwable $e) {
  51. return ResponseLib::sendFail('Failed to generate PIX QR Code: ' . $e->getMessage(), [], 'E_INTERNAL')->withStatus(500);
  52. }
  53. try {
  54. $paymentId = $this->paymentService->createPendingPayment($pixData['item_id'], $statusId, $userId);
  55. } catch (\Throwable $e) {
  56. return ResponseLib::sendFail('Failed to create payment record: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  57. }
  58. try {
  59. $record = $this->cprModel->create($body, $statusId, $paymentId, $userId, $companyId);
  60. } catch (\InvalidArgumentException $e) {
  61. return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
  62. } catch (\Throwable $e) {
  63. return ResponseLib::sendFail('Failed to create CPR: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  64. }
  65. return ResponseLib::sendOk([
  66. 'cpr' => $record,
  67. 'pix' => [
  68. 'qrcode_url' => $pixData['qrcode_url'],
  69. ]
  70. ], 'S_CREATED');
  71. }
  72. private function generateDynamicQrcode(): array
  73. {
  74. $cliPath = dirname(__DIR__) . '/bin/genial-cli';
  75. if (!is_file($cliPath) || !is_executable($cliPath)) {
  76. throw new \RuntimeException('genial-cli executable not found or not executable');
  77. }
  78. $amount = self::PIX_VALUE;
  79. $command = sprintf('%s qrcodedynamic %s', escapeshellarg($cliPath), escapeshellarg($amount));
  80. $result = BashExecutor::run($command, 60);
  81. if (($result['exitCode'] ?? 1) !== 0) {
  82. $this->logCliResult($result, 'genial-cli non-zero exit');
  83. $message = $result['error'] ?: $result['output'] ?: 'Unknown error';
  84. throw new \RuntimeException($message);
  85. }
  86. $output = $result['output'] ?? '';
  87. try {
  88. $parsed = $this->decodeCliOutput($output);
  89. } catch (\Throwable $e) {
  90. $this->logCliResult($result, 'genial-cli parse failure');
  91. throw $e;
  92. }
  93. $items = $parsed['data']['items'] ?? null;
  94. if (!is_array($items) || empty($items) || !is_array($items[0])) {
  95. throw new \RuntimeException('genial-cli output is missing items array');
  96. }
  97. $firstItem = $items[0];
  98. $itemId = $firstItem['itemId'] ?? null;
  99. $qrcodeUrl = $firstItem['data']['qrcodeURL'] ?? null;
  100. if (!$itemId || !$qrcodeUrl) {
  101. throw new \RuntimeException('Unable to parse itemId or qrcodeURL from genial-cli output');
  102. }
  103. return [
  104. 'item_id' => $itemId,
  105. 'qrcode_url' => $qrcodeUrl,
  106. ];
  107. }
  108. private function decodeCliOutput(string $content): array
  109. {
  110. $clean = trim($content);
  111. if ($clean === '') {
  112. throw new \RuntimeException('genial-cli returned empty output');
  113. }
  114. $decoded = json_decode($clean, true);
  115. if (json_last_error() === JSON_ERROR_NONE) {
  116. return $decoded;
  117. }
  118. $normalized = $this->normalizeCliOutput($clean);
  119. $decoded = json_decode($normalized, true);
  120. if (json_last_error() !== JSON_ERROR_NONE) {
  121. throw new \RuntimeException('Failed to decode genial-cli output: ' . json_last_error_msg());
  122. }
  123. return $decoded;
  124. }
  125. private function normalizeCliOutput(string $content): string
  126. {
  127. // Strip ANSI escape codes, just in case
  128. $normalized = preg_replace('/\e\[[\d;]*m/', '', $content);
  129. // Convert single-quoted strings to JSON-compatible double-quoted ones
  130. $normalized = preg_replace_callback(
  131. "/'([^'\\\\]*(?:\\\\.[^'\\\\]*)*)'/",
  132. static function (array $matches): string {
  133. $inner = str_replace(['\\', '"'], ['\\\\', '\\"'], $matches[1]);
  134. return '"' . $inner . '"';
  135. },
  136. $normalized
  137. );
  138. // Quote object keys so json_decode can understand them
  139. $normalized = preg_replace(
  140. '/(?<=\{|\[|,|\n)\s*([A-Za-z_][A-Za-z0-9_]*)\s*:/',
  141. '"$1":',
  142. $normalized
  143. );
  144. return $normalized;
  145. }
  146. private function logCliResult(array $result, string $context): void
  147. {
  148. $exitCode = $result['exitCode'] ?? 'null';
  149. $stdout = trim($result['output'] ?? '');
  150. $stderr = trim($result['error'] ?? '');
  151. error_log(sprintf(
  152. '[RegisterCprController] %s | exitCode: %s | stdout: %s | stderr: %s',
  153. $context,
  154. (string)$exitCode,
  155. $stdout === '' ? '<empty>' : $stdout,
  156. $stderr === '' ? '<empty>' : $stderr
  157. ));
  158. }
  159. }