Skip to content

Commit b98ebba

Browse files
chenge gerencianet bank to new label Efi
1 parent 4df1d33 commit b98ebba

File tree

339 files changed

+42923
-320
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

339 files changed

+42923
-320
lines changed

build/MagnusBilling-current.tar.gz

318 KB
Binary file not shown.

lib/composer.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

lib/composer.lock

Lines changed: 0 additions & 74 deletions
This file was deleted.

lib/efi/composer.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "efipay/sdk-php-apis-efi",
3+
"description": "SDK PHP para APIs Efi Pay",
4+
"homepage": "https://github.com/efipay/sdk-php-apis-efi/",
5+
"version": "1.10.1",
6+
"type": "library",
7+
"require": {
8+
"php": ">=7.2.5",
9+
"guzzlehttp/guzzle": "^7.0",
10+
"symfony/cache": "^5.0 || ^6.0"
11+
},
12+
"support": {
13+
"docs": "https://dev.efipay.com.br/",
14+
"discord": "https://comunidade.sejaefi.com.br/"
15+
},
16+
"license": "MIT",
17+
"authors": [
18+
{
19+
"name": "Guilherme Cota"
20+
},
21+
{
22+
"name": "Jéssica Gava"
23+
},
24+
{
25+
"name": "Sady Coimbra"
26+
},
27+
{
28+
"name": "Matheus Rodrigues"
29+
},
30+
{
31+
"name": "João Oliveira"
32+
},
33+
{
34+
"name": "Paloma Brito"
35+
}
36+
],
37+
"autoload": {
38+
"psr-4": {
39+
"Efi\\": "src/Efi"
40+
}
41+
},
42+
"config": {
43+
"sort-packages": true,
44+
"optimize-autoloader": true,
45+
"minimum-stability": "stable"
46+
}
47+
}

lib/efi/src/Efi/ApiRequest.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace Efi;
4+
5+
use Efi\Exception\EfiException;
6+
use Efi\Security;
7+
use GuzzleHttp\Exception\ClientException;
8+
9+
class ApiRequest extends BaseModel
10+
{
11+
private $auth;
12+
private $cache;
13+
private $request;
14+
private $options;
15+
private $cacheScopes = null;
16+
private $cacheAccessToken = null;
17+
private $cacheAccessTokenExpires = null;
18+
19+
/**
20+
* Initializes a new instance of the ApiRequest class.
21+
*
22+
* @param array|null $options The options to configure the ApiRequest.
23+
*/
24+
public function __construct(?array $options = null)
25+
{
26+
$this->options = Config::options($options);
27+
$this->auth = new Auth($options);
28+
$this->request = new Request($options);
29+
$this->cache = new CacheRetriever();
30+
}
31+
32+
/**
33+
* Sends an HTTP request.
34+
*
35+
* @param string $method The HTTP method.
36+
* @param string $route The URL route.
37+
* @param array $body The request body.
38+
* @return #The response data.
39+
* @throws EfiException If there is an EFI specific error.
40+
*/
41+
public function send(string $method, string $route, string $scope, array $body)
42+
{
43+
$this->loadAccessTokenFromCache();
44+
45+
if (!$this->isAccessTokenValid() || !$this->options['cache']) {
46+
$this->auth->authorize();
47+
} else {
48+
if (in_array($scope, $this->cacheScopes) && $this->cacheAccessToken) {
49+
$this->auth->setAccessToken($this->cacheAccessToken);
50+
} else {
51+
$this->auth->authorize();
52+
}
53+
}
54+
55+
$requestTimeout = $this->options['timeout'];
56+
$requestHeaders = $this->buildRequestHeaders();
57+
58+
try {
59+
return $this->request->send($method, $route, [
60+
'json' => empty($body) ? null : $body,
61+
'timeout' => $requestTimeout,
62+
'headers' => $requestHeaders
63+
]);
64+
} catch (ClientException $e) {
65+
throw new EfiException(
66+
$this->options['api'],
67+
[
68+
'error' => $e->getResponse(),
69+
'error_description' => $e->getResponse()->getBody()
70+
],
71+
$e->getResponse()->getStatusCode(),
72+
$e->getResponse()->getHeaders()
73+
);
74+
}
75+
}
76+
77+
/**
78+
* Loads the access token from cache if available.
79+
*/
80+
private function loadAccessTokenFromCache(): void
81+
{
82+
$cacheAccessTokenEncrypted = $this->cache->get(Security::getHash('accessToken', $this->options['api'], $this->options['clientId']));
83+
$security = new Security(Security::getHash('accessToken', $this->options['api'], $this->options['clientSecret']));
84+
$this->cacheAccessToken = $security->decrypt($cacheAccessTokenEncrypted);
85+
$this->cacheAccessTokenExpires = $this->cache->get(Security::getHash('accessTokenExpires', $this->options['api'], $this->options['clientId']));
86+
$this->cacheScopes = $this->cache->get(Security::getHash('scopes', $this->options['api'], $this->options['clientId']));
87+
}
88+
89+
/**
90+
* Checks if the cached access token is valid.
91+
*
92+
* @return bool True if the access token is valid, otherwise false.
93+
*/
94+
private function isAccessTokenValid(): bool
95+
{
96+
return $this->cacheAccessToken !== null && $this->cacheAccessTokenExpires > (time() - 5);
97+
}
98+
99+
/**
100+
* Builds the headers for the HTTP request.
101+
*
102+
* @return array The headers for the HTTP request.
103+
*/
104+
private function buildRequestHeaders(): array
105+
{
106+
$composerData = Utils::getComposerData();
107+
$requestHeaders = [
108+
'Authorization' => 'Bearer ' . $this->auth->getAccessToken(),
109+
'api-sdk' => 'efi-php-' . $composerData['version']
110+
];
111+
112+
if (isset($this->options['partnerToken'])) {
113+
$requestHeaders['partner-token'] = $this->options['partnerToken'] ?? $this->options['partner-token'];
114+
}
115+
116+
return $requestHeaders;
117+
}
118+
}

0 commit comments

Comments
 (0)