RegisterCprController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. $message = $result['error'] ?: $result['output'] ?: 'Unknown error';
  84. throw new \RuntimeException($message);
  85. }
  86. $output = $result['output'] ?? '';
  87. $itemId = $this->extractCliValue('itemId', $output);
  88. $qrcodeUrl = $this->extractCliValue('qrcodeURL', $output);
  89. if (!$itemId || !$qrcodeUrl) {
  90. throw new \RuntimeException('Unable to parse itemId or qrcodeURL from genial-cli output');
  91. }
  92. return [
  93. 'item_id' => $itemId,
  94. 'qrcode_url' => $qrcodeUrl,
  95. ];
  96. }
  97. private function extractCliValue(string $key, string $content): ?string
  98. {
  99. $pattern = sprintf('#%s\s*[:=]\s*(?:"([^"]+)"|\' . "'" . '([^\' . "'" . ']+)\' . "'" . '|([^\s,{}]+))#i', preg_quote($key, '#'));
  100. if (preg_match($pattern, $content, $matches)) {
  101. foreach (array_slice($matches, 1) as $match) {
  102. if ($match !== '' && $match !== null) {
  103. return trim($match);
  104. }
  105. }
  106. }
  107. return null;
  108. }
  109. }