-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chenge gerencianet bank to new label Efi
- Loading branch information
1 parent
4df1d33
commit b98ebba
Showing
339 changed files
with
42,923 additions
and
320 deletions.
There are no files selected for viewing
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.