B3CprRegisterController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. $body = json_decode((string)$request->getBody(), true);
  47. if (!is_array($body)) {
  48. return ResponseLib::sendFail('Invalid JSON body', [], 'E_VALIDATE')->withStatus(400);
  49. }
  50. $cpr = $body['cpr'] ?? null;
  51. if (!is_array($cpr)) {
  52. $hasCprKeys = false;
  53. foreach ($body as $k => $_) {
  54. if (is_string($k) && substr($k, 0, 4) === 'cpr_') {
  55. $hasCprKeys = true;
  56. break;
  57. }
  58. }
  59. if ($hasCprKeys) {
  60. $cpr = $body;
  61. }
  62. }
  63. if (!is_array($cpr)) {
  64. return ResponseLib::sendFail('Missing CPR payload (array) in body as cpr', [], 'E_VALIDATE')->withStatus(400);
  65. }
  66. try {
  67. $cpr = $this->applyFixedCprDefaults($cpr);
  68. } catch (\InvalidArgumentException $e) {
  69. return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
  70. }
  71. $userId = (int)($request->getAttribute('api_user_id') ?? 0);
  72. if ($userId <= 0) {
  73. return ResponseLib::sendFail('Authenticated user not found', [], 'E_VALIDATE')->withStatus(401);
  74. }
  75. $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
  76. if ($companyId <= 0) {
  77. return ResponseLib::sendFail('Authenticated company not found', [], 'E_VALIDATE')->withStatus(401);
  78. }
  79. $statusId = $this->statusModel->getIdByStatus('pending');
  80. if ($statusId === null) {
  81. return ResponseLib::sendFail('Pending status not found', [], 'E_DATABASE')->withStatus(500);
  82. }
  83. try {
  84. $paymentData = $this->paymentService->initiatePayment(self::PAYMENT_VALUE);
  85. } catch (\Throwable $e) {
  86. return ResponseLib::sendFail('Failed to initiate payment: ' . $e->getMessage(), [], 'E_INTERNAL')->withStatus(500);
  87. }
  88. try {
  89. $record = $this->cprModel->create($cpr, $statusId, (int)$paymentData['payment_id'], $userId, $companyId);
  90. } catch (\InvalidArgumentException $e) {
  91. return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
  92. } catch (\Throwable $e) {
  93. return ResponseLib::sendFail('Failed to create CPR: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  94. }
  95. return ResponseLib::sendOk([
  96. 'cpr_id' => $record['cpr_id'] ?? null,
  97. 'payment_id' => $paymentData['payment_id'],
  98. 'payment_code' => $paymentData['payment_code'],
  99. ], 'S_CREATED');
  100. }
  101. }