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/teamdemo.sports-crowd.com/app/Console/Commands/SymbolicTicketPayments.php
<?php

namespace App\Console\Commands;

use App\Parameter;
use App\GatewayPayment;
use App\SymbolicTicketUser;
use Illuminate\Console\Command;

class SymbolicTicketPayments extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sportscrowd:symbolicTicketPayments';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Valida cierto numero de veces si un pago de una boleta simbolica fue aprobado';
    private $url_ws, $private_key, $gateway_data;
    private $__PAYMENT_ATTEMPTS = 3; // Cuantas veces se intenta establecer el estado del pago.

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->determinateWebService();
        $symbolicTicketsPending = SymbolicTicketUser::where('payment_state', 'PENDING')
            ->where('payment_attempts', '<', $this->__PAYMENT_ATTEMPTS)
            ->get();

        foreach ($symbolicTicketsPending as $s) {
            $s->payment_attempts = $s->payment_attempts  + 1;
            $s->update();
            $this->updateStatusTicketFromReference($s);
        }
    }

    public function updateStatusTicketFromReference($o)
    {
        $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 && count($body->data)) {
            $ticket = end($body->data);
            $isApproved = collect($body->data)->where('status', "APPROVED")->first();
            if ($isApproved) {
                $ticket = $isApproved;
            }
            if ($ticket->status == "APPROVED") {
                $o->payment_state = "CONFIRMED";
                $o->payment_comment = $ticket->status;
                $o->update();
            }
        }
    }

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

        $this->gateway_data = GatewayPayment::where("name", "wompiTicketOffice")
            ->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;
        }
    }
}