Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED !== true) {
- die();
- }
- if (!CModule::IncludeModule("sale")) return;
- use \Bitrix\Main\Localization\Loc;
- use \Bitrix\Main\Loader;
- use \Bitrix\Main\Service\GeoIp;
- class customOrderComponent extends CBitrixComponent
- {
- /**
- * @var \Bitrix\Sale\Order
- */
- public $order;
- protected $errors = [];
- protected $arResponse = [
- 'errors' => [],
- 'html' => ''
- ];
- function __construct($component = null)
- {
- parent::__construct($component);
- if (!Loader::includeModule('sale')) {
- $this->errors[] = 'No sale module';
- };
- if (!Loader::includeModule('catalog')) {
- $this->errors[] = 'No catalog module';
- };
- }
- function onPrepareComponentParams($arParams)
- {
- if (isset($arParams['PERSON_TYPE_ID']) && intval($arParams['PERSON_TYPE_ID']) > 0) {
- $arParams['PERSON_TYPE_ID'] = intval($arParams['PERSON_TYPE_ID']);
- } else {
- if (intval($this->request['payer']['person_type_id']) > 0) {
- $arParams['PERSON_TYPE_ID'] = intval($this->request['payer']['person_type_id']);
- } else {
- $arParams['PERSON_TYPE_ID'] = 1;
- }
- }
- if (
- isset($arParams['IS_AJAX'])
- && ($arParams['IS_AJAX'] == 'Y' || $arParams['IS_AJAX'] == 'N')
- ) {
- $arParams['IS_AJAX'] = $arParams['IS_AJAX'] == 'Y';
- } else {
- if (
- isset($this->request['is_ajax'])
- && ($this->request['is_ajax'] == 'Y' || $this->request['is_ajax'] == 'N')
- ) {
- $arParams['IS_AJAX'] = $this->request['is_ajax'] == 'Y';
- } else {
- $arParams['IS_AJAX'] = false;
- }
- }
- if (isset($arParams['ACTION']) && strlen($arParams['ACTION']) > 0) {
- $arParams['ACTION'] = strval($arParams['ACTION']);
- } else {
- if (isset($this->request['action']) && strlen($this->request['action']) > 0) {
- $arParams['ACTION'] = strval($this->request['action']);
- } else {
- $arParams['ACTION'] = '';
- }
- }
- if(isset($arParams['PATH_TO_BASKET']) && strlen($arParams['PATH_TO_BASKET']) > 0){
- $arParams['PATH_TO_BASKET'] = strval($arParams['PATH_TO_BASKET']);
- }
- else{
- $arParams['PATH_TO_BASKET'] = '/basket/';
- }
- if(isset($arParams['MIN_SUM']) && intval($arParams['MIN_SUM']) > 0){
- $arParams['MIN_SUM'] = intval($arParams['MIN_SUM']);
- }
- else{
- $arParams['MIN_SUM'] = 500;
- }
- return $arParams;
- }
- protected function createVirtualOrder()
- {
- global $USER;
- try {
- $siteId = \Bitrix\Main\Context::getCurrent()->getSite();
- $basketItems = \Bitrix\Sale\Basket::loadItemsForFUser(
- \CSaleBasket::GetBasketUserID(),
- $siteId
- )
- ->getOrderableItems();
- if (count($basketItems) == 0 && $basketItems->getPrice() < $this->arParams['MIN_SUM']) {
- LocalRedirect($this->arParams['PATH_TO_BASKET']);
- }
- if ($USER->IsAuthorized()){
- $user_id = $USER->GetID();
- }
- else{
- $user_id = \CSaleUser::GetAnonymousUserID();
- }
- $this->order = \Bitrix\Sale\Order::create($siteId, $user_id);
- $this->order->setPersonTypeId($this->arParams['PERSON_TYPE_ID']);
- $this->order->setBasket($basketItems);
- $this->setOrderProps();
- //устанавливаем комментарий покупателя из свойства
- if(!empty($this->request['ORDER_DESCRIPTION'])):
- $this->order->setField('USER_DESCRIPTION', $this->request['ORDER_DESCRIPTION']);
- endif;
- if (intval($this->request['location_code']) > 0) {
- $location_code = $this->request['location_code'];
- }
- else{
- $ipAddress = GeoIp\Manager::getRealIp();
- $location_code = \Bitrix\Sale\Location\GeoIp::getLocationCode($ipAddress, LANGUAGE_ID);
- }
- foreach ($this->order->getPropertyCollection() as $prop) {
- switch ($prop->getField('CODE')):
- case 'LOCATION':
- $prop->setValue($location_code);
- break;
- endswitch;
- }
- /* @var $shipmentCollection \Bitrix\Sale\ShipmentCollection */
- $shipmentCollection = $this->order->getShipmentCollection();
- if (intval($this->request['delivery_id']) > 0) {
- $shipment = $shipmentCollection->createItem(
- Bitrix\Sale\Delivery\Services\Manager::getObjectById(
- intval($this->request['delivery_id'])
- )
- );
- } else {
- $shipment = $shipmentCollection->createItem();
- }
- /** @var $shipmentItemCollection \Bitrix\Sale\ShipmentItemCollection */
- $shipmentItemCollection = $shipment->getShipmentItemCollection();
- $shipment->setField('CURRENCY', $this->order->getCurrency());
- foreach ($this->order->getBasket()->getOrderableItems() as $item) {
- /**
- * @var $item \Bitrix\Sale\BasketItem
- * @var $shipmentItem \Bitrix\Sale\ShipmentItem
- * @var $item \Bitrix\Sale\BasketItem
- */
- $shipmentItem = $shipmentItemCollection->createItem($item);
- $shipmentItem->setQuantity($item->getQuantity());
- $shipmentItem->setField('DELIVERY_ID', $this->request['delivery_id']);
- }
- } catch (\Exception $e) {
- $this->errors[] = $e->getMessage();
- }
- }
- protected function setOrderProps()
- {
- global $USER;
- $arUser = $USER->GetByID(intval($USER->GetID()))
- ->Fetch();
- if (is_array($arUser)) {
- $fio = $arUser['LAST_NAME'] . ' ' . $arUser['NAME'] . ' ' . $arUser['SECOND_NAME'];
- $fio = trim($fio);
- $arUser['FIO'] = $fio;
- }
- foreach ($this->order->getPropertyCollection() as $prop) {
- /** @var \Bitrix\Sale\PropertyValue $prop */
- $value = '';
- if (empty($value)) {
- foreach ($this->request as $key => $val) {
- if (strtolower($key) == strtolower($prop->getField('CODE'))) {
- $value = $val;
- }
- }
- }
- if (empty($value)) {
- $value = $prop->getProperty()['DEFAULT_VALUE'];
- }
- if (!empty($value)) {
- $prop->setValue($value);
- }
- }
- }
- protected function calcAction()
- {
- $this->setTemplateName('');
- //Собираем ID доставок
- $deliveryIDs = [];
- if (isset($this->request['delivery_id'])) {
- if (is_array($this->request['delivery_id'])) {
- foreach ($this->request['delivery_id'] as $val) {
- if (intval($val) > 0) {
- $deliveryIDs[intval($val)] = intval($val);
- }
- }
- } elseif (intval($this->request['delivery_id']) > 0) {
- $deliveryIDs = [intval($this->request['delivery_id'])];
- } else {
- $deliveryIDs = [];
- }
- }
- if(empty($deliveryIDs)):
- $deliveries = \Bitrix\Sale\Delivery\Services\Manager::getActiveList();
- foreach ( $deliveries as $index => $value) {
- $deliveryIDs[$index] = $index;
- }
- endif;
- //На выходе в любом случае будет массив
- sort($deliveryIDs);
- if (empty($deliveryIDs)) {
- throw new \Exception('Нет доставок для расчета');
- }
- $shipment = false;
- /** @var \Bitrix\Sale\Shipment $shipmentItem */
- foreach ($this->order->getShipmentCollection() as $shipmentItem) {
- if (!$shipmentItem->isSystem()) {
- $shipment = $shipmentItem;
- break;
- }
- }
- if (!$shipment) {
- throw new \Exception('Отгрузка не найдена');
- }
- //Массив с доставками,
- $availableDeliveries = \Bitrix\Sale\Delivery\Services\Manager::getRestrictedObjectsList(
- $shipment
- );
- foreach ($deliveryIDs as $deliveryId) {
- $obDelivery = false;
- if (isset($availableDeliveries[$deliveryId])) {
- //Если переданный из запроса ID доставки доступен покупателю
- $obDelivery = $availableDeliveries[$deliveryId];
- }
- if ($obDelivery) {
- $arDelivery = [
- 'id' => $obDelivery->getId(),
- 'name' => $obDelivery->getName(),
- 'logo_path' => $obDelivery->getLogotipPath(),
- 'show' => false,
- 'calculated' => false,
- 'period' => '',
- 'price' => 0,
- 'price_formated' => '',
- ];
- $calcResult = $obDelivery->calculate($shipment);
- if ($calcResult->isSuccess()) {
- $arDelivery['calculated'] = true;
- $arDelivery["price"] = $calcResult->getPrice();
- $arDelivery["price_formated"] = \SaleFormatCurrency(
- $calcResult->getPrice(),
- $this->order->getCurrency()
- );
- if (strlen($calcResult->getPeriodDescription()) > 0) {
- $arDelivery["period_text"] = $calcResult->getPeriodDescription();
- }
- }
- if (floatval($arDelivery['price']) > 0) {
- $arDelivery['show'] = true;
- }
- if (empty($arDelivery["period_text"])) {
- $arDelivery["period_text"] = '...';
- }
- $this->arResponse['deliveries'][$arDelivery['ID']] = $arDelivery;
- } else {
- //В аякс ответе, даже недоступную доставку возвращаем
- $this->arResponse['deliveries'][$deliveryId] = [
- 'id' => $deliveryId,
- 'show' => false
- ];
- }
- }
- }
- protected function getDeliveries(){
- $shipment = false;
- /** @var \Bitrix\Sale\Shipment $shipmentItem */
- foreach ($this->order->getShipmentCollection() as $shipmentItem) {
- if (!$shipmentItem->isSystem()) {
- $shipment = $shipmentItem;
- break;
- }
- }
- if (!$shipment) {
- throw new \Exception('Отгрузка не найдена');
- }
- //Массив с доставками,
- $availableDeliveries = \Bitrix\Sale\Delivery\Services\Manager::getRestrictedObjectsList(
- $shipment
- );
- $arDeliveries = array();
- foreach ($availableDeliveries as $delivery) {
- $obDelivery = $delivery;
- if ($obDelivery) {
- $arDelivery = [
- 'id' => $obDelivery->getId(),
- 'name' => $obDelivery->getName(),
- 'logo_path' => $obDelivery->getLogotipPath(),
- 'show' => false,
- 'calculated' => false,
- 'period' => '',
- 'price' => 0,
- 'price_formated' => '',
- ];
- $shipment_inner = $this->order->getShipmentCollection()->createItem(
- Bitrix\Sale\Delivery\Services\Manager::getObjectById(
- intval($obDelivery->getId())
- )
- );
- $calcResult = $obDelivery->calculate($shipment_inner);
- if ($calcResult->isSuccess()) {
- $arDelivery['calculated'] = true;
- $arDelivery["price"] = $calcResult->getPrice();
- $arDelivery["price_formated"] = \SaleFormatCurrency(
- $calcResult->getPrice(),
- $this->order->getCurrency()
- );
- if (strlen($calcResult->getPeriodDescription()) > 0) {
- $arDelivery["period_text"] = $calcResult->getPeriodDescription();
- }
- }
- if (floatval($arDelivery['price']) > 0) {
- $arDelivery['show'] = true;
- }
- if (empty($arDelivery["period_text"])) {
- $arDelivery["period_text"] = '...';
- }
- $shipment_inner->delete();
- $arDeliveries[] = $arDelivery;
- }
- }
- return $arDeliveries;
- }
- public function getPropByCode($code)
- {
- $result = false;
- $propId = 0;
- if (isset($this->propMap[$code])) {
- $propId = $this->propMap[$code];
- }
- if ($propId > 0) {
- $result = $this->order
- ->getPropertyCollection()
- ->getItemByOrderPropertyId($propId);
- }
- return $result;
- }
- public function getPropDataByCode($code)
- {
- $result = [];
- $propId = 0;
- if (isset($this->propMap[$code])) {
- $propId = $this->propMap[$code];
- }
- if ($propId > 0) {
- $result = $this->order
- ->getPropertyCollection()
- ->getItemByOrderPropertyId($propId)
- ->getFieldValues();
- }
- return $result;
- }
- function executeComponent()
- {
- global $APPLICATION;
- if ($this->arParams['IS_AJAX']) {
- $APPLICATION->RestartBuffer();
- }
- $this->createVirtualOrder();
- $this->arDeliveries = $this->getDeliveries();
- $this->createVirtualOrder();
- if(!empty($this->arParams['ACTION'])) {
- if (is_callable([$this, $this->arParams['ACTION'] . "Action"])) {
- try {
- call_user_func([$this, $this->arParams['ACTION'] . "Action"]);
- } catch (\Exception $e) {
- $this->errors[] = $e->getMessage();
- }
- }
- }
- if (isset($this->request['save']) && $this->request['save'] == 'Y') {
- if (intval($this->request['payment_id']) > 0) {
- $paymentCollection = $this->order->getPaymentCollection();
- $payment = $paymentCollection->createItem(
- Bitrix\Sale\PaySystem\Manager::getObjectById(
- intval($this->request['payment_id'])
- )
- );
- $payment->setField("SUM", $this->order->getPrice());
- $payment->setField("CURRENCY", $this->order->getCurrency());
- }
- else{
- $payment_id = 19;
- $paymentCollection = $this->order->getPaymentCollection();
- $payment = $paymentCollection->createItem(
- Bitrix\Sale\PaySystem\Manager::getObjectById($payment_id)
- );
- $payment->setField("SUM", $this->order->getPrice());
- $payment->setField("CURRENCY", $this->order->getCurrency());
- }
- $this->order->save();
- $url = 'https://secure.payonlinesystem.com/ru/payment/select/';
- $postData = array(
- 'MerchantId' => '77863',
- 'OrderId' => $this->order->getId(),
- 'Amount'=> number_format($this->order->getPrice(), 2, ".", ""),
- 'Currency' => $this->order->getCurrency(),
- 'PrivateSecurityKey'=> 'a7affa62-3e13-432d-93fd-fef97f7a27a9',
- 'ReturnUrl' => SITE_SERVER_NAME.'/bitrix/tools/biznesup.payonline/result.php',
- 'FailUrl' => SITE_SERVER_NAME.'/bitrix/tools/biznesup.payonline/result.php'
- );
- $hash = "MerchantId=".$postData['MerchantId']."&".
- "OrderId=".$postData['OrderId']."&".
- "Amount=".$postData['Amount']."&".
- "Currency=".$postData['Currency']."&".
- "PrivateSecurityKey=".$postData['PrivateSecurityKey'];
- $hash = md5($hash);
- $global_url = $url."?"."MerchantId=".$postData['MerchantId']."&".
- "OrderId=".$postData['OrderId']."&".
- "Amount=".$postData['Amount']."&".
- "Currency=".$postData['Currency']."&".
- "SecurityKey=".$hash."&".
- "ReturnUrl=".$postData['ReturnUrl']."&".
- "FailUrl=".$postData['ReturnUrl'];
- LocalRedirect($global_url);
- }
- if ($this->arParams['IS_AJAX']) {
- if ($this->getTemplateName() != '') {
- ob_start();
- $this->includeComponentTemplate();
- $this->arResponse['html'] = ob_get_contents();
- ob_end_clean();
- }
- $this->arResponse['errors'] = $this->errors;
- header('Content-Type: application/json');
- echo json_encode($this->arResponse);
- $APPLICATION->FinalActions();
- die();
- } else {
- $this->includeComponentTemplate();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement