-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathUnitPay.php
379 lines (341 loc) · 10.1 KB
/
UnitPay.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* Value object for paid goods
*/
final class CashItem
{
const NDS_NONE = 'none';
const NDS_0 = 'vat0';
const NDS_10 = 'vat10';
const NDS_20 = 'vat20';
/** Товар */
const PAYMENT_OBJECT_COMMODITY = 'commodity';
/** Подакцизный товар */
const PAYMENT_OBJECT_EXCISE = 'excise';
/** Работа */
const PAYMENT_OBJECT_JOB = 'job';
/** Услуга */
const PAYMENT_OBJECT_SERVICE = 'service';
/** Ставка */
const PAYMENT_OBJECT_GAMBLING_BET = 'gambling_bet';
/** Выигрыш */
const PAYMENT_OBJECT_GAMBLING_PRIZE = 'gambling_prize';
/** Лотерейный билет */
const PAYMENT_OBJECT_LOTTERY = 'lottery';
/** Выигрыш лотереи */
const PAYMENT_OBJECT_LOTTERY_PRIZE = 'lottery_prize';
/** Результаты интеллектуальной деятельности */
const PAYMENT_OBJECT_INTELLECTUAL_ACTIVITY = 'intellectual_activity';
/** Платёж */
const PAYMENT_OBJECT_PAYMENT = 'payment';
/** Агентское вознаграждение */
const PAYMENT_OBJECT_AGENT_COMMISSION = 'agent_commission';
/** Составной предмет расчёта */
const PAYMENT_OBJECT_COMPOSITE = 'composite';
/** Иной предмет расчёта */
const PAYMENT_OBJECT_ANOTHER = 'another';
/** */
const PAYMENT_OBJECT_PROPERTY_RIGHT = 'property_right';
/** */
const PAYMENT_OBJECT_NON_OPERATING_GAIN = 'non-operating_gain';
/** */
const PAYMENT_OBJECT_INSURANCE_PREMIUM = 'insurance_premium';
/** Налог с продажи */
const PAYMENT_OBJECT_SALES_TAX = 'sales_tax';
/** Курортный сбор */
const PAYMENT_OBJECT_RESORT_FEE = 'resort_fee';
/** 100% предоплата */
const PAYMENT_METHOD_PREPAYMENT_FULL = 'full_prepayment';
/** Частичная предоплата */
const PAYMENT_METHOD_PREPAYMENT = 'prepayment';
/** Аванс */
const PAYMENT_METHOD_ADVANCE = 'advance';
/** Полный расчёт */
const PAYMENT_METHOD_PAYMENT_FULL = 'full_payment';
private $name;
private $count;
private $price;
private $nds;
private $type;
private $paymentMethod;
/**
* @param string $name
* @param int $count
* @param float $price
* @param string $nds
* @param string $type
* @param string $paymentMethod
*/
public function __construct(
$name,
$count,
$price,
$nds = self::NDS_NONE,
$type = self::PAYMENT_OBJECT_COMMODITY,
$paymentMethod = self::PAYMENT_METHOD_PREPAYMENT_FULL
)
{
$this->name = $name;
$this->count = $count;
$this->price = $price;
$this->nds = $nds;
$this->type = $type;
$this->paymentMethod = $paymentMethod;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return int
*/
public function getCount()
{
return $this->count;
}
/**
* @return float
*/
public function getPrice()
{
return $this->price;
}
/**
* @return string
*/
public function getNds()
{
return $this->nds;
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @return string
*/
public function getPaymentMethod()
{
return $this->paymentMethod;
}
}
/**
* Payment method Unitpay process
*/
class UnitPay
{
private $supportedUnitpayMethods = ['initPayment', 'getPayment'];
private $requiredUnitpayMethodsParams = [
'initPayment' => ['desc', 'account', 'sum'],
'getPayment' => ['paymentId'],
];
private $supportedPartnerMethods = ['check', 'pay', 'error'];
private $supportedUnitpayIp = [
'31.186.100.49',
'52.29.152.23',
'52.19.56.234',
'127.0.0.1' // for debug
];
private $secretKey;
private $params = [];
private $apiUrl;
private $formUrl;
public function __construct($domain, $secretKey = null)
{
$this->secretKey = $secretKey;
$this->apiUrl = "https://$domain/api";
$this->formUrl = "https://$domain/pay/";
}
/**
* Create SHA-256 digital signature
* @param array $params
* @param string|null $method
* @return string
*/
public function getSignature(array $params, $method = null)
{
unset($params['signature']);
ksort($params);
$params[] = $this->secretKey;
if ($method !== null) {
array_unshift($params, $method);
}
return hash('sha256', implode('{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 = [
'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 $this->formUrl . $publicKey . '?' . http_build_query($this->params);
}
/**
* Set customer email
* @param string $email
* @return $this
*/
public function setCustomerEmail($email)
{
$this->params['customerEmail'] = $email;
return $this;
}
/**
* Set customer phone number
* @param string $phone
* @return $this
*/
public function setCustomerPhone($phone)
{
$this->params['customerPhone'] = $phone;
return $this;
}
/**
* Set list of paid goods
* @param CashItem[] $items
* @return $this
*/
public function setCashItems(array $items)
{
$this->params['cashItems'] = base64_encode(
json_encode(
/** @var CashItem $item */
array_map(static function ($item) {
return [
'name' => $item->getName(),
'count' => $item->getCount(),
'price' => $item->getPrice(),
'nds' => $item->getNds(),
'type' => $item->getType(),
'paymentMethod' => $item->getPaymentMethod(),
];
}, $items)));
return $this;
}
/**
* Set callback URL
* @param string $backUrl
* @return $this
*/
public function setBackUrl($backUrl)
{
$this->params['backUrl'] = $backUrl;
return $this;
}
/**
* Call API
* @param string $method
* @param array $params
* @return object
*
* @throws InvalidArgumentException
* @throws UnexpectedValueException
*/
public function api($method, array $params = [])
{
if (!in_array($method, $this->supportedUnitpayMethods, true)) {
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 = $this->apiUrl . '?' . 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) = [$_GET['method'], $_GET['params']];
if (!in_array($method, $this->supportedPartnerMethods, true)) {
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 https://help.unitpay.ru/book-of-reference/ip-addresses
*/
if (!in_array($ip, $this->supportedUnitpayIp, true)) {
throw new InvalidArgumentException('IP address Error');
}
return true;
}
/**
* Response for Unitpay if handle success
* @param string $message
* @return string
*/
public function getSuccessHandlerResponse($message)
{
return json_encode(['result' => ['message' => $message]]);
}
/**
* Response for Unitpay if handle error
* @param string $message
* @return string
*/
public function getErrorHandlerResponse($message)
{
return json_encode(['error' => ['message' => $message]]);
}
}