1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Larium\Pay\Client; |
6
|
|
|
|
7
|
|
|
use Http\Discovery\Psr17FactoryDiscovery; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
class XmlClient extends AbstractClient |
12
|
|
|
{ |
13
|
|
|
private array $headers = []; |
14
|
|
|
|
15
|
|
|
public function __construct( |
16
|
|
|
private readonly string $uri, |
17
|
|
|
array $options = [] |
18
|
|
|
) { |
19
|
|
|
$this->options = $options; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function addHeader(string $name, string $value): void |
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(string $xml): array |
34
|
|
|
{ |
35
|
|
|
$factory = Psr17FactoryDiscovery::findRequestFactory(); |
36
|
|
|
$request = $factory->createRequest('POST', $this->uri); |
37
|
|
|
foreach ($this->headers as $name => $value) { |
38
|
|
|
$request = $request->withHeader($name, $value); |
39
|
|
|
}; |
40
|
|
|
|
41
|
|
|
$body = Psr17FactoryDiscovery::findStreamFactory()->createStream($xml); |
42
|
|
|
$request = $request->withBody($body); |
43
|
|
|
|
44
|
|
|
return $this->resolveResponse($this->sendRequest($request)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Resolve the response from client. |
49
|
|
|
* |
50
|
|
|
* @param ResponseInterface $response |
51
|
|
|
* @return array An array with following values: |
52
|
|
|
* 'status' : The Http status of response |
53
|
|
|
* 'headers' : An array of response headers |
54
|
|
|
* 'body' : The response string. |
55
|
|
|
* 'raw_response': The raw body response for logging purposes. |
56
|
|
|
* 'raw_request' : The raw body request for logging purposes. |
57
|
|
|
*/ |
58
|
|
|
protected function resolveResponse(ResponseInterface $response): array |
59
|
|
|
{ |
60
|
|
|
$body = $response->getBody()->__toString(); |
61
|
|
|
|
62
|
|
|
return [ |
63
|
|
|
'status' => $response->getStatusCode(), |
64
|
|
|
'headers' => $response->getHeaders(), |
65
|
|
|
'body' => $body, |
66
|
|
|
'raw_response' => $body, |
67
|
|
|
'raw_request' => $this->rawRequest, |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function authenticate(RequestInterface $request): RequestInterface |
72
|
|
|
{ |
73
|
|
|
return $request; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|