|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* MIT License |
|
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace SprykerEco\Zed\Payone\Business\Api\Adapter\Http; |
|
9
|
|
|
|
|
10
|
|
|
use SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface; |
|
11
|
|
|
use SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer; |
|
12
|
|
|
use SprykerEco\Zed\Payone\Business\Exception\TimeoutException; |
|
13
|
|
|
|
|
14
|
|
|
abstract class AbstractHttpAdapter implements AdapterInterface |
|
15
|
|
|
{ |
|
16
|
|
|
public const DEFAULT_TIMEOUT = 45; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var int |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $timeout = self::DEFAULT_TIMEOUT; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $url; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $params = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $rawResponse; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $paymentGatewayUrl |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($paymentGatewayUrl) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->url = $paymentGatewayUrl; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param int $timeout |
|
48
|
|
|
* |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
|
public function setTimeout($timeout) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->timeout = $timeout; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return int |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getTimeout() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->timeout; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param array $params |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
public function sendRawRequest(array $params) |
|
70
|
|
|
{ |
|
71
|
|
|
$rawResponse = $this->performRequest($params); |
|
72
|
|
|
$result = $this->parseResponse($rawResponse); |
|
73
|
|
|
|
|
74
|
|
|
return $result; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer $container |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function sendRequest(AbstractRequestContainer $container) |
|
83
|
|
|
{ |
|
84
|
|
|
try { |
|
85
|
|
|
return $this->sendRawRequest($container->toArray()); |
|
86
|
|
|
} catch (TimeoutException $e) { |
|
87
|
|
|
$fakeArray = [ |
|
88
|
|
|
'status' => 'TIMEOUT', |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
return $fakeArray; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param array $params |
|
97
|
|
|
* |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
abstract protected function performRequest(array $params); |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param array $params |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function generateUrlArray(array $params) |
|
108
|
|
|
{ |
|
109
|
|
|
$urlRequest = $this->getUrl() . '?' . http_build_query($params, null, '&'); |
|
110
|
|
|
$urlArray = parse_url($urlRequest); |
|
111
|
|
|
|
|
112
|
|
|
return $urlArray; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param array $responseRaw |
|
117
|
|
|
* |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function parseResponse(array $responseRaw = []) |
|
121
|
|
|
{ |
|
122
|
|
|
$result = []; |
|
123
|
|
|
|
|
124
|
|
|
if (count($responseRaw) === 0) { |
|
125
|
|
|
return $result; |
|
126
|
|
|
} elseif (strpos($responseRaw[0], '%PDF-') !== false) { |
|
127
|
|
|
return ['rawResponse' => base64_encode(implode("\n", array_values($responseRaw)))]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
foreach ($responseRaw as $key => $line) { |
|
131
|
|
|
$pos = strpos($line, '='); |
|
132
|
|
|
|
|
133
|
|
|
if ($pos === false) { |
|
134
|
|
|
if (strlen($line) > 0) { |
|
135
|
|
|
$result[$key] = $line; |
|
136
|
|
|
} |
|
137
|
|
|
continue; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$lineArray = explode('=', $line); |
|
141
|
|
|
$resultKey = array_shift($lineArray); |
|
142
|
|
|
$result[$resultKey] = implode('=', $lineArray); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $result; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getUrl() |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->url; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param array $params |
|
158
|
|
|
* |
|
159
|
|
|
* @return void |
|
160
|
|
|
*/ |
|
161
|
|
|
public function setParams(array $params) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->params = $params; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getParams() |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->params; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return string |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getRawResponse() |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->rawResponse; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param string $rawResponse |
|
184
|
|
|
* |
|
185
|
|
|
* @return void |
|
186
|
|
|
*/ |
|
187
|
|
|
protected function setRawResponse($rawResponse) |
|
188
|
|
|
{ |
|
189
|
|
|
$this->rawResponse = $rawResponse; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|