|
|
@@ -4,25 +4,23 @@ namespace Controllers;
|
|
|
|
|
|
use Libs\ResponseLib;
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
-use React\Http\Message\Response;
|
|
|
-use Services\B3CprService;
|
|
|
use Models\CprModel;
|
|
|
use Models\StatusModel;
|
|
|
-use Models\PaymentModel;
|
|
|
+use Services\PaymentService;
|
|
|
|
|
|
class B3CprRegisterController
|
|
|
{
|
|
|
- private B3CprService $service;
|
|
|
private CprModel $cprModel;
|
|
|
private StatusModel $statusModel;
|
|
|
- private PaymentModel $paymentModel;
|
|
|
+ private PaymentService $paymentService;
|
|
|
+
|
|
|
+ private const PAYMENT_VALUE = 10000;
|
|
|
|
|
|
public function __construct()
|
|
|
{
|
|
|
- $this->service = new B3CprService();
|
|
|
$this->cprModel = new CprModel();
|
|
|
$this->statusModel = new StatusModel();
|
|
|
- $this->paymentModel = new PaymentModel();
|
|
|
+ $this->paymentService = new PaymentService();
|
|
|
}
|
|
|
|
|
|
private function applyFixedCprDefaults(array $cpr): array
|
|
|
@@ -61,18 +59,8 @@ class B3CprRegisterController
|
|
|
return ResponseLib::sendFail('Invalid JSON body', [], 'E_VALIDATE')->withStatus(400);
|
|
|
}
|
|
|
|
|
|
- $token = $body['b3_access_token'] ?? ($body['access_token'] ?? null);
|
|
|
- if (!$token) {
|
|
|
- $b3Auth = $request->getHeaderLine('X-B3-Authorization') ?: '';
|
|
|
- if (stripos($b3Auth, 'Bearer ') === 0) {
|
|
|
- $token = trim(substr($b3Auth, 7));
|
|
|
- }
|
|
|
- }
|
|
|
- if (!$token) {
|
|
|
- $token = $request->getHeaderLine('X-B3-Access-Token') ?: null;
|
|
|
- }
|
|
|
-
|
|
|
$cpr = $body['cpr'] ?? null;
|
|
|
+
|
|
|
if (!is_array($cpr)) {
|
|
|
$hasCprKeys = false;
|
|
|
foreach ($body as $k => $_) {
|
|
|
@@ -112,39 +100,23 @@ class B3CprRegisterController
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- $paymentExternalId = 'B3_DIRECT_' . time();
|
|
|
- $paymentId = $this->paymentModel->create($paymentExternalId, $statusId, $userId);
|
|
|
+ $paymentData = $this->paymentService->initiatePayment(self::PAYMENT_VALUE);
|
|
|
} catch (\Throwable $e) {
|
|
|
- return ResponseLib::sendFail('Failed to create payment record: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
|
|
|
+ return ResponseLib::sendFail('Failed to initiate payment: ' . $e->getMessage(), [], 'E_INTERNAL')->withStatus(500);
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- $record = $this->cprModel->create($cpr, $statusId, $paymentId, $userId, $companyId);
|
|
|
+ $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);
|
|
|
}
|
|
|
|
|
|
- try {
|
|
|
- $payload = $this->service->mapToB3($cpr);
|
|
|
- if (!$token) {
|
|
|
- $token = $this->service->getAccessToken();
|
|
|
- }
|
|
|
- $result = $this->service->postCpr($token, $payload);
|
|
|
- } catch (\Throwable $e) {
|
|
|
- return ResponseLib::sendFail('Failed to send CPR to B3: ' . $e->getMessage(), [], 'E_EXTERNAL')->withStatus(502);
|
|
|
- }
|
|
|
-
|
|
|
- if (isset($result['error'])) {
|
|
|
- return ResponseLib::sendFail('cURL error during B3 CPR request', ['error' => $result['error']], 'E_EXTERNAL')->withStatus(502);
|
|
|
- }
|
|
|
-
|
|
|
- $status = (int)($result['status'] ?? 200);
|
|
|
- if (isset($result['json'])) {
|
|
|
- return Response::json($result['json'])->withStatus($status ?: 200);
|
|
|
- }
|
|
|
-
|
|
|
- return Response::json(['raw' => $result['raw'] ?? null, 'status' => $status])->withStatus($status ?: 502);
|
|
|
+ return ResponseLib::sendOk([
|
|
|
+ 'cpr_id' => $record['cpr_id'] ?? null,
|
|
|
+ 'payment_id' => $paymentData['payment_id'],
|
|
|
+ 'payment_code' => $paymentData['payment_code'],
|
|
|
+ ], 'S_CREATED');
|
|
|
}
|
|
|
}
|