42 private $handler =
null;
45 private $fields = array();
61 [$className, $handlerType] = Manager::includeHandler($fields[
'ACTION_FILE']);
63 $this->fields = $fields;
64 $this->handler =
new $className($handlerType, $this);
77 $onBeforeInitResult = $this->callEventOnBeforeInitiatePay($payment);
78 if (!$onBeforeInitResult->isSuccess())
80 $error = implode(
"\n", $onBeforeInitResult->getErrorMessages());
81 Logger::addError(get_class($this->handler) .
'. OnBeforeInitiatePay: ' . $error);
83 $this->markPayment($payment, $onBeforeInitResult);
85 return $onBeforeInitResult;
88 $this->handler->setInitiateMode($mode);
89 $initResult = $this->handler->initiatePay($payment, $request);
91 $psData = $initResult->getPsData();
94 $setResult = $payment->setFields($psData);
95 if ($setResult->isSuccess())
100 $saveResult = $order->save();
101 if (!$saveResult->isSuccess())
103 $initResult->addErrors($saveResult->getErrors());
109 $initResult->addErrors($setResult->getErrors());
113 if ($initResult->isSuccess())
115 $this->callEventOnInitiatePaySuccess($payment);
119 $error = implode(
"\n", $initResult->getErrorMessages());
120 Logger::addError(get_class($this->handler) .
'. InitiatePay: ' . $error);
122 $this->markPayment($payment, $onBeforeInitResult);
124 $this->callEventOnInitiatePayError($payment, $initResult);
136 self::EVENT_BEFORE_ON_INITIATE_PAY,
138 'payment' => $payment,
145 foreach($event->getResults() as $eventResult)
147 if ($eventResult->getType() === EventResult::ERROR)
149 $parameters = $eventResult->getParameters();
150 $error = $parameters[
'ERROR'] ??
null;
153 $error instanceof
Error
156 Loc::getMessage(
'SALE_PS_SERVICE_ERROR_ON_BEFORE_INITIATE_PAY')
165 private function callEventOnInitiatePaySuccess(Payment $payment)
169 self::EVENT_INITIATE_PAY_SUCCESS,
171 'payment' => $payment
178 private function callEventOnInitiatePayError(Payment $payment, ServiceResult $result)
182 self::EVENT_INITIATE_PAY_ERROR,
184 'payment' => $payment,
185 'errors' => $result->getErrorMessages(),
198 return $this->handler->isRefundableExtended();
201 return $this->handler instanceof
IRefund;
211 public function refund(
Payment $payment, $refundableSum = 0)
213 if ($this->isRefundable())
223 if ($refundableSum == 0)
224 $refundableSum = $payment->
getSum();
227 $result = $this->handler->refund($payment, $refundableSum);
228 if (!$result->isSuccess())
230 Logger::addError(get_class($this->handler).
': refund: '.implode(
"\n", $result->getErrorMessages()));
236 throw new SystemException();
252 $processResult =
new Result();
254 if (!($this->handler instanceof ServiceHandler))
256 return $processResult;
259 $debugInfo = http_build_query($request->toArray(),
"",
"\n");
260 if (empty($debugInfo))
262 $debugInfo = file_get_contents(
"php://input");
264 Logger::addDebugInfo(
265 get_class($this->handler).
" ProcessRequest. paySystemId=".$this->
getField(
"ID").
", request=".($debugInfo ? $debugInfo :
"empty")
268 $paymentId = $this->handler->getPaymentIdFromRequest($request);
270 if (empty($paymentId))
275 get_class($this->handler).
'. ProcessRequest: '.
Loc::getMessage(
'SALE_PS_SERVICE_PAYMENT_ERROR_EMPTY')
278 return $processResult;
281 [$orderId, $paymentId] = Manager::getIdsByPayment($paymentId, $this->
getField(
'ENTITY_REGISTRY_TYPE'));
285 $errorMessage = str_replace(
'#ORDER_ID#', $orderId,
Loc::getMessage(
'SALE_PS_SERVICE_ORDER_ERROR'));
286 $processResult->addError(
new Error($errorMessage));
288 Logger::addError(get_class($this->handler).
'. ProcessRequest: '.$errorMessage);
290 return $processResult;
293 $registry = Registry::getInstance($this->
getField(
'ENTITY_REGISTRY_TYPE'));
295 $orderClassName = $registry->getOrderClassName();
297 $order = $orderClassName::load($orderId);
300 $errorMessage = str_replace(
'#ORDER_ID#', $orderId,
Loc::getMessage(
'SALE_PS_SERVICE_ORDER_ERROR'));
301 $processResult->addError(
new Error($errorMessage));
303 Logger::addError(get_class($this->handler).
'. ProcessRequest: '.$errorMessage);
305 return $processResult;
308 if ($order->isCanceled())
310 $errorMessage = str_replace(
'#ORDER_ID#', $orderId,
Loc::getMessage(
'SALE_PS_SERVICE_ORDER_CANCELED'));
311 $processResult->addError(
new Error($errorMessage));
313 Logger::addError(get_class($this->handler).
'. ProcessRequest: '.$errorMessage);
315 return $processResult;
319 $collection = $order->getPaymentCollection();
322 $payment = $collection->getItemById($paymentId);
326 $errorMessage = str_replace(
'#PAYMENT_ID#', $paymentId,
Loc::getMessage(
'SALE_PS_SERVICE_PAYMENT_ERROR'));
327 $processResult->addError(
new Error($errorMessage));
329 Logger::addError(get_class($this->handler).
'. ProcessRequest: '.$errorMessage);
331 return $processResult;
335 $serviceResult = $this->handler->processRequest($payment, $request);
336 if ($serviceResult->isSuccess())
339 $operationType = $serviceResult->getOperationType();
341 if ($operationType === ServiceResult::MONEY_COMING)
345 else if ($operationType === ServiceResult::MONEY_LEAVING)
350 if ($status !==
null)
352 $event =
new Event(
'sale', self::EVENT_ON_BEFORE_PAYMENT_PAID,
354 'payment' => $payment,
356 'pay_system_id' => $this->
getField(
'ID')
363 $payment->setFieldsNoDemand([
364 'IS_RETURN' => Payment::RETURN_PS,
365 'PAY_RETURN_DATE' =>
new Date(),
369 $paidResult = $payment->setPaid($status);
370 if (!$paidResult->isSuccess())
372 $error =
'PAYMENT SET PAID: '.join(
' ', $paidResult->getErrorMessages());
373 Logger::addError(get_class($this->handler).
'. ProcessRequest: '.$error);
375 $serviceResult->setResultApplied(
false);
379 $psData = $serviceResult->getPsData();
382 $res = $payment->setFields($psData);
383 if (!$res->isSuccess())
385 $error =
'PAYMENT SET PAID: '.join(
' ', $res->getErrorMessages());
386 Logger::addError(get_class($this->handler).
'. ProcessRequest: '.$error);
388 $serviceResult->setResultApplied(
false);
392 $saveResult = $order->save();
394 if (!$saveResult->isSuccess())
396 $error =
'ORDER SAVE: '.join(
' ', $saveResult->getErrorMessages());
397 Logger::addError(get_class($this->handler).
'. ProcessRequest: '.$error);
399 $serviceResult->setResultApplied(
false);
402 PullManager::onSuccessfulPayment($payment);
406 $serviceResult->setResultApplied(
false);
407 $processResult->addErrors($serviceResult->getErrors());
409 $error = implode(
"\n", $serviceResult->getErrorMessages());
410 Logger::addError(get_class($this->handler).
'. ProcessRequest Error: '.$error);
412 $this->markPayment($payment, $serviceResult);
414 PullManager::onFailurePayment($payment);
419 self::EVENT_ON_AFTER_PROCESS_REQUEST,
421 'payment' => $payment,
422 'serviceResult' => $serviceResult,
423 'request' => $request,
428 $this->handler->sendResponse($serviceResult, $request);
430 return $processResult;
438 $id = $this->fields[
'ID'] ?? 0;
440 return static::PAY_SYSTEM_PREFIX.$id;
448 return $this->handler->getDescription();
456 return $this->handler instanceof
IHold || $this->handler instanceof
IPartialHold;
468 return $this->handler->cancel($payment);
486 return $this->handler->confirm($payment, $sum);
493 return $this->handler->confirm($payment);
505 return $this->fields[$name] ??
null;
513 return $this->handler->getCurrencyList();
523 return $this->handler->getClientType(
524 $this->fields[
'PS_MODE'] ??
null
543 return $this->fields[
'IS_CASH'] ===
'Y';
551 return $this->fields[
'CAN_PRINT_CHECK'] ===
'Y';
558 public function canPrintCheckSelf(
Payment $payment): bool
561 if (!$service || !$this->isSupportPrintCheck() || !$this->canPrintCheck())
567 $cashboxClass = $this->getCashboxClass();
568 $kkm = $cashboxClass::getKkmValue($service);
570 return (
bool)Cashbox\Manager::getList([
574 '=HANDLER' => $cashboxClass,
586 return $this->handler->creditNoDemand($payment);
595 return $this->handler->debitNoDemand($payment);
603 if ($this->handler instanceof
IPayable)
606 if (method_exists($this->handler,
'isPayableCompatibility'))
607 return $this->handler->isPayableCompatibility();
617 return $this->handler instanceof
IPdf;
637 return $this->handler->getContent($payment);
652 return $this->handler->getFile($payment);
668 return $this->handler->registerCallbackOnGenerate($payment, $params);
671 throw new NotSupportedException(
'Handler is not implemented interface '.IDocumentGeneratePdf::class);
683 return $this->handler->isGenerated($payment);
696 return $this->handler->getPrice($payment);
706 $this->handler->setExtraParams($params);
716 return $this->handler->showTemplate($payment, $templateName);
735 return $this->handler->initPrePayment($payment, $request);
747 return $this->handler->getProps();
760 return $this->handler->basketButtonAction($orderData);
773 return $this->handler->setOrderConfig($orderData);
786 return $this->handler->payOrder($orderData);
796 return $this->fields;
804 return $this->fields[
'ALLOW_EDIT_PAYMENT'] ==
'Y';
815 if (method_exists($this->handler,
'isCheckableCompatibility'))
816 return $this->handler->isCheckableCompatibility();
825 public function check(
Payment $payment)
829 if ($this->isCheckable())
835 $order = $paymentCollection->getOrder();
837 if (!$order->isCanceled())
840 $checkResult = $this->handler->check($payment);
841 if ($checkResult instanceof
ServiceResult && $checkResult->isSuccess())
843 $psData = $checkResult->getPsData();
846 $res = $payment->setFields($psData);
847 if (!$res->isSuccess())
848 $result->addErrors($res->getErrors());
851 if ($checkResult->getOperationType() == ServiceResult::MONEY_COMING)
853 $res = $payment->setPaid(
'Y');
854 if (!$res->isSuccess())
855 $result->addErrors($res->getErrors());
858 $res = $order->
save();
859 if (!$res->isSuccess())
860 $result->addErrors($res->getErrors());
862 elseif (!$checkResult)
864 $result->addError(
new Error(
Loc::getMessage(
'SALE_PS_SERVICE_ERROR_CONNECT_PS')));
869 $result->addError(
new EntityError(
Loc::getMessage(
'SALE_PS_SERVICE_ORDER_CANCELED', array(
'#ORDER_ID#' => $order->getId()))));
883 if ($this->
isClone() && $cloneEntity->contains($this))
885 return $cloneEntity[$this];
888 $paySystemServiceClone = clone $this;
889 $paySystemServiceClone->isClone =
true;
891 if (!$cloneEntity->contains($this))
893 $cloneEntity[$this] = $paySystemServiceClone;
896 if ($handler = $this->handler)
898 if (!$cloneEntity->contains($handler))
900 $cloneEntity[$handler] = $handler->createClone($cloneEntity);
903 if ($cloneEntity->contains($handler))
905 $paySystemServiceClone->handler = $cloneEntity[$handler];
909 return $paySystemServiceClone;
917 return $this->isClone;
925 return in_array($this->handler->getHandlerType(), array(
'CUSTOM',
'USER'));
934 return $this->handler->getParamsBusValue($payment);
942 return $this->handler->isTuned();
950 return $this->handler->getDemoParams();
958 $this->handler->setInitiateMode($mode);
966 return $this->context;
990 return $this->handler->repeatRecurrent($payment, $request);
1007 return $this->handler->cancelRecurrent($payment, $request);
1033 $cashboxClassName = $this->handler::getCashboxClass();
1034 if (!
Cashbox\Manager::isPaySystemCashbox($cashboxClassName))
1037 'Cashbox is not extended class '.
Cashbox\CashboxPaySystem::class
1041 return $cashboxClassName;
1045 'Handler is not implemented interface '.
Sale\PaySystem\
Cashbox\ISupportPrintCheck::class
1070 return $this->handler->isFiscalizationEnabled($payment);
1074 'Handler does not implement interface '.
Sale\PaySystem\
Cashbox\IFiscalizationAware::class
1082 return $this->handler->getRestrictionList();
1090 return (
int)($this->
getField(
'ID') ?? 0);
1096 ->mark($serviceResult)