-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTinkoffMerchantAPI.php
More file actions
339 lines (311 loc) · 8.56 KB
/
Copy pathTinkoffMerchantAPI.php
File metadata and controls
339 lines (311 loc) · 8.56 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* File TinkoffMerchantAPI
*
* PHP version 5.3
*
* @category Tinkoff
* @package Tinkoff
* @author Shuyskiy Sergey <s.shuyskiy@tinkoff.ru>
* @license http://opensource.org/licenses/MIT MIT license
* @link http://tinkoff.ru
*/
//namespace Tinkoff;
//use HttpException;
/**
* Class TinkoffMerchantAPI
*
* @category Tinkoff
* @package Tinkoff
* @author Shuyskiy Sergey <s.shuyskiy@tinkoff.ru>
* @license http://opensource.org/licenses/MIT MIT license
* @link http://tinkoff.ru
* @property integer orderId
* @property integer Count
* @property bool|string error
* @property bool|string response
* @property bool|string customerKey
* @property bool|string status
* @property bool|string paymentUrl
* @property bool|string paymentId
*/
class TinkoffMerchantAPI
{
private $_api_url;
private $_terminalKey;
private $_secretKey;
private $_paymentId;
private $_status;
private $_error;
private $_response;
private $_paymentUrl;
/**
* Constructor
*
* @param string $terminalKey Your Terminal name
* @param string $secretKey Secret key for terminal
* @param string $api_url Url for API
*/
public function __construct($terminalKey, $secretKey, $api_url)
{
$this->_api_url = $api_url;
$this->_terminalKey = $terminalKey;
$this->_secretKey = $secretKey;
}
/**
* Get class property or json key value
*
* @param mixed $name Name for property or json key
*
* @return bool|string
*/
public function __get($name)
{
switch ($name) {
case 'paymentId':
return $this->_paymentId;
case 'status':
return $this->_status;
case 'error':
return $this->_error;
case 'paymentUrl':
return $this->_paymentUrl;
case 'response':
return htmlentities($this->_response);
default:
if ($this->_response) {
if ($json = json_decode($this->_response, true)) {
foreach ($json as $key => $value) {
if (strtolower($name) == strtolower($key)) {
return $json[$key];
}
}
}
}
return false;
}
}
/**
* Initialize the payment
*
* @param mixed $args mixed You could use associative array or url params string
*
* @return bool
*/
public function init($args)
{
return $this->buildQuery('Init', $args);
}
/**
* Get state of payment
*
* @param mixed $args Can be associative array or string
*
* @return mixed
*/
public function getState($args)
{
return $this->buildQuery('GetState', $args);
}
/**
* Confirm 2-staged payment
*
* @param mixed $args Can be associative array or string
*
* @return mixed
*/
public function confirm($args)
{
return $this->buildQuery('Confirm', $args);
}
/**
* Performs recursive (re) payment - direct debiting of funds from the
* account of the Buyer's credit card.
*
* @param mixed $args Can be associative array or string
*
* @return mixed
*/
public function charge($args)
{
return $this->buildQuery('Charge', $args);
}
/**
* Registers in the terminal buyer Seller. (Init do it automatically)
*
* @param mixed $args Can be associative array or string
*
* @return mixed
*/
public function addCustomer($args)
{
return $this->buildQuery('AddCustomer', $args);
}
/**
* Returns the data stored for the terminal buyer Seller.
*
* @param mixed $args Can be associative array or string
*
* @return mixed
*/
public function getCustomer($args)
{
return $this->buildQuery('GetCustomer', $args);
}
/**
* Deletes the data of the buyer.
*
* @param mixed $args Can be associative array or string
*
* @return mixed
*/
public function removeCustomer($args)
{
return $this->buildQuery('RemoveCustomer', $args);
}
/**
* Returns a list of bounded card from the buyer.
*
* @param mixed $args Can be associative array or string
*
* @return mixed
*/
public function getCardList($args)
{
return $this->buildQuery('GetCardList', $args);
}
/**
* Removes the customer's bounded card.
*
* @param mixed $args Can be associative array or string
*
* @return mixed
*/
public function removeCard($args)
{
return $this->buildQuery('RemoveCard', $args);
}
/**
* The method is designed to send all unsent notification
*
* @return mixed
*/
public function resend()
{
return $this->buildQuery('Resend', array());
}
/**
* Builds a query string and call sendRequest method.
* Could be used to custom API call method.
*
* @param string $path API method name
* @param mixed $args query params
*
* @return mixed
* @throws HttpException
*/
public function buildQuery($path, $args)
{
$url = $this->_api_url;
if (is_array($args)) {
if (! array_key_exists('TerminalKey', $args)) {
$args['TerminalKey'] = $this->_terminalKey;
}
if (! array_key_exists('Token', $args)) {
$args['Token'] = $this->_genToken($args);
}
}
$url = $this->_combineUrl($url, $path);
return $this->_sendRequest($url, $args);
}
/**
* Generates token
*
* @param array $args array of query params
*
* @return string
*/
private function _genToken($args)
{
$token = '';
$args['Password'] = $this->_secretKey;
ksort($args);
foreach ($args as $arg) {
$token .= $arg;
}
$token = hash('sha256', $token);
return $token;
}
/**
* Combines parts of URL. Simply gets all parameters and puts '/' between
*
* @return string
*/
private function _combineUrl()
{
$args = func_get_args();
$url = '';
foreach ($args as $arg) {
if (is_string($arg)) {
if ($arg[strlen($arg) - 1] !== '/') {
$arg .= '/';
}
$url .= $arg;
} else {
continue;
}
}
return $url;
}
/**
* Main method. Call API with params
*
* @param string $api_url API Url
* @param array $args API params
*
* @return mixed
* @throws HttpException
*/
private function _sendRequest($api_url, $args)
{
$this->_error = '';
//todo add string $args support
//$proxy = 'http://192.168.5.22:8080';
//$proxyAuth = '';
if (is_array($args)) {
$args = http_build_query($args);
}
//Debug::trace($args);
$out = file_get_contents($api_url.'?'.$args);
if($out){
return $out;
}
if ($curl = curl_init()) {
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $args);
$out = curl_exec($curl);
$this->_response = $out;
$json = json_decode($out);
if ($json) {
if (@$json->ErrorCode !== "0") {
$this->_error = @$json->Details;
} else {
$this->_paymentUrl = @$json->PaymentURL;
$this->_paymentId = @$json->PaymentId;
$this->_status = @$json->Status;
}
}
curl_close($curl);
return $out;
} else {
throw new HttpException(
'Can not create connection to ' . $api_url . ' with args '
. $args, 404
);
}
}
}