B3CprRegisterController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use React\Http\Message\Response;
  6. use Services\B3CprService;
  7. use Models\CprModel;
  8. use Models\StatusModel;
  9. use Models\PaymentModel;
  10. class B3CprRegisterController
  11. {
  12. private B3CprService $service;
  13. private CprModel $cprModel;
  14. private StatusModel $statusModel;
  15. private PaymentModel $paymentModel;
  16. public function __construct()
  17. {
  18. $this->service = new B3CprService();
  19. $this->cprModel = new CprModel();
  20. $this->statusModel = new StatusModel();
  21. $this->paymentModel = new PaymentModel();
  22. }
  23. public function __invoke(ServerRequestInterface $request)
  24. {
  25. $body = json_decode((string)$request->getBody(), true);
  26. if (!is_array($body)) {
  27. return ResponseLib::sendFail('Invalid JSON body', [], 'E_VALIDATE')->withStatus(400);
  28. }
  29. $token = $body['b3_access_token'] ?? ($body['access_token'] ?? null);
  30. if (!$token) {
  31. $b3Auth = $request->getHeaderLine('X-B3-Authorization') ?: '';
  32. if (stripos($b3Auth, 'Bearer ') === 0) {
  33. $token = trim(substr($b3Auth, 7));
  34. }
  35. }
  36. if (!$token) {
  37. $token = $request->getHeaderLine('X-B3-Access-Token') ?: null;
  38. }
  39. $cpr = $body['cpr'] ?? null;
  40. if (!is_array($cpr)) {
  41. $hasCprKeys = false;
  42. foreach ($body as $k => $_) {
  43. if (is_string($k) && substr($k, 0, 4) === 'cpr_') {
  44. $hasCprKeys = true;
  45. break;
  46. }
  47. }
  48. if ($hasCprKeys) {
  49. $cpr = $body;
  50. }
  51. }
  52. if (!is_array($cpr)) {
  53. return ResponseLib::sendFail('Missing CPR payload (array) in body as cpr', [], 'E_VALIDATE')->withStatus(400);
  54. }
  55. $userId = (int)($request->getAttribute('api_user_id') ?? 0);
  56. if ($userId <= 0) {
  57. return ResponseLib::sendFail('Authenticated user not found', [], 'E_VALIDATE')->withStatus(401);
  58. }
  59. $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
  60. if ($companyId <= 0) {
  61. return ResponseLib::sendFail('Authenticated company not found', [], 'E_VALIDATE')->withStatus(401);
  62. }
  63. $statusId = $this->statusModel->getIdByStatus('pending');
  64. if ($statusId === null) {
  65. return ResponseLib::sendFail('Pending status not found', [], 'E_DATABASE')->withStatus(500);
  66. }
  67. try {
  68. $paymentExternalId = 'B3_DIRECT_' . time();
  69. $paymentId = $this->paymentModel->create($paymentExternalId, $statusId, $userId);
  70. } catch (\Throwable $e) {
  71. return ResponseLib::sendFail('Failed to create payment record: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  72. }
  73. try {
  74. $record = $this->cprModel->create($cpr, $statusId, $paymentId, $userId, $companyId);
  75. } catch (\InvalidArgumentException $e) {
  76. return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
  77. } catch (\Throwable $e) {
  78. return ResponseLib::sendFail('Failed to create CPR: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  79. }
  80. try {
  81. $payload = $this->service->mapToB3($cpr);
  82. if (!$token) {
  83. $token = $this->service->getAccessToken();
  84. }
  85. $result = $this->service->postCpr($token, $payload);
  86. } catch (\Throwable $e) {
  87. return ResponseLib::sendFail('Failed to send CPR to B3: ' . $e->getMessage(), [], 'E_EXTERNAL')->withStatus(502);
  88. }
  89. if (isset($result['error'])) {
  90. return ResponseLib::sendFail('cURL error during B3 CPR request', ['error' => $result['error']], 'E_EXTERNAL')->withStatus(502);
  91. }
  92. $status = (int)($result['status'] ?? 200);
  93. if (isset($result['json'])) {
  94. return Response::json($result['json'])->withStatus($status ?: 200);
  95. }
  96. return Response::json(['raw' => $result['raw'] ?? null, 'status' => $status])->withStatus($status ?: 502);
  97. }
  98. }