-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathClient.php
More file actions
179 lines (151 loc) · 5.92 KB
/
Client.php
File metadata and controls
179 lines (151 loc) · 5.92 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* Copyright (c) 2018 Alma / Nabla SAS
*
* THE MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @author Alma / Nabla SAS <contact@getalma.eu>
* @copyright Copyright (c) 2018 Alma / Nabla SAS
* @license https://opensource.org/licenses/MIT The MIT License
*
*/
namespace Alma\API;
use Alma\API\Endpoints;
use Alma\API\Lib\ClientOptionsValidator;
class Client
{
const VERSION = '2.5.1';
const LIVE_MODE = 'live';
const TEST_MODE = 'test';
const LIVE_API_URL = 'https://api.getalma.eu';
const SANDBOX_API_URL = 'https://api.sandbox.getalma.eu';
protected $context;
/***** API ENDPOINTS *****/
/**
* @var Endpoints\Payments
*/
public $payments;
/**
* @var Endpoints\Merchants
*/
public $merchants;
/**
* @var Endpoints\Orders
*/
public $orders;
/**
* @var Endpoints\Webhooks
*/
public $webhooks;
/**
* @var Endpoints\ShareOfCheckout
*/
public $shareOfCheckout;
/**
* @var Endpoints\Insurance
*/
public $insurance;
/**
* @var Endpoints\DataExports
*/
public $dataExports;
/*************************/
/**
* @var Endpoints\Configuration
*/
public $configuration;
/*************************/
/**
* Alma client initialization.
*
* @param string $api_key a valid API key for the service
*
*
* @param $options
* - api_root string|array[$mode => string] API root URL to use. If you need different URLs for the
* test and live modes, provide an array with the keys
* 'test' and 'live' and the URLs to use as values.
* Default: "https://api.getalma.eu"
*
* - force_tls int|boolean 0, 1 or 2 will force TLS 1.0, 1.1 or 1.2 when connecting to the API.
* `false` will not try to force TLS; `true` wil fallback to default value.
* If set to 0/1/2/true, TLS will be forced even if the API ROOT uses the
* "http://" scheme.
* Default: 2
* - mode string 'test' or 'live'. Default: 'live'
* - logger Psr\Log\LoggerInterface The logger instance to use for errors/warnings
*
* @throws DependenciesError
* @throws ParamsError
*/
public function __construct($api_key, $options = array())
{
$this->checkDependencies();
if (empty($api_key)) {
throw new ParamsError('An API key is required to instantiate new Alma\Client');
}
$options = ClientOptionsValidator::validateOptions($options);
$this->context = new ClientContext($api_key, $options);
$this->initUserAgent();
$this->initEndpoints();
}
public function addUserAgentComponent($component, $version) {
$this->context->addUserAgentComponent($component, $version);
}
/**
* @throws DependenciesError
*/
private function checkDependencies()
{
if (!function_exists('curl_init')) {
throw new DependenciesError('Alma requires the CURL PHP extension.');
}
if (!function_exists('json_decode')) {
throw new DependenciesError('Alma requires the JSON PHP extension.');
}
$openssl_exception = new DependenciesError('Alma requires OpenSSL >= 1.0.1');
if (!defined('OPENSSL_VERSION_TEXT')) {
throw $openssl_exception;
}
preg_match('/^(?:Libre|Open)SSL ([\d.]+)/', OPENSSL_VERSION_TEXT, $matches);
if (empty($matches[1])) {
throw $openssl_exception;
}
if (!version_compare($matches[1], '1.0.1', '>=')) {
throw $openssl_exception;
}
}
private function initEndpoints()
{
$this->payments = new Endpoints\Payments($this->context);
$this->merchants = new Endpoints\Merchants($this->context);
$this->orders = new Endpoints\Orders($this->context);
$this->shareOfCheckout = new Endpoints\ShareOfCheckout($this->context);
$this->webhooks = new Endpoints\Webhooks($this->context);
$this->insurance = new Endpoints\Insurance($this->context);
$this->dataExports = new Endpoints\DataExports($this->context);
$this->configuration = new Endpoints\Configuration($this->context);
}
private function initUserAgent()
{
$phpVersion = rtrim(str_replace(PHP_EXTRA_VERSION, '', PHP_VERSION), '-');
$this->addUserAgentComponent('PHP', $phpVersion);
$this->addUserAgentComponent('Alma for PHP', self::VERSION);
}
}
// Keep those here for backward compatibility
const LIVE_MODE = Client::LIVE_MODE;
const TEST_MODE = Client::TEST_MODE;