HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/comfama.sports-crowd.com/app/Http/Controllers/AbonadosController.php
<?php

namespace App\Http\Controllers;

use App\CorporateIdentity;
use DB;
use App\User;
use App\Parameter;
use App\TicketMain;
use App\PreSubscriber;
use Carbon\Carbon;
use App\Http\Controllers\Api\TicketApiController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AbonadosController extends Controller
{

    const SUBSCRIBER_TICKET = 2;

    public function index()
    {
        $corporateIdentity = CorporateIdentity::first();
        $parameters = Parameter::select('web_presuscription')->first();
        if ($parameters->web_presuscription)
            return view('subscriber.abonados');
        else
            return view('subscriber.notAvailable', compact('corporateIdentity'));
    }

    public function validation($document, $api = false)
    {
        $corporateIdentity = CorporateIdentity::first();
        $parameters = Parameter::select('web_presuscription', 'presuscription_msj')->first();
        if (!$parameters->web_presuscription && !$api)
            return view('subscriber.notAvailable', compact('corporateIdentity'));

        if (Auth::user() && $api) {
            $user = Auth::user();
        } else {
            $user = User::select('id', 'document', 'email', 'first_name', 'last_name')
                ->where([['document', $document], ['active', 1]])->orderBy('last_session', 'desc')->first();
        }
        if (!$user) {
            return response(array('r' => false, 'message' => trans('messages.subscribers.title_1')));
        }

        $pre_subscribers = PreSubscriber::select('id', 'document', 'email', 'payment', 'is_credit', 'price', 'seat_id')
            ->where([['document', $document], ['email', 'LIKE', '%' . $user->email . '%']])->with('seat')->get();
        if (count($pre_subscribers) == 0) {
            return response(array('r' => false, 'message' => $parameters->presuscription_msj));
        }

        $sql = "SELECT tm.id 
            FROM `ticket_mains` tm
            RIGHT JOIN `ticket_user_blocks` tu ON tu.ticket_main_id = tm.id AND tu.is_social_distancing = false AND tu.ticket_type_id = " . self::SUBSCRIBER_TICKET . "
            WHERE tm.user_id_log =" . $user->id;
        $tickets = DB::select(DB::raw($sql));
        if (count($tickets) > 0) {
            return response(array('r' => false, 'message' => 'Se registra un pago pendiente, por favor finalice el pago y vuelva a intentarlo'));
        }

        return response(array('r' => true, 'message' => 'Usuario registrado', 'user' => $user, 'pre_subscriber' => $pre_subscribers));
    }

    public function makePayment(Request $request, $api = false)
    {
        $corporateIdentity = CorporateIdentity::first();
        $parameters = Parameter::select('web_presuscription', 'presuscription_msj')->first();
        if (!$parameters->web_presuscription && !$api)
            return view('subscriber.notAvailable', compact('corporateIdentity'));

        $subscribedUser = User::select('id')->where([['document', $request['user_abonado']], ['active', 1]])->orderBy('last_session', 'desc')->first();
        $pre_subscribers = PreSubscriber::where([['document', $request['user_abonado']], ['email', 'LIKE', '%' . $subscribedUser->email . '%']])->whereIn('id', $request['abonados_array'])->with('seat')->get();
        if (count($pre_subscribers) == 0) {
            return response(array('r' => false, 'm' => $parameters->presuscription_msj ?? 'No se han encontrado abonos, por favor revisar si el usuario que intenta realizar la compra no corresponde al usuario registrado para el abono'));
        }
        $total = 0;
        $tickets_array = [];

        if (isset($request['match_event_id']))
            $match_events = DB::table('match_events')
                ->select('match_events.id', 'match_events.name', 'match_events.season_id')
                ->whereIn('match_events.id', [$request['match_event_id']])
                ->get();
        else
            $match_events = DB::table('match_events')
                ->select('match_events.id', 'match_events.name', 'match_events.season_id')
                ->join('seasons', 'seasons.id', '=', 'match_events.season_id')
                ->where([['seasons.is_suscription', true], ['seasons.active', true]])
                ->where([['match_events.event_start', '>', Carbon::now()], ['match_events.active', true]])
                ->get();

        // Calcula el valor total a pagar por los abonos seleccionados
        $seasonId = 1;
        foreach ($pre_subscribers as $item) {
            $data_ticket = array();
            $price = $item->price;
            $total += $price;
            foreach ($match_events as $key => $match_event) {
                if ($key == 0) {
                    $seasonId = $match_event->season_id;
                }
                $data_ticket = array(
                    'seat'              => $item->seat,
                    'match_event_id'    => $match_event->id,
                    'ticket_type_id'    => self::SUBSCRIBER_TICKET,
                    'match_event_price' => null,
                    'price'             => $price,
                    'zone_name'         => $item->seat->zone->name,
                );
            }
            array_push($tickets_array, $data_ticket);
        }

        $request = new Request([
            'tickets'           => $tickets_array,
            'type_process'      => "block",
            'amount'            => $total,
            'ticket_type_id'    => self::SUBSCRIBER_TICKET,
            'season_id'         => $seasonId,
        ]);

        $ticketApiController = new TicketApiController;
        return $ticketApiController->createBlocks($request, $subscribedUser->id);
    }

    public function purchaseAbono($id)
    {
        $ticketApiController = new TicketApiController;
        $ticketApiController->determinateWebService();

        $wompiController = new WompiController;

        // Verifico sin consultar a Wompi.
        $ticket = TicketMain::where('id', $id)->first();
        if ($ticket->payment_state == 'CONFIRMED') {
            return view("wompi.webcheckout-done");
        }

        // Consulto a Wompi el estado
        $ticketApiController->updateStatusOrderFromReference($id);
        $ticket = TicketMain::where('id', $id)->first();
        if ($ticket->payment_state == 'CONFIRMED') {
            return view("wompi.webcheckout-done");
        }

        // Se actualiza el ticket con la referencia de pago
        $ticket->payment_reference = strtoupper(hash("md5", $ticket->id)) . '_' . $ticket->pin;
        $ticket->payment_state = "PENDING";
        $ticket->gateway_payments_id = $ticketApiController->gateway_data->id;
        $ticket->update();

        $amountInCents = $ticket->total * 100;
        $expirationTime = $wompiController->expirationTime(25);
        $signature = $wompiController->generateSignature($ticket->payment_reference, $amountInCents, $ticketApiController->gateway_data->currency, $ticketApiController->gateway_data->client_signature, $expirationTime);

        return view("wompi.webcheckout")
            ->with('gateway_data', $ticketApiController->gateway_data)
            ->with('amountInCents', $amountInCents)
            ->with('reference', $ticket->payment_reference)
            ->with('confirm_url', config('app.url') .  '/store/payment?paymentGatewayId=' . $ticket->gateway_payments_id . '&origin=ticket')
            ->with('signature', $signature)
            ->with('expirationTime', $expirationTime);
    }
}