feat: enhance Telepagos integration with token refresh handling and refactor CheckoutService to use updateOrCreate for purchases

This commit is contained in:
ncoronel 2026-07-06 16:24:00 -03:00
parent 3703ae9bcf
commit 1fcb3129ea
2 changed files with 42 additions and 17 deletions

View File

@ -106,6 +106,29 @@ class TelepagosIntegrationService extends BaseIntegrationService
return $token;
}
/**
* Send a request to Telepagos, handling 401 Unauthorized for token refresh.
*
* @param string $method
* @param string $endpoint
* @param array $data
* @return \Illuminate\Http\Client\Response
*/
protected function sendRequest(string $method, string $endpoint, array $data = []): \Illuminate\Http\Client\Response
{
$response = $this->client()->$method($endpoint, $data);
if ($response->status() === 401) {
Log::info("Telepagos 401 Unauthorized. Refreshing token and retrying...");
$this->clearToken();
$response = $this->client()->$method($endpoint, $data);
}
return $response;
}
/**
* Generate a QR code for cash-in.
*
@ -117,17 +140,15 @@ class TelepagosIntegrationService extends BaseIntegrationService
*/
public function generateQr(float $amount, string $concept, string $description): array
{
$response = $this->client()->post('/v2/payment/cashin/qr/generate', [
$payload = [
'amount' => $amount,
'concept' => $concept,
'description' => $description,
]);
];
return $this->handleResponse($response, 'QR generation', [
'amount' => $amount,
'concept' => $concept,
'description' => $description,
]);
$response = $this->sendRequest('post', '/v2/payment/cashin/qr/generate', $payload);
return $this->handleResponse($response, 'QR generation', $payload);
}
/**
@ -139,7 +160,7 @@ class TelepagosIntegrationService extends BaseIntegrationService
*/
public function getCashinDetails(int $cashinId): array
{
$response = $this->client()->get("/v2/payment/cashin/{$cashinId}");
$response = $this->sendRequest('get', "/v2/payment/cashin/{$cashinId}");
return $this->handleResponse($response, 'get cash-in details', [
'cashin_id' => $cashinId,

View File

@ -30,15 +30,19 @@ class CheckoutService
}
/** @var Purchase $purchase */
$purchase = Purchase::query()->create([
...$purchaseData,
'cart_id' => $cart->getKey(),
'tenant_codigo' => $tenant->codigo,
'user_id' => $userId,
'status' => 'pending',
'payment_status' => 'pending',
'payment_method' => null,
]);
$purchase = Purchase::query()->updateOrCreate(
[
'cart_id' => $cart->getKey(),
'status' => 'pending',
'tenant_codigo' => $tenant->codigo,
'user_id' => $userId,
],
[
...$purchaseData,
'payment_status' => 'pending',
'payment_method' => null,
]
);
return $purchase->load(['items.variant.product', 'items.variant.definitions.productAttribute.attribute']);
});