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/WompiController.php
<?php

namespace App\Http\Controllers;

use App\GatewayPayment;
use App\Order;
use App\Parameter;
use Illuminate\Http\Request;

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

    public function __construct(){
        $this->determinateWebService();
    }

    public function webcheckout(Request $request){
        $id = $request->input("uid");

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

        // Consulto a Wompi el estado
        $this->updateStatusOrderFromReference($id);

        $o = Order::where('id', $id)->first();

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


        $o->gw_code_transaction = time() . "_" . $o->code;
        $o->gw_state = "PENDING";
        $o->update();


        return view("wompi.webcheckout")
                ->with('gateway_data', $this->gateway_data)
                ->with('total_price', $o->total_price)
                ->with('reference', $o->gw_code_transaction)
                ->with('confirm_url', env('APP_URL') . '/store/wompi/confirm')
                ->with("order_id", $id);
    }

    public function confirmFromWompi(Request $request){
        $id = $request->input("id");

        $this->updateStatusOrderFromID($id);

        return view("wompi.webcheckout-confirm")
                ->with("id", $id);
    }

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

    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 = Order::where('gw_code_transaction', $body->data->reference)->first();
        $o->gw_state = $body->data->status == "APPROVED" ? "CONFIRMED" : "PENDING";
        $o->update();
    }

    public function updateStatusOrderFromReference($order_id){
        $o = Order::where('id', $order_id)->first();
        $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();
        }
    }

    public function getStatusTransaction($order_id){
        $o = Order::where('id', $order_id)->first();
        $client = new \GuzzleHttp\Client();

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

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