RegisterCprController.php 6.6 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. $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. 'payment' => [
  64. 'payment_id' => $paymentId,
  65. 'payment_external_id' => $pixData['item_id'],
  66. 'status_id' => $statusId,
  67. ],
  68. 'pix' => [
  69. 'qrcode_url' => $pixData['qrcode_url'],
  70. ]
  71. ], 'S_CREATED');
  72. }
  73. private function generateDynamicQrcode(): array
  74. {
  75. $cliPath = dirname(__DIR__) . '/bin/genial-cli';
  76. if (!is_file($cliPath) || !is_executable($cliPath)) {
  77. throw new \RuntimeException('genial-cli executable not found or not executable');
  78. }
  79. $amount = self::PIX_VALUE;
  80. $command = sprintf('%s qrcodedynamic %s', escapeshellarg($cliPath), escapeshellarg($amount));
  81. $result = BashExecutor::run($command, 60);
  82. if (($result['exitCode'] ?? 1) !== 0) {
  83. $this->logCliResult($result, 'genial-cli non-zero exit');
  84. $message = $result['error'] ?: $result['output'] ?: 'Unknown error';
  85. throw new \RuntimeException($message);
  86. }
  87. $output = $result['output'] ?? '';
  88. try {
  89. $parsed = $this->decodeCliOutput($output);
  90. } catch (\Throwable $e) {
  91. $this->logCliResult($result, 'genial-cli parse failure');
  92. throw $e;
  93. }
  94. $items = $parsed['data']['items'] ?? null;
  95. if (!is_array($items) || empty($items) || !is_array($items[0])) {
  96. throw new \RuntimeException('genial-cli output is missing items array');
  97. }
  98. $firstItem = $items[0];
  99. $itemId = $firstItem['itemId'] ?? null;
  100. $qrcodeUrl = $firstItem['data']['qrcodeURL'] ?? null;
  101. if (!$itemId || !$qrcodeUrl) {
  102. throw new \RuntimeException('Unable to parse itemId or qrcodeURL from genial-cli output');
  103. }
  104. return [
  105. 'item_id' => $itemId,
  106. 'qrcode_url' => $qrcodeUrl,
  107. ];
  108. }
  109. private function decodeCliOutput(string $content): array
  110. {
  111. $clean = trim($content);
  112. if ($clean === '') {
  113. throw new \RuntimeException('genial-cli returned empty output');
  114. }
  115. $decoded = json_decode($clean, true);
  116. if (json_last_error() === JSON_ERROR_NONE) {
  117. return $decoded;
  118. }
  119. $normalized = $this->normalizeCliOutput($clean);
  120. $decoded = json_decode($normalized, true);
  121. if (json_last_error() !== JSON_ERROR_NONE) {
  122. throw new \RuntimeException('Failed to decode genial-cli output: ' . json_last_error_msg());
  123. }
  124. return $decoded;
  125. }
  126. private function normalizeCliOutput(string $content): string
  127. {
  128. // Strip ANSI escape codes, just in case
  129. $normalized = preg_replace('/\e\[[\d;]*m/', '', $content);
  130. // Convert single-quoted strings to JSON-compatible double-quoted ones
  131. $normalized = preg_replace_callback(
  132. "/'([^'\\\\]*(?:\\\\.[^'\\\\]*)*)'/",
  133. static function (array $matches): string {
  134. $inner = str_replace(['\\', '"'], ['\\\\', '\\"'], $matches[1]);
  135. return '"' . $inner . '"';
  136. },
  137. $normalized
  138. );
  139. // Quote object keys so json_decode can understand them
  140. $normalized = preg_replace(
  141. '/(?<=\{|\[|,|\n)\s*([A-Za-z_][A-Za-z0-9_]*)\s*:/',
  142. '"$1":',
  143. $normalized
  144. );
  145. return $normalized;
  146. }
  147. private function logCliResult(array $result, string $context): void
  148. {
  149. $exitCode = $result['exitCode'] ?? 'null';
  150. $stdout = trim($result['output'] ?? '');
  151. $stderr = trim($result['error'] ?? '');
  152. error_log(sprintf(
  153. '[RegisterCprController] %s | exitCode: %s | stdout: %s | stderr: %s',
  154. $context,
  155. (string)$exitCode,
  156. $stdout === '' ? '<empty>' : $stdout,
  157. $stderr === '' ? '<empty>' : $stderr
  158. ));
  159. }
  160. }