B3CprRegisterController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use Models\CprModel;
  6. use Models\StatusModel;
  7. use Services\PaymentService;
  8. class B3CprRegisterController
  9. {
  10. private CprModel $cprModel;
  11. private StatusModel $statusModel;
  12. private PaymentService $paymentService;
  13. private const PAYMENT_VALUE = 1000000;
  14. public function __construct()
  15. {
  16. $this->cprModel = new CprModel();
  17. $this->statusModel = new StatusModel();
  18. $this->paymentService = new PaymentService();
  19. }
  20. private function applyFixedCprDefaults(array $cpr): array
  21. {
  22. $nowBr = new \DateTimeImmutable('now', new \DateTimeZone('America/Sao_Paulo'));
  23. $currentDate = $nowBr->format('Y-m-d');
  24. $cpr['cpr_type_code'] = 'P';
  25. $cpr['cpr_otc_register_account_code'] = '64359.40-5';
  26. $cpr['cpr_otc_payment_agent_account_code'] = '64359.40-5';
  27. $cpr['cpr_otc_custodian_account_code'] = '64359.00-3';
  28. $cpr['cpr_electronic_emission_indicator'] = 'S';
  29. $cpr['cpr_automatic_expiration_indicator'] = 'N';
  30. $cpr['cpr_issue_date'] = $currentDate;
  31. $cpr['cpr_profitability_start_date'] = $currentDate;
  32. $cpr['cpr_issue_quantity'] = '1';
  33. if (!array_key_exists('cpr_issue_value', $cpr)) {
  34. throw new \InvalidArgumentException('Missing field: cpr_issue_value');
  35. }
  36. $issueValue = (string)$cpr['cpr_issue_value'];
  37. $cpr['cpr_issue_financial_value'] = $issueValue;
  38. $cpr['cpr_creditor_name'] = 'TOO EASY TRADING LTDA';
  39. $cpr['cpr_creditor_document_number'] = '47.175.222/0001-09';
  40. $cpr['cpr_scr_type_code'] = 'N';
  41. $cpr['cpr_finality_code'] = '6099';
  42. return $cpr;
  43. }
  44. private function calculatePaymentValue(?string $discountCode): int
  45. {
  46. $baseValue = self::PAYMENT_VALUE;
  47. if ($discountCode === null) {
  48. return $baseValue;
  49. }
  50. $normalized = strtoupper(trim($discountCode));
  51. $normalized = str_replace('%', '', $normalized);
  52. $discountPercent = 0;
  53. switch ($normalized) {
  54. case '00000000 00000000 00000000 01001100 01100101 01101111':
  55. $discountPercent = 10;
  56. break;
  57. case '00000000 01000100 01101001 01101111 01100111 01101111':
  58. $discountPercent = 20;
  59. break;
  60. case '00000000 00000000 00000000 01011010 01100101 01110000':
  61. $discountPercent = 30;
  62. break;
  63. case '00000000 00000000 00000000 01000111 01110101 01110011':
  64. $discountPercent = 40;
  65. break;
  66. case '00000000 01001101 01100101 01101110 01100111 01101111':
  67. $discountPercent = 50;
  68. break;
  69. case '00000000 00000000 00000000 00000000 01000100 01110101':
  70. $discountPercent = 60;
  71. break;
  72. case '01000001 01101100 01110110 01100001 01110010 01101111':
  73. $discountPercent = 70;
  74. break;
  75. case '00000000 00000000 01000010 01101001 01100101 01101100':
  76. $discountPercent = 80;
  77. break;
  78. case '00000000 01000001 01101100 01101100 01100001 01101110':
  79. $discountPercent = 90;
  80. break;
  81. case '00000000 01001100 01110101 01110110 01100001 01110011':
  82. $discountPercent = 100;
  83. break;
  84. default:
  85. $discountPercent = 0;
  86. }
  87. if ($discountPercent <= 0) {
  88. return $baseValue;
  89. }
  90. $multiplier = max(0, (100 - $discountPercent)) / 100;
  91. $discountedValue = (int) round($baseValue * $multiplier);
  92. return max(0, $discountedValue);
  93. }
  94. public function __invoke(ServerRequestInterface $request)
  95. {
  96. $timezone = $_ENV['APP_TIMEZONE'] ?? 'America/Sao_Paulo';
  97. $now = new \DateTimeImmutable('now', new \DateTimeZone($timezone));
  98. $hour = (int)$now->format('H');
  99. if ($hour >= 20 || $hour < 8) {
  100. return ResponseLib::sendFail(
  101. 'B3 se encontra offline no momento. Tente novamente entre 08:00 e 20:00.',
  102. ['current_time' => $now->format('Y-m-d H:i:s'), 'timezone' => $timezone],
  103. 'E_B3_OFFLINE'
  104. )->withStatus(503);
  105. }
  106. $body = json_decode((string)$request->getBody(), true);
  107. if (!is_array($body)) {
  108. return ResponseLib::sendFail('Invalid JSON body', [], 'E_VALIDATE')->withStatus(400);
  109. }
  110. $cpr = $body['cpr'] ?? null;
  111. if (!is_array($cpr)) {
  112. $hasCprKeys = false;
  113. foreach ($body as $k => $_) {
  114. if (is_string($k) && substr($k, 0, 4) === 'cpr_') {
  115. $hasCprKeys = true;
  116. break;
  117. }
  118. }
  119. if ($hasCprKeys) {
  120. $cpr = $body;
  121. }
  122. }
  123. if (!is_array($cpr)) {
  124. return ResponseLib::sendFail('Missing CPR payload (array) in body as cpr', [], 'E_VALIDATE')->withStatus(400);
  125. }
  126. try {
  127. $cpr = $this->applyFixedCprDefaults($cpr);
  128. } catch (\InvalidArgumentException $e) {
  129. return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
  130. }
  131. if (!array_key_exists('cpr_ticker', $cpr)) {
  132. $cpr['cpr_ticker'] = '';
  133. }
  134. $userId = (int)($request->getAttribute('api_user_id') ?? 0);
  135. if ($userId <= 0) {
  136. return ResponseLib::sendFail('Authenticated user not found', [], 'E_VALIDATE')->withStatus(401);
  137. }
  138. $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
  139. if ($companyId <= 0) {
  140. return ResponseLib::sendFail('Authenticated company not found', [], 'E_VALIDATE')->withStatus(401);
  141. }
  142. $statusId = $this->statusModel->getIdByStatus('pending');
  143. if ($statusId === null) {
  144. return ResponseLib::sendFail('Pending status not found', [], 'E_DATABASE')->withStatus(500);
  145. }
  146. $discountCode = isset($body['discount']) ? (string)$body['discount'] : null;
  147. $paymentValue = $this->calculatePaymentValue($discountCode);
  148. try {
  149. $paymentData = $this->paymentService->initiatePayment($paymentValue);
  150. } catch (\Throwable $e) {
  151. return ResponseLib::sendFail('Failed to initiate payment: ' . $e->getMessage(), [], 'E_INTERNAL')->withStatus(500);
  152. }
  153. try {
  154. $record = $this->cprModel->create($cpr, $statusId, (int)$paymentData['payment_id'], $userId, $companyId);
  155. } catch (\InvalidArgumentException $e) {
  156. return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
  157. } catch (\Throwable $e) {
  158. return ResponseLib::sendFail('Failed to create CPR: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  159. }
  160. return ResponseLib::sendOk([
  161. 'cpr_id' => $record['cpr_id'] ?? null,
  162. 'payment_id' => $paymentData['payment_id'],
  163. 'payment_code' => $paymentData['payment_code'],
  164. ], 'S_CREATED');
  165. }
  166. }