-
Notifications
You must be signed in to change notification settings - Fork 0
/
Api.php
121 lines (105 loc) · 3.29 KB
/
Api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Payum\Paymill;
use GuzzleHttp\Psr7\Request;
use Paymill\API\CommunicationAbstract;
use Payum\Core\Bridge\Guzzle\HttpClientFactory;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\LogicException;
use Payum\Core\HttpClientInterface;
class Api extends CommunicationAbstract
{
const API_URL = 'https://api.paymill.com/v2.1/';
/**
* @var HttpClientInterface
*/
protected $client;
/**
* @var array
*/
protected $options = [
'api_private_key' => null,
'api_public_key' => null,
'test_private_key' => null,
'test_public_key' => null,
'sandbox' => null,
];
/**
* @param array $options
* @param HttpClientInterface $client
*
* @throws \Payum\Core\Exception\InvalidArgumentException if an option is invalid
* @throws \Payum\Core\Exception\LogicException if a sandbox is not boolean
*/
public function __construct(array $options, HttpClientInterface $client = null)
{
$options = ArrayObject::ensureArrayObject($options);
$options->validatedKeysSet([
'api_private_key',
'api_public_key',
'test_private_key',
'test_public_key',
]);
if (false == is_bool($options['sandbox'])) {
throw new LogicException('The boolean sandbox option must be set.');
}
$this->options = $options;
$this->client = HttpClientFactory::create();
}
/**
* @return string
*/
public function getPrivateKey()
{
return $this->options['sandbox'] ? $this->options['test_private_key'] : $this->options['api_private_key'];
}
/**
* @return string
*/
public function getPublicKey()
{
return $this->options['sandbox'] ? $this->options['test_public_key'] : $this->options['api_public_key'];
}
/**
* Perform HTTP request to REST endpoint
*
* @param string $action
* @param array $params
* @param string $method
* @return array
*/
public function requestApi($action = '', $params = array(), $method = 'POST')
{
$headers = [];
$request = new Request($method, $this->getApiEndpoint($action), $headers, http_build_query($params));
$options = [
'Authorization' => $this->getPrivateKey() .':',
];
$response = $this->client->send($request, $options);
$content = $response->getBody();
$type = $response->getHeaderLine('Content-Type');
if (false == ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300)) {
$content = ['error' => $response->getReasonPhrase()];
}
if ('application/json' === $type) {
$content = json_decode($content, true);
} elseif ((false !== strpos(strtolower($type), 'text/csv')) && !isset($content['error'])) {
return $content;
}
return [
'header' => [
'status' => $response->getStatusCode(),
'reason' => $response->getReasonPhrase(),
],
'body' => $content,
];
}
/**
* @param string $action
*
* @return string
*/
protected function getApiEndpoint($action)
{
return self::API_URL . $action;
}
}