Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cashboxpaysystem.php
1<?php
2
3namespace Bitrix\Sale\Cashbox;
4
8
9Main\Localization\Loc::loadMessages(__FILE__);
10
15abstract class CashboxPaySystem extends Cashbox implements IPrintImmediately, ICheckable
16{
17 public const CACHE_ID = '';
18 private const TTL = 31536000;
19 protected const SEND_METHOD_HTTP_POST = 'POST';
20 protected const SEND_METHOD_HTTP_GET = 'GET';
21
22 abstract protected function getPrintUrl(): string;
23
24 abstract protected function getCheckUrl(): string;
25
26 abstract protected function send(string $url, Sale\Payment $payment, array $fields, string $method = self::SEND_METHOD_HTTP_POST): Sale\Result;
27
28 abstract protected function processPrintResult(Sale\Result $result): Sale\Result;
29
30 abstract protected function getDataForCheck(Sale\Payment $payment): array;
31
32 abstract protected function processCheckResult(Sale\Result $result): Sale\Result;
33
34 abstract protected function onAfterProcessCheck(Sale\Result $result, Sale\Payment $payment): Sale\Result;
35
36 abstract public static function getPaySystemCodeForKkm(): string;
37
41 public static function getSupportedKkmModels()
42 {
43 $supportedKkmModels = [];
44
45 $paySystemIterator = Sale\PaySystem\Manager::getList([
46 'filter' => [
47 '=ACTIVE' => 'Y',
48 ]
49 ]);
50 while ($paySystemItem = $paySystemIterator->fetch())
51 {
52 $paySystemService = new Sale\PaySystem\Service($paySystemItem);
53 if (
54 $paySystemService->isSupportPrintCheck()
55 && $paySystemService->getCashboxClass() === '\\'.static::class
56 )
57 {
58 $supportedKkmModels[] = static::getKkmValue($paySystemService);
59 }
60 }
61
62 $supportedKkmModels = array_unique(array_merge(...$supportedKkmModels));
63
64 $result = [];
65 foreach ($supportedKkmModels as $supportedKkm)
66 {
67 $result[$supportedKkm] = [
68 'NAME' => $supportedKkm
69 ];
70 }
71
72 return $result;
73 }
74
80 protected function getPaySystemSetting(Sale\Payment $payment, string $code)
81 {
82 $params = $payment->getPaySystem()->getParamsBusValue($payment);
83 return $params[$code] ?? null;
84 }
85
91 protected function checkParams(Check $check): Sale\Result
92 {
93 $result = new Sale\Result();
94
95 $payment = CheckManager::getPaymentByCheck($check);
96 if ($payment && $service = $payment->getPaySystem())
97 {
98 if (!$service->isSupportPrintCheck())
99 {
100 $result->addError(
101 new Main\Error(
102 Main\Localization\Loc::getMessage(
103 'SALE_CASHBOX_PAYSYSTEM_PAYSYSTEM_NOT_SUPPORT_PRINT_CHECK',
104 [
105 '#PAY_SYSTEM_NAME#' => $service->getField('NAME')
106 ]
107 )
108 )
109 );
110 }
111
112 if (!$service->canPrintCheckSelf($payment))
113 {
114 $result->addError(
115 new Main\Error(
116 Main\Localization\Loc::getMessage(
117 'SALE_CASHBOX_PAYSYSTEM_PAYSYSTEM_CANT_PRINT_CHECK_SELF',
118 [
119 '#PAY_SYSTEM_NAME#' => $service->getField('NAME')
120 ]
121 )
122 )
123 );
124 }
125 }
126 else
127 {
128 $result->addError(
129 new Main\Error(
130 Main\Localization\Loc::getMessage('SALE_CASHBOX_PAYSYSTEM_PAYMENT_NOT_FOUND')
131 )
132 );
133 }
134
135 return $result;
136 }
137
143 public function printImmediately(Check $check): Sale\Result
144 {
145 $result = new Sale\Result();
146
147 $checkParamsResult = $this->checkParams($check);
148 if (!$checkParamsResult->isSuccess())
149 {
150 $result->addErrors($checkParamsResult->getErrors());
151 return $result;
152 }
153
154 if ($this->needPrintCheck($check))
155 {
156 $payment = CheckManager::getPaymentByCheck($check);
157 if (!$payment)
158 {
159 $result->addError(
160 new Main\Error(
161 Main\Localization\Loc::getMessage('SALE_CASHBOX_PAYSYSTEM_PAYMENT_NOT_FOUND')
162 )
163 );
164 return $result;
165 }
166
167 $url = $this->getPrintUrl();
168 $fields = $this->buildCheckQuery($check);
169
170 $sendResult = $this->send($url, $payment, $fields);
171 if ($sendResult->isSuccess())
172 {
173 $processPrintResult = $this->processPrintResult($sendResult);
174 if ($processPrintResult->isSuccess())
175 {
176 $result->setData($processPrintResult->getData());
177 }
178 else
179 {
180 $result->addErrors($processPrintResult->getErrors());
181 }
182 }
183 else
184 {
185 $result->addErrors($sendResult->getErrors());
186 }
187 }
188
189 return $result;
190 }
191
198 protected function needPrintCheck(Check $check): bool
199 {
200 $isShipmentEntity = (bool)array_filter($check->getEntities(), static function ($entity) {
201 return $entity instanceof Sale\Shipment;
202 });
203
204 return $check::getType() === SellCheck::getType() && $isShipmentEntity;
205 }
206
207 public function buildZReportQuery($id)
208 {
209 return [];
210 }
211
222 public function check(Check $check): Sale\Result
223 {
224 $result = new Sale\Result();
225
226 $checkParamsResult = $this->checkParams($check);
227 if (!$checkParamsResult->isSuccess())
228 {
229 $result->addErrors($checkParamsResult->getErrors());
230 return $result;
231 }
232
233 $payment = CheckManager::getPaymentByCheck($check);
234 if (!$payment)
235 {
236 $result->addError(
237 new Main\Error(
238 Main\Localization\Loc::getMessage('SALE_CASHBOX_PAYSYSTEM_PAYMENT_NOT_FOUND')
239 )
240 );
241 return $result;
242 }
243
244 $url = $this->getCheckUrl();
245 $fields = $this->getDataForCheck($payment);
246 $sendMethod = $this->getCheckHttpMethod();
247
248 $sendResult = $this->send($url, $payment, $fields, $sendMethod);
249 if (!$sendResult->isSuccess())
250 {
251 $result->addErrors($sendResult->getErrors());
252 return $result;
253 }
254
255 $processCheckResult = $this->processCheckResult($sendResult);
256 if ($processCheckResult->isSuccess())
257 {
258 $onAfterProcessCheckResult = $this->onAfterProcessCheck($processCheckResult, $payment);
259 if (!$onAfterProcessCheckResult->isSuccess())
260 {
261 $result->addErrors($onAfterProcessCheckResult->getErrors());
262 }
263 }
264 else
265 {
266 $result->addErrors($processCheckResult->getErrors());
267 }
268
269 return $result;
270 }
271
275 protected function getCheckHttpMethod(): string
276 {
278 }
279
283 public static function getFfdVersion(): ?float
284 {
285 return 1.05;
286 }
287
292 public static function getKkmValue(Sale\PaySystem\Service $service): array
293 {
294 $paySystemCodeForKkm = static::getPaySystemCodeForKkm();
295 $supportedKkmModels = BusinessValue::getValuesByCode(
296 $service->getConsumerName(),
297 $paySystemCodeForKkm
298 );
299
300 return $supportedKkmModels;
301 }
302}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getValuesByCode(string $consumerName, string $code)
buildCheckQuery(Check $check)
send(string $url, Sale\Payment $payment, array $fields, string $method=self::SEND_METHOD_HTTP_POST)
onAfterProcessCheck(Sale\Result $result, Sale\Payment $payment)
getPaySystemSetting(Sale\Payment $payment, string $code)
getDataForCheck(Sale\Payment $payment)
static getKkmValue(Sale\PaySystem\Service $service)
processCheckResult(Sale\Result $result)
processPrintResult(Sale\Result $result)
static getPaymentByCheck(Check $check)