|
1
|
|
|
<?php namespace Ntholenaar\MultiSafepayClient\Api; |
|
2
|
|
|
|
|
3
|
|
|
use Ntholenaar\MultiSafepayClient\Client; |
|
4
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
5
|
|
|
|
|
6
|
|
|
abstract class AbstractApi implements ApiInterface |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* HultiSafepay API Client. |
|
10
|
|
|
* |
|
11
|
|
|
* @var Client |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $client; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param Client $client |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct(Client $client) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->client = $client; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Execute an Http GET request. |
|
25
|
|
|
* |
|
26
|
|
|
* @param $path |
|
27
|
|
|
* @param array $parameters |
|
28
|
|
|
* @return mixed |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function get($path, array $parameters = array()) |
|
31
|
|
|
{ |
|
32
|
|
|
if (count($parameters) > 0) { |
|
33
|
|
|
$path .= '?' . http_build_query($parameters); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$response = $this->client->getHttpClient()->get($path); |
|
37
|
|
|
|
|
38
|
|
|
return $this->parseResponse($response); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Execute an http POST request. |
|
43
|
|
|
* |
|
44
|
|
|
* @param $path |
|
45
|
|
|
* @param array $parameters |
|
46
|
|
|
* @param array $body |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function post($path, array $parameters = array(), $body = array()) |
|
50
|
|
|
{ |
|
51
|
|
|
if (count($parameters) > 0) { |
|
52
|
|
|
$path .= '?' . http_build_query($parameters); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$body = json_encode($body); |
|
56
|
|
|
|
|
57
|
|
|
$response = $this->client->getHttpClient()->post($path, [], $body); |
|
58
|
|
|
|
|
59
|
|
|
return $this->parseResponse($response); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Parse the response. |
|
64
|
|
|
* |
|
65
|
|
|
* @param ResponseInterface $response |
|
66
|
|
|
* @return mixed |
|
67
|
|
|
* @throws \Exception |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function parseResponse(ResponseInterface $response) |
|
70
|
|
|
{ |
|
71
|
|
|
$response = json_decode($response->getBody()->getContents()); |
|
72
|
|
|
|
|
73
|
|
|
if (isset($response->success) && $response->success) { |
|
74
|
|
|
return $response->data; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
throw new \Exception($response->error_info, $response->error_code); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|