Server : LiteSpeed System : Linux server335.web-hosting.com 4.18.0-553.62.1.lve.el8.x86_64 #1 SMP Mon Jul 21 17:50:35 UTC 2025 x86_64 User : cardxfeb ( 2452) PHP Version : 8.1.34 Disable Function : NONE Directory : /home/cardxfeb/www/app/Console/Commands/ |
<?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;
}
}