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/qas.sports-crowd.com/app/Http/Controllers/Api/GanaApiController.php
<?php

namespace App\Http\Controllers\Api;

use App\Core\Payment\PaymentStatusEnum;
use App\Http\Controllers\Controller;
use App\Http\Controllers\TicketsController;
use App\IntegrationTicket;
use App\TicketMain;
use Illuminate\Http\Request;

class GanaApiController extends Controller
{

    public function arrayResponse($statusCode, $message, $payment, $data)
    {
        return array('statusCode' => $statusCode, 'message' => $message, 'payment' => $payment, 'data' => $data);
    }

    /** Obtiene el status de un pin y retorna los tickets_user_blocks, que contengan este pin
     * @param $request
     * @return bloqueos del pin y status del mismo.
     */
    public function pin_state(Request $request)
    {
        $api_key = $request->header('api-key');
        $authorization = $request->header('Authorization');

        if (!$request->pin_id) {
            return response($this->arrayResponse(400, 'Please specify pin', null, null));
        }

        if (!$api_key) {
            return response($this->arrayResponse(401, 'Unauthorized', null, null));
        }

        try {
            if (!$this->checkAuthentication($authorization, $api_key)) {
                return response($this->arrayResponse(401, 'Unauthorized', null, null));
            }
        } catch (\Exception $ex) {
            return response($this->arrayResponse(401, 'Unauthorized', null, null));
        }

        // Obtiene bloqueos de este pin.
        $pin_tickets = TicketMain::where('pin_tercero', $request->pin_id)
            ->with(['ticket_user_blocks' => function ($q) {
                $q->where('ticket_user_blocks.is_social_distancing', false);
            }])
            ->with('tickets')
            ->first();

        if (!$pin_tickets) {
            return response($this->arrayResponse(400, 'Pin does not exist', null, null));
        }

        if (!$pin_tickets->ticket_user_blocks->isEmpty()) {
            return response($this->arrayResponse(200, 'Pin pending payment', 'PENDING', $pin_tickets->ticket_user_blocks));
        }

        if (!$pin_tickets->tickets->isEmpty()) {
            return response($this->arrayResponse(200, 'Pin successful', 'CONFIRMED', $pin_tickets->tickets));
        }

        return response($this->arrayResponse(400, 'Pin Expired', null, null));
    }

    /** Confirma un pin para proceder a la creación del ticket.
     * @param $request
     * @return tickets Tickets generados a partir del pin y la referencia de pago, junto a los datos de identificación del mismo.
     */
    public function pin_confirm(Request $request)
    {
        $api_key = $request->header('api-key');
        $authorization = $request->header('Authorization');

        if (!$request->pin_id) {
            return response($this->arrayResponse(400, 'Please specify pin', null, null));
        }

        if (!$api_key) {
            return response($this->arrayResponse(401, 'Unauthorized', null, null));
        }

        if (!$request->reference) {
            return response($this->arrayResponse(400, 'Please specify reference', null, null));
        }

        try {
            if (!$this->checkAuthentication($authorization, $api_key)) {
                return response($this->arrayResponse(401, 'Unauthorized', null, null));
            }
        } catch (\Exception $ex) {
            return response($this->arrayResponse(401, 'Unauthorized', null, null));
        }

        $pin_tickets = TicketMain::where('pin_tercero', $request->pin_id)
            ->with(['ticket_user_blocks' => function ($q) {
                $q->where('ticket_user_blocks.is_social_distancing', false);
            }])
            ->first();

        if (!$pin_tickets) {
            return response($this->arrayResponse(400, 'Pin does not exist', null, null));
        }

        if ($pin_tickets->ticket_user_blocks->isEmpty()) {
            return response($this->arrayResponse(400, 'Pin Expired', null, null));
        }

        try {
            $ticketController = new TicketsController;
            if ($ticketController->generateTickets($pin_tickets->ticket_user_blocks, $pin_tickets->id)) {
                $pin_tickets->reference_tercero = $request->reference;
                $pin_tickets->payment_state = PaymentStatusEnum::CONFIRMED;
                $pin_tickets->update();

                $l = TicketMain::where('pin_tercero', $request->pin_id)->with('tickets')->first();
                return response($this->arrayResponse(200, 'Pin successful', 'CONFIRMED', $l->tickets));
            }
            return response($this->arrayResponse(400, 'Error creating tickets', null, null));
        } catch (\Exception $e) {
            return response($this->arrayResponse(400, 'Error creating tickets', null, $e->getMessage()));
        }
    }

    /**
     * Valida la lógica de autenticación user,pass y api_key.
     * @param authorization -> Basic auth.
     * @param api_key -> identificador del proveedor.
     */
    protected function checkAuthentication($authorization, $api_key)
    {
        $authorization = substr($authorization, 6); // Remove Basic
        $authorization = base64_decode($authorization);
        $authorization = explode(":", $authorization);
        $username = $authorization[0];
        $password = $authorization[1];

        // Verificar el proveedor
        $integration_ticket_provider = IntegrationTicket::where('api_key', $api_key)
            ->where('username', $username)
            ->where('password', $password)
            ->first();
        if (!$integration_ticket_provider) {
            return false;
        }
        return true;
    }
}