Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
payment.php
1<?php
2
3
5
6
14use Bitrix\Sale;
16
22class Payment extends Controller
23{
24 public function getPrimaryAutoWiredParameter()
25 {
26 return new ExactParameter(
27 Sale\Payment::class,
28 'payment',
29 function($className, $id) {
30
31 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
32
34 $paymentClass = $registry->getPaymentClassName();
35
36 $r = $paymentClass::getList([
37 'select'=>['ORDER_ID'],
38 'filter'=>['ID'=>$id]
39 ]);
40
41 if($row = $r->fetch())
42 {
44 $orderClass = $registry->getOrderClassName();
45
46 $order = $orderClass::load($row['ORDER_ID']);
47 $payment = $order->getPaymentCollection()->getItemById($id);
48 if ($payment)
49 {
50 return $payment;
51 }
52 }
53 else
54 {
55 $this->addError(new Error('payment is not exists', 200640400001));
56 }
57 return null;
58 }
59 );
60 }
61
62 //region Actions
63 public function getFieldsAction()
64 {
65 $entity = new \Bitrix\Sale\Rest\Entity\Payment();
66 return ['PAYMENT'=>$entity->prepareFieldInfos(
67 $entity->getFields()
68 )];
69 }
70
71 public function modifyAction($fields)
72 {
73 $builder = $this->getBuilder();
74 $builder->buildEntityPayments($fields);
75
76 if($builder->getErrorsContainer()->getErrorCollection()->count()>0)
77 {
78 $this->addErrors($builder->getErrorsContainer()->getErrors());
79 return null;
80 }
81
82 $order = $builder->getOrder();
83
84 $r = $order->save();
85 if(!$r->isSuccess())
86 {
87 $this->addErrors($r->getErrors());
88 return null;
89 }
90
91 //TODO: return $payment->toArray();
92 return ['PAYMENTS'=>$this->toArray($order)['ORDER']['PAYMENTS']];
93 }
94
95 public function addAction(array $fields)
96 {
97 $result = null;
98
99 $data = [];
100 $data['ORDER']['ID'] = $fields['ORDER_ID'];
101 $data['ORDER']['PAYMENTS'] = [$fields];
102
103 $builder = $this->getBuilder(
105 'deletePaymentIfNotExists' => false
106 ])
107 );
108
109 $builder->buildEntityPayments($data);
110
111 if($builder->getErrorsContainer()->getErrorCollection()->count()>0)
112 {
113 $this->addErrors($builder->getErrorsContainer()->getErrors());
114 return null;
115 }
116
117 $order=$builder->getOrder();
118 $idx=0;
119 $collection = $order->getPaymentCollection();
121 foreach($collection as $payment)
122 {
123 if($payment->getId() <= 0)
124 {
125 $idx = $payment->getInternalIndex();
126 break;
127 }
128 }
129
130 $r = $order->save();
131 if(!$r->isSuccess())
132 {
133 $this->addErrors($r->getErrors());
134 return null;
135 }
136
138 $entity = $order->getPaymentCollection()->getItemByIndex($idx);
139 return ['PAYMENT'=>$this->get($entity)];
140 }
141
142 public function updateAction(\Bitrix\Sale\Payment $payment, array $fields)
143 {
144 $data = [];
145
146 $fields['ID'] = $payment->getId();
147 $fields['ORDER_ID'] = $payment->getOrderId();
148
149 $data['ORDER']['ID'] = $fields['ORDER_ID'];
150 $data['ORDER']['PAYMENTS'] = [$fields];
151
152 $builder = $this->getBuilder(
153 new SettingsContainer([
154 'deletePaymentIfNotExists' => false
155 ])
156 );
157 $builder->buildEntityPayments($data);
158
159 if($builder->getErrorsContainer()->getErrorCollection()->count()>0)
160 {
161 $this->addErrors($builder->getErrorsContainer()->getErrors());
162 return null;
163 }
164
165 $order = $builder->getOrder();
166
167 $r = $order->save();
168 if(!$r->isSuccess())
169 {
170 $this->addErrors($r->getErrors());
171 return null;
172 }
173 if($r->hasWarnings())
174 {
175 $this->addErrors($r->getWarnings());
176 return null;
177 }
178
180 $entity = $order->getPaymentCollection()->getItemById($payment->getId());
181 return ['PAYMENT'=>$this->get($entity)];
182 }
183
184 public function deleteAction(\Bitrix\Sale\Payment $payment)
185 {
186 $r = $payment->delete();
187 return $this->save($payment, $r);
188 }
189
190 public function getAction(\Bitrix\Sale\Payment $payment)
191 {
192 return ['PAYMENT'=>$this->get($payment)];
193 }
194
195 public function listAction(PageNavigation $pageNavigation, array $select = [], array $filter = [], array $order = []): Page
196 {
197 $select = empty($select) ? ['*'] : $select;
198 $order = empty($order) ? ['ID'=>'ASC'] : $order;
199
200 $runtime = [
201 new \Bitrix\Main\Entity\ReferenceField(
202 'PAY_SYSTEM',
203 '\Bitrix\Sale\Internals\PaySystemActionTable',
204 array('=this.PAY_SYSTEM_ID' => 'ref.ID')
205 )
206 ];
207
208 $payments = \Bitrix\Sale\Payment::getList(
209 [
210 'select' => $select,
211 'filter' => $filter,
212 'order' => $order,
213 'offset' => $pageNavigation->getOffset(),
214 'limit' => $pageNavigation->getLimit(),
215 'runtime' => $runtime
216 ]
217 )->fetchAll();
218
219 return new Page('payments', $payments, function() use ($select, $filter, $runtime)
220 {
221 return count(
222 \Bitrix\Sale\Payment::getList(['select'=>$select, 'filter'=>$filter, 'runtime'=>$runtime])->fetchAll()
223 );
224 });
225 }
226
227 public function getOrderIdAction(\Bitrix\Sale\Payment $payment)
228 {
229 return $payment->getOrderId();
230 }
231
232 public function getPaymentSystemIdAction(\Bitrix\Sale\Payment $payment)
233 {
234 return $payment->getPaymentSystemId();
235 }
236
238 {
239 return $payment->getPaymentSystemName();
240 }
241
242 public function getPersonTypeIdAction(\Bitrix\Sale\Payment $payment)
243 {
244 return $payment->getPersonTypeId();
245 }
246
247 public function getSumAction(\Bitrix\Sale\Payment $payment)
248 {
249 return $payment->getSum();
250 }
251
252 public function getSumPaidAction(\Bitrix\Sale\Payment $payment)
253 {
254 return $payment->getSumPaid();
255 }
256
257 public function isInnerAction(\Bitrix\Sale\Payment $payment)
258 {
259 return $payment->isInner()? 'Y':'N';
260 }
261
262 public function isMarkedAction(\Bitrix\Sale\Payment $payment)
263 {
264 return $payment->isMarked()? 'Y':'N';
265 }
266
267 public function isPaidAction(\Bitrix\Sale\Payment $payment)
268 {
269 return $payment->isPaid()? 'Y':'N';
270 }
271
272 public function isReturnAction(\Bitrix\Sale\Payment $payment)
273 {
274 return $payment->isReturn()? 'Y':'N';
275 }
276
277 public function setPaidAction(\Bitrix\Sale\Payment $payment, $value)
278 {
279 $r = $payment->setPaid($value);
280 if($r->isSuccess())
281 {
282 $this->save($payment, $r);
283 }
284
285 if(!$r->isSuccess())
286 {
287 $this->addErrors($r->getErrors());
288 return null;
289 }
290
291 return true;
292 }
293/*
294 public function setAccountNumberAction(\Bitrix\Sale\Payment $payment, $id)
295 {
296 $r = $payment->setAccountNumber($id);
297 return $this->save($payment, $r);
298 }
299*/
300 public function setReturnAction(\Bitrix\Sale\Payment $payment, $value)
301 {
302 $r = $payment->setReturn($value);
303 return $this->save($payment, $r);
304 }
305
316 {
317 $result = PaymentAvailablesPaySystems::clearBindings($payment->getId());
318 if ($errors = $result->getErrors())
319 {
320 $this->addErrors($errors);
321 return null;
322 }
323 return true;
324 }
325
336 public function setAvailablePaySystemsAction(\Bitrix\Sale\Payment $payment, array $paySystemIds)
337 {
338 $result = PaymentAvailablesPaySystems::setBindings($payment->getId(), $paySystemIds);
339 if ($errors = $result->getErrors())
340 {
341 $this->addErrors($errors);
342 return null;
343 }
344 return true;
345 }
346 //endregion
347
348 private function save(\Bitrix\Sale\Payment $payment, Result $r)
349 {
350 if(!$r->isSuccess())
351 {
352 $this->addErrors($r->getErrors());
353 return null;
354 }
355 else
356 {
358 $collection = $payment->getCollection();
359 $r = $collection->getOrder()->save();
360 if(!$r->isSuccess())
361 {
362 $this->addErrors($r->getErrors());
363 return null;
364 }
365 }
366 return $r->isSuccess();
367 }
368
369 protected function get(\Bitrix\Sale\Payment $payment, array $fields=[])
370 {
371 $payments = $this->toArray($payment->getCollection()->getOrder(), $fields)['ORDER']['PAYMENTS'];
372 foreach ($payments as $item)
373 {
374 if($item['ID']==$payment->getId())
375 {
376 return $item;
377 }
378 }
379 return [];
380 }
381
382 static public function prepareFields($fields)
383 {
384 return isset($fields['PAYMENTS'])?['PAYMENT'=>$fields['PAYMENTS']]:[];
385 }
386
387 protected function checkPermissionEntity($name)
388 {
389 if($name == 'getorderid'
390 || $name == 'getpaymentsystemid'
391 || $name == 'getpaymentsystemname'
392 || $name == 'getpersontypeid'
393 || $name == 'getsum'
394 || $name == 'getsumpaid'
395 || $name == 'isinner'
396 || $name == 'ismarked'
397 || $name == 'isnarked'
398 || $name == 'ispaid'
399 || $name == 'isreturn'
400 )
401 {
402 $r = $this->checkReadPermissionEntity();
403 }
404 elseif($name == 'setpaid'
405 || $name == 'setavailablepaysystems'
406 || $name == 'clearavailablepaysystems'
407 || $name == 'setreturn')
408 {
409 $r = $this->checkModifyPermissionEntity();
410 }
411 else
412 {
413 $r = parent::checkPermissionEntity($name);
414 }
415 return $r;
416 }
417}
static prepareFields($fields)
Definition payment.php:382
setReturnAction(\Bitrix\Sale\Payment $payment, $value)
Definition payment.php:300
isReturnAction(\Bitrix\Sale\Payment $payment)
Definition payment.php:272
getOrderIdAction(\Bitrix\Sale\Payment $payment)
Definition payment.php:227
setAvailablePaySystemsAction(\Bitrix\Sale\Payment $payment, array $paySystemIds)
Definition payment.php:336
isInnerAction(\Bitrix\Sale\Payment $payment)
Definition payment.php:257
getPaymentSystemIdAction(\Bitrix\Sale\Payment $payment)
Definition payment.php:232
getPaymentSystemNameAction(\Bitrix\Sale\Payment $payment)
Definition payment.php:237
isMarkedAction(\Bitrix\Sale\Payment $payment)
Definition payment.php:262
isPaidAction(\Bitrix\Sale\Payment $payment)
Definition payment.php:267
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[])
Definition payment.php:195
getAction(\Bitrix\Sale\Payment $payment)
Definition payment.php:190
getSumPaidAction(\Bitrix\Sale\Payment $payment)
Definition payment.php:252
clearAvailablePaySystemsAction(\Bitrix\Sale\Payment $payment)
Definition payment.php:315
getPersonTypeIdAction(\Bitrix\Sale\Payment $payment)
Definition payment.php:242
deleteAction(\Bitrix\Sale\Payment $payment)
Definition payment.php:184
getSumAction(\Bitrix\Sale\Payment $payment)
Definition payment.php:247
setPaidAction(\Bitrix\Sale\Payment $payment, $value)
Definition payment.php:277
static setBindings(int $paymentId, array $paySystemIds)
static getList(array $parameters=[])
Definition payment.php:1235