Bitrix-D7 22.6
 
Загрузка...
Поиск...
Не найдено
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
62 protected function onBeforeSetFields(array $values)
63 {
64 if (isset($values['PAID']))
65 {
66 if ($this->getField('PAID') === 'Y')
67 {
68 if ($values['PAID'] === 'N')
69 {
70 $values = ['PAID' => $values['PAID']] + $values;
71 }
72 }
73 else
74 {
75 if ($values['PAID'] === 'Y')
76 {
77 // move to the end of array
78 unset($values['PAID']);
79 $values['PAID'] = 'Y';
80 }
81 }
82 }
83
84 return $values;
85 }
86
90 public static function getAvailableFields()
91 {
92 return [
93 'PAID',
94 'DATE_PAID',
95 'EMP_PAID_ID',
96 'PAY_SYSTEM_ID',
97 'PS_STATUS',
98 'PS_STATUS_CODE',
99 'PS_STATUS_DESCRIPTION',
100 'PS_STATUS_MESSAGE',
101 'PS_SUM',
102 'PS_CURRENCY',
103 'PS_RESPONSE_DATE',
104 'PS_RECURRING_TOKEN',
105 'PS_CARD_NUMBER',
106 'PAY_VOUCHER_NUM',
107 'PAY_VOUCHER_DATE',
108 'DATE_PAY_BEFORE',
109 'DATE_BILL',
110 'XML_ID',
111 'SUM',
112 'CURRENCY',
113 'PAY_SYSTEM_NAME',
114 'COMPANY_ID',
115 'PAY_RETURN_NUM',
116 'PRICE_COD',
117 'PAY_RETURN_DATE',
118 'EMP_RETURN_ID',
119 'PAY_RETURN_COMMENT',
120 'RESPONSIBLE_ID',
121 'EMP_RESPONSIBLE_ID',
122 'DATE_RESPONSIBLE_ID',
123 'IS_RETURN',
124 'COMMENTS',
125 'ACCOUNT_NUMBER',
126 'UPDATED_1C',
127 'ID_1C',
128 'VERSION_1C',
129 'EXTERNAL_PAYMENT',
130 'PS_INVOICE_ID',
131 'MARKED',
132 'REASON_MARKED',
133 'DATE_MARKED',
134 'EMP_MARKED_ID',
135 ];
136 }
137
141 protected static function getMeaningfulFields()
142 {
143 return ['PAY_SYSTEM_ID'];
144 }
145
151 protected static function createPaymentObject(array $fields = [])
152 {
153 $registry = Registry::getInstance(static::getRegistryType());
154 $paymentClassName = $registry->getPaymentClassName();
155
156 return new $paymentClassName($fields);
157 }
158
162 public static function getRegistryType()
163 {
165 }
166
176 public static function create(PaymentCollection $collection, Sale\PaySystem\Service $paySystem = null)
177 {
178 $fields = [
179 'DATE_BILL' => new Main\Type\DateTime(),
180 'SUM' => 0,
181 'PAID' => 'N',
182 'XML_ID' => static::generateXmlId(),
183 'IS_RETURN' => static::RETURN_NONE,
184 'CURRENCY' => $collection->getOrder()->getCurrency(),
185 'ORDER_ID' => $collection->getOrder()->getId()
186 ];
187
188 $payment = static::createPaymentObject();
189 $payment->setFieldsNoDemand($fields);
190 $payment->setCollection($collection);
191
192 if ($paySystem !== null)
193 {
194 $payment->setPaySystemService($paySystem);
195 }
196
197 return $payment;
198 }
199
205 public function setPaySystemService(Sale\PaySystem\Service $service)
206 {
207 $this->service = $service;
208 $result = $this->setField("PAY_SYSTEM_ID", $service->getField('ID'));
209 if ($result->isSuccess())
210 {
211 $this->setField("PAY_SYSTEM_NAME", $service->getField('NAME'));
212 }
213 }
214
218 protected static function generateXmlId()
219 {
220 return uniqid('bx_');
221 }
222
231 public static function loadForOrder($id)
232 {
233 if (intval($id) <= 0)
234 {
235 throw new Main\ArgumentNullException("id");
236 }
237
238 $payments = [];
239
240 $paymentDataList = static::getList(['filter' => ['=ORDER_ID' => $id]]);
241 while ($paymentData = $paymentDataList->fetch())
242 {
243 $payments[] = static::createPaymentObject($paymentData);
244 }
245
246 return $payments;
247 }
248
257 public static function deleteNoDemand($orderId)
258 {
259 $result = new Result();
260
261 $dbRes = static::getList([
262 "select" => ["ID"],
263 "filter" => ["=ORDER_ID" => $orderId]
264 ]);
265
266 while ($payment = $dbRes->fetch())
267 {
268 $r = static::deleteInternal($payment['ID']);
269 if (!$r->isSuccess())
270 {
271 $result->addErrors($r->getErrors());
272 }
273 }
274
275 return $result;
276 }
277
283 public function delete()
284 {
285 $result = new Result();
286
287 if ($this->isPaid())
288 {
289 $result->addError(new ResultError(Loc::getMessage('SALE_PAYMENT_DELETE_EXIST_PAID'), 'SALE_PAYMENT_DELETE_EXIST_PAID'));
290 return $result;
291 }
292
293 $r = $this->callEventOnBeforeEntityDeleted();
294 if (!$r->isSuccess())
295 {
296 return $result->addErrors($r->getErrors());
297 }
298
299 $r = parent::delete();
300 if (!$r->isSuccess())
301 {
302 $result->addErrors($r->getErrors());
303 }
304
305 $r = $this->callEventOnEntityDeleted();
306 if (!$r->isSuccess())
307 {
308 $result->addErrors($r->getErrors());
309 }
310
311 return $result;
312 }
313
317 private function callEventOnBeforeEntityDeleted()
318 {
319 $result = new Result();
320
322 $event = new Main\Event('sale', "OnBeforeSalePaymentEntityDeleted", [
323 'ENTITY' => $this,
324 'VALUES' => $this->fields->getOriginalValues(),
325 ]);
326 $event->send();
327
328 if ($event->getResults())
329 {
331 foreach($event->getResults() as $eventResult)
332 {
333 if ($eventResult->getType() == Main\EventResult::ERROR)
334 {
335 $errorMsg = new ResultError(
336 Loc::getMessage('SALE_EVENT_ON_BEFORE_SALEPAYMENT_ENTITY_DELETED_ERROR'),
337 'SALE_EVENT_ON_BEFORE_SALEPAYMENT_ENTITY_DELETED_ERROR'
338 );
339 if ($eventResultData = $eventResult->getParameters())
340 {
341 if (isset($eventResultData) && $eventResultData instanceof ResultError)
342 {
344 $errorMsg = $eventResultData;
345 }
346 }
347
348 $result->addError($errorMsg);
349 }
350 }
351 }
352
353 return $result;
354 }
355
359 private function callEventOnEntityDeleted()
360 {
361 $result = new Result();
362
364 $event = new Main\Event('sale', "OnSalePaymentEntityDeleted", [
365 'ENTITY' => $this,
366 'VALUES' => $this->fields->getOriginalValues(),
367 ]);
368 $event->send();
369
370 if ($event->getResults())
371 {
373 foreach($event->getResults() as $eventResult)
374 {
375 if($eventResult->getType() == Main\EventResult::ERROR)
376 {
377 $errorMsg = new ResultError(
378 Loc::getMessage('SALE_EVENT_ON_SALEPAYMENT_ENTITY_DELETED_ERROR'),
379 'SALE_EVENT_ON_SALEPAYMENT_ENTITY_DELETED_ERROR'
380 );
381 if ($eventResultData = $eventResult->getParameters())
382 {
383 if (isset($eventResultData) && $eventResultData instanceof ResultError)
384 {
386 $errorMsg = $eventResultData;
387 }
388 }
389
390 $result->addError($errorMsg);
391 }
392 }
393 }
394
395 return $result;
396 }
397
409 protected function onFieldModify($name, $oldValue, $value)
410 {
411 global $USER;
412
413 $result = new Result();
414
415 if ($name === "PAID")
416 {
417 if ($value === "Y")
418 {
419 if (!$this->getFields()->isChanged('DATE_PAID'))
420 {
421 $this->setField('DATE_PAID', new Main\Type\DateTime());
422 }
423
424 $this->setField('EMP_PAID_ID', $USER->GetID());
425
426 if ($this->getField('IS_RETURN') === self::RETURN_INNER)
427 {
428 $paySystemId = Sale\PaySystem\Manager::getInnerPaySystemId();
429 }
430 else
431 {
432 $paySystemId = $this->getPaymentSystemId();
433 }
434
435 $service = Sale\PaySystem\Manager::getObjectById($paySystemId);
436 if ($service)
437 {
438 $operationResult = $service->creditNoDemand($this);
439 if (!$operationResult->isSuccess())
440 {
441 return $result->addErrors($operationResult->getErrors());
442 }
443 }
444
445 $this->setField('IS_RETURN', static::RETURN_NONE);
446
447 Internals\EventsPool::addEvent(
448 'p'.$this->getInternalIndex(),
450 [
451 'ENTITY' => $this,
452 'VALUES' => $this->fields->getOriginalValues(),
453 ]
454 );
455 }
456
457 $this->addCashboxChecks();
458 }
459 elseif ($name === "IS_RETURN")
460 {
461 if ($value === static::RETURN_NONE)
462 {
463 return $result;
464 }
465
466 if ($oldValue === static::RETURN_NONE)
467 {
468 $this->setField('EMP_RETURN_ID', $USER->GetID());
469 }
470
472 $collection = $this->getCollection();
473
474 $creditSum = 0;
475 $overPaid = $collection->getPaidSum() - $collection->getOrder()->getPrice();
476
477 if ($overPaid <= 0)
478 {
479 $creditSum = $this->getSum();
480 $overPaid = 0;
481 }
482 elseif ($this->getSum() - $overPaid > 0)
483 {
484 $creditSum = $this->getSum() - $overPaid;
485 }
486
487 if ($value == static::RETURN_PS)
488 {
489 $psId = $this->getPaymentSystemId();
490 }
491 else
492 {
493 $psId = Sale\PaySystem\Manager::getInnerPaySystemId();
494 }
495
496 $service = Sale\PaySystem\Manager::getObjectById($psId);
497
498 if ($service && $service->isRefundable())
499 {
500 if ($creditSum)
501 {
502 if ($value == static::RETURN_PS)
503 {
504 if ($overPaid > 0)
505 {
506 $userBudget = Internals\UserBudgetPool::getUserBudgetByOrder($collection->getOrder());
507 if (PriceMaths::roundPrecision($overPaid) > PriceMaths::roundPrecision($userBudget))
508 {
509 return $result->addError(
510 new Entity\EntityError(
511 Loc::getMessage('SALE_ORDER_PAYMENT_RETURN_PAID'),
512 'SALE_ORDER_PAYMENT_RETURN_PAID'
513 )
514 );
515 }
516 }
517 }
518
519 $refResult = $service->refund($this);
520 if (!$refResult->isSuccess())
521 {
522 return $result->addErrors($refResult->getErrors());
523 }
524
525 $refResultOperation = $refResult->getOperationType();
526 if ($refResultOperation === ServiceResult::MONEY_LEAVING)
527 {
528 $setUnpaidResult = $this->setField('PAID', 'N');
529 if (!$setUnpaidResult->isSuccess())
530 {
531 return $result->addErrors($setUnpaidResult->getErrors());
532 }
533 }
534 }
535 }
536 else
537 {
538 return $result->addError(
539 new Entity\EntityError(
540 Loc::getMessage('SALE_ORDER_PAYMENT_RETURN_NO_SUPPORTED'),
541 'SALE_ORDER_PAYMENT_RETURN_NO_SUPPORTED'
542 )
543 );
544 }
545 }
546 elseif($name === "SUM")
547 {
548 if($this->isPaid())
549 {
550 $result = new Result();
551
552 return $result->addError(
553 new ResultError(
554 Loc::getMessage('SALE_PAYMENT_NOT_ALLOWED_CHANGE_SUM'),
555 'SALE_PAYMENT_NOT_ALLOWED_CHANGE_SUM'
556 )
557 );
558 }
559 }
560 elseif ($name === "MARKED")
561 {
562 if ($oldValue !== "Y")
563 {
564 $this->setField('DATE_MARKED', new Main\Type\DateTime());
565
566 if (is_object($USER))
567 {
568 $this->setField('EMP_MARKED_ID', $USER->GetID());
569 }
570 }
571 elseif ($value === "N")
572 {
573 $r = $this->setField('REASON_MARKED', '');
574 if (!$r->isSuccess())
575 {
576 return $result->addErrors($r->getErrors());
577 }
578 }
579 }
580 elseif ($name === 'RESPONSIBLE_ID')
581 {
582 $this->setField('DATE_RESPONSIBLE_ID', new Main\Type\DateTime());
583 }
584
585 return parent::onFieldModify($name, $oldValue, $value);
586 }
587
588 public function onBeforeBasketItemDelete(BasketItem $basketItem)
589 {
590 $result = new Result();
591
592 $r = $this->getPayableItemCollection()->onBeforeBasketItemDelete($basketItem);
593 if (!$r->isSuccess())
594 {
595 $result->addErrors($r->getErrors());
596 }
597
598 return $result;
599 }
600
610 public function save()
611 {
612 $this->checkCallingContext();
613
614 $result = new Result();
615
616 $id = $this->getId();
617 $isNew = $id <= 0;
618
619 $this->callEventOnBeforeEntitySaved();
620
621 if (!$this->isChanged())
622 {
623 return $result;
624 }
625
626 if ($id > 0)
627 {
628 $r = $this->update();
629 }
630 else
631 {
632 $r = $this->add();
633 if ($r->getId() > 0)
634 {
635 $id = $r->getId();
636 }
637 }
638
639 if (!$r->isSuccess())
640 {
641 $result->addErrors($r->getErrors());
642 return $result;
643 }
644
645 if ($id > 0)
646 {
647 $result->setId($id);
648 }
649
650 if ($this->fields->isChanged('PAID'))
651 {
652 $this->calculateStatistic();
653 }
654
655 $this->callEventOnEntitySaved();
656
657 $this->callDelayedEvents();
658
659 $payableItemCollection = $this->getPayableItemCollection();
660 $r = $payableItemCollection->save();
661 if (!$r->isSuccess())
662 {
663 return $result->addErrors($r->getErrors());
664 }
665
666 $this->onAfterSave($isNew);
667
668 return $result;
669 }
670
671 public function isChanged()
672 {
673 $isChanged = parent::isChanged();
674 if ($isChanged)
675 {
676 return true;
677 }
678
679 return $this->getPayableItemCollection()->isChanged();
680 }
681
685 private function checkCallingContext()
686 {
687 $order = $this->getOrder();
688
689 if (!$order->isSaveRunning())
690 {
691 trigger_error("Incorrect call to the save process. Use method save() on \Bitrix\Sale\Order entity", E_USER_WARNING);
692 }
693 }
694
698 public function getOrder()
699 {
700 return $this->getCollection()->getOrder();
701 }
702
706 protected function addCashboxChecks()
707 {
708 $service = $this->getPaySystem();
709 if ($service && $service->getField("CAN_PRINT_CHECK") === "Y")
710 {
711 Cashbox\Internals\Pool::addDoc($this->getOrder()->getInternalId(), $this);
712 }
713 }
714
718 protected function calculateStatistic()
719 {
721 $order = $this->getOrder();
722
723 BuyerStatistic::calculate($order->getUserId(), $order->getCurrency(), $order->getSiteId());
724 }
725
732 private function add()
733 {
734 $result = new Result();
735
736 $registry = Registry::getInstance(static::getRegistryType());
738 $orderHistory = $registry->getOrderHistoryClassName();
739
740 if ($this->getOrderId() === 0)
741 {
742 $this->setFieldNoDemand('ORDER_ID', $this->getOrder()->getId());
743 }
744
745 $r = $this->addInternal($this->getFields()->getValues());
746 if (!$r->isSuccess())
747 {
748 $orderHistory::addAction(
749 'PAYMENT',
750 $this->getOrderId(),
751 'PAYMENT_ADD_ERROR',
752 null,
753 $this,
754 ["ERROR" => $r->getErrorMessages()]
755 );
756
757 $result->addErrors($r->getErrors());
758 return $result;
759 }
760
761 $id = $r->getId();
762 $this->setFieldNoDemand('ID', $id);
763 $result->setId($id);
764
765 $this->setAccountNumber($id);
766
767 $orderHistory::addAction(
768 'PAYMENT',
769 $this->getOrderId(),
770 'PAYMENT_ADDED',
771 $id,
772 $this
773 );
774
775 return $result;
776 }
777
784 private function update()
785 {
786 $result = new Result();
787
788 $r = static::updateInternal($this->getId(), $this->getFields()->getChangedValues());
789 if (!$r->isSuccess())
790 {
791 $registry = Registry::getInstance(static::getRegistryType());
792
794 $orderHistory = $registry->getOrderHistoryClassName();
795
796 $orderHistory::addAction(
797 'PAYMENT',
798 $this->getOrderId(),
799 'PAYMENT_UPDATE_ERROR',
800 $this->getId(),
801 $this,
802 ["ERROR" => $r->getErrorMessages()]
803 );
804
805 $result->addErrors($r->getErrors());
806 }
807
808 return $result;
809 }
810
814 private function callEventOnBeforeEntitySaved()
815 {
817 $event = new Main\Event('sale', 'OnBeforeSalePaymentEntitySaved', [
818 'ENTITY' => $this,
819 'VALUES' => $this->fields->getOriginalValues()
820 ]);
821
822 $event->send();
823 }
824
828 private function callEventOnEntitySaved()
829 {
831 $event = new Main\Event('sale', 'OnSalePaymentEntitySaved', [
832 'ENTITY' => $this,
833 'VALUES' => $this->fields->getOriginalValues(),
834 ]);
835
836 $event->send();
837 }
838
842 private function callDelayedEvents()
843 {
844 $eventList = Internals\EventsPool::getEvents('p'.$this->getInternalIndex());
845 if ($eventList)
846 {
847 foreach ($eventList as $eventName => $eventData)
848 {
849 $event = new Main\Event('sale', $eventName, $eventData);
850 $event->send();
851
852 $registry = Registry::getInstance(static::getRegistryType());
853
855 $notifyClassName = $registry->getNotifyClassName();
856 $notifyClassName::callNotify($this, $eventName);
857 }
858
859 Internals\EventsPool::resetEvents('p'.$this->getInternalIndex());
860 }
861 }
862
866 protected function onAfterSave($isNew)
867 {
868 return;
869 }
870
874 public function getSum()
875 {
876 return floatval($this->getField('SUM'));
877 }
878
882 public function getSumPaid()
883 {
884 return $this->getField('PS_SUM');
885 }
886
890 public function isPaid()
891 {
892 return $this->getField('PAID') === 'Y';
893 }
894
898 public function isReturn()
899 {
900 return
901 $this->getField('IS_RETURN') === static::RETURN_INNER
902 ||
903 $this->getField('IS_RETURN') === static::RETURN_PS
904 ;
905 }
906
910 public function getOrderId() : int
911 {
912 return (int)$this->getField('ORDER_ID');
913 }
914
918 public function getPaySystem()
919 {
920 if ($this->service === null)
921 {
922 $this->service = $this->loadPaySystem();
923 }
924
925 return $this->service;
926 }
927
931 protected function loadPaySystem()
932 {
933 if ($paySystemId = $this->getPaymentSystemId())
934 {
935 return Sale\PaySystem\Manager::getObjectById($paySystemId);
936 }
937
938 return null;
939 }
940
944 public function getPaymentSystemId()
945 {
946 return (int)$this->getField('PAY_SYSTEM_ID');
947 }
948
952 public function getPaymentSystemName()
953 {
954 return $this->getField('PAY_SYSTEM_NAME');
955 }
956
964 public function setPaid($value)
965 {
966 $result = new Result();
967
969 $r = $this->setField('PAID', $value);
970 if (!$r->isSuccess())
971 {
972 $result->addErrors($r->getErrors());
973 }
974 elseif($r->hasWarnings())
975 {
976 $result->addWarnings($r->getWarnings());
977 }
978
979 return $result;
980 }
981
989 public function setReturn($value)
990 {
991 $result = new Result();
992
993 if ($value === static::RETURN_INNER || $value === static::RETURN_PS)
994 {
995 if ($this->isReturn())
996 {
997 return new Result();
998 }
999 }
1000 elseif($value === static::RETURN_NONE)
1001 {
1002 if (!$this->isReturn())
1003 {
1004 return new Result();
1005 }
1006 }
1007 else
1008 {
1009 throw new Main\ArgumentOutOfRangeException('value');
1010 }
1011
1013 $r = $this->setField('IS_RETURN', $value);
1014 if (!$r->isSuccess())
1015 {
1016 $result->addErrors($r->getErrors());
1017 }
1018
1019 return $result;
1020 }
1021
1025 public function isInner()
1026 {
1027 return $this->getPaymentSystemId() === Sale\PaySystem\Manager::getInnerPaySystemId();
1028 }
1029
1038 protected function normalizeValue($name, $value)
1039 {
1040 if ($this->isPriceField($name))
1041 {
1042 $value = PriceMaths::roundPrecision($value);
1043 }
1044 elseif ($name === 'REASON_MARKED')
1045 {
1046 $value = (string)$value;
1047 if (mb_strlen($value) > 255)
1048 {
1049 $value = mb_substr($value, 0, 255);
1050 }
1051 }
1052
1053 return parent::normalizeValue($name, $value);
1054 }
1055
1062 protected function checkValueBeforeSet($name, $value)
1063 {
1064 $result = parent::checkValueBeforeSet($name, $value);
1065
1066 if ($name == "PAY_SYSTEM_ID")
1067 {
1068 if (intval($value) > 0 && !Sale\PaySystem\Manager::isExist($value))
1069 {
1070 $result->addError(
1071 new ResultError(
1072 Loc::getMessage('SALE_PAYMENT_WRONG_PAYMENT_SERVICE'),
1073 'SALE_PAYMENT_WRONG_PAYMENT_SERVICE'
1074 )
1075 );
1076 }
1077 }
1078 elseif ($name === 'ACCOUNT_NUMBER')
1079 {
1080 $dbRes = static::getList([
1081 'select' => ['ID'],
1082 'filter' => ['=ACCOUNT_NUMBER' => $value]
1083 ]);
1084
1085 if ($dbRes->fetch())
1086 {
1087 $result->addError(
1088 new ResultError(
1089 Loc::getMessage('SALE_PAYMENT_ACCOUNT_NUMBER_EXISTS')
1090 )
1091 );
1092 }
1093 }
1094
1095 return $result;
1096 }
1097
1103 protected function addChangesToHistory($name, $oldValue = null, $value = null)
1104 {
1105 if ($this->getId() > 0)
1106 {
1107 $order = $this->getOrder();
1108
1109 if ($order && $order->getId() > 0)
1110 {
1112 'PAYMENT',
1113 $order->getId(),
1114 $name,
1115 $oldValue,
1116 $value,
1117 $this->getId(),
1118 $this
1119 );
1120 }
1121 }
1122 }
1123
1127 public function verify()
1128 {
1129 $result = new Result();
1130 if ($this->getPaymentSystemId() <= 0)
1131 {
1132 $result->addError(new ResultError(Loc::getMessage("SALE_PAYMENT_PAYMENT_SERVICE_EMPTY"), 'SALE_PAYMENT_PAYMENT_SERVICE_EMPTY'));
1133 }
1134 return $result;
1135 }
1136
1144 public function setAccountNumber($id)
1145 {
1146 $result = new Sale\Result();
1147
1148 $value = Internals\AccountNumberGenerator::generateForPayment($this);
1149
1150 try
1151 {
1152 $r = static::updateInternal($id, ["ACCOUNT_NUMBER" => $value]);
1153 $res = $r->isSuccess(true);
1154 }
1155 catch (\Exception $exception)
1156 {
1157 $res = false;
1158 }
1159
1160 if ($res)
1161 {
1162 $this->setFieldNoDemand('ACCOUNT_NUMBER', $value);
1163 }
1164
1165 return $result;
1166 }
1167
1172 public function getBusinessValueProviderInstance($mapping)
1173 {
1174 $providerInstance = null;
1175
1176 if (is_array($mapping) && isset($mapping['PROVIDER_KEY']))
1177 {
1178 switch ($mapping['PROVIDER_KEY'])
1179 {
1180 case 'PAYMENT':
1181 $providerInstance = $this;
1182 break;
1183 case 'COMPANY':
1184 $providerInstance = $this->getField('COMPANY_ID');
1185 break;
1186 default:
1187 $order = $this->getOrder();
1188 if ($order)
1189 {
1190 $providerInstance = $order->getBusinessValueProviderInstance($mapping);
1191 }
1192 }
1193 }
1194
1195 return $providerInstance;
1196 }
1197
1201 public function getPersonTypeId()
1202 {
1203 $order = $this->getOrder();
1204 if ($order)
1205 {
1206 return $order->getPersonTypeId();
1207 }
1208
1209 return null;
1210 }
1211
1219 public static function getList(array $parameters = [])
1220 {
1221 return Internals\PaymentTable::getList($parameters);
1222 }
1223
1230 public function createClone(\SplObjectStorage $cloneEntity)
1231 {
1232 if ($this->isClone() && $cloneEntity->contains($this))
1233 {
1234 return $cloneEntity[$this];
1235 }
1236
1238 $paymentClone = parent::createClone($cloneEntity);
1239
1241 if ($paySystem = $this->getPaySystem())
1242 {
1243 if (!$cloneEntity->contains($paySystem))
1244 {
1245 $cloneEntity[$paySystem] = $paySystem->createClone($cloneEntity);
1246 }
1247
1248 if ($cloneEntity->contains($paySystem))
1249 {
1250 $paymentClone->service = $cloneEntity[$paySystem];
1251 }
1252 }
1253
1254 return $paymentClone;
1255 }
1256
1261 public function getHash()
1262 {
1263 $order = $this->getOrder();
1264
1265 return md5(
1266 $this->getId().
1268 $order->getId()
1269 );
1270 }
1271
1281 public function isAllowPay()
1282 {
1284 $order = $this->getOrder();
1285
1286 return $order->isAllowPay();
1287 }
1288
1292 public function isMarked()
1293 {
1294 return $this->getField('MARKED') == "Y";
1295 }
1296
1302 public function getErrorEntity($value)
1303 {
1304 static $className = null;
1305 $errorsList = static::getAutoFixErrorsList();
1306 if (is_array($errorsList) && in_array($value, $errorsList))
1307 {
1308 if ($className === null)
1309 $className = static::getClassName();
1310 }
1311
1312 return $className;
1313 }
1314
1320 public function canAutoFixError($value)
1321 {
1322 $autoFix = false;
1323 $errorsList = static::getAutoFixErrorsList();
1324 if (is_array($errorsList) && in_array($value, $errorsList))
1325 {
1326 $autoFix = true;
1327 }
1328 return $autoFix;
1329 }
1330
1334 public function getAutoFixErrorsList()
1335 {
1336 return [];
1337 }
1338
1344 public function tryFixError($code)
1345 {
1346 return new Result();
1347 }
1348
1352 public function canMarked()
1353 {
1354 return true;
1355 }
1356
1360 public function getMarkField()
1361 {
1362 return 'MARKED';
1363 }
1364
1365 protected function isPriceField(string $name) : bool
1366 {
1367 return
1368 $name === 'PRICE_COD'
1369 || $name === 'SUM'
1370 ;
1371 }
1372
1379 protected function addInternal(array $data)
1380 {
1381 return Internals\PaymentTable::add($data);
1382 }
1383
1390 protected function updateInternal($primary, array $data)
1391 {
1392 return Internals\PaymentTable::update($primary, $data);
1393 }
1394
1400 protected static function deleteInternal($primary)
1401 {
1402 return Internals\PaymentTable::delete($primary);
1403 }
1404
1408 protected static function getFieldsMap()
1409 {
1410 return Internals\PaymentTable::getMap();
1411 }
1412
1416 public static function getUfId()
1417 {
1418 return Internals\PaymentTable::getUfId();
1419 }
1420
1426 public static function getEntityEventName()
1427 {
1428 return 'SalePayment';
1429 }
1430
1431}
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())
onAfterSave($isNew)
Definition: payment.php:866
static deleteNoDemand($orderId)
Definition: payment.php:257
static getRegistryEntity()
Definition: payment.php:33
updateInternal($primary, array $data)
Definition: payment.php:1390
static createPaymentObject(array $fields=[])
Definition: payment.php:151
checkValueBeforeSet($name, $value)
Definition: payment.php:1062
getBusinessValueProviderInstance($mapping)
Definition: payment.php:1172
static getMeaningfulFields()
Definition: payment.php:141
static create(PaymentCollection $collection, Sale\PaySystem\Service $paySystem=null)
Definition: payment.php:176
static getFieldsMap()
Definition: payment.php:1408
onBeforeBasketItemDelete(BasketItem $basketItem)
Definition: payment.php:588
static loadForOrder($id)
Definition: payment.php:231
static getEntityEventName()
Definition: payment.php:1426
static getAvailableFields()
Definition: payment.php:90
onBeforeSetFields(array $values)
Definition: payment.php:62
isPriceField(string $name)
Definition: payment.php:1365
addInternal(array $data)
Definition: payment.php:1379
canAutoFixError($value)
Definition: payment.php:1320
addChangesToHistory($name, $oldValue=null, $value=null)
Definition: payment.php:1103
static generateXmlId()
Definition: payment.php:218
static deleteInternal($primary)
Definition: payment.php:1400
getErrorEntity($value)
Definition: payment.php:1302
normalizeValue($name, $value)
Definition: payment.php:1038
static getRegistryType()
Definition: payment.php:162
setPaySystemService(Sale\PaySystem\Service $service)
Definition: payment.php:205
static getList(array $parameters=[])
Definition: payment.php:1219
static roundPrecision($value)
Definition: pricemaths.php:17
static getInstance($type)
Definition: registry.php:183
const REGISTRY_TYPE_ORDER
Definition: registry.php:16