forked from payuru/php-payu4
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAuthorization.php
More file actions
258 lines (216 loc) · 8.3 KB
/
Authorization.php
File metadata and controls
258 lines (216 loc) · 8.3 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
<?php
declare(strict_types=1);
namespace Ypmn;
use Ypmn\Traits\ProtobufSerializable;
/**
* Авторизация платежа
*/
class Authorization implements AuthorizationInterface
{
/**
* Включить страницу оплаты Ypmn
* @var bool страница оплаты Ypmn включена?
*/
private bool $usePaymentPage = true;
/**
* Платёжный метод
* @var string|null
*/
private ?string $paymentMethod = PaymentMethods::CCVISAMC;
/**
* @var array|null
*/
private ?array $threeDSecure = null;
/**
* @var CardDetailsInterface|null Данные карты
*/
private ?CardDetailsInterface $cardDetails = null;
/** @var MerchantTokenInterface|null Данные карты (в виде объекта токена) */
private ?MerchantTokenInterface $merchantToken = null;
/** @var OneTimeUseToken|null Одноразовый токен оплаты */
private ?OneTimeUseToken $oneTimeUseToken = null;
/** @var PaymentPageOptions|null */
private ?PaymentPageOptions $paymentPageOptions = null;
/** Protobuf generation Trait */
use ProtobufSerializable;
/**
* Создать Авторизацию платежа
* (можно указать метод из справочника PaymentMethods.php,
* или передать null, чтобы плательщик выбрал метод сам)
* @param string|null $paymentMethodType Метод оплаты (из справочника)
* @param bool $isPaymentPageUsed страница оплаты Ypmn включена?
* @throws PaymentException Ошибка оплаты
*/
public function __construct(
?string $paymentMethodType = null,
bool $isPaymentPageUsed = true
) {
$this->setPaymentMethod($paymentMethodType);
$this->setUsePaymentPage($isPaymentPageUsed);
}
use ProtobufSerializable;
/** @inheritDoc */
public function setPaymentMethod(?string $paymentMethod = null) : self
{
if (is_string($paymentMethod)) {
$paymentMethod = strtoupper($paymentMethod);
}
switch ($paymentMethod) {
case PaymentMethods::CCVISAMC:
case PaymentMethods::FASTER_PAYMENTS:
case PaymentMethods::INTCARD:
case PaymentMethods::ALFAPAY:
case PaymentMethods::TPAY:
case PaymentMethods::SBERPAY:
case PaymentMethods::BNPL:
case null:
$this->paymentMethod = $paymentMethod;
break;
case '':
$this->paymentMethod = PaymentMethods::CCVISAMC;
break;
default:
throw new PaymentException('Неверный тип оплаты в авторизации');
}
return $this;
}
/** @inheritDoc */
public function setUsePaymentPage(?bool $isUsed) : self
{
if ($isUsed === true) {
if (is_null($this->merchantToken) && is_null($this->cardDetails)) {
$this->usePaymentPage = $isUsed;
} else {
throw new PaymentException('For using PaymentPage need to make MerchantToken = NULL and CardDetails = NULL');
}
} else {
$this->usePaymentPage = $isUsed;
}
return $this;
}
/** @inheritDoc */
public function getUsePaymentPage(): bool
{
return $this->usePaymentPage;
}
/** @inheritDoc */
public function getPaymentMethod(): ?string
{
return $this->paymentMethod;
}
/** @inheritDoc */
public function getCardDetails(): ?CardDetailsInterface
{
return $this->cardDetails;
}
/** @inheritDoc */
public function setCardDetails(?CardDetailsInterface $cardDetails): self
{
if (is_null($this->merchantToken) && $this->usePaymentPage === false) {
$this->cardDetails = $cardDetails;
return $this;
} else {
throw new PaymentException('For using CardDetails need to make MerchantToken = NULL and usePaymentPage = false');
}
}
/** @inheritDoc */
public function getMerchantToken(): ?MerchantTokenInterface
{
return $this->merchantToken;
}
/** @inheritDoc */
public function setOneTimeUseToken(?OneTimeUseToken $oneTimeUseToken): self
{
$this->setCardDetails(null);
$this->setUsePaymentPage(false);
$this->oneTimeUseToken = $oneTimeUseToken;
return $this;
}
/** @inheritDoc */
public function getOneTimeUseToken(): ?OneTimeUseToken
{
return $this->oneTimeUseToken;
}
/** @inheritDoc */
public function setMerchantToken(?MerchantTokenInterface $merchantToken): self
{
if (is_null($this->getCardDetails())) {
$this->merchantToken = $merchantToken;
return $this;
} else {
throw new PaymentException('For using MerchantToken, make sure CardDetails = NULL');
}
}
/** @inheritDoc */
public function setPaymentPageOptions(PaymentPageOptionsInterface $paymentPageOptions): self
{
$this->paymentPageOptions = $paymentPageOptions;
return $this;
}
/** @inheritDoc */
public function getPaymentPageOptions(): PaymentPageOptionsInterface
{
return $this->paymentPageOptions;
}
/** @inheritDoc */
public function setThreeDSecure(
int $screenHeight = null,
int $screenWidth = null,
int $timezone = null,
string $userAgent = null,
string $colorDepth = null,
string $language = null,
string $requestIp = null
): self
{
if (empty($userAgent)) {
if (!empty($_SERVER['HTTP_USER_AGENT'])) {
$userAgent = $_SERVER['HTTP_USER_AGENT'];
} else {
$userAgent = 'Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/140.0.0.0 Safari\/537.36';
}
}
$this->threeDSecure = [
'strongCustomerAuthentication' => [
'clientEnvironment' => [
'browser' => [
'screenHeight' => (!empty($screenHeight) ? $screenHeight : '1440'),
'screenWidth' => (!empty($screenWidth) ? $screenWidth : '2561'),
'timezone' => (!empty($timezone) ? $timezone : '-180'),
'userAgent' => (!empty($userAgent) ? $userAgent : 'Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/140.0.0.0 Safari\/537.36'),
'colorDepth' => (!empty($colorDepth) ? $colorDepth : '24'),
'language' => (!empty($language) ? $language : 'ru-RU'),
'requestIp' => (filter_var($requestIp, FILTER_VALIDATE_IP) ? $requestIp : Std::get_client_ip()),
'acceptHeader' => '*\/*',
'javaEnabled' => 'NO',
]
]
]
];
return $this;
}
/** @return array */
public function arraySerialize(): array
{
$resultArray = [
'usePaymentPage' => ($this->usePaymentPage ? 'YES' : 'NO'),
'paymentMethod' => $this->paymentMethod,
];
if (!is_null($this->cardDetails)) {
$resultArray['cardDetails'] = $this->cardDetails->toArray();
}
if (!is_null($this->oneTimeUseToken)) {
$resultArray['oneTimeUseToken'] = $this->oneTimeUseToken->toArray();
}
if (!is_null($this->merchantToken)) {
$resultArray['merchantToken'] = $this->merchantToken->toArray();
}
if (!is_null($this->threeDSecure)) {
$resultArray['threeDSecure'] = $this->threeDSecure;
}
if (!is_null($this->paymentPageOptions) && $this->paymentPageOptions->getOrderTimeout() > 0) {
$resultArray['paymentPageOptions'] = $this->paymentPageOptions->toArray();
}
return $resultArray;
}
}