1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shetabit\Multipay\Http; |
4
|
|
|
|
5
|
|
|
class Client implements HttpAdapter |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* guzzle http client. |
9
|
|
|
* |
10
|
|
|
* @var Client |
11
|
|
|
*/ |
12
|
|
|
protected $client; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Payment Driver. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $driver; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Http constructor. |
23
|
|
|
* |
24
|
|
|
* @param string $baseUrl |
25
|
|
|
* @param string $driver |
26
|
|
|
*/ |
27
|
|
|
public function __construct(string $baseUrl, string $driver) |
28
|
|
|
{ |
29
|
|
|
$this->driver = $driver; |
30
|
|
|
$this->client = new \GuzzleHttp\Client([ |
|
|
|
|
31
|
|
|
'headers' => [ |
32
|
|
|
'Content-Type' => 'application/json' |
33
|
|
|
], |
34
|
|
|
'base_uri' => $baseUrl, |
35
|
|
|
'http_errors' =>false, |
36
|
|
|
'allow_redirects'=> false, |
37
|
|
|
]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Sends a GET request. |
43
|
|
|
* |
44
|
|
|
* @param string $url |
45
|
|
|
* @param array $data |
46
|
|
|
* @param array $headers |
47
|
|
|
* |
48
|
|
|
* |
49
|
|
|
* @return Response |
50
|
|
|
*/ |
51
|
|
|
public function get(string $url, array $data = [], array $headers = []): Response |
52
|
|
|
{ |
53
|
|
|
return $this->request('GET', $url, ['query'=>$data], $headers); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Sends a Post request. |
58
|
|
|
* |
59
|
|
|
* @param string $url |
60
|
|
|
* @param array $data |
61
|
|
|
* @param array $headers |
62
|
|
|
* |
63
|
|
|
* |
64
|
|
|
* @return Response |
65
|
|
|
*/ |
66
|
|
|
public function post(string $url, array $data = [], array $headers = []): Response |
67
|
|
|
{ |
68
|
|
|
return $this->request('POST', $url, ['json'=>$data], $headers); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Sends a Put request. |
73
|
|
|
* |
74
|
|
|
* @param string $url |
75
|
|
|
* @param array $data |
76
|
|
|
* @param array $headers |
77
|
|
|
* |
78
|
|
|
* |
79
|
|
|
* @return Response |
80
|
|
|
*/ |
81
|
|
|
public function put(string $url, array $data = [], array $headers = []): Response |
82
|
|
|
{ |
83
|
|
|
return $this->request('PUT', $url, ['json'=>$data], $headers); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sends a Patch request. |
88
|
|
|
* |
89
|
|
|
* @param string $url |
90
|
|
|
* @param array $data |
91
|
|
|
* @param array $headers |
92
|
|
|
* |
93
|
|
|
* |
94
|
|
|
* @return Response |
95
|
|
|
*/ |
96
|
|
|
public function patch(string $url, array $data = [], array $headers = []): Response |
97
|
|
|
{ |
98
|
|
|
return $this->request('PATCH', $url, ['json'=>$data], $headers); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Sends a Delete request. |
103
|
|
|
* |
104
|
|
|
* @param string $url |
105
|
|
|
* @param array $data |
106
|
|
|
* @param array $headers |
107
|
|
|
* |
108
|
|
|
* |
109
|
|
|
* @return Response |
110
|
|
|
*/ |
111
|
|
|
public function delete(string $url, array $data = [], array $headers = []): Response |
112
|
|
|
{ |
113
|
|
|
return $this->request('DELETE', $url, ['json'=>$data], $headers); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Sends a request. |
118
|
|
|
* |
119
|
|
|
* @param string $method |
120
|
|
|
* @param string $url |
121
|
|
|
* @param array $data |
122
|
|
|
* @param array $headers |
123
|
|
|
* |
124
|
|
|
* |
125
|
|
|
* @return Response |
126
|
|
|
*/ |
127
|
|
|
public function request(string $method, string $url, array $data = [], array $headers = []): Response |
128
|
|
|
{ |
129
|
|
|
$response = $this->client->request($method, $url, $data, $headers); |
130
|
|
|
|
131
|
|
|
$responseData = json_decode($response->getBody()->getContents(), true); |
|
|
|
|
132
|
|
|
|
133
|
|
|
return $this->response($responseData??[], $response->getStatusCode()); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Return Response. |
138
|
|
|
* |
139
|
|
|
* @param array $data |
140
|
|
|
* @param int $statusCode |
141
|
|
|
* @return Response |
142
|
|
|
*/ |
143
|
|
|
protected function response(array $data, int $statusCode): Response |
144
|
|
|
{ |
145
|
|
|
$r = new Response($this->driver, $statusCode); |
146
|
|
|
$r->data($data); |
147
|
|
|
|
148
|
|
|
return $r; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..