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

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\GatewayPayment;
use App\Parameter;

class LealPayController extends Controller
{
    private $urlApiLealPay;
    private $userApiLealPay;
    private $passApiLealPay;
    private $timeout = 30; // Response timeout
    private $connect_timeout = 30; // Connection timeout

    public function __construct()
    {
        $gw = GatewayPayment::where('name', 'Leal_PRD')->first();
        if (config('app.debug')) {
            $gw = GatewayPayment::where('name', 'Leal_sandbox')->first();
        }
        if (!$gw) {
            return;
        }

        $this->urlApiLealPay            = $gw->gw_url_prd;
        $this->userApiLealPay           = $gw->user;
        $this->passApiLealPay           = $gw->pass;
    }

    public function syncDataLealAPI()
    {
        $token = $this->login();
        if ($token) {
            $data = $this->getAllCoinStoreData($token);
            if ($data && $data[0]) {
                $responseApi = $data[0];
                $parameters = Parameter::first();
                if ($responseApi->config->valor_por_punto_redencion && $responseApi->config->valor_por_punto_redencion != $parameters->value_points_coin) {
                    $parameters->value_points_coin = $responseApi->config->valor_por_punto_redencion;
                }
                if ($responseApi->id_comercio && intval($responseApi->id_comercio) != intval($parameters->coin_store_identifier)) {
                    $parameters->coin_store_identifier = intval($responseApi->id_comercio);
                }
                if ($responseApi->uid_cms && $responseApi->uid_cms != $parameters->coin_cms_identifier) {
                    $parameters->coin_cms_identifier = $responseApi->uid_cms;
                }
                if ($responseApi->id_sucursal && $responseApi->id_sucursal != $parameters->coin_branch_identifier) {
                    $parameters->coin_branch_identifier = $responseApi->id_sucursal;
                }
                $parameters->update();
                return true;
            }
        }
        return false;
    }

    /**
     * Generates token based on leal API user and pass data,
     * to be used later in leal queries (accumulation and redemption).
     */
    public function login()
    {
        // Datos peticion LealPay
        $dataRequest = new \stdClass();
        $dataRequest->usuario = $this->userApiLealPay;
        $dataRequest->contrasena = $this->passApiLealPay;
        try {
            $clientGuzzle = new \GuzzleHttp\Client([
                'base_uri' => $this->urlApiLealPay,
                'defaults' => [
                    'exceptions' => false,
                ]
            ]);

            $responseGuzzle = $clientGuzzle->post('/apiexterno/api/comercios/login_cajero', [
                \GuzzleHttp\RequestOptions::JSON      => $dataRequest,
                'headers' => [
                    'Accept' => 'application/json',
                ],
                'timeout' => $this->timeout,
                'connect_timeout' => $this->connect_timeout
            ]);

            $body = (string) $responseGuzzle->getBody();
            if ($responseGuzzle->getStatusCode() == 200) {
                $body = json_decode($body);
                if ($body->code == 100) {
                    return $body->token;
                }
            }
        } catch (\Exception $e) {
            // return array($e->getMessage());
        }
        return false;
    }

    public function getAllCoinStoreData($token)
    {
        try {
            $client = new \GuzzleHttp\Client();
            $responseGuzzle = $client->get($this->urlApiLealPay . '/comercios/me', [
                'headers' => ['Authorization' => 'Bearer ' . $token]
            ]);
            $body = $responseGuzzle->getBody()->getContents();
            $body = json_decode($body);
            if ($body->code == 100) {
                return array($body->user);
            }
        } catch (\Exception $e) {
            // return array($e->getMessage());
        }
        return false;
    }
}