1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sebdesign\VivaPayments; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Psr\Http\Message\UriInterface; |
8
|
|
|
|
9
|
|
|
class Client |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Demo environment URL. |
13
|
|
|
*/ |
14
|
|
|
const DEMO_URL = 'https://demo.vivapayments.com'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Production environment URL. |
18
|
|
|
*/ |
19
|
|
|
const PRODUCTION_URL = 'https://www.vivapayments.com'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \GuzzleHttp\Client |
23
|
|
|
*/ |
24
|
|
|
protected $client; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor. |
28
|
|
|
*/ |
29
|
|
|
public function __construct(GuzzleClient $client) |
30
|
|
|
{ |
31
|
|
|
$this->client = $client; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Make a GET request. |
36
|
|
|
* |
37
|
|
|
* @param string $url |
38
|
|
|
* @param array $options |
39
|
|
|
* @return \stdClass |
40
|
|
|
*/ |
41
|
|
|
public function get(string $url, array $options = []) |
42
|
|
|
{ |
43
|
|
|
$response = $this->client->get($url, $options); |
44
|
|
|
|
45
|
|
|
return $this->getBody($response); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Make a POST request. |
50
|
|
|
* |
51
|
|
|
* @param string $url |
52
|
|
|
* @param array $options |
53
|
|
|
* @return \stdClass |
54
|
|
|
*/ |
55
|
|
|
public function post(string $url, array $options = []) |
56
|
|
|
{ |
57
|
|
|
$response = $this->client->post($url, $options); |
58
|
|
|
|
59
|
|
|
return $this->getBody($response); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Make a PATCH request. |
64
|
|
|
* |
65
|
|
|
* @param string $url |
66
|
|
|
* @param array $options |
67
|
|
|
* @return \stdClass|null |
68
|
|
|
*/ |
69
|
|
|
public function patch(string $url, array $options = []) |
70
|
|
|
{ |
71
|
|
|
$response = $this->client->patch($url, $options); |
72
|
|
|
|
73
|
|
|
return $this->getBody($response); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Make a DELETE request. |
78
|
|
|
* |
79
|
|
|
* @param string $url |
80
|
|
|
* @param array $options |
81
|
|
|
* @return \stdClass |
82
|
|
|
*/ |
83
|
|
|
public function delete(string $url, array $options = []) |
84
|
|
|
{ |
85
|
|
|
$response = $this->client->delete($url, $options); |
86
|
|
|
|
87
|
|
|
return $this->getBody($response); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the response body. |
92
|
|
|
* |
93
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
94
|
|
|
* @return \stdClass|null |
95
|
|
|
* |
96
|
|
|
* @throws \Sebdesign\VivaPayments\VivaException |
97
|
|
|
*/ |
98
|
|
|
protected function getBody(ResponseInterface $response) |
99
|
|
|
{ |
100
|
|
|
/** @var \stdClass|null $body */ |
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
|
|
|
public function getUrl(): UriInterface |
114
|
|
|
{ |
115
|
|
|
return $this->client->getConfig('base_uri'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the Guzzle client. |
120
|
|
|
*/ |
121
|
|
|
public function getClient(): GuzzleClient |
122
|
|
|
{ |
123
|
|
|
return $this->client; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the public key as query string. |
128
|
|
|
*/ |
129
|
|
|
public function getKey(): string |
130
|
|
|
{ |
131
|
|
|
return config('services.viva.public_key'); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|