B3CprRegisterController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use React\Http\Message\Response;
  6. use Services\B3CprService;
  7. use Models\CprModel;
  8. use Models\StatusModel;
  9. use Models\PaymentModel;
  10. class B3CprRegisterController
  11. {
  12. private B3CprService $service;
  13. private CprModel $cprModel;
  14. private StatusModel $statusModel;
  15. private PaymentModel $paymentModel;
  16. public function __construct()
  17. {
  18. $this->service = new B3CprService();
  19. $this->cprModel = new CprModel();
  20. $this->statusModel = new StatusModel();
  21. $this->paymentModel = new PaymentModel();
  22. }
  23. private function applyFixedCprDefaults(array $cpr): array
  24. {
  25. $nowBr = new \DateTimeImmutable('now', new \DateTimeZone('America/Sao_Paulo'));
  26. $currentDate = $nowBr->format('Y-m-d');
  27. $cpr['cpr_type_code'] = 'P';
  28. $cpr['cpr_otc_register_account_code'] = '64359.40-5';
  29. $cpr['cpr_otc_payment_agent_account_code'] = '64359.40-5';
  30. $cpr['cpr_otc_custodian_account_code'] = '64359.00-3';
  31. $cpr['cpr_electronic_emission_indicator'] = 'S';
  32. $cpr['cpr_automatic_expiration_indicator'] = 'N';
  33. $cpr['cpr_issue_date'] = $currentDate;
  34. $cpr['cpr_profitability_start_date'] = $currentDate;
  35. $cpr['cpr_issue_quantity'] = '1';
  36. if (!array_key_exists('cpr_issue_value', $cpr)) {
  37. throw new \InvalidArgumentException('Missing field: cpr_issue_value');
  38. }
  39. $issueValue = (string)$cpr['cpr_issue_value'];
  40. $cpr['cpr_issue_financial_value'] = $issueValue;
  41. $cpr['cpr_creditor_name'] = 'TOO EASY TRADING LTDA';
  42. $cpr['cpr_creditor_document_number'] = '47.175.222/0001-09';
  43. $cpr['cpr_scr_type_code'] = 'N';
  44. $cpr['cpr_finality_code'] = '6099';
  45. return $cpr;
  46. }
  47. public function __invoke(ServerRequestInterface $request)
  48. {
  49. $body = json_decode((string)$request->getBody(), true);
  50. if (!is_array($body)) {
  51. return ResponseLib::sendFail('Invalid JSON body', [], 'E_VALIDATE')->withStatus(400);
  52. }
  53. $token = $body['b3_access_token'] ?? ($body['access_token'] ?? null);
  54. if (!$token) {
  55. $b3Auth = $request->getHeaderLine('X-B3-Authorization') ?: '';
  56. if (stripos($b3Auth, 'Bearer ') === 0) {
  57. $token = trim(substr($b3Auth, 7));
  58. }
  59. }
  60. if (!$token) {
  61. $token = $request->getHeaderLine('X-B3-Access-Token') ?: null;
  62. }
  63. $cpr = $body['cpr'] ?? null;
  64. if (!is_array($cpr)) {
  65. $hasCprKeys = false;
  66. foreach ($body as $k => $_) {
  67. if (is_string($k) && substr($k, 0, 4) === 'cpr_') {
  68. $hasCprKeys = true;
  69. break;
  70. }
  71. }
  72. if ($hasCprKeys) {
  73. $cpr = $body;
  74. }
  75. }
  76. if (!is_array($cpr)) {
  77. return ResponseLib::sendFail('Missing CPR payload (array) in body as cpr', [], 'E_VALIDATE')->withStatus(400);
  78. }
  79. try {
  80. $cpr = $this->applyFixedCprDefaults($cpr);
  81. } catch (\InvalidArgumentException $e) {
  82. return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
  83. }
  84. $userId = (int)($request->getAttribute('api_user_id') ?? 0);
  85. if ($userId <= 0) {
  86. return ResponseLib::sendFail('Authenticated user not found', [], 'E_VALIDATE')->withStatus(401);
  87. }
  88. $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
  89. if ($companyId <= 0) {
  90. return ResponseLib::sendFail('Authenticated company not found', [], 'E_VALIDATE')->withStatus(401);
  91. }
  92. $statusId = $this->statusModel->getIdByStatus('pending');
  93. if ($statusId === null) {
  94. return ResponseLib::sendFail('Pending status not found', [], 'E_DATABASE')->withStatus(500);
  95. }
  96. try {
  97. $paymentExternalId = 'B3_DIRECT_' . time();
  98. $paymentId = $this->paymentModel->create($paymentExternalId, $statusId, $userId);
  99. } catch (\Throwable $e) {
  100. return ResponseLib::sendFail('Failed to create payment record: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  101. }
  102. try {
  103. $record = $this->cprModel->create($cpr, $statusId, $paymentId, $userId, $companyId);
  104. } catch (\InvalidArgumentException $e) {
  105. return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
  106. } catch (\Throwable $e) {
  107. return ResponseLib::sendFail('Failed to create CPR: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  108. }
  109. try {
  110. $payload = $this->service->mapToB3($cpr);
  111. if (!$token) {
  112. $token = $this->service->getAccessToken();
  113. }
  114. $result = $this->service->postCpr($token, $payload);
  115. } catch (\Throwable $e) {
  116. return ResponseLib::sendFail('Failed to send CPR to B3: ' . $e->getMessage(), [], 'E_EXTERNAL')->withStatus(502);
  117. }
  118. if (isset($result['error'])) {
  119. return ResponseLib::sendFail('cURL error during B3 CPR request', ['error' => $result['error']], 'E_EXTERNAL')->withStatus(502);
  120. }
  121. $status = (int)($result['status'] ?? 200);
  122. if (isset($result['json'])) {
  123. return Response::json($result['json'])->withStatus($status ?: 200);
  124. }
  125. return Response::json(['raw' => $result['raw'] ?? null, 'status' => $status])->withStatus($status ?: 502);
  126. }
  127. }