| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace Controllers;
- use Libs\ResponseLib;
- use Psr\Http\Message\ServerRequestInterface;
- use Models\CprModel;
- use Models\StatusModel;
- use Services\PaymentService;
- class B3CprRegisterController
- {
- private CprModel $cprModel;
- private StatusModel $statusModel;
- private PaymentService $paymentService;
- private const PAYMENT_VALUE = 10000;
- public function __construct()
- {
- $this->cprModel = new CprModel();
- $this->statusModel = new StatusModel();
- $this->paymentService = new PaymentService();
- }
- private function applyFixedCprDefaults(array $cpr): array
- {
- $nowBr = new \DateTimeImmutable('now', new \DateTimeZone('America/Sao_Paulo'));
- $currentDate = $nowBr->format('Y-m-d');
- $cpr['cpr_type_code'] = 'P';
- $cpr['cpr_otc_register_account_code'] = '64359.40-5';
- $cpr['cpr_otc_payment_agent_account_code'] = '64359.40-5';
- $cpr['cpr_otc_custodian_account_code'] = '64359.00-3';
- $cpr['cpr_electronic_emission_indicator'] = 'S';
- $cpr['cpr_automatic_expiration_indicator'] = 'N';
- $cpr['cpr_issue_date'] = $currentDate;
- $cpr['cpr_profitability_start_date'] = $currentDate;
- $cpr['cpr_issue_quantity'] = '1';
- if (!array_key_exists('cpr_issue_value', $cpr)) {
- throw new \InvalidArgumentException('Missing field: cpr_issue_value');
- }
- $issueValue = (string)$cpr['cpr_issue_value'];
- $cpr['cpr_issue_financial_value'] = $issueValue;
- $cpr['cpr_creditor_name'] = 'TOO EASY TRADING LTDA';
- $cpr['cpr_creditor_document_number'] = '47.175.222/0001-09';
- $cpr['cpr_scr_type_code'] = 'N';
- $cpr['cpr_finality_code'] = '6099';
- return $cpr;
- }
- public function __invoke(ServerRequestInterface $request)
- {
- $body = json_decode((string)$request->getBody(), true);
- if (!is_array($body)) {
- return ResponseLib::sendFail('Invalid JSON body', [], 'E_VALIDATE')->withStatus(400);
- }
- $cpr = $body['cpr'] ?? null;
- if (!is_array($cpr)) {
- $hasCprKeys = false;
- foreach ($body as $k => $_) {
- if (is_string($k) && substr($k, 0, 4) === 'cpr_') {
- $hasCprKeys = true;
- break;
- }
- }
- if ($hasCprKeys) {
- $cpr = $body;
- }
- }
- if (!is_array($cpr)) {
- return ResponseLib::sendFail('Missing CPR payload (array) in body as cpr', [], 'E_VALIDATE')->withStatus(400);
- }
- try {
- $cpr = $this->applyFixedCprDefaults($cpr);
- } catch (\InvalidArgumentException $e) {
- return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
- }
- $userId = (int)($request->getAttribute('api_user_id') ?? 0);
- if ($userId <= 0) {
- return ResponseLib::sendFail('Authenticated user not found', [], 'E_VALIDATE')->withStatus(401);
- }
- $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
- if ($companyId <= 0) {
- return ResponseLib::sendFail('Authenticated company not found', [], 'E_VALIDATE')->withStatus(401);
- }
- $statusId = $this->statusModel->getIdByStatus('pending');
- if ($statusId === null) {
- return ResponseLib::sendFail('Pending status not found', [], 'E_DATABASE')->withStatus(500);
- }
- try {
- $paymentData = $this->paymentService->initiatePayment(self::PAYMENT_VALUE);
- } catch (\Throwable $e) {
- return ResponseLib::sendFail('Failed to initiate payment: ' . $e->getMessage(), [], 'E_INTERNAL')->withStatus(500);
- }
- try {
- $record = $this->cprModel->create($cpr, $statusId, (int)$paymentData['payment_id'], $userId, $companyId);
- } catch (\InvalidArgumentException $e) {
- return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
- } catch (\Throwable $e) {
- return ResponseLib::sendFail('Failed to create CPR: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
- }
- return ResponseLib::sendOk([
- 'cpr_id' => $record['cpr_id'] ?? null,
- 'payment_id' => $paymentData['payment_id'],
- 'payment_code' => $paymentData['payment_code'],
- ], 'S_CREATED');
- }
- }
|