1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AcquiroPay; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\Psr7\Request; |
10
|
|
|
use AcquiroPay\Contracts\Cache; |
11
|
|
|
|
12
|
|
|
class Api |
13
|
|
|
{ |
14
|
|
|
protected $cache; |
15
|
|
|
protected $http; |
16
|
|
|
|
17
|
|
|
protected $url; |
18
|
|
|
protected $username; |
19
|
|
|
protected $password; |
20
|
|
|
|
21
|
|
|
public function __construct(Cache $cache, Client $http) |
22
|
|
|
{ |
23
|
|
|
$this->cache = $cache; |
24
|
|
|
$this->http = $http; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function setUrl(string $url): self |
28
|
|
|
{ |
29
|
|
|
$this->url = $url; |
30
|
|
|
|
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function setUsername(string $username): self |
35
|
|
|
{ |
36
|
|
|
$this->username = $username; |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setPassword(string $password): self |
42
|
|
|
{ |
43
|
|
|
$this->password = $password; |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function callService(string $service, string $method, string $endpoint, array $parameters = null) |
49
|
|
|
{ |
50
|
|
|
return $this->call($method, '/services/'.$service, ['Endpoint' => $endpoint], $parameters); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function call(string $method, string $endpoint, array $headers = [], array $parameters = null) |
54
|
|
|
{ |
55
|
|
|
$body = null; |
56
|
|
|
|
57
|
|
|
$headers = array_merge([ |
58
|
|
|
'Accept' => 'application/json', |
59
|
|
|
'Content-Type' => 'application/json', |
60
|
|
|
'Authorization' => 'Bearer '.$this->token(), |
61
|
|
|
], $headers); |
62
|
|
|
|
63
|
|
|
if (!Str::startsWith($endpoint, ['http://', 'https://'])) { |
64
|
|
|
$endpoint = $this->url.'/'.ltrim($endpoint, '/'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($parameters !== null) { |
68
|
|
|
$body = json_encode($parameters); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$response = $this->http->send(new Request($method, $endpoint, $headers, $body)); |
72
|
|
|
|
73
|
|
|
return json_decode((string) $response->getBody()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function authorize(string $token, string $service, string $method, string $endpoint): bool |
77
|
|
|
{ |
78
|
|
|
try { |
79
|
|
|
$headers = ['Content-Type' => 'application/json']; |
80
|
|
|
|
81
|
|
|
$url = $this->url.'/authorize'; |
82
|
|
|
|
83
|
|
|
$body = json_encode(compact('token', 'service', 'method', 'endpoint')); |
84
|
|
|
|
85
|
|
|
$this->http->send(new Request('POST', $url, $headers, $body)); |
86
|
|
|
|
87
|
|
|
return true; |
88
|
|
|
} catch (Exception $exception) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function token(): ?string |
94
|
|
|
{ |
95
|
|
|
return $this->cache->remember('api_token_'.md5($this->url), 10, function () { |
96
|
|
|
$response = $this->http->post($this->url.'/login', ['form_params' => ['username' => $this->username, 'password' => $this->password]]); |
97
|
|
|
|
98
|
|
|
return (string) $response->getBody(); |
99
|
|
|
}); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|