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/Console/Commands/OrderPayments.php
<?php

namespace App\Console\Commands;

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

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Valida cierto numero de veces si un pago de un pedido 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();
        $ordersPending = Order::where('gw_state', 'PENDING')
                                ->where('payment_attempts' , '<', $this->__PAYMENT_ATTEMPTS)
                                ->get();

        foreach($ordersPending as $o){
            $o->payment_attempts = $o->payment_attempts + 1;
            $o->update();
            $this->updateStatusOrderFromReference($o);
        }
    }

    public function updateStatusOrderFromReference($o){
        $client = new \GuzzleHttp\Client();

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

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

        if($body->data){
            $o->gw_state = $body->data[0]->status == "APPROVED" ? "CONFIRMED" : "PENDING";
            $o->update();
        }
    }

    private 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;
        }
    }
}