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/agile-selling-wpb/app/Http/Controllers/Api/TicketApiController.php
<?php

namespace App\Http\Controllers\Api;

use DB;
use App\Team;
use App\Parameter;
use App\TicketMain;
use App\GatewayPayment;
use App\TokenUserTicket;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Builder;
use App\Http\Controllers\TicketsController;
use App\Http\Controllers\PrintTicketController;

class TicketApiController extends Controller
{
    private $url_ws;
    private $private_key;
    private $gateway_data;

    public function matchEvents(){
        $tickets = new TicketsController();
        $data = $tickets->getMatchEvents();
        return response()->json($data, 200);
    }

    public function getSeatPrice($zone_id,$event_id)
    {
        $tickets = new TicketsController();
        $data = $tickets->getSeatPrice($zone_id,$event_id);
        return response()->json($data, 200);
    }

    public function getSeats($sub_zone_id,$event_id)
    {
        $tickets = new TicketsController();
        $data = $tickets->getSeats($sub_zone_id,$event_id);
        return response()->json($data, 200);
    }

    public function validateSell(Request $request)
    {
        $tickets = new TicketsController();
        $data = $tickets->validateSell($request->zone_id,$request->event_id,$request->seat_id,Auth::user()->id,$request->ticket_type_id,null,$request->type_process,0,true);
        return response()->json($data, 200);
    }

    public function getMatchEventsSuscription($match_event_id)
    {
        $tickets = new TicketsController();
        return $tickets->getMatchEventsSuscription($match_event_id);
    }

    public function createBlocks(Request $request)
    {
        DB::beginTransaction();
        try {
            $tickets = new TicketsController();
            $main = TicketMain::create([
                'payment_reference' => "PENDING",
                'payment_state'     => "PENDING",
                'total'             => $request->input('amount'),
                'pin'               => $this->gen_uid(),
            ]);

            foreach ($request->input('tickets') as $key => $ticket) {
                $zone_id = $ticket["seat"]["zone_id"];
                $event_id = $ticket["match_event_id"];
                $seat_id = $ticket["seat"]["id"];
                $user_id = Auth::user()->id;

                // creamos los bloqueos
                $data = $tickets->validateSell($zone_id,$event_id,$seat_id,$user_id,$ticket["ticket_type_id"],$main->id,$request->input('type_process'),0);
                $response = json_decode($data->getContent());
                if(!$response->r){
                    DB::rollback();
                    return $data;
                    break;
                }
            }
            DB::commit();
            return response(array("r" => true, "type" => "", "title" => "ok", "m" => $response->m, "data" => $main->id));
        } catch (\Throwable $th) {
            DB::rollback();
            return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_global'), "data" => $th->getMessage()));
        }
    }

    public function confirmFromWompiTicket(Request $request){
        $this->determinateWebService();
        $id = $request->input("id");
        $this->updateStatusOrderFromID($id);
        return view("wompi.webcheckout-confirm")->with("id", $id);
    }

    public function updateStatusOrderFromID($wompi_id){
        $client = new \GuzzleHttp\Client();
        $response = $client->get($this->url_ws . "/transactions/" . $wompi_id);

        $body = $response->getBody()->getContents();
        $body = json_decode($body);

        $o = TicketMain::where('payment_reference', $body->data->reference)
                        ->with(['ticket_user_blocks' => function ($q){
                            $q->where('ticket_user_blocks.is_social_distancing', false);
                        }])
                        ->first();
        if($body->data->status == "APPROVED" && !$o->ticket_user_blocks->isEmpty()){
            $tickets = new TicketsController();
            $tickets->generateTickets($o->ticket_user_blocks,$o->id);
        }

        $o->payment_state = $body->data->status == "APPROVED" ? "CONFIRMED" : "PENDING";
        $o->payment_comment = $body->data->status;
        $o->update();
    }

    public function updateStatusOrderFromReference($id){
        $o = TicketMain::where('id', $id)
                        ->with(['ticket_user_blocks' => function ($q){
                            $q->where('ticket_user_blocks.is_social_distancing', false);
                        }])
                        ->first();

        $client = new \GuzzleHttp\Client();
        $response = $client->get($this->url_ws . "/transactions?reference=" . $o->payment_reference, [
            'headers' => ['Authorization' => 'Bearer ' . $this->private_key]
        ]);

        $body = $response->getBody()->getContents();
        $body = json_decode($body);


        if($body->data){
            if($body->data[0]->status == "APPROVED" && !$o->ticket_user_blocks->isEmpty()){
                $tickets = new TicketsController();
                $tickets->generateTickets($o->ticket_user_blocks,$o->id);
            }
            $o->payment_state = $body->data[0]->status == "APPROVED" ? "CONFIRMED" : "PENDING";
            $o->payment_comment = $body->data[0]->status;
            $o->update();
        }
    }

    public function determinateWebService(){
        $parameters = Parameter::first();
        $is_production = $parameters->is_production_gateway; // TRUE: Production ; FALSE: Test

        $this->gateway_data = GatewayPayment::where("name", "wompi")
                            ->where("is_productive", $is_production)
                            ->first();

        $this->private_key = $this->gateway_data->client_secret;
        $this->public_key = $this->gateway_data->client_public;

        if($is_production){ // Si se encuentra en producción el gateway.
            $this->url_ws = $this->gateway_data->gw_url_prd;
        }else{
            $this->url_ws = $this->gateway_data->gw_url_sandbox;
        }
    }

    function purchase(Request $request){
        $this->determinateWebService();
        $id = $request->input("id");

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

        // Consulto a Wompi el estado
        $this->updateStatusOrderFromReference($id);
        $o = TicketMain::where('id', $id)->first();

        if($o->payment_state == 'CONFIRMED'){
            return view("wompi.webcheckout-done");
        }

        $key_reference = time() . "_" . $o->id . '_' . $o->pin;

        // actualizamos ticket con la referencia de pago
        $o->payment_reference = $key_reference;
        $o->payment_state = "PENDING";
        $o->update();

        return view("wompi.webcheckout")
                ->with('gateway_data', $this->gateway_data)
                ->with('total_price', $o->total)
                ->with('reference', $key_reference)
                ->with('confirm_url', env('APP_URL') . '/store/wompi/confirm_ticket');
    }

    public function get_tickets($take, $offset)
    {
        try {
            $data = TicketMain::whereHas('tickets', function (Builder $query) {
                $query->where('user_id', Auth::user()->id);
            })
            ->orWhereHas('ticket_user_blocks', function (Builder $query) {
                $query->where('user_id', Auth::user()->id);
            })
            ->with('tickets')
            ->with(['ticket_user_blocks' => function ($q){
                $q->where('ticket_user_blocks.is_social_distancing', false);
            }])
            ->take($take)
            ->offset($offset)
            ->orderBy('created_at', 'desc')
            ->get();

            return response()->json($data, 200);
        } catch (\Throwable $th) {
            return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_global'), "data" => $th->getMessage()));
        }
    }

    public function team_main()
    {
        $data = Team::where([['is_main', true],['active', true]])->first();
        return response()->json($data, 200);
    }

    public function generateTokenTicket(Request $request)
    {
        try {
            if($request->platform == "android"){
                $token = str_random(30);
                $print = new PrintTicketController();
                $print->printTicket($request->code,Auth::user()->id,$token);
                return response(array("r" => true, "type" => "", "title" => "ok", "m" => "", "data" => $token));
            }else{
                $token = TokenUserTicket::create(['user_id' => Auth::user()->id, 'token' => str_random(30), 'code' => $request->code]);
                return response(array("r" => true, "type" => "", "title" => "ok", "m" => "", "data" => $token->token));
            }
        } catch (\Throwable $th) {
            return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_global'), "data" => $th->getMessage()));
        }
    }
}