B3CprRegisterController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use Models\CprModel;
  6. use Models\DiscountModel;
  7. use Models\StatusModel;
  8. use Services\PaymentService;
  9. class B3CprRegisterController
  10. {
  11. private CprModel $cprModel;
  12. private DiscountModel $discountModel;
  13. private StatusModel $statusModel;
  14. private PaymentService $paymentService;
  15. private const PAYMENT_VALUE = 1000000;
  16. public function __construct()
  17. {
  18. $this->cprModel = new CprModel();
  19. $this->discountModel = new DiscountModel();
  20. $this->statusModel = new StatusModel();
  21. $this->paymentService = new PaymentService();
  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. private function calculatePaymentValue(?string $discountCode): int
  48. {
  49. $baseValue = self::PAYMENT_VALUE;
  50. if ($discountCode === null) {
  51. return $baseValue;
  52. }
  53. $normalized = trim($discountCode);
  54. if ($normalized === '') {
  55. return $baseValue;
  56. }
  57. try {
  58. $row = $this->discountModel->findByCode($normalized);
  59. } catch (\Throwable $e) {
  60. return $baseValue;
  61. }
  62. if (!$row) {
  63. return $baseValue;
  64. }
  65. $value = (int)($row['discount_value'] ?? 0);
  66. if ($value <= 0) {
  67. return $baseValue;
  68. }
  69. return $value;
  70. }
  71. public function __invoke(ServerRequestInterface $request)
  72. {
  73. $timezone = $_ENV['APP_TIMEZONE'] ?? 'America/Sao_Paulo';
  74. $now = new \DateTimeImmutable('now', new \DateTimeZone($timezone));
  75. $hour = (int)$now->format('H');
  76. if ($hour >= 20 || $hour < 8) {
  77. return ResponseLib::sendFail(
  78. 'B3 se encontra offline no momento. Tente novamente entre 08:00 e 20:00.',
  79. ['current_time' => $now->format('Y-m-d H:i:s'), 'timezone' => $timezone],
  80. 'E_B3_OFFLINE'
  81. )->withStatus(503);
  82. }
  83. $body = json_decode((string)$request->getBody(), true);
  84. if (!is_array($body)) {
  85. return ResponseLib::sendFail('Invalid JSON body', [], 'E_VALIDATE')->withStatus(400);
  86. }
  87. $cpr = $body['cpr'] ?? null;
  88. if (!is_array($cpr)) {
  89. $hasCprKeys = false;
  90. foreach ($body as $k => $_) {
  91. if (is_string($k) && substr($k, 0, 4) === 'cpr_') {
  92. $hasCprKeys = true;
  93. break;
  94. }
  95. }
  96. if ($hasCprKeys) {
  97. $cpr = $body;
  98. }
  99. }
  100. if (!is_array($cpr)) {
  101. return ResponseLib::sendFail('Missing CPR payload (array) in body as cpr', [], 'E_VALIDATE')->withStatus(400);
  102. }
  103. try {
  104. $cpr = $this->applyFixedCprDefaults($cpr);
  105. } catch (\InvalidArgumentException $e) {
  106. return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
  107. }
  108. if (!array_key_exists('cpr_ticker', $cpr)) {
  109. $cpr['cpr_ticker'] = '';
  110. }
  111. $userId = (int)($request->getAttribute('api_user_id') ?? 0);
  112. if ($userId <= 0) {
  113. return ResponseLib::sendFail('Authenticated user not found', [], 'E_VALIDATE')->withStatus(401);
  114. }
  115. $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
  116. if ($companyId <= 0) {
  117. return ResponseLib::sendFail('Authenticated company not found', [], 'E_VALIDATE')->withStatus(401);
  118. }
  119. $statusId = $this->statusModel->getIdByStatus('pending');
  120. if ($statusId === null) {
  121. return ResponseLib::sendFail('Pending status not found', [], 'E_DATABASE')->withStatus(500);
  122. }
  123. $discountCode = isset($body['discount']) ? (string)$body['discount'] : null;
  124. $paymentValue = $this->calculatePaymentValue($discountCode);
  125. try {
  126. $paymentData = $this->paymentService->initiatePayment($paymentValue);
  127. } catch (\Throwable $e) {
  128. return ResponseLib::sendFail('Failed to initiate payment: ' . $e->getMessage(), [], 'E_INTERNAL')->withStatus(500);
  129. }
  130. try {
  131. $record = $this->cprModel->create($cpr, $statusId, (int)$paymentData['payment_id'], $userId, $companyId);
  132. } catch (\InvalidArgumentException $e) {
  133. return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
  134. } catch (\Throwable $e) {
  135. return ResponseLib::sendFail('Failed to create CPR: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  136. }
  137. return ResponseLib::sendOk([
  138. 'cpr_id' => $record['cpr_id'] ?? null,
  139. 'payment_id' => $paymentData['payment_id'],
  140. 'payment_code' => $paymentData['payment_code'],
  141. ], 'S_CREATED');
  142. }
  143. }