|
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
|
|
|
$json = json_decode((string) $response->getBody()); |
|
74
|
|
|
|
|
75
|
|
|
if (json_last_error() === JSON_ERROR_NONE) { |
|
76
|
|
|
return $json; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return (string) $response->getBody(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function authorize(string $token, string $service, string $method, string $endpoint): bool |
|
83
|
|
|
{ |
|
84
|
|
|
try { |
|
85
|
|
|
$headers = ['Content-Type' => 'application/json']; |
|
86
|
|
|
|
|
87
|
|
|
$url = $this->url.'/authorize'; |
|
88
|
|
|
|
|
89
|
|
|
$body = json_encode(compact('token', 'service', 'method', 'endpoint')); |
|
90
|
|
|
|
|
91
|
|
|
$this->http->send(new Request('POST', $url, $headers, $body)); |
|
92
|
|
|
|
|
93
|
|
|
return true; |
|
94
|
|
|
} catch (Exception $exception) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function token(): ?string |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->cache->remember('api_token_'.md5($this->url), 10, function () { |
|
102
|
|
|
$response = $this->http->post($this->url.'/login', ['form_params' => ['username' => $this->username, 'password' => $this->password]]); |
|
103
|
|
|
|
|
104
|
|
|
return (string) $response->getBody(); |
|
105
|
|
|
}); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|