PATH:
home
/
cardxfeb
/
public_html
/
app
/
Console
/
Commands
/
Editing: CheckPendingPayments.php
<?php namespace App\Console\Commands; use App\Models\VCard; use App\Mail\PaymentWarningMail; use Illuminate\Console\Command; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Log; class CheckPendingPayments extends Command { protected $signature = 'payments:check-pending'; protected $description = 'Avisa utilizadores com pagamento pendente (24h) e remove cartoes apos 48h'; public function handle(): int { $now = now(); $this->info("[{$now}] Iniciando verificacao de pagamentos pendentes..."); // AVISO 24H // VCards criados entre 24h e 36h atras, ainda sem pagamento $toWarn = VCard::where('status', 'pending_payment') ->whereBetween('created_at', [ $now->copy()->subHours(36), $now->copy()->subHours(24), ]) ->whereNull('warned_at') ->with('user') ->get(); foreach ($toWarn as $vcard) { try { Mail::to($vcard->user->email)->send(new PaymentWarningMail($vcard, 'warning')); $vcard->update(['warned_at' => $now]); $this->line(" [AVISO] Email enviado para: {$vcard->user->email}"); Log::info("[CheckPendingPayments] Aviso 24h enviado: user#{$vcard->user->id}"); } catch (\Exception $e) { Log::error("[CheckPendingPayments] Erro ao enviar aviso: " . $e->getMessage()); } } // SOFT DELETE 48H // VCards criados ha mais de 48h, ainda sem pagamento Soft Delete $toDelete = VCard::where('status', 'pending_payment') ->where('created_at', '<', $now->copy()->subHours(48)) ->with('user') ->get(); foreach ($toDelete as $vcard) { try { // Notifica antes de apagar Mail::to($vcard->user->email)->send(new PaymentWarningMail($vcard, 'deleted')); $vcard->delete(); // Soft delete (preserva dados, permite recuperacao) $this->line(" [APAGADO] VCard#{$vcard->id} do user {$vcard->user->email} removido (soft)"); Log::warning("[CheckPendingPayments] Soft delete: vcard#{$vcard->id} user#{$vcard->user->id}"); } catch (\Exception $e) { Log::error("[CheckPendingPayments] Erro ao apagar: " . $e->getMessage()); } } $this->info("Concluido. Avisos: {$toWarn->count()} | Apagados: {$toDelete->count()}"); return Command::SUCCESS; } }
SAVE
CANCEL