File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/TicketMainController.php
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
use App\TicketMain;
use App\Core\Ticket\Application\TicketService;
class TicketMainController extends Controller
{
private $ticketService;
public function __construct()
{
$this->ticketService = new TicketService();
}
public function validatePayments($validateHistoricalPayments = false)
{
$now = Carbon::now();
$ticketMains = TicketMain::select([
'ticket_mains.*'
])
->distinct()
->join('ticket_user_block_backups', 'ticket_user_block_backups.ticket_main_id', '=', 'ticket_mains.id')
->where('ticket_mains.payment_state', '!=', 'CONFIRMED')
->whereNotNull('ticket_mains.gateway_payments_id')
->where('ticket_user_block_backups.active', true)
->where(function ($query) use ($validateHistoricalPayments, $now) {
if ($validateHistoricalPayments) {
$query->where('ticket_mains.created_at', '>', $now->subDays(3)->startOfDay());
} else {
$query->where(function ($query) use ($now) {
$query->where('ticket_mains.payment_attempts', '<', config('payment_gateway.attempts'))
->orWhere('ticket_mains.created_at', '>', $now->subHours(3));
});
}
})
->orderBy('ticket_mains.created_at', 'DESC')
->get();
foreach ($ticketMains as $ticketMain) {
try {
$this->ticketService->validatePayment($ticketMain);
} catch (\Throwable $th) {
$error = [
'ticketMain' => $ticketMain,
'message' => $th->getMessage(),
'getFile' => $th->getFile(),
'getLine' => $th->getLine(),
];
$util = new UtilController;
$util->logFile(json_encode($error));
}
}
return array(
'r' => true,
'data' => null,
'm' => 'Se validaron ' . count($ticketMains) . ' pagos pendientes.'
);
}
public function validatePayment($ticketMain)
{
$ticketMain->payment_attempts = $ticketMain->payment_attempts + 1;
$ticketMain->update();
$this->ticketService->validatePayment($ticketMain);
}
}