Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
orderpaymentresolver.php
1<?php
2
4
8
13{
23 public static function findOrCreatePaymentEqualOrderSum(int $orderId, ?int $paySystemId = null): ?Payment
24 {
25 if ($payment = self::findPaymentEqualOrderSum($orderId))
26 {
27 return $payment;
28 }
29
30 $order = Order::load($orderId);
31 if (!$order)
32 {
33 return null;
34 }
35
36 if (!$service = self::buildPaySystemService($order, $paySystemId))
37 {
38 return null;
39 }
40
41 $paymentSum = $order->getPrice();
42 $paymentCollection = $order->getPaymentCollection();
43 $payment = $paymentCollection->createItem($service);
44 $result = $payment->setField('SUM', $paymentSum);
45
46 $result = $result->isSuccess() ? $order->save() : $result;
47
48 if ($result->isSuccess())
49 {
50 return $payment;
51 }
52
53 return null;
54 }
55
62 private static function findPaymentEqualOrderSum(int $orderId): ?Payment
63 {
64 $order = Order::load($orderId);
65 if (!$order)
66 {
67 return null;
68 }
69
70 $paymentSum = $order->getPrice();
71
72 $filter = [
73 'ORDER_ID' => $order->getId(),
74 'SUM' => $paymentSum,
75 ];
76
77 $paymentRow = Payment::getList([
78 'filter' => $filter,
79 'select' => ['ORDER_ID', 'ID'],
80 'limit' => 1
81 ]);
82 if ($paymentData = $paymentRow->fetch())
83 {
84 $paymentId = (int)$paymentData['ID'];
86 $payment = $order->getPaymentCollection()->getItemById($paymentId);
87
88 return $payment;
89 }
90 return null;
91 }
92
100 private static function buildPaySystemService(Order $order, ?int $paySystemId = null): ?PaySystem\Service
101 {
102 if ($paySystemId === null)
103 {
104 $paySystemId = self::getDefaultPaySystemId($order);
105 }
106
107 return PaySystem\Manager::getObjectById($paySystemId);
108 }
109
117 private static function getDefaultPaySystemId(Order $order): int
118 {
119 $paySystem = [];
120 $paySystemList = PaySystem\Manager::getListWithRestrictionsByOrder($order);
121
122 foreach ($paySystemList as $item)
123 {
124 if ($item['ACTION_FILE'] === 'cash')
125 {
126 $paySystem = $item;
127 break;
128 }
129 }
130
131 if (!$paySystem)
132 {
133 $paySystem = current($paySystemList);
134 }
135
136 return (int)$paySystem['ID'];
137 }
138}
static findOrCreatePaymentEqualOrderSum(int $orderId, ?int $paySystemId=null)
static getList(array $parameters=[])
Definition payment.php:1235