From 5edd42ba6cc89fcde4adb8e53699f1955540aef2 Mon Sep 17 00:00:00 2001 From: lizandrogd <77708265+lizandrogd@users.noreply.github.com> Date: Tue, 2 Sep 2025 20:44:19 -0500 Subject: [PATCH] Update ShowServicios.php --- app/Http/Livewire/ShowServicios.php | 39 +++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/app/Http/Livewire/ShowServicios.php b/app/Http/Livewire/ShowServicios.php index b95b86d..ceababf 100755 --- a/app/Http/Livewire/ShowServicios.php +++ b/app/Http/Livewire/ShowServicios.php @@ -27,6 +27,7 @@ use Illuminate\Support\Facades\Notification; use Twilio\Rest\Client; use AshAllenDesign\ShortURL\Facades\ShortURL; use Illuminate\Support\Facades\DB; +use Illuminate\Database\QueryException; class ShowServicios extends Component { @@ -550,20 +551,38 @@ class ShowServicios extends Component $this->tarjetaPlan = false; $this->cantidad_tarjetaPlan = 1; - } catch (Exception $e) { - // Hacer rollback de cualquier cambio realizado en la base de datos + + // si todo salió bien → salir del bucle + break; + } catch (QueryException $e) { DB::rollBack(); - if ($intento >= $maxIntentos) { - $this->credenciales = false; - $this->cuentas = []; + // PostgreSQL/MariaDB lanza 23505 cuando hay conflicto de índice único + if ($e->getCode() == '23505') { + // ⚡ Reintentar, otra transacción tomó la misma cuenta + if ($intento >= $maxIntentos) { + $this->alert('warning', 'Error: Otra persona tomó la cuenta al mismo tiempo. Intenta nuevamente.', [ + 'position' => 'top' + ]); + return; + } - $this->alert('warning', 'Error al comprar: Esta cuenta ha sido tomada por otro usuario al mismo tiempo, intenta nuevamente. Error: ' . $e->getMessage(), [ - 'position' => 'top' - ]); - return; + usleep(200000); // 200ms antes de reintentar + continue; } - usleep(200000); // 200ms + + // Otros errores no son de concurrencia + $this->alert('error', 'Error inesperado en la compra: ' . $e->getMessage(), [ + 'position' => 'top' + ]); + return; + } catch (\Throwable $e) { + DB::rollBack(); + + $this->alert('error', 'Error inesperado: ' . $e->getMessage(), [ + 'position' => 'top' + ]); + return; } } }