B3CprRegisterController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 = 100;
  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. public function __invoke(ServerRequestInterface $request)
  45. {
  46. $timezone = $_ENV['APP_TIMEZONE'] ?? 'America/Sao_Paulo';
  47. $now = new \DateTimeImmutable('now', new \DateTimeZone($timezone));
  48. $hour = (int)$now->format('H');
  49. if ($hour >= 20 || $hour < 8) {
  50. return ResponseLib::sendFail(
  51. 'B3 se encontra offline no momento. Tente novamente entre 08:00 e 20:00.',
  52. ['current_time' => $now->format('Y-m-d H:i:s'), 'timezone' => $timezone],
  53. 'E_B3_OFFLINE'
  54. )->withStatus(503);
  55. }
  56. $body = json_decode((string)$request->getBody(), true);
  57. if (!is_array($body)) {
  58. return ResponseLib::sendFail('Invalid JSON body', [], 'E_VALIDATE')->withStatus(400);
  59. }
  60. $cpr = $body['cpr'] ?? null;
  61. if (!is_array($cpr)) {
  62. $hasCprKeys = false;
  63. foreach ($body as $k => $_) {
  64. if (is_string($k) && substr($k, 0, 4) === 'cpr_') {
  65. $hasCprKeys = true;
  66. break;
  67. }
  68. }
  69. if ($hasCprKeys) {
  70. $cpr = $body;
  71. }
  72. }
  73. if (!is_array($cpr)) {
  74. return ResponseLib::sendFail('Missing CPR payload (array) in body as cpr', [], 'E_VALIDATE')->withStatus(400);
  75. }
  76. try {
  77. $cpr = $this->applyFixedCprDefaults($cpr);
  78. } catch (\InvalidArgumentException $e) {
  79. return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
  80. }
  81. $userId = (int)($request->getAttribute('api_user_id') ?? 0);
  82. if ($userId <= 0) {
  83. return ResponseLib::sendFail('Authenticated user not found', [], 'E_VALIDATE')->withStatus(401);
  84. }
  85. $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
  86. if ($companyId <= 0) {
  87. return ResponseLib::sendFail('Authenticated company not found', [], 'E_VALIDATE')->withStatus(401);
  88. }
  89. $statusId = $this->statusModel->getIdByStatus('pending');
  90. if ($statusId === null) {
  91. return ResponseLib::sendFail('Pending status not found', [], 'E_DATABASE')->withStatus(500);
  92. }
  93. try {
  94. $paymentData = $this->paymentService->initiatePayment(self::PAYMENT_VALUE);
  95. } catch (\Throwable $e) {
  96. return ResponseLib::sendFail('Failed to initiate payment: ' . $e->getMessage(), [], 'E_INTERNAL')->withStatus(500);
  97. }
  98. try {
  99. $record = $this->cprModel->create($cpr, $statusId, (int)$paymentData['payment_id'], $userId, $companyId);
  100. } catch (\InvalidArgumentException $e) {
  101. return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
  102. } catch (\Throwable $e) {
  103. return ResponseLib::sendFail('Failed to create CPR: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  104. }
  105. return ResponseLib::sendOk([
  106. 'cpr_id' => $record['cpr_id'] ?? null,
  107. 'payment_id' => $paymentData['payment_id'],
  108. 'payment_code' => $paymentData['payment_code'],
  109. ], 'S_CREATED');
  110. }
  111. }