-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathUnitPay.php
More file actions
299 lines (264 loc) · 7.32 KB
/
UnitPay.php
File metadata and controls
299 lines (264 loc) · 7.32 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
<?php
/**
* UnitPay Payment Module
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category UnitPay
* @package unitpay/unitpay
* @version 1.0.0
* @author UnitPay
* @copyright Copyright (c) 2015 UnitPay
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
* EXTENSION INFORMATION
*
* UNITPAY API https://unitpay.ru/doc
*
*/
namespace unitpay;
/**
* Payment method UnitPay process
*
* @author UnitPay <support@unitpay.ru>
*/
class UnitPay
{
private $supportedCurrencies = array('EUR','UAH', 'BYR', 'USD','RUB');
private $supportedUnitpayMethods = array('initPayment', 'getPayment');
private $requiredUnitpayMethodsParams = array(
'initPayment' => array('desc', 'account', 'sum'),
'getPayment' => array('paymentId')
);
private $supportedPartnerMethods = array('check', 'pay', 'error');
private $supportedUnitpayIp = array(
'31.186.100.49',
'178.132.203.105',
'52.29.152.23',
'52.19.56.234',
'127.0.0.1' // for debug
);
private $secretKey;
private $params = array();
const API_URL = 'https://unitpay.ru/api';
const FORM_URL = 'https://unitpay.ru/pay/';
public function __construct($secretKey = null)
{
$this->secretKey = $secretKey;
}
/**
* Create SHA-256 digital signature
*
* @param array $params
* @param $method
*
* @return string
*/
function getSignature(array $params, $method = null)
{
ksort($params);
unset($params['sign']);
unset($params['signature']);
array_push($params, $this->secretKey);
if ($method) {
array_unshift($params, $method);
}
return hash('sha256', join('{up}', $params));
}
/**
* Return IP address
*
* @return string
*/
protected function getIp()
{
return $_SERVER['REMOTE_ADDR'];
}
/**
* Get URL for pay through the form
*
* @param string $publicKey
* @param string|float|int $sum
* @param string $account
* @param string $desc
* @param string $currency
* @param string $locale
*
* @return string
*/
public function form($publicKey, $sum, $account, $desc, $currency = 'RUB', $locale = 'ru')
{
$vitalParams = array(
'account' => $account,
'currency' => $currency,
'desc' => $desc,
'sum' => $sum
);
$this->params = array_merge($this->params, $vitalParams);
if ($this->secretKey) {
$this->params['signature'] = $this->getSignature($vitalParams);
}
$this->params['locale'] = $locale;
return self::FORM_URL . $publicKey . '?' . http_build_query($this->params);
}
/**
* Set customer email
*
* @param string $email
*
* @return UnitPay
*/
public function setCustomerEmail($email)
{
$this->params['customerEmail'] = $email;
return $this;
}
/**
* Set customer phone number
*
* @param string $phone
*
* @return UnitPay
*/
public function setCustomerPhone($phone)
{
$this->params['customerPhone'] = $phone;
return $this;
}
/**
* Set list of paid goods
*
* @param CashItem[] $items
*
* @return UnitPay
*/
public function setCashItems($items)
{
$this->params['cashItems'] = base64_encode(
json_encode(
array_map(function ($item) {
/** @var CashItem $item */
return array(
'name' => $item->getName(),
'count' => $item->getCount(),
'price' => $item->getPrice()
);
}, $items)));
return $this;
}
/**
* Set callback URL
*
* @param string $backUrl
* @return UnitPay
*/
public function setBackUrl($backUrl)
{
$this->params['backUrl'] = $backUrl;
return $this;
}
/**
* Call API
*
* @param $method
* @param array $params
*
* @return object
*
* @throws InvalidArgumentException
* @throws UnexpectedValueException
*/
public function api($method, $params = array())
{
if (!in_array($method, $this->supportedUnitpayMethods)) {
throw new UnexpectedValueException('Method is not supported');
}
if (isset($this->requiredUnitpayMethodsParams[$method])) {
foreach ($this->requiredUnitpayMethodsParams[$method] as $rParam) {
if (!isset($params[$rParam])) {
throw new InvalidArgumentException('Param '.$rParam.' is null');
}
}
}
$params['secretKey'] = $this->secretKey;
if (empty($params['secretKey'])) {
throw new InvalidArgumentException('SecretKey is null');
}
$requestUrl = self::API_URL . '?' . http_build_query([
'method' => $method,
'params' => $params
], null, '&', PHP_QUERY_RFC3986);
$response = json_decode(file_get_contents($requestUrl));
if (!is_object($response)) {
throw new InvalidArgumentException('Temporary server error. Please try again later.');
}
return $response;
}
/**
* Check request on handler from UnitPay
*
* @return bool
*
* @throws InvalidArgumentException
* @throws UnexpectedValueException
*/
public function checkHandlerRequest()
{
$ip = $this->getIp();
if (!isset($_GET['method'])) {
throw new InvalidArgumentException('Method is null');
}
if (!isset($_GET['params'])) {
throw new InvalidArgumentException('Params is null');
}
list($method, $params) = array($_GET['method'], $_GET['params']);
if (!in_array($method, $this->supportedPartnerMethods)) {
throw new UnexpectedValueException('Method is not supported');
}
if (!isset($params['signature']) || $params['signature'] != $this->getSignature($params, $method)) {
throw new InvalidArgumentException('Wrong signature');
}
/**
* IP address check
* @link http://help.unitpay.ru/article/67-ip-addresses
*/
if (!in_array($ip, $this->supportedUnitpayIp)) {
throw new InvalidArgumentException('IP address Error');
}
return true;
}
/**
* Response for UnitPay if handle success
*
* @param $message
*
* @return string
*/
public function getSuccessHandlerResponse($message)
{
return json_encode(array(
"result" => array(
"message" => $message
)
));
}
/**
* Response for UnitPay if handle error
*
* @param $message
*
* @return string
*/
public function getErrorHandlerResponse($message)
{
return json_encode(array(
"error" => array(
"message" => $message
)
));
}
}