1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GloBee\PaymentApi\Connectors; |
4
|
|
|
|
5
|
|
|
use GloBee\PaymentApi\PaymentApi; |
6
|
|
|
|
7
|
|
|
class GloBeeCurlConnector extends Connector |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
private $apiKey; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $baseUrl; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var CurlWrapper |
21
|
|
|
*/ |
22
|
|
|
private $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $platforms; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* GloBeeCurlConnector constructor. |
31
|
|
|
* |
32
|
|
|
* @param string $apiKey |
|
|
|
|
33
|
|
|
* @param bool $live |
|
|
|
|
34
|
|
|
* @param array $platforms |
|
|
|
|
35
|
|
|
* @param CurlWrapper|null $curlConnector |
|
|
|
|
36
|
|
|
*/ |
37
|
12 |
|
public function __construct($apiKey, $live = true, array $platforms = [], CurlWrapper $curlConnector = null) |
|
|
|
|
38
|
|
|
{ |
39
|
12 |
|
$this->apiKey = $apiKey; |
40
|
12 |
|
$this->baseUrl = $live ? 'https://globee.com/payment-api' : 'https://test.globee.com/payment-api'; |
41
|
12 |
|
$this->platforms = $platforms; |
42
|
12 |
|
$this->client = $curlConnector ?: new CurlWrapper(); |
43
|
12 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $uri |
|
|
|
|
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
10 |
|
public function getJson($uri) |
|
|
|
|
51
|
|
|
{ |
52
|
10 |
|
return $this->send('GET', $uri); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $uri |
|
|
|
|
57
|
|
|
* @param array $data |
|
|
|
|
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
1 |
|
public function postJson($uri, array $data) |
|
|
|
|
62
|
|
|
{ |
63
|
1 |
|
$json = json_encode($data); |
64
|
|
|
|
65
|
1 |
|
return $this->send('POST', $uri, $json, [ |
66
|
1 |
|
'Content-Type' => 'application/json', |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $method |
|
|
|
|
72
|
|
|
* @param string $uri |
|
|
|
|
73
|
|
|
* @param string $body |
|
|
|
|
74
|
|
|
* @param array $headers |
|
|
|
|
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
11 |
|
protected function send($method, $uri, $body = null, array $headers = []) |
|
|
|
|
79
|
|
|
{ |
80
|
11 |
|
$headers = $this->compileHeaders($headers); |
81
|
|
|
|
82
|
11 |
|
$this->client->setOptions([ |
83
|
11 |
|
CURLOPT_RETURNTRANSFER => true, |
84
|
11 |
|
CURLOPT_URL => $this->baseUrl.'/'.$uri, |
85
|
11 |
|
CURLOPT_ACCEPT_ENCODING => 'application/json', |
86
|
11 |
|
CURLOPT_CUSTOMREQUEST => $method, |
87
|
11 |
|
CURLOPT_HTTPHEADER => $headers, |
88
|
11 |
|
CURLOPT_USERAGENT => $this->getUserAgentString(), |
89
|
|
|
]); |
90
|
|
|
|
91
|
11 |
|
if ($method !== 'GET' && $body) { |
92
|
1 |
|
$this->client->setOption(CURLOPT_POSTFIELDS, $body); |
93
|
|
|
} |
94
|
|
|
|
95
|
11 |
|
$response = $this->client->exec(); |
96
|
11 |
|
$httpcode = $this->client->getInfo(CURLINFO_HTTP_CODE); |
97
|
|
|
|
98
|
11 |
|
if ($httpcode >= 400) { |
99
|
6 |
|
$this->handleErrors($httpcode, $response); |
100
|
|
|
} |
101
|
|
|
|
102
|
5 |
|
return json_decode($response, true); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $headers |
|
|
|
|
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
11 |
|
protected function compileHeaders(array $headers = []) |
111
|
|
|
{ |
112
|
|
|
$headers = [ |
113
|
11 |
|
'X-AUTH-KEY' => $this->apiKey, |
114
|
11 |
|
] + $headers; |
115
|
|
|
|
116
|
11 |
|
$return = []; |
117
|
11 |
|
foreach ($headers as $key => $value) { |
118
|
11 |
|
$return[] = $key.': '.$value; |
119
|
|
|
} |
120
|
|
|
|
121
|
11 |
|
return $return; |
122
|
|
|
} |
123
|
|
|
|
124
|
11 |
|
protected function getUserAgentString() |
|
|
|
|
125
|
|
|
{ |
126
|
11 |
|
$platforms = $this->platforms; |
127
|
|
|
array_walk($platforms, function (&$version, $platform) { |
128
|
1 |
|
$version = $platform.'/'.$version; |
129
|
11 |
|
}); |
130
|
11 |
|
$platforms = implode(' ', $platforms); |
131
|
11 |
|
$userAgent = sprintf('GloBeePaymentSdk/%s (%s) PHP/%s', PaymentApi::VERSION, PHP_OS, PHP_VERSION); |
132
|
|
|
|
133
|
11 |
|
return trim($userAgent.' '.$platforms); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $url |
|
|
|
|
138
|
|
|
*/ |
|
|
|
|
139
|
1 |
|
public function setBaseUrl($url) |
|
|
|
|
140
|
|
|
{ |
141
|
1 |
|
$this->baseUrl = $url; |
142
|
1 |
|
} |
143
|
|
|
} |
144
|
|
|
|