Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
payment.php
1<?php
2
3namespace Bitrix\Sale;
4
11
12Loc::loadMessages(__FILE__);
13
19{
20 const RETURN_NONE = 'N';
21 const RETURN_INNER = 'Y';
22 const RETURN_PS = 'P';
23
25 protected $service;
26
29
33 public static function getRegistryEntity()
34 {
36 }
37
44 public function getPayableItemCollection() : PayableItemCollection
45 {
46 if ($this->payableItemCollection === null)
47 {
48 $registry = Registry::getInstance(static::getRegistryType());
49
51 $itemCollectionClassName = $registry->getPayableItemCollectionClassName();
52 $this->payableItemCollection = $itemCollectionClassName::load($this);
53 }
54
56 }
57
58 public function getBasketItemQuantity(BasketItem $basketItem) : float
59 {
60 $quantity = 0;
61
63 foreach ($this->getPayableItemCollection()->getBasketItems() as $payableBasketItem)
64 {
65 if ($payableBasketItem->getEntityObject()->getBasketCode() === $basketItem->getBasketCode())
66 {
67 $quantity += $payableBasketItem->getQuantity();
68 }
69 }
70
71 return $quantity;
72 }
73
78 protected function onBeforeSetFields(array $values)
79 {
80 if (isset($values['PAID']))
81 {
82 if ($this->getField('PAID') === 'Y')
83 {
84 if ($values['PAID'] === 'N')
85 {
86 $values = ['PAID' => $values['PAID']] + $values;
87 }
88 }
89 else
90 {
91 if ($values['PAID'] === 'Y')
92 {
93 // move to the end of array
94 unset($values['PAID']);
95 $values['PAID'] = 'Y';
96 }
97 }
98 }
99
100 return $values;
101 }
102
106 public static function getAvailableFields()
107 {
108 return [
109 'PAID',
110 'DATE_PAID',
111 'EMP_PAID_ID',
112 'PAY_SYSTEM_ID',
113 'PS_STATUS',
114 'PS_STATUS_CODE',
115 'PS_STATUS_DESCRIPTION',
116 'PS_STATUS_MESSAGE',
117 'PS_SUM',
118 'PS_CURRENCY',
119 'PS_RESPONSE_DATE',
120 'PS_RECURRING_TOKEN',
121 'PS_CARD_NUMBER',
122 'PAY_VOUCHER_NUM',
123 'PAY_VOUCHER_DATE',
124 'DATE_PAY_BEFORE',
125 'DATE_BILL',
126 'XML_ID',
127 'SUM',
128 'CURRENCY',
129 'PAY_SYSTEM_NAME',
130 'COMPANY_ID',
131 'PAY_RETURN_NUM',
132 'PRICE_COD',
133 'PAY_RETURN_DATE',
134 'EMP_RETURN_ID',
135 'PAY_RETURN_COMMENT',
136 'RESPONSIBLE_ID',
137 'EMP_RESPONSIBLE_ID',
138 'DATE_RESPONSIBLE_ID',
139 'IS_RETURN',
140 'COMMENTS',
141 'ACCOUNT_NUMBER',
142 'UPDATED_1C',
143 'ID_1C',
144 'VERSION_1C',
145 'EXTERNAL_PAYMENT',
146 'PS_INVOICE_ID',
147 'MARKED',
148 'REASON_MARKED',
149 'DATE_MARKED',
150 'EMP_MARKED_ID',
151 ];
152 }
153
157 protected static function getMeaningfulFields()
158 {
159 return ['PAY_SYSTEM_ID'];
160 }
161
167 protected static function createPaymentObject(array $fields = [])
168 {
169 $registry = Registry::getInstance(static::getRegistryType());
170 $paymentClassName = $registry->getPaymentClassName();
171
172 return new $paymentClassName($fields);
173 }
174
178 public static function getRegistryType()
179 {
181 }
182
192 public static function create(PaymentCollection $collection, Sale\PaySystem\Service $paySystem = null)
193 {
194 $fields = [
195 'DATE_BILL' => new Main\Type\DateTime(),
196 'SUM' => 0,
197 'PAID' => 'N',
198 'XML_ID' => static::generateXmlId(),
199 'IS_RETURN' => static::RETURN_NONE,
200 'CURRENCY' => $collection->getOrder()->getCurrency(),
201 'ORDER_ID' => $collection->getOrder()->getId()
202 ];
203
204 $payment = static::createPaymentObject();
205 $payment->setFieldsNoDemand($fields);
206 $payment->setCollection($collection);
207
208 if ($paySystem !== null)
209 {
210 $payment->setPaySystemService($paySystem);
211 }
212
213 return $payment;
214 }
215
221 public function setPaySystemService(Sale\PaySystem\Service $service)
222 {
223 $this->service = $service;
224 $result = $this->setField("PAY_SYSTEM_ID", $service->getField('ID'));
225 if ($result->isSuccess())
226 {
227 $this->setField("PAY_SYSTEM_NAME", $service->getField('NAME'));
228 }
229 }
230
234 protected static function generateXmlId()
235 {
236 return uniqid('bx_');
237 }
238
247 public static function loadForOrder($id)
248 {
249 if (intval($id) <= 0)
250 {
251 throw new Main\ArgumentNullException("id");
252 }
253
254 $payments = [];
255
256 $paymentDataList = static::getList(['filter' => ['=ORDER_ID' => $id]]);
257 while ($paymentData = $paymentDataList->fetch())
258 {
259 $payments[] = static::createPaymentObject($paymentData);
260 }
261
262 return $payments;
263 }
264
273 public static function deleteNoDemand($orderId)
274 {
275 $result = new Result();
276
277 $dbRes = static::getList([
278 "select" => ["ID"],
279 "filter" => ["=ORDER_ID" => $orderId]
280 ]);
281
282 while ($payment = $dbRes->fetch())
283 {
284 $r = static::deleteInternal($payment['ID']);
285 if (!$r->isSuccess())
286 {
287 $result->addErrors($r->getErrors());
288 }
289 }
290
291 return $result;
292 }
293
299 public function delete()
300 {
301 $result = new Result();
302
303 if ($this->isPaid())
304 {
305 $result->addError(new ResultError(Loc::getMessage('SALE_PAYMENT_DELETE_EXIST_PAID'), 'SALE_PAYMENT_DELETE_EXIST_PAID'));
306 return $result;
307 }
308
309 $r = $this->callEventOnBeforeEntityDeleted();
310 if (!$r->isSuccess())
311 {
312 return $result->addErrors($r->getErrors());
313 }
314
315 $r = parent::delete();
316 if (!$r->isSuccess())
317 {
318 $result->addErrors($r->getErrors());
319 }
320
321 $r = $this->callEventOnEntityDeleted();
322 if (!$r->isSuccess())
323 {
324 $result->addErrors($r->getErrors());
325 }
326
327 return $result;
328 }
329
333 private function callEventOnBeforeEntityDeleted()
334 {
335 $result = new Result();
336
338 $event = new Main\Event('sale', "OnBeforeSalePaymentEntityDeleted", [
339 'ENTITY' => $this,
340 'VALUES' => $this->fields->getOriginalValues(),
341 ]);
342 $event->send();
343
344 if ($event->getResults())
345 {
347 foreach($event->getResults() as $eventResult)
348 {
349 if ($eventResult->getType() == Main\EventResult::ERROR)
350 {
351 $errorMsg = new ResultError(
352 Loc::getMessage('SALE_EVENT_ON_BEFORE_SALEPAYMENT_ENTITY_DELETED_ERROR'),
353 'SALE_EVENT_ON_BEFORE_SALEPAYMENT_ENTITY_DELETED_ERROR'
354 );
355 if ($eventResultData = $eventResult->getParameters())
356 {
357 if (isset($eventResultData) && $eventResultData instanceof ResultError)
358 {
360 $errorMsg = $eventResultData;
361 }
362 }
363
364 $result->addError($errorMsg);
365 }
366 }
367 }
368
369 return $result;
370 }
371
375 private function callEventOnEntityDeleted()
376 {
377 $result = new Result();
378
380 $event = new Main\Event('sale', "OnSalePaymentEntityDeleted", [
381 'ENTITY' => $this,
382 'VALUES' => $this->fields->getOriginalValues(),
383 ]);
384 $event->send();
385
386 if ($event->getResults())
387 {
389 foreach($event->getResults() as $eventResult)
390 {
391 if($eventResult->getType() == Main\EventResult::ERROR)
392 {
393 $errorMsg = new ResultError(
394 Loc::getMessage('SALE_EVENT_ON_SALEPAYMENT_ENTITY_DELETED_ERROR'),
395 'SALE_EVENT_ON_SALEPAYMENT_ENTITY_DELETED_ERROR'
396 );
397 if ($eventResultData = $eventResult->getParameters())
398 {
399 if (isset($eventResultData) && $eventResultData instanceof ResultError)
400 {
402 $errorMsg = $eventResultData;
403 }
404 }
405
406 $result->addError($errorMsg);
407 }
408 }
409 }
410
411 return $result;
412 }
413
425 protected function onFieldModify($name, $oldValue, $value)
426 {
427 global $USER;
428
429 $result = new Result();
430
431 if ($name === "PAID")
432 {
433 if ($value === "Y")
434 {
435 if (!$this->getFields()->isChanged('DATE_PAID'))
436 {
437 $this->setField('DATE_PAID', new Main\Type\DateTime());
438 }
439
440 $this->setField('EMP_PAID_ID', $USER->GetID());
441
442 if ($this->getField('IS_RETURN') === self::RETURN_INNER)
443 {
444 $paySystemId = Sale\PaySystem\Manager::getInnerPaySystemId();
445 }
446 else
447 {
448 $paySystemId = $this->getPaymentSystemId();
449 }
450
451 $service = Sale\PaySystem\Manager::getObjectById($paySystemId);
452 if ($service)
453 {
454 $operationResult = $service->creditNoDemand($this);
455 if (!$operationResult->isSuccess())
456 {
457 return $result->addErrors($operationResult->getErrors());
458 }
459 }
460
461 $this->setField('IS_RETURN', static::RETURN_NONE);
462
463 Internals\EventsPool::addEvent(
464 'p'.$this->getInternalIndex(),
466 [
467 'ENTITY' => $this,
468 'VALUES' => $this->fields->getOriginalValues(),
469 ]
470 );
471 }
472
473 $this->addCashboxChecks();
474 }
475 elseif ($name === "IS_RETURN")
476 {
477 if ($value === static::RETURN_NONE)
478 {
479 return $result;
480 }
481
482 if ($oldValue === static::RETURN_NONE)
483 {
484 $this->setField('EMP_RETURN_ID', $USER->GetID());
485 }
486
488 $collection = $this->getCollection();
489
490 $creditSum = 0;
491 $overPaid = $collection->getPaidSum() - $collection->getOrder()->getPrice();
492
493 if ($overPaid <= 0)
494 {
495 $creditSum = $this->getSum();
496 $overPaid = 0;
497 }
498 elseif ($this->getSum() - $overPaid > 0)
499 {
500 $creditSum = $this->getSum() - $overPaid;
501 }
502
503 if ($value == static::RETURN_PS)
504 {
505 $psId = $this->getPaymentSystemId();
506 }
507 else
508 {
509 $psId = Sale\PaySystem\Manager::getInnerPaySystemId();
510 }
511
512 $service = Sale\PaySystem\Manager::getObjectById($psId);
513
514 if ($service && $service->isRefundable())
515 {
516 if ($creditSum)
517 {
518 if ($value == static::RETURN_PS)
519 {
520 if ($overPaid > 0)
521 {
522 $userBudget = Internals\UserBudgetPool::getUserBudgetByOrder($collection->getOrder());
523 if (PriceMaths::roundPrecision($overPaid) > PriceMaths::roundPrecision($userBudget))
524 {
525 return $result->addError(
526 new Entity\EntityError(
527 Loc::getMessage('SALE_ORDER_PAYMENT_RETURN_PAID'),
528 'SALE_ORDER_PAYMENT_RETURN_PAID'
529 )
530 );
531 }
532 }
533 }
534
535 $refResult = $service->refund($this);
536 if (!$refResult->isSuccess())
537 {
538 return $result->addErrors($refResult->getErrors());
539 }
540
541 $refResultOperation = $refResult->getOperationType();
542 if ($refResultOperation === ServiceResult::MONEY_LEAVING)
543 {
544 $setUnpaidResult = $this->setField('PAID', 'N');
545 if (!$setUnpaidResult->isSuccess())
546 {
547 return $result->addErrors($setUnpaidResult->getErrors());
548 }
549 }
550 }
551 }
552 else
553 {
554 return $result->addError(
555 new Entity\EntityError(
556 Loc::getMessage('SALE_ORDER_PAYMENT_RETURN_NO_SUPPORTED'),
557 'SALE_ORDER_PAYMENT_RETURN_NO_SUPPORTED'
558 )
559 );
560 }
561 }
562 elseif($name === "SUM")
563 {
564 if($this->isPaid())
565 {
566 $result = new Result();
567
568 return $result->addError(
569 new ResultError(
570 Loc::getMessage('SALE_PAYMENT_NOT_ALLOWED_CHANGE_SUM'),
571 'SALE_PAYMENT_NOT_ALLOWED_CHANGE_SUM'
572 )
573 );
574 }
575 }
576 elseif ($name === "MARKED")
577 {
578 if ($oldValue !== "Y")
579 {
580 $this->setField('DATE_MARKED', new Main\Type\DateTime());
581
582 if (is_object($USER))
583 {
584 $this->setField('EMP_MARKED_ID', $USER->GetID());
585 }
586 }
587 elseif ($value === "N")
588 {
589 $r = $this->setField('REASON_MARKED', '');
590 if (!$r->isSuccess())
591 {
592 return $result->addErrors($r->getErrors());
593 }
594 }
595 }
596 elseif ($name === 'RESPONSIBLE_ID')
597 {
598 $this->setField('DATE_RESPONSIBLE_ID', new Main\Type\DateTime());
599 }
600
601 return parent::onFieldModify($name, $oldValue, $value);
602 }
603
604 public function onBeforeBasketItemDelete(BasketItem $basketItem)
605 {
606 $result = new Result();
607
608 $r = $this->getPayableItemCollection()->onBeforeBasketItemDelete($basketItem);
609 if (!$r->isSuccess())
610 {
611 $result->addErrors($r->getErrors());
612 }
613
614 return $result;
615 }
616
626 public function save()
627 {
628 $this->checkCallingContext();
629
630 $result = new Result();
631
632 $id = $this->getId();
633 $isNew = $id <= 0;
634
635 $this->callEventOnBeforeEntitySaved();
636
637 if (!$this->isChanged())
638 {
639 return $result;
640 }
641
642 if ($id > 0)
643 {
644 $r = $this->update();
645 }
646 else
647 {
648 $r = $this->add();
649 if ($r->getId() > 0)
650 {
651 $id = $r->getId();
652 }
653 }
654
655 if (!$r->isSuccess())
656 {
657 $result->addErrors($r->getErrors());
658 return $result;
659 }
660
661 if ($id > 0)
662 {
663 $result->setId($id);
664 }
665
666 if ($this->fields->isChanged('PAID'))
667 {
668 $this->calculateStatistic();
669 }
670
671 $this->callEventOnEntitySaved();
672
673 $this->callDelayedEvents();
674
675 $payableItemCollection = $this->getPayableItemCollection();
676 $r = $payableItemCollection->save();
677 if (!$r->isSuccess())
678 {
679 return $result->addErrors($r->getErrors());
680 }
681
682 $this->onAfterSave($isNew);
683
684 return $result;
685 }
686
687 public function isChanged()
688 {
689 $isChanged = parent::isChanged();
690 if ($isChanged)
691 {
692 return true;
693 }
694
695 return $this->getPayableItemCollection()->isChanged();
696 }
697
701 private function checkCallingContext()
702 {
703 $order = $this->getOrder();
704
705 if (!$order->isSaveRunning())
706 {
707 trigger_error("Incorrect call to the save process. Use method save() on \Bitrix\Sale\Order entity", E_USER_WARNING);
708 }
709 }
710
714 public function getOrder()
715 {
716 return $this->getCollection()->getOrder();
717 }
718
722 protected function addCashboxChecks()
723 {
724 $service = $this->getPaySystem();
725 if ($service && $service->getField("CAN_PRINT_CHECK") === "Y")
726 {
727 Cashbox\Internals\Pool::addDoc($this->getOrder()->getInternalId(), $this);
728 }
729 }
730
734 protected function calculateStatistic()
735 {
737 $order = $this->getOrder();
738
739 BuyerStatistic::calculate($order->getUserId(), $order->getCurrency(), $order->getSiteId());
740 }
741
748 private function add()
749 {
750 $result = new Result();
751
752 $registry = Registry::getInstance(static::getRegistryType());
754 $orderHistory = $registry->getOrderHistoryClassName();
755
756 if ($this->getOrderId() === 0)
757 {
758 $this->setFieldNoDemand('ORDER_ID', $this->getOrder()->getId());
759 }
760
761 $r = $this->addInternal($this->getFields()->getValues());
762 if (!$r->isSuccess())
763 {
764 $orderHistory::addAction(
765 'PAYMENT',
766 $this->getOrderId(),
767 'PAYMENT_ADD_ERROR',
768 null,
769 $this,
770 ["ERROR" => $r->getErrorMessages()]
771 );
772
773 $result->addErrors($r->getErrors());
774 return $result;
775 }
776
777 $id = $r->getId();
778 $this->setFieldNoDemand('ID', $id);
779 $result->setId($id);
780
781 $this->setAccountNumber($id);
782
783 $orderHistory::addAction(
784 'PAYMENT',
785 $this->getOrderId(),
786 'PAYMENT_ADDED',
787 $id,
788 $this
789 );
790
791 return $result;
792 }
793
800 private function update()
801 {
802 $result = new Result();
803
804 $r = static::updateInternal($this->getId(), $this->getFields()->getChangedValues());
805 if (!$r->isSuccess())
806 {
807 $registry = Registry::getInstance(static::getRegistryType());
808
810 $orderHistory = $registry->getOrderHistoryClassName();
811
812 $orderHistory::addAction(
813 'PAYMENT',
814 $this->getOrderId(),
815 'PAYMENT_UPDATE_ERROR',
816 $this->getId(),
817 $this,
818 ["ERROR" => $r->getErrorMessages()]
819 );
820
821 $result->addErrors($r->getErrors());
822 }
823
824 return $result;
825 }
826
830 private function callEventOnBeforeEntitySaved()
831 {
833 $event = new Main\Event('sale', 'OnBeforeSalePaymentEntitySaved', [
834 'ENTITY' => $this,
835 'VALUES' => $this->fields->getOriginalValues()
836 ]);
837
838 $event->send();
839 }
840
844 private function callEventOnEntitySaved()
845 {
847 $event = new Main\Event('sale', 'OnSalePaymentEntitySaved', [
848 'ENTITY' => $this,
849 'VALUES' => $this->fields->getOriginalValues(),
850 ]);
851
852 $event->send();
853 }
854
858 private function callDelayedEvents()
859 {
860 $eventList = Internals\EventsPool::getEvents('p'.$this->getInternalIndex());
861 if ($eventList)
862 {
863 foreach ($eventList as $eventName => $eventData)
864 {
865 $event = new Main\Event('sale', $eventName, $eventData);
866 $event->send();
867
868 $registry = Registry::getInstance(static::getRegistryType());
869
871 $notifyClassName = $registry->getNotifyClassName();
872 $notifyClassName::callNotify($this, $eventName);
873 }
874
875 Internals\EventsPool::resetEvents('p'.$this->getInternalIndex());
876 }
877 }
878
882 protected function onAfterSave($isNew)
883 {
884 return;
885 }
886
890 public function getSum()
891 {
892 return floatval($this->getField('SUM'));
893 }
894
898 public function getSumPaid()
899 {
900 return $this->getField('PS_SUM');
901 }
902
906 public function isPaid()
907 {
908 return $this->getField('PAID') === 'Y';
909 }
910
914 public function isReturn()
915 {
916 return
917 $this->getField('IS_RETURN') === static::RETURN_INNER
918 ||
919 $this->getField('IS_RETURN') === static::RETURN_PS
920 ;
921 }
922
926 public function getOrderId() : int
927 {
928 return (int)$this->getField('ORDER_ID');
929 }
930
934 public function getPaySystem()
935 {
936 if ($this->service === null)
937 {
938 $this->service = $this->loadPaySystem();
939 }
940
941 return $this->service;
942 }
943
947 protected function loadPaySystem()
948 {
949 if ($paySystemId = $this->getPaymentSystemId())
950 {
951 return Sale\PaySystem\Manager::getObjectById($paySystemId);
952 }
953
954 return null;
955 }
956
960 public function getPaymentSystemId()
961 {
962 return (int)$this->getField('PAY_SYSTEM_ID');
963 }
964
968 public function getPaymentSystemName()
969 {
970 return $this->getField('PAY_SYSTEM_NAME');
971 }
972
980 public function setPaid($value)
981 {
982 $result = new Result();
983
985 $r = $this->setField('PAID', $value);
986 if (!$r->isSuccess())
987 {
988 $result->addErrors($r->getErrors());
989 }
990 elseif($r->hasWarnings())
991 {
992 $result->addWarnings($r->getWarnings());
993 }
994
995 return $result;
996 }
997
1005 public function setReturn($value)
1006 {
1007 $result = new Result();
1008
1009 if ($value === static::RETURN_INNER || $value === static::RETURN_PS)
1010 {
1011 if ($this->isReturn())
1012 {
1013 return new Result();
1014 }
1015 }
1016 elseif($value === static::RETURN_NONE)
1017 {
1018 if (!$this->isReturn())
1019 {
1020 return new Result();
1021 }
1022 }
1023 else
1024 {
1025 throw new Main\ArgumentOutOfRangeException('value');
1026 }
1027
1029 $r = $this->setField('IS_RETURN', $value);
1030 if (!$r->isSuccess())
1031 {
1032 $result->addErrors($r->getErrors());
1033 }
1034
1035 return $result;
1036 }
1037
1041 public function isInner()
1042 {
1043 return $this->getPaymentSystemId() === Sale\PaySystem\Manager::getInnerPaySystemId();
1044 }
1045
1054 protected function normalizeValue($name, $value)
1055 {
1056 if ($this->isPriceField($name))
1057 {
1058 $value = PriceMaths::roundPrecision($value);
1059 }
1060 elseif ($name === 'REASON_MARKED')
1061 {
1062 $value = (string)$value;
1063 if (mb_strlen($value) > 255)
1064 {
1065 $value = mb_substr($value, 0, 255);
1066 }
1067 }
1068
1069 return parent::normalizeValue($name, $value);
1070 }
1071
1078 protected function checkValueBeforeSet($name, $value)
1079 {
1080 $result = parent::checkValueBeforeSet($name, $value);
1081
1082 if ($name == "PAY_SYSTEM_ID")
1083 {
1084 if (intval($value) > 0 && !Sale\PaySystem\Manager::isExist($value))
1085 {
1086 $result->addError(
1087 new ResultError(
1088 Loc::getMessage('SALE_PAYMENT_WRONG_PAYMENT_SERVICE'),
1089 'SALE_PAYMENT_WRONG_PAYMENT_SERVICE'
1090 )
1091 );
1092 }
1093 }
1094 elseif ($name === 'ACCOUNT_NUMBER')
1095 {
1096 $dbRes = static::getList([
1097 'select' => ['ID'],
1098 'filter' => ['=ACCOUNT_NUMBER' => $value]
1099 ]);
1100
1101 if ($dbRes->fetch())
1102 {
1103 $result->addError(
1104 new ResultError(
1105 Loc::getMessage('SALE_PAYMENT_ACCOUNT_NUMBER_EXISTS')
1106 )
1107 );
1108 }
1109 }
1110
1111 return $result;
1112 }
1113
1119 protected function addChangesToHistory($name, $oldValue = null, $value = null)
1120 {
1121 if ($this->getId() > 0)
1122 {
1123 $order = $this->getOrder();
1124
1125 if ($order && $order->getId() > 0)
1126 {
1128 'PAYMENT',
1129 $order->getId(),
1130 $name,
1131 $oldValue,
1132 $value,
1133 $this->getId(),
1134 $this
1135 );
1136 }
1137 }
1138 }
1139
1143 public function verify()
1144 {
1145 $result = new Result();
1146 if ($this->getPaymentSystemId() <= 0)
1147 {
1148 $result->addError(new ResultError(Loc::getMessage("SALE_PAYMENT_PAYMENT_SERVICE_EMPTY"), 'SALE_PAYMENT_PAYMENT_SERVICE_EMPTY'));
1149 }
1150 return $result;
1151 }
1152
1160 public function setAccountNumber($id)
1161 {
1162 $result = new Sale\Result();
1163
1164 $value = Internals\AccountNumberGenerator::generateForPayment($this);
1165
1166 try
1167 {
1168 $r = static::updateInternal($id, ["ACCOUNT_NUMBER" => $value]);
1169 $res = $r->isSuccess(true);
1170 }
1171 catch (\Exception $exception)
1172 {
1173 $res = false;
1174 }
1175
1176 if ($res)
1177 {
1178 $this->setFieldNoDemand('ACCOUNT_NUMBER', $value);
1179 }
1180
1181 return $result;
1182 }
1183
1188 public function getBusinessValueProviderInstance($mapping)
1189 {
1190 $providerInstance = null;
1191
1192 if (is_array($mapping) && isset($mapping['PROVIDER_KEY']))
1193 {
1194 switch ($mapping['PROVIDER_KEY'])
1195 {
1196 case 'PAYMENT':
1197 $providerInstance = $this;
1198 break;
1199 case 'COMPANY':
1200 $providerInstance = $this->getField('COMPANY_ID');
1201 break;
1202 default:
1203 $order = $this->getOrder();
1204 if ($order)
1205 {
1206 $providerInstance = $order->getBusinessValueProviderInstance($mapping);
1207 }
1208 }
1209 }
1210
1211 return $providerInstance;
1212 }
1213
1217 public function getPersonTypeId()
1218 {
1219 $order = $this->getOrder();
1220 if ($order)
1221 {
1222 return $order->getPersonTypeId();
1223 }
1224
1225 return null;
1226 }
1227
1235 public static function getList(array $parameters = [])
1236 {
1237 return Internals\PaymentTable::getList($parameters);
1238 }
1239
1246 public function createClone(\SplObjectStorage $cloneEntity)
1247 {
1248 if ($this->isClone() && $cloneEntity->contains($this))
1249 {
1250 return $cloneEntity[$this];
1251 }
1252
1254 $paymentClone = parent::createClone($cloneEntity);
1255
1257 if ($paySystem = $this->getPaySystem())
1258 {
1259 if (!$cloneEntity->contains($paySystem))
1260 {
1261 $cloneEntity[$paySystem] = $paySystem->createClone($cloneEntity);
1262 }
1263
1264 if ($cloneEntity->contains($paySystem))
1265 {
1266 $paymentClone->service = $cloneEntity[$paySystem];
1267 }
1268 }
1269
1270 return $paymentClone;
1271 }
1272
1277 public function getHash()
1278 {
1279 $order = $this->getOrder();
1280
1281 return md5(
1282 $this->getId().
1284 $order->getId()
1285 );
1286 }
1287
1297 public function isAllowPay()
1298 {
1300 $order = $this->getOrder();
1301
1302 return $order->isAllowPay();
1303 }
1304
1308 public function isMarked()
1309 {
1310 return $this->getField('MARKED') == "Y";
1311 }
1312
1318 public function getErrorEntity($value)
1319 {
1320 static $className = null;
1321 $errorsList = static::getAutoFixErrorsList();
1322 if (is_array($errorsList) && in_array($value, $errorsList))
1323 {
1324 if ($className === null)
1325 $className = static::getClassName();
1326 }
1327
1328 return $className;
1329 }
1330
1336 public function canAutoFixError($value)
1337 {
1338 $autoFix = false;
1339 $errorsList = static::getAutoFixErrorsList();
1340 if (is_array($errorsList) && in_array($value, $errorsList))
1341 {
1342 $autoFix = true;
1343 }
1344 return $autoFix;
1345 }
1346
1350 public function getAutoFixErrorsList()
1351 {
1352 return [];
1353 }
1354
1360 public function tryFixError($code)
1361 {
1362 return new Result();
1363 }
1364
1368 public function canMarked()
1369 {
1370 return true;
1371 }
1372
1376 public function getMarkField()
1377 {
1378 return 'MARKED';
1379 }
1380
1381 protected function isPriceField(string $name) : bool
1382 {
1383 return
1384 $name === 'PRICE_COD'
1385 || $name === 'SUM'
1386 ;
1387 }
1388
1395 protected function addInternal(array $data)
1396 {
1397 return Internals\PaymentTable::add($data);
1398 }
1399
1406 protected function updateInternal($primary, array $data)
1407 {
1408 return Internals\PaymentTable::update($primary, $data);
1409 }
1410
1416 protected static function deleteInternal($primary)
1417 {
1418 return Internals\PaymentTable::delete($primary);
1419 }
1420
1424 protected static function getFieldsMap()
1425 {
1426 return Internals\PaymentTable::getMap();
1427 }
1428
1432 public static function getUfId()
1433 {
1434 return Internals\PaymentTable::getUfId();
1435 }
1436
1442 public static function getEntityEventName()
1443 {
1444 return 'SalePayment';
1445 }
1446
1447}
static update($id, array $data)
Definition entity.php:229
static add(array $data)
Definition entity.php:150
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static calculate($userId, $currency, $lid)
static addField($entityName, $orderId, $field, $oldValue=null, $value=null, $id=null, $entity=null, array $fields=array())
static deleteNoDemand($orderId)
Definition payment.php:273
static getRegistryEntity()
Definition payment.php:33
updateInternal($primary, array $data)
Definition payment.php:1406
static createPaymentObject(array $fields=[])
Definition payment.php:167
checkValueBeforeSet($name, $value)
Definition payment.php:1078
getBusinessValueProviderInstance($mapping)
Definition payment.php:1188
static getMeaningfulFields()
Definition payment.php:157
static create(PaymentCollection $collection, Sale\PaySystem\Service $paySystem=null)
Definition payment.php:192
static getFieldsMap()
Definition payment.php:1424
onBeforeBasketItemDelete(BasketItem $basketItem)
Definition payment.php:604
static loadForOrder($id)
Definition payment.php:247
static getEntityEventName()
Definition payment.php:1442
static getAvailableFields()
Definition payment.php:106
onBeforeSetFields(array $values)
Definition payment.php:78
isPriceField(string $name)
Definition payment.php:1381
addInternal(array $data)
Definition payment.php:1395
canAutoFixError($value)
Definition payment.php:1336
addChangesToHistory($name, $oldValue=null, $value=null)
Definition payment.php:1119
static generateXmlId()
Definition payment.php:234
static deleteInternal($primary)
Definition payment.php:1416
normalizeValue($name, $value)
Definition payment.php:1054
static getRegistryType()
Definition payment.php:178
setPaySystemService(Sale\PaySystem\Service $service)
Definition payment.php:221
static getList(array $parameters=[])
Definition payment.php:1235
static roundPrecision($value)
static getInstance($type)
Definition registry.php:183