CleanupOpenPayments.php 1018 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Payment;
  4. use Illuminate\Console\Command;
  5. class CleanupOpenPayments extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'payments:open:clear';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Clears all payments from the database that have state "open"';
  19. /**
  20. * Execute the console command.
  21. *
  22. * @return int
  23. */
  24. public function handle()
  25. {
  26. // delete all payments that have state "open" and are older than 1 hour
  27. try {
  28. Payment::where('status', 'open')->where('updated_at', '<', now()->subHour())->delete();
  29. } catch (\Exception $e) {
  30. $this->error('Could not delete payments: ' . $e->getMessage());
  31. return 1;
  32. }
  33. $this->info('Successfully deleted all open payments');
  34. return Command::SUCCESS;
  35. }
  36. }