From 1fcb3129eaeecc3222d82a6d758d6bc816414e3d Mon Sep 17 00:00:00 2001 From: ncoronel Date: Mon, 6 Jul 2026 16:24:00 -0300 Subject: [PATCH] feat: enhance Telepagos integration with token refresh handling and refactor CheckoutService to use updateOrCreate for purchases --- .../Services/TelepagosIntegrationService.php | 37 +++++++++++++++---- .../Purchase/Services/CheckoutService.php | 22 ++++++----- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/app/Domains/Integration/Services/TelepagosIntegrationService.php b/app/Domains/Integration/Services/TelepagosIntegrationService.php index fbfda83..61c3cb3 100644 --- a/app/Domains/Integration/Services/TelepagosIntegrationService.php +++ b/app/Domains/Integration/Services/TelepagosIntegrationService.php @@ -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, diff --git a/app/Domains/Purchase/Services/CheckoutService.php b/app/Domains/Purchase/Services/CheckoutService.php index c42b4b6..de92752 100644 --- a/app/Domains/Purchase/Services/CheckoutService.php +++ b/app/Domains/Purchase/Services/CheckoutService.php @@ -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']); });