1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace B2Binpay; |
5
|
|
|
|
6
|
|
|
use B2Binpay\Exception\ConnectionErrorException; |
7
|
|
|
use B2Binpay\Exception\EmptyResponseException; |
8
|
|
|
use B2Binpay\Exception\ServerApiException; |
9
|
|
|
use B2Binpay\Exception\UpdateTokenException; |
10
|
|
|
use GuzzleHttp\Client; |
11
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Send and validate requests through GuzzleHttp |
15
|
|
|
* |
16
|
|
|
* @package B2Binpay |
17
|
|
|
*/ |
18
|
|
|
class Request |
19
|
|
|
{ |
20
|
|
|
const ERROR_UPDATE_TOKEN = ['-240', 'RESULT_TOKEN_ERROR_EXPIRED']; |
21
|
|
|
|
22
|
28 |
|
/** |
23
|
|
|
* @var Client |
24
|
28 |
|
*/ |
25
|
28 |
|
private $client; |
26
|
|
|
|
27
|
|
|
public function __construct(Client $client = null) |
28
|
|
|
{ |
29
|
|
|
$this->client = $client ?? new Client(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $token |
34
|
11 |
|
* @param string $method |
35
|
|
|
* @param string $url |
36
|
|
|
* @param array|null $params |
37
|
11 |
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public function send(string $token, string $method, string $url, array $params = []) |
40
|
|
|
{ |
41
|
11 |
|
$header = [ |
42
|
11 |
|
'Authorization' => 'Bearer ' . $token |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
$_request = [ |
46
|
11 |
|
'headers' => $header, |
47
|
|
|
'http_errors' => false |
48
|
7 |
|
]; |
49
|
|
|
|
50
|
|
|
$request = array_merge($_request, $params); |
51
|
|
|
|
52
|
|
|
return $this->execute($method, $url, $request); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
11 |
|
* @param string $authBasic |
57
|
|
|
* @param string $url |
58
|
|
|
* @return string |
59
|
11 |
|
*/ |
60
|
|
|
public function token(string $authBasic, string $url) : string |
61
|
|
|
{ |
62
|
|
|
$header = [ |
63
|
11 |
|
'Authorization' => 'Basic ' . $authBasic |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
$request = [ |
67
|
11 |
|
'headers' => $header, |
68
|
|
|
'http_errors' => false |
69
|
11 |
|
]; |
70
|
|
|
|
71
|
8 |
|
$method = 'get'; |
72
|
|
|
|
73
|
|
|
$responseDecode = $this->execute($method, $url, $request); |
74
|
|
|
|
75
|
|
|
return $responseDecode->access_token; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $method |
80
|
|
|
* @param string $url |
81
|
|
|
* @param array $request |
82
|
|
|
* @return mixed |
83
|
|
|
* @throws ConnectionErrorException |
84
|
16 |
|
* @throws EmptyResponseException |
85
|
|
|
* @throws ServerApiException |
86
|
|
|
* @throws UpdateTokenException |
87
|
16 |
|
*/ |
88
|
2 |
|
private function execute(string $method, string $url, array $request) |
89
|
2 |
|
{ |
90
|
|
|
try { |
91
|
|
|
$response = $this->client->request($method, $url, $request); |
92
|
14 |
|
} catch (GuzzleException $e) { |
93
|
14 |
|
throw new ConnectionErrorException($e); |
94
|
14 |
|
} |
95
|
|
|
|
96
|
14 |
|
$status = $response->getStatusCode(); |
97
|
2 |
|
$body = (string)$response->getBody(); |
98
|
|
|
$responseDecode = json_decode($body); |
99
|
|
|
|
100
|
12 |
|
if (empty($responseDecode)) { |
101
|
3 |
|
throw new EmptyResponseException($url); |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
if (!empty($responseDecode->error)) { |
105
|
2 |
|
if ([$responseDecode->code, $responseDecode->error] == self::ERROR_UPDATE_TOKEN) { |
106
|
|
|
throw new UpdateTokenException(); |
107
|
|
|
} |
108
|
9 |
|
|
109
|
|
|
throw new ServerApiException($responseDecode->error, $responseDecode->code, $status); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $responseDecode; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|