protected function updatePaymentStatusAction() { global $USER; if (!isset($this->request['orderId']) || intval($this->request['orderId']) <= 0) { throw new ArgumentNullException("orderId"); } if (!isset($this->request['paymentId']) || intval($this->request['paymentId']) <= 0) { throw new ArgumentNullException("paymentId"); } $fields = array(); $orderStatusId = ''; /** @var \Bitrix\Sale\Order $order */ $order = \Bitrix\Sale\Order::load($this->request['orderId']); /** @var \Bitrix\Sale\Payment $payment */ $payment = $order->getPaymentCollection()->getItemById($this->request['paymentId']); if ($this->request['method'] == 'save') { if ($payment->getField('IS_RETURN') == 'Y') { $res = $payment->setReturn('N'); if (!$res->isSuccess()) { $this->addResultError(join("\n", $res->getErrorMessages())); } } else { $res = $payment->setPaid('Y'); if (!$res->isSuccess()) { $this->addResultError(join("\n", $res->getErrorMessages())); } } foreach ($this->request['data'] as $key => $value) { $newKey = substr($key, 0, strripos($key, '_')); if (strpos($newKey, 'PAY_VOUCHER') !== false) { $fields[$newKey] = $value; } if ($newKey == 'ORDER_STATUS_ID') { $orderStatusId = $value; } } $fields['PAY_VOUCHER_DATE'] = new \Bitrix\Main\Type\Date($fields['PAY_VOUCHER_DATE']); } else { foreach ($this->request['data'] as $key => $value) { $newKey = substr($key, 0, strripos($key, '_')); if (strpos($newKey, 'PAY_RETURN') !== false) { $fields[$newKey] = $value; } } if (isset($fields['PAY_RETURN_OPERATION_ID']) && $fields['PAY_RETURN_OPERATION_ID'] == 'RETURN') { $res = $payment->setReturn('Y'); if (!$res->isSuccess()) { $this->addResultError(join("\n", $res->getErrorMessages())); } unset($fields['PAY_RETURN_OPERATION_ID']); } else { $res = $payment->setPaid('N'); if (!$res->isSuccess()) { $this->addResultError(join("\n", $res->getErrorMessages())); } } $fields['PAY_RETURN_DATE'] = new \Bitrix\Main\Type\Date($fields['PAY_RETURN_DATE']); } $saveResult = $payment->setFields($fields); if ($saveResult->isSuccess()) { if (!empty($orderStatusId)) { if ($USER && $USER->isAuthorized()) { $statusesList = Sale\OrderStatus::getAllowedUserStatuses($USER->getID(), $order->getField('STATUS_ID')); } else { $statusesList = Sale\OrderStatus::getAllStatuses(); } if ($order->getField('STATUS_ID') != $orderStatusId && array_key_exists($orderStatusId, $statusesList)) { $res = $order->setField('STATUS_ID', $orderStatusId); if (!$res->isSuccess()) { $this->addResultError(join("\n", $res->getErrorMessages())); return; } } } $result = $order->save(); if ($result->isSuccess()) { $preparedData = \Bitrix\Sale\Helpers\Admin\Blocks\OrderFinanceInfo::prepareData($order); $preparedData["PAYMENT_PAID_" . $payment->getId()] = $payment->isPaid() ? "Y" : "N"; $this->addResultData("RESULT", $preparedData); } else { $this->addResultError(join("\n", $result->getErrorMessages())); } } else { $this->addResultError(join("\n", $saveResult->getErrorMessages())); } }
/** * @param null $oldPaid * @return Result * @throws Main\ObjectNotFoundException */ protected function onAfterSyncPaid($oldPaid = null) { global $USER; $result = new Result(); /** @var PaymentCollection $paymentCollection */ if (!($paymentCollection = $this->getPaymentCollection())) { throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found'); } /** @var ShipmentCollection $shipmentCollection */ if (!($shipmentCollection = $this->getShipmentCollection())) { throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found'); } $oldPaidBool = null; if ($oldPaid !== null) { $oldPaidBool = $oldPaid == "Y"; } if ($oldPaid == "N" && $this->isPaid()) { $orderStatus = Config\Option::get('sale', 'status_on_paid', ''); if (strval($orderStatus) != '') { if ($USER && $USER->isAuthorized()) { $statusesList = OrderStatus::getAllowedUserStatuses($USER->getID(), $this->getField('STATUS_ID')); $statusesList = array_keys($statusesList); } else { $statusesList = OrderStatus::getAllStatuses(); } if ($this->getField('STATUS_ID') != $orderStatus && in_array($orderStatus, $statusesList)) { $this->setField('STATUS_ID', $orderStatus); } } } if ($oldPaid !== null && $this->isPaid() != $oldPaidBool) { Internals\EventsPool::addEvent($this, EventActions::EVENT_ON_ORDER_PAID, array('ENTITY' => $this)); Internals\EventsPool::addEvent($this, EventActions::EVENT_ON_ORDER_PAID_SEND_MAIL, array('ENTITY' => $this)); } if (Configuration::getProductReservationCondition() == Configuration::RESERVE_ON_PAY) { if ($paymentCollection->hasPaidPayment()) { $r = $shipmentCollection->tryReserve(); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } } else { $r = $shipmentCollection->tryUnreserve(); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } } } elseif (Configuration::getProductReservationCondition() == Configuration::RESERVE_ON_FULL_PAY) { if ($oldPaid == "N" && $this->isPaid()) { $r = $shipmentCollection->tryReserve(); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } } elseif ($oldPaid == "Y" && !$this->isPaid()) { $r = $shipmentCollection->tryUnreserve(); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } } } if (Configuration::needAllowDeliveryOnPay()) { if ($oldPaid == "N" && $this->isPaid()) { $r = $shipmentCollection->allowDelivery(); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } } elseif ($oldPaid == "Y" && !$this->isPaid()) { $r = $shipmentCollection->disallowDelivery(); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); } } } return $result; }
/** * Get additional data for cache * * @return array */ protected function getAdditionalRefereneces() { if (!$this->isSale) { return array(); } return array('ORDER_STATUS' => Sale\OrderStatus::getAllStatuses()); }