File: /var/www/vhost/disk-apps/demo.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;
}
}