Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/grain.iiko/lib/rest/ |
Upload File : |
<?php namespace Grain\Iiko\Rest; use \Bitrix\Main\Localization\Loc; use \Bitrix\Main\Web\Json; use \Bitrix\Main\Application; use \Bitrix\Main\Loader; use \Bitrix\Main\Web\Uri; use \Bitrix\Main\Config\Option; use \Grain\Iiko\Rest; Loc::loadMessages(__FILE__); class v1 { const apiProtocol = 'https'; const apiPath = '/api/1'; const timeout = 60; public static function query($path,$body,$method='POST',$account) { $accessToken = self::getAuthHash($account); if(!$accessToken['result']) return $accessToken; $header = 'Authorization: Bearer '.$accessToken['result']['token']; $header .= "\r\nTimeout: ".static::timeout; $result = static::_query($method,$path,$header,$body,$account); return $result; } public static function getAuthHash($account) { return static::_query('POST','/access_token','Timeout: 15',array("apiLogin"=>$account['LOGIN']),$account); } public static function _query($method='GET',$path,$header='',$body=false,$account=false) { $method = strtoupper($method); $opts = array( 'method' => $method, 'timeout' => static::timeout, 'ignore_errors'=> true, ); if(is_array($body)) { if($method=='POST') { $body = Json::encode($body); $opts['header'] = $header.(strlen($header)>0?"\r\n":"")."Content-Type: application/json\r\nContent-Length: ".strlen($body)."\r\nAccept: application/json"; $opts['content'] = $body; } else { $uri = new Uri($path); $uri->addParams($body); $path = $uri->getUri(); } } $context = stream_context_create(array('http'=>$opts)); // do not use https $stream = @fopen(static::apiProtocol.'://'.$account['HOST'].':'.$account['PORT'].static::apiPath.$path, 'r', false, $context); //var_dump(static::apiProtocol.'://'.$account['HOST'].':'.$account['PORT'].static::apiPath.$path); if($stream!==false) { $result = stream_get_contents($stream); fclose($stream); $error = false; try { $arRes = Json::decode($result); } catch (\Bitrix\Main\SystemException $e) { $error = true; //$e->getMessage(); } if($error) { return array('error'=>Loc::getMessage('GRAIN_IIKO_REST_JSON_ERROR')); } elseif(is_array($arRes) && (array_key_exists('errorDescription', $arRes) || array_key_exists('error', $arRes))) { return array('error'=>strval($arRes['error']).(array_key_exists('error',$arRes) && array_key_exists('errorDescription',$arRes)?' ':'').strval($arRes['errorDescription'])); } else { return array('result'=>$arRes); } } else { $error = error_get_last(); return array('error'=>$error['message']);//.print_r($error,true); } } public static function buildIikoOrderBody($params,$restaurantId) { $order = array ( 'phone' => $params['phone'], 'orderServiceType' => $params['orderServiceType'], 'items' => array(), 'comment' => $params['comment'], ); if(isset($params['date'])) $order['completeBefore'] = $params['date']->format('Y-m-d H:i:s.000'); if($params['queryType']=='loyaltyCalculate' && (!isset($params['phone']) || strlen($params['phone'])<=0)) $order['phone'] = '+7 (000) 000-00-00'; if(in_array($params['queryType'],array('orderCheckCreate','orderCreate')) && is_string($params['orderUuid']) && strlen($params['orderUuid'])>0) $order['id'] = $params['orderUuid']; foreach($params['items'] as $item) { $priceWOModifiers = $item['price']; if(is_array($item['modifiers']) && count($item['modifiers'])>0) { foreach($item['modifiers'] as $modifier) { if(!isset($modifier['price'])) continue; $priceWOModifiers -= floatval($modifier['price'])*floatval($modifier['amount']); } } $newItem = array( 'productId' => $item['id'], 'amount' => $item['amount'], 'type' => 'Product', 'price' => $priceWOModifiers, 'positionId' => Rest::generateUuid('positionId-'.$item['basketId']), ); if(is_array($item['productSize'])) $newItem['productSizeId'] = $item['productSize']['sizeId']; if(is_array($item['modifiers']) && count($item['modifiers'])>0) { $newItem['modifiers'] = array(); foreach($item['modifiers'] as $modifier) { $newModifier = array( 'productId' => $modifier['id'], 'price' => $modifier['price'], 'amount' => $modifier['amount'], ); if($modifier['groupId']) $newModifier['productGroupId'] = $modifier['groupId']; $newItem['modifiers'][] = $newModifier; } } $order['items'][] = $newItem; } if($params['orderServiceType']=='DeliveryByCourier' && isset($params['deliveryServiceProductId'])) { $order['items'][] = array( 'productId' => $params['deliveryServiceProductId'], 'amount' => 1, 'type' => 'Product', // orderItemType in menu, so it 'Product', not 'Service' 'price' => $params['deliveryPrice'], ); } if(!!$params['discounts']) $order['discountsInfo'] = array('discounts' => $params['discounts']); if(is_array($params['payments']) && count($params['payments'])>0) { foreach($params['payments'] as $payment) { $newPayment = array( 'paymentTypeKind' => $payment['paymentTypeKind'], 'sum' => $payment['sum'], 'paymentTypeId' => $payment['paymentType']['id'], 'isProcessedExternally' => $payment['isProcessedExternally'], //'isFiscalizedExternally' => , ); if(array_key_exists('paymentAdditionalData', $payment)) $newPayment['paymentAdditionalData'] = $payment['paymentAdditionalData']; $order['payments'][] = $newPayment; } } if($params['orderServiceType']=='DeliveryByCourier') { $order['deliveryPoint'] = array( 'address' => array( 'street' => array( 'name' => $params['street'], 'city' => $params['city'], ), 'house' => $params['house'], 'building' => $params['building'], 'flat' => $params['flat'], 'entrance' => $params['entrance'], 'floor' => $params['floor'], 'doorphone' => $params['doorphone'], ), ); } list($name,$surName) = explode(" ",$params['name'],2); $order['customer'] = array( 'name' => trim(strval($name)), 'surname' => trim(strval($surName)), 'email' => trim(strval($params['email'])), ); if( Loader::includeModule('grain.iikocard') && ($params['queryType']=='orderCreate' || $params['queryType']=='orderCheckCreate') ){ if(array_key_exists('couponCode',$params)) { if(!isset($order['loyaltyInfo'])) $order['loyaltyInfo'] = array(); $order['loyaltyInfo']['coupon'] = $params['couponCode']; if(!isset($order['externalData'])) $order['externalData'] = array(); $order['externalData'][] = array( 'key' => 'coupon', 'value' => $params['couponCode'], 'isPublic' => true, ); } if(array_key_exists('marketingCampaignId',$params)) { //if(!isset($order['loyaltyInfo'])) //$order['loyaltyInfo'] = array(); //$order['loyaltyInfo']['applicableManualConditions'] = $params['marketingCampaignId']; // but it doesn't seem to work (or manual conditions require not marketing campaign id, something other), so we store coupon in external order data, specify coupon in loyaltyInfo and use base price } } if($params['queryType']=='orderCreate') { if(!isset($order['externalData'])) $order['externalData'] = array(); $order['externalData'][] = array( 'key' => 'bitrixOrderId', 'value' => $params['orderId'], 'isPublic' => true, ); $order['externalData'][] = array( 'key' => 'bitrixOrderAccountNumber', 'value' => $params['orderAccountNumber'], 'isPublic' => true, ); } $orderPropTimeMin = intval(Option::get('grain.iiko','order_prop_time_min')); $transportToFrontTimeout = isset($params['secondsToCompleteBefore'])?($params['secondsToCompleteBefore']-($orderPropTimeMin+5)*60):86400; if($transportToFrontTimeout<60) $transportToFrontTimeout = 60; $body = array ( 'organizationId' => $params['organizationId'], 'createOrderSettings' => array( 'transportToFrontTimeout' => $transportToFrontTimeout, // for night orders, we need big values, but if completeBefore specified, it cannot be larger than it ), 'order' => $order, ); if($params['queryType']=='loyaltyCalculate' && array_key_exists('couponCode',$params)) $body['coupon'] = $params['couponCode']; if($params['terminalGroupId']) $body['terminalGroupId'] = $params['terminalGroupId']; return $body; } public static function checkAccess($account) { $connection = @fsockopen($account['HOST'], $account['PORT']); if (is_resource($connection)) { fclose($connection); return true; } else { return false; } } }