From b26f6f1130a2b9db4a38d950d7732ab81cb15827 Mon Sep 17 00:00:00 2001 From: Lizandro Date: Sat, 18 Jul 2026 16:15:45 +0000 Subject: [PATCH] Services and promos purchasable only with balance; redirect to recharge if short Remove MercadoPago as a direct payment option for services and promotions. If the user has enough balance: show only "Pagar con saldo". If not: show current balance, amount missing, and a "Recargar saldo" button that leads to the recharge flow (where MercadoPago and Bre-B remain available). Applies to both TelegramBotService (startPurchase, viewPromo) and PublicChat (iniciarCompra, verPromocion). Co-Authored-By: Claude Sonnet 4.6 --- app/Http/Livewire/Chat/PublicChat.php | 87 ++++++++++++++++----------- app/Services/TelegramBotService.php | 52 +++++++++++----- 2 files changed, 89 insertions(+), 50 deletions(-) diff --git a/app/Http/Livewire/Chat/PublicChat.php b/app/Http/Livewire/Chat/PublicChat.php index a72da6a..d012a86 100755 --- a/app/Http/Livewire/Chat/PublicChat.php +++ b/app/Http/Livewire/Chat/PublicChat.php @@ -648,26 +648,36 @@ class PublicChat extends Component 'pantallas' => $tarifa->pantallas, ]; - $detalle = $tarifa->servicio->por_tiempo + $detalle = $tarifa->servicio->por_tiempo ? "{$tarifa->dias} dias" : "{$tarifa->pantallas} pantalla(s) / {$tarifa->dias} dias"; - $saldoInsuficiente = ! $this->userId || ((float) $this->saldoUsuario < $precio); - ChatMessage::create([ - 'conversation_id' => $this->convId, - 'tipo' => 'bot', - 'tipo_ui' => 'buttons', - 'contenido' => "{$tarifa->servicio->nombre} - {$detalle}\nPrecio: $" . number_format($precio) . "\n\nElige como pagar:", - 'payload' => ['botones' => [ - ['label' => 'Saldo ($' . number_format($precio) . ')', - 'action' => 'compra.pagar.saldo', - 'data' => [], - 'disabled' => $saldoInsuficiente], - ['label' => 'MercadoPago', 'action' => 'compra.pagar.mp', 'data' => []], - ['label' => 'Volver', 'action' => 'servicios.listar', 'data' => []], - ]], - 'leido' => true, - ]); + if ((float) $this->saldoUsuario >= $precio) { + ChatMessage::create([ + 'conversation_id' => $this->convId, + 'tipo' => 'bot', + 'tipo_ui' => 'buttons', + 'contenido' => "{$tarifa->servicio->nombre} - {$detalle}\nPrecio: $" . number_format($precio) . "\n\nElige como pagar:", + 'payload' => ['botones' => [ + ['label' => 'Pagar con saldo ($' . number_format($precio) . ')', 'action' => 'compra.pagar.saldo', 'data' => []], + ['label' => 'Volver', 'action' => 'servicios.listar', 'data' => []], + ]], + 'leido' => true, + ]); + } else { + $falta = $precio - (float) $this->saldoUsuario; + ChatMessage::create([ + 'conversation_id' => $this->convId, + 'tipo' => 'bot', + 'tipo_ui' => 'buttons', + 'contenido' => "{$tarifa->servicio->nombre} - {$detalle}\nPrecio: $" . number_format($precio) . "\n\nSaldo actual: $" . number_format($this->saldoUsuario) . " โ€” te faltan $" . number_format($falta) . "\n\nRecarga tu saldo para continuar:", + 'payload' => ['botones' => [ + ['label' => 'Recargar saldo', 'action' => 'recarga.iniciar', 'data' => []], + ['label' => 'Volver', 'action' => 'servicios.listar', 'data' => []], + ]], + 'leido' => true, + ]); + } } private function pagarConSaldo(): void @@ -843,23 +853,32 @@ class PublicChat extends Component $tp = $promo->tarifaPromo->first(); $precio = $upric ? $upric->precio : ($tp ? $tp->precio : $promo->precio); - $saldoInsuficiente = ! $this->userId || ($this->saldoUsuario < $precio); - - ChatMessage::create([ - 'conversation_id' => $this->convId, - 'tipo' => 'bot', - 'tipo_ui' => 'buttons', - 'contenido' => ($promo->nombre ?? 'Promocion') . "\n$" . number_format($precio) . "\n\nElige como pagar:", - 'payload' => ['botones' => [ - ['label' => 'Saldo ($' . number_format($precio) . ')', - 'action' => 'promo.comprar.saldo', - 'data' => ['id' => $id], - 'disabled' => $saldoInsuficiente], - ['label' => 'MercadoPago', 'action' => 'promo.comprar.mp', 'data' => ['id' => $id]], - ['label' => 'Volver', 'action' => 'promo.listar', 'data' => []], - ]], - 'leido' => true, - ]); + if ((float) $this->saldoUsuario >= $precio) { + ChatMessage::create([ + 'conversation_id' => $this->convId, + 'tipo' => 'bot', + 'tipo_ui' => 'buttons', + 'contenido' => ($promo->nombre ?? 'Promocion') . "\n$" . number_format($precio) . "\n\nElige como pagar:", + 'payload' => ['botones' => [ + ['label' => 'Pagar con saldo ($' . number_format($precio) . ')', 'action' => 'promo.comprar.saldo', 'data' => ['id' => $id]], + ['label' => 'Volver', 'action' => 'promo.listar', 'data' => []], + ]], + 'leido' => true, + ]); + } else { + $falta = $precio - (float) $this->saldoUsuario; + ChatMessage::create([ + 'conversation_id' => $this->convId, + 'tipo' => 'bot', + 'tipo_ui' => 'buttons', + 'contenido' => ($promo->nombre ?? 'Promocion') . "\n$" . number_format($precio) . "\n\nSaldo actual: $" . number_format($this->saldoUsuario) . " โ€” te faltan $" . number_format($falta) . "\n\nRecarga tu saldo para continuar:", + 'payload' => ['botones' => [ + ['label' => 'Recargar saldo', 'action' => 'recarga.iniciar', 'data' => []], + ['label' => 'Volver', 'action' => 'promo.listar', 'data' => []], + ]], + 'leido' => true, + ]); + } } private function comprarPromoConSaldo(int $promoId): void diff --git a/app/Services/TelegramBotService.php b/app/Services/TelegramBotService.php index 1cd0bf9..0ae7a18 100755 --- a/app/Services/TelegramBotService.php +++ b/app/Services/TelegramBotService.php @@ -700,17 +700,27 @@ class TelegramBotService : "{$tarifa->pantallas} pantalla(s) / {$tarifa->dias} dรญas"; $buttons = []; + if ($saldo >= $valor) { $buttons[] = [['text' => "๐Ÿ’ณ Pagar con saldo (\$" . number_format($valor) . ")", 'callback_data' => 'buy_saldo']]; - } - $buttons[] = [['text' => '๐Ÿฆ MercadoPago', 'callback_data' => 'buy_mp']]; - $buttons[] = [['text' => '๐Ÿ”™ Ver planes', 'callback_data' => 'svc_view|' . $tarifa->servicio_id]]; + $buttons[] = [['text' => '๐Ÿ”™ Ver planes', 'callback_data' => 'svc_view|' . $tarifa->servicio_id]]; - $this->sendWithKeyboard( - $chatId, - "*{$tarifa->servicio->nombre}* โ€” {$detalle}\nPrecio: *\$" . number_format($valor) . "*\n\nElige cรณmo pagar:", - $buttons - ); + $this->sendWithKeyboard( + $chatId, + "*{$tarifa->servicio->nombre}* โ€” {$detalle}\nPrecio: *\$" . number_format($valor) . "*\n\nElige cรณmo pagar:", + $buttons + ); + } else { + $falta = $valor - $saldo; + $buttons[] = [['text' => '๐Ÿ’ฐ Recargar saldo', 'callback_data' => 'rec_init']]; + $buttons[] = [['text' => '๐Ÿ”™ Ver planes', 'callback_data' => 'svc_view|' . $tarifa->servicio_id]]; + + $this->sendWithKeyboard( + $chatId, + "*{$tarifa->servicio->nombre}* โ€” {$detalle}\nPrecio: *\$" . number_format($valor) . "*\n\nSaldo actual: \$" . number_format($saldo) . " โ€” te faltan *\$" . number_format($falta) . "*\n\nRecarga para continuar:", + $buttons + ); + } } private function payWithSaldo(string $chatId): void @@ -883,17 +893,27 @@ class TelegramBotService $precio = $upric ? $upric->precio : ($tp ? $tp->precio : $promo->precio); $buttons = []; + if ($saldo >= $precio) { $buttons[] = [['text' => "๐Ÿ’ณ Pagar con saldo (\$" . number_format($precio) . ")", 'callback_data' => 'promo_saldo|' . $promoId]]; - } - $buttons[] = [['text' => '๐Ÿฆ MercadoPago', 'callback_data' => 'promo_mp|' . $promoId]]; - $buttons[] = [['text' => '๐Ÿ”™ Promociones', 'callback_data' => 'promo_list']]; + $buttons[] = [['text' => '๐Ÿ”™ Promociones', 'callback_data' => 'promo_list']]; - $this->sendWithKeyboard( - $chatId, - "*{$promo->nombre}*\n\$" . number_format($precio) . "\n\nElige cรณmo pagar:", - $buttons - ); + $this->sendWithKeyboard( + $chatId, + "*{$promo->nombre}*\n\$" . number_format($precio) . "\n\nElige cรณmo pagar:", + $buttons + ); + } else { + $falta = $precio - $saldo; + $buttons[] = [['text' => '๐Ÿ’ฐ Recargar saldo', 'callback_data' => 'rec_init']]; + $buttons[] = [['text' => '๐Ÿ”™ Promociones', 'callback_data' => 'promo_list']]; + + $this->sendWithKeyboard( + $chatId, + "*{$promo->nombre}*\n\$" . number_format($precio) . "\n\nSaldo actual: \$" . number_format($saldo) . " โ€” te faltan *\$" . number_format($falta) . "*\n\nRecarga para continuar:", + $buttons + ); + } } private function buyPromoSaldo(string $chatId, int $promoId): void