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

This commit is contained in:
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; 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. * 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 public function generateQr(float $amount, string $concept, string $description): array
{ {
$response = $this->client()->post('/v2/payment/cashin/qr/generate', [ $payload = [
'amount' => $amount, 'amount' => $amount,
'concept' => $concept, 'concept' => $concept,
'description' => $description, 'description' => $description,
]); ];
return $this->handleResponse($response, 'QR generation', [ $response = $this->sendRequest('post', '/v2/payment/cashin/qr/generate', $payload);
'amount' => $amount,
'concept' => $concept, return $this->handleResponse($response, 'QR generation', $payload);
'description' => $description,
]);
} }
/** /**
@@ -139,7 +160,7 @@ class TelepagosIntegrationService extends BaseIntegrationService
*/ */
public function getCashinDetails(int $cashinId): array 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', [ return $this->handleResponse($response, 'get cash-in details', [
'cashin_id' => $cashinId, 'cashin_id' => $cashinId,

View File

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