1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sebdesign\VivaPayments; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
use GuzzleHttp\Psr7\Uri; |
7
|
|
|
use GuzzleHttp\RequestOptions; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\UriInterface; |
11
|
|
|
|
12
|
|
|
class Client |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Demo environment URL. |
16
|
|
|
*/ |
17
|
|
|
const DEMO_URL = 'https://demo.vivapayments.com'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Production environment URL. |
21
|
|
|
*/ |
22
|
|
|
const PRODUCTION_URL = 'https://www.vivapayments.com'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Demo environment accounts URL. |
26
|
|
|
*/ |
27
|
|
|
const DEMO_ACCOUNTS_URL = 'https://demo-accounts.vivapayments.com'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Production environment accounts URL. |
31
|
|
|
*/ |
32
|
|
|
const PRODUCTION_ACCOUNTS_URL = 'https://accounts.vivapayments.com'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Demo environment URL. |
36
|
|
|
*/ |
37
|
|
|
const DEMO_API_URL = 'https://demo-api.vivapayments.com'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Production environment URL. |
41
|
|
|
*/ |
42
|
|
|
const PRODUCTION_API_URL = 'https://api.vivapayments.com'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var \GuzzleHttp\Client |
46
|
|
|
*/ |
47
|
|
|
protected $client; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $environment; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $token; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Constructor. |
61
|
|
|
*/ |
62
|
|
|
public function __construct(GuzzleClient $client, string $environment) |
63
|
|
|
{ |
64
|
|
|
$this->client = $client; |
65
|
|
|
|
66
|
|
|
if (! in_array($environment, ['demo', 'production'])) { |
67
|
|
|
throw new InvalidArgumentException( |
68
|
|
|
'The Viva Payments environment must be demo or production.' |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->environment = $environment; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Make a GET request. |
77
|
|
|
* |
78
|
|
|
* @param string $url |
79
|
|
|
* @param array $options |
80
|
|
|
* @return \stdClass |
81
|
|
|
*/ |
82
|
|
|
public function get(string $url, array $options = []) |
83
|
|
|
{ |
84
|
|
|
$response = $this->client->get($url, $options); |
85
|
|
|
|
86
|
|
|
return $this->getBody($response); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Make a POST request. |
91
|
|
|
* |
92
|
|
|
* @param string $url |
93
|
|
|
* @param array $options |
94
|
|
|
* @return \stdClass |
95
|
|
|
*/ |
96
|
|
|
public function post(string $url, array $options = []) |
97
|
|
|
{ |
98
|
|
|
$response = $this->client->post($url, $options); |
99
|
|
|
|
100
|
|
|
return $this->getBody($response); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Make a PATCH request. |
105
|
|
|
* |
106
|
|
|
* @param string $url |
107
|
|
|
* @param array $options |
108
|
|
|
* @return \stdClass|null |
109
|
|
|
*/ |
110
|
|
|
public function patch(string $url, array $options = []) |
111
|
|
|
{ |
112
|
|
|
$response = $this->client->patch($url, $options); |
113
|
|
|
|
114
|
|
|
return $this->getBody($response); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Make a DELETE request. |
119
|
|
|
* |
120
|
|
|
* @param string $url |
121
|
|
|
* @param array $options |
122
|
|
|
* @return \stdClass |
123
|
|
|
*/ |
124
|
|
|
public function delete(string $url, array $options = []) |
125
|
|
|
{ |
126
|
|
|
$response = $this->client->delete($url, $options); |
127
|
|
|
|
128
|
|
|
return $this->getBody($response); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the response body. |
133
|
|
|
* |
134
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
135
|
|
|
* @return \stdClass|null |
136
|
|
|
* |
137
|
|
|
* @throws \Sebdesign\VivaPayments\VivaException |
138
|
|
|
*/ |
139
|
|
|
protected function getBody(ResponseInterface $response) |
140
|
|
|
{ |
141
|
|
|
/** @var \stdClass|null $body */ |
142
|
|
|
$body = json_decode($response->getBody(), false, 512, JSON_BIGINT_AS_STRING); |
143
|
|
|
|
144
|
|
|
if (isset($body->ErrorCode) && $body->ErrorCode !== 0) { |
145
|
|
|
throw new VivaException($body->ErrorText, $body->ErrorCode); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $body; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the URL. |
153
|
|
|
*/ |
154
|
|
|
public function getUrl(): UriInterface |
155
|
|
|
{ |
156
|
|
|
$uris = [ |
157
|
|
|
'production' => self::PRODUCTION_URL, |
158
|
|
|
'demo' => self::DEMO_URL, |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
return new Uri($uris[$this->environment]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get the accounts URL. |
166
|
|
|
*/ |
167
|
|
|
public function getAccountsUrl(): UriInterface |
168
|
|
|
{ |
169
|
|
|
$uris = [ |
170
|
|
|
'production' => self::PRODUCTION_ACCOUNTS_URL, |
171
|
|
|
'demo' => self::DEMO_ACCOUNTS_URL, |
172
|
|
|
]; |
173
|
|
|
|
174
|
|
|
return new Uri($uris[$this->environment]); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get the API URL. |
179
|
|
|
*/ |
180
|
|
|
public function getApiUrl(): UriInterface |
181
|
|
|
{ |
182
|
|
|
$uris = [ |
183
|
|
|
'production' => self::PRODUCTION_API_URL, |
184
|
|
|
'demo' => self::DEMO_API_URL, |
185
|
|
|
]; |
186
|
|
|
|
187
|
|
|
return new Uri($uris[$this->environment]); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get the Guzzle client. |
192
|
|
|
*/ |
193
|
|
|
public function getClient(): GuzzleClient |
194
|
|
|
{ |
195
|
|
|
return $this->client; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Authenticate using basic auth. |
200
|
|
|
*/ |
201
|
|
|
public function authenticateWithBasicAuth(): array |
202
|
|
|
{ |
203
|
|
|
return [ |
204
|
|
|
RequestOptions::AUTH => [ |
205
|
|
|
config('services.viva.merchant_id'), |
206
|
|
|
config('services.viva.api_key'), |
207
|
|
|
], |
208
|
|
|
]; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Authenticate using the public key as a query string. |
213
|
|
|
*/ |
214
|
|
|
public function authenticateWithPublicKey(): array |
215
|
|
|
{ |
216
|
|
|
return [ |
217
|
|
|
RequestOptions::QUERY => [ |
218
|
|
|
'key' => config('services.viva.public_key'), |
219
|
|
|
], |
220
|
|
|
]; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Authenticate using the bearer token as an authorization header. |
225
|
|
|
*/ |
226
|
|
|
public function authenticateWithBearerToken(): array |
227
|
|
|
{ |
228
|
|
|
return [ |
229
|
|
|
RequestOptions::HEADERS => [ |
230
|
|
|
'Authorization' => "Bearer {$this->token}", |
231
|
|
|
], |
232
|
|
|
]; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function withToken(string $token): self |
236
|
|
|
{ |
237
|
|
|
$this->token = $token; |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|