Skip to content

Commit

Permalink
chenge gerencianet bank to new label Efi
Browse files Browse the repository at this point in the history
  • Loading branch information
callcenter-magnus committed Dec 23, 2024
1 parent 4df1d33 commit b98ebba
Show file tree
Hide file tree
Showing 339 changed files with 42,923 additions and 320 deletions.
Binary file modified build/MagnusBilling-current.tar.gz
Binary file not shown.
5 changes: 0 additions & 5 deletions lib/composer.json

This file was deleted.

74 changes: 0 additions & 74 deletions lib/composer.lock

This file was deleted.

47 changes: 47 additions & 0 deletions lib/efi/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "efipay/sdk-php-apis-efi",
"description": "SDK PHP para APIs Efi Pay",
"homepage": "https://github.com/efipay/sdk-php-apis-efi/",
"version": "1.10.1",
"type": "library",
"require": {
"php": ">=7.2.5",
"guzzlehttp/guzzle": "^7.0",
"symfony/cache": "^5.0 || ^6.0"
},
"support": {
"docs": "https://dev.efipay.com.br/",
"discord": "https://comunidade.sejaefi.com.br/"
},
"license": "MIT",
"authors": [
{
"name": "Guilherme Cota"
},
{
"name": "Jéssica Gava"
},
{
"name": "Sady Coimbra"
},
{
"name": "Matheus Rodrigues"
},
{
"name": "João Oliveira"
},
{
"name": "Paloma Brito"
}
],
"autoload": {
"psr-4": {
"Efi\\": "src/Efi"
}
},
"config": {
"sort-packages": true,
"optimize-autoloader": true,
"minimum-stability": "stable"
}
}
118 changes: 118 additions & 0 deletions lib/efi/src/Efi/ApiRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Efi;

use Efi\Exception\EfiException;
use Efi\Security;
use GuzzleHttp\Exception\ClientException;

class ApiRequest extends BaseModel
{
private $auth;
private $cache;
private $request;
private $options;
private $cacheScopes = null;
private $cacheAccessToken = null;
private $cacheAccessTokenExpires = null;

/**
* Initializes a new instance of the ApiRequest class.
*
* @param array|null $options The options to configure the ApiRequest.
*/
public function __construct(?array $options = null)
{
$this->options = Config::options($options);
$this->auth = new Auth($options);
$this->request = new Request($options);
$this->cache = new CacheRetriever();
}

/**
* Sends an HTTP request.
*
* @param string $method The HTTP method.
* @param string $route The URL route.
* @param array $body The request body.
* @return #The response data.
* @throws EfiException If there is an EFI specific error.
*/
public function send(string $method, string $route, string $scope, array $body)
{
$this->loadAccessTokenFromCache();

if (!$this->isAccessTokenValid() || !$this->options['cache']) {
$this->auth->authorize();
} else {
if (in_array($scope, $this->cacheScopes) && $this->cacheAccessToken) {
$this->auth->setAccessToken($this->cacheAccessToken);
} else {
$this->auth->authorize();
}
}

$requestTimeout = $this->options['timeout'];
$requestHeaders = $this->buildRequestHeaders();

try {
return $this->request->send($method, $route, [
'json' => empty($body) ? null : $body,
'timeout' => $requestTimeout,
'headers' => $requestHeaders
]);
} catch (ClientException $e) {
throw new EfiException(
$this->options['api'],
[
'error' => $e->getResponse(),
'error_description' => $e->getResponse()->getBody()
],
$e->getResponse()->getStatusCode(),
$e->getResponse()->getHeaders()
);
}
}

/**
* Loads the access token from cache if available.
*/
private function loadAccessTokenFromCache(): void
{
$cacheAccessTokenEncrypted = $this->cache->get(Security::getHash('accessToken', $this->options['api'], $this->options['clientId']));
$security = new Security(Security::getHash('accessToken', $this->options['api'], $this->options['clientSecret']));
$this->cacheAccessToken = $security->decrypt($cacheAccessTokenEncrypted);
$this->cacheAccessTokenExpires = $this->cache->get(Security::getHash('accessTokenExpires', $this->options['api'], $this->options['clientId']));
$this->cacheScopes = $this->cache->get(Security::getHash('scopes', $this->options['api'], $this->options['clientId']));
}

/**
* Checks if the cached access token is valid.
*
* @return bool True if the access token is valid, otherwise false.
*/
private function isAccessTokenValid(): bool
{
return $this->cacheAccessToken !== null && $this->cacheAccessTokenExpires > (time() - 5);
}

/**
* Builds the headers for the HTTP request.
*
* @return array The headers for the HTTP request.
*/
private function buildRequestHeaders(): array
{
$composerData = Utils::getComposerData();
$requestHeaders = [
'Authorization' => 'Bearer ' . $this->auth->getAccessToken(),
'api-sdk' => 'efi-php-' . $composerData['version']
];

if (isset($this->options['partnerToken'])) {
$requestHeaders['partner-token'] = $this->options['partnerToken'] ?? $this->options['partner-token'];
}

return $requestHeaders;
}
}
Loading

0 comments on commit b98ebba

Please sign in to comment.