1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larium\Pay\Client; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\RequestInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Http\Discovery\HttpClientDiscovery; |
8
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
9
|
|
|
|
10
|
|
|
class XmlClient extends AbstractClient |
11
|
|
|
{ |
12
|
|
|
private $uri; |
13
|
|
|
|
14
|
|
|
private $headers = []; |
15
|
|
|
|
16
|
|
|
public function __construct($uri, array $options = []) |
17
|
|
|
{ |
18
|
|
|
$this->uri = $uri; |
19
|
|
|
$this->options = $options; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function addHeader($name, $value) |
23
|
|
|
{ |
24
|
|
|
$this->headers[$name] = $value; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Post given xml string to remote gateway. |
29
|
|
|
* |
30
|
|
|
* @param string $xml |
31
|
|
|
* @return array @see self::resolveResponse |
32
|
|
|
*/ |
33
|
|
|
public function post($xml) |
34
|
|
|
{ |
35
|
|
|
$factory = $this->getMessageFactory(); |
36
|
|
|
$request = $factory->createRequest('POST', $this->uri, $this->headers, $xml); |
37
|
|
|
|
38
|
|
|
return $this->resolveResponse($this->sendRequest($request)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function getMessageFactory() |
42
|
|
|
{ |
43
|
|
|
return MessageFactoryDiscovery::find(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Resolve the response from client. |
48
|
|
|
* |
49
|
|
|
* @param ResponseInterface $response |
50
|
|
|
* @return array An array with following values: |
51
|
|
|
* 'status' : The Http status of response |
52
|
|
|
* 'headers' : An array of response headers |
53
|
|
|
* 'body' : The response string. |
54
|
|
|
* 'raw_response': The raw body response for logging purposes. |
55
|
|
|
* 'raw_request' : The raw body request for logging purposes. |
56
|
|
|
*/ |
57
|
|
|
protected function resolveResponse(ResponseInterface $response) |
58
|
|
|
{ |
59
|
|
|
$body = $response->getBody()->__toString(); |
60
|
|
|
|
61
|
|
|
return array( |
62
|
|
|
'status' => $response->getStatusCode(), |
63
|
|
|
'headers' => $response->getHeaders(), |
64
|
|
|
'body' => $body, |
65
|
|
|
'raw_response' => $body, |
66
|
|
|
'raw_request' => $this->rawRequest, |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function authenticate(RequestInterface $request) |
71
|
|
|
{ |
72
|
|
|
return $request; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|