feat: add QR code generation and response handling to TelepagosIntegrationService with logging for errors

This commit is contained in:
2026-07-06 08:58:14 -03:00
parent 1af5b1d7ed
commit 04b2bb0b2a
2 changed files with 166 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ namespace App\Domains\Integration\Services;
use Exception;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Carbon\Carbon;
class TelepagosIntegrationService extends BaseIntegrationService
@@ -84,12 +85,12 @@ class TelepagosIntegrationService extends BaseIntegrationService
'password' => $password,
]);
if ($response->failed() || $response->json('status') !== 'ok') {
throw new Exception("Telepagos authentication failed: " . ($response->json('message') ?? $response->body()));
}
$data = $this->handleResponse($response, 'authentication', [
'username' => $username,
]);
$token = $response->json('token');
$expiresAtStr = $response->json('expires_at');
$token = $data['token'] ?? null;
$expiresAtStr = $data['expires_at'] ?? null;
if (!$token || !$expiresAtStr) {
throw new Exception("Telepagos authentication response is missing token or expires_at.");
@@ -105,6 +106,54 @@ class TelepagosIntegrationService extends BaseIntegrationService
return $token;
}
/**
* Generate a QR code for cash-in.
*
* @param float $amount
* @param string $concept
* @param string $description
* @return array
* @throws Exception
*/
public function generateQr(float $amount, string $concept, string $description): array
{
$response = $this->client()->post('/v2/payment/cashin/qr/generate', [
'amount' => $amount,
'concept' => $concept,
'description' => $description,
]);
return $this->handleResponse($response, 'QR generation', [
'amount' => $amount,
'concept' => $concept,
'description' => $description,
]);
}
/**
* Handle the Telepagos API response, logging any failures and throwing Exceptions.
*
* @param \Illuminate\Http\Client\Response $response
* @param string $actionDescription
* @param array $context
* @return array
* @throws Exception
*/
protected function handleResponse(\Illuminate\Http\Client\Response $response, string $actionDescription, array $context = []): array
{
if ($response->failed() || $response->json('status') !== 'ok') {
$errorMessage = $response->json('message') ?? $response->body();
Log::error("Telepagos {$actionDescription} failed: {$errorMessage}", array_merge([
'response_status' => $response->status(),
'response_body' => $response->json() ?? $response->body(),
], $context));
throw new Exception("Telepagos {$actionDescription} failed: {$errorMessage}");
}
return $response->json() ?? [];
}
/**
* Clear the cached token.
*