1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sebdesign\VivaPayments; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use GuzzleHttp\Psr7\Response; |
7
|
|
|
|
8
|
|
|
class Client |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Demo environment URL. |
12
|
|
|
*/ |
13
|
|
|
const DEMO_URL = 'https://demo.vivapayments.com'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Production environment URL. |
17
|
|
|
*/ |
18
|
|
|
const PRODUCTION_URL = 'https://www.vivapayments.com'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \GuzzleHttp\ClientInterface |
22
|
|
|
*/ |
23
|
|
|
protected $client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor. |
27
|
|
|
* |
28
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
29
|
|
|
*/ |
30
|
|
|
public function __construct(ClientInterface $client) |
31
|
|
|
{ |
32
|
|
|
$this->client = $client; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Make a GET request. |
37
|
|
|
* |
38
|
|
|
* @param string $url |
39
|
|
|
* @param array $options |
40
|
|
|
* @return object |
41
|
|
|
*/ |
42
|
|
|
public function get($url, array $options = []) |
43
|
|
|
{ |
44
|
|
|
$response = $this->client->get($url, $options); |
45
|
|
|
|
46
|
|
|
return $this->getBody($response); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Make a POST request. |
51
|
|
|
* |
52
|
|
|
* @param string $url |
53
|
|
|
* @param array $options |
54
|
|
|
* @return object |
55
|
|
|
*/ |
56
|
|
|
public function post($url, array $options = []) |
57
|
|
|
{ |
58
|
|
|
$response = $this->client->post($url, $options); |
59
|
|
|
|
60
|
|
|
return $this->getBody($response); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Make a PATCH request. |
65
|
|
|
* |
66
|
|
|
* @param string $url |
67
|
|
|
* @param array $options |
68
|
|
|
* @return object |
69
|
|
|
*/ |
70
|
|
|
public function patch($url, array $options = []) |
71
|
|
|
{ |
72
|
|
|
$response = $this->client->patch($url, $options); |
73
|
|
|
|
74
|
|
|
return $this->getBody($response); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Make a DELETE request. |
79
|
|
|
* |
80
|
|
|
* @param string $url |
81
|
|
|
* @param array $options |
82
|
|
|
* @return object |
83
|
|
|
*/ |
84
|
|
|
public function delete($url, array $options = []) |
85
|
|
|
{ |
86
|
|
|
$response = $this->client->delete($url, $options); |
87
|
|
|
|
88
|
|
|
return $this->getBody($response); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the response body. |
93
|
|
|
* |
94
|
|
|
* @param \GuzzleHttp\Psr7\Response $response |
95
|
|
|
* @return mixed |
96
|
|
|
* |
97
|
|
|
* @throws \Sebdesign\VivaPayments\VivaException |
98
|
|
|
*/ |
99
|
|
|
protected function getBody(Response $response) |
100
|
|
|
{ |
101
|
|
|
$body = json_decode($response->getBody(), false, 512, JSON_BIGINT_AS_STRING); |
102
|
|
|
|
103
|
|
|
if (isset($body->ErrorCode) && $body->ErrorCode !== 0) { |
104
|
|
|
throw new VivaException($body->ErrorText, $body->ErrorCode); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $body; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the URL. |
112
|
|
|
* |
113
|
|
|
* @return \GuzzleHttp\Psr7\Uri |
114
|
|
|
*/ |
115
|
|
|
public function getUrl() |
116
|
|
|
{ |
117
|
|
|
return $this->client->getConfig('base_uri'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the Guzzle client. |
122
|
|
|
* |
123
|
|
|
* @return \GuzzleHttp\Client |
124
|
|
|
*/ |
125
|
|
|
public function getClient() |
126
|
|
|
{ |
127
|
|
|
return $this->client; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|