restClient = $restClient;
$this->mailer = $mailer;
/*if (App::environment() == 'production') {
$this->login = 'extranetwork-xml-100378788';
$this->password = 'Burhan21';
} else {
$this->login = 'ExtranetWorktest-xml-100378347';
$this->password = 'ifXHs3EJc-$>';
}*/
$this->login = null;
$this->password = null;
$this->url = 'https://api.mirai.com/XMLIntegrationx/';
$this->channelManagerId = 7;
$this->channelManagerName = 'Mirai';
$this->channelManagerPropertyMappingService = $channelManagerPropertyMappingService;
$this->channelManagerMappingService = $channelManagerMappingService;
}
public function setupUser($login, $password)
{
$this->login = $login;
$this->password = $password;
}
public function request($type, $method, $xmlPayload)
{
$response = ['status' => false, 'message' => ''];
try {
$this->restClient = new Client(['http_errors' => false]);
$formAuthParam = [
'login' => $this->login,
'password' => $this->password,
];
if(empty($this->login)) {
throw new ApiErrorException('Login data not set!');
}
$parameter = [
'headers' => [],
'query' => $formAuthParam,
];
if (!empty($xmlPayload)) {
$parameter['body'] = $xmlPayload;
}
//Log::debug($xmlPayload);
$request = $this->restClient->request($type, $this->url . $method, $parameter);
$getResponseBody = $request->getBody();
$getResponse = $getResponseBody->getContents();
$getResponseXml = new \SimpleXMLElement($getResponse);
$getResponse = json_decode(json_encode($getResponseXml), 1);
if (isset($getResponse['message']['error'])) {
$errorMessage = is_array($getResponse['message']['error']['comment']) ? implode(',', $getResponse['message']['error']['comment']) : $getResponse['message']['error']['comment'];
throw new ApiErrorException($errorMessage);
}
if ($request->getStatusCode() != 200) {
if($request->getReasonPhrase()) {
$firstError = $request->getReasonPhrase();
} else {
$firstError = is_array($getResponse['message']['error']['comment']) ? implode(',', $getResponse['message']['error']['comment']) : $getResponse['message']['error']['comment'];
}
/*Log::debug('__REQ__');
Log::debug($this->url);
Log::debug($type);
Log::debug($method);
Log::debug($xmlPayload);
Log::debug('__RES__');
Log::debug($getResponse);
Log::debug(PHP_EOL); */
throw new ApiErrorException($firstError);
}
$response = ["status" => true, 'message' => '', "data" => fillOnUndefined($getResponse, 'data', [])];
} catch (ApiErrorException $e) {
$response['message'] = $e->getMessage();
} catch (Exception $e) {
$message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
Log::error($message);
$response['message'] = $e->getMessage();
}
return $response;
}
public function inventoryRoomRateUpdate($method, $xmlPayload)
{
$response = ['status' => false, 'message' => ''];
try {
$request = $this->request('POST', $method, $xmlPayload);
if (!$request['status']) {
throw new ApiErrorException($request['message']);
}
$response = [
'status' => true,
'data' => null
];
} catch (ApiErrorException $e) {
$response['message'] = implode(', ', $e->getMessageArr());
} catch (Exception $e) {
$message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
Log::error($message);
$response['message'] = $e->getMessage();
}
return $response;
}
/** Pattern Functions */
/*
public function availabilityUpdateRequestMultiParam($propertyId, $propertyRoomRateMapping, $params)
{
$idList = [];
$requestParam['values'] = [];
ksort($params);
$dataByRoomDate = [];
$propertyRoomRateMappingCollect = collect($propertyRoomRateMapping);
foreach ($params as $currentDate => $rooms) {
foreach ($rooms as $roomId => $value) {
$uniqueKey = $value['availability'];
$roomIdManipulate = $roomId;
$roomIdManipulateCheck = $propertyRoomRateMappingCollect->where('channel_manager_room_id', $roomId)->where('property_room_rate_mapping.status', 1)->first();
if (!empty($roomIdManipulateCheck)) {
$roomIdManipulate = $roomIdManipulateCheck['channel_manager_room_rate_id'];
}
$dataByRoomDate[$roomIdManipulate][$uniqueKey][$currentDate] = $value;
}
}
$requestParam = [];
$valueKey = 0;
foreach ($dataByRoomDate as $roomId => $rooms) {
foreach ($rooms as $uniqueKey => $uniqueValues) {
$uniqueValueDate = array_keys($uniqueValues);
$dateKey = 0;
$dateGroup = [];
for ($i = 0; $i < count($uniqueValueDate); $i++) {
$dateGroup[$dateKey][] = $uniqueValueDate[$i];
if ($i + 1 < count($uniqueValueDate)) {
if (Carbon::parse($uniqueValueDate[$i])->diffInDays(Carbon::parse($uniqueValueDate[$i + 1])) > 1) {
$dateKey++;
}
}
}
foreach ($uniqueValues as $uniqueValue) {
$idList = array_merge($idList, $uniqueValue['idList']);
}
$currentValue = reset($uniqueValues);
foreach ($dateGroup as $dates) {
if (count($dates) > 1) {
$requestParam[$roomId]['data'][$valueKey] = [
'property_id' => $propertyId,
'room_type_id' => $roomId,
'date_from' => reset($dates),
'date_to' => last($dates),
'availability' => $currentValue['availability']
];
} else {
$requestParam[$roomId]['data'][$valueKey] = [
'property_id' => $propertyId,
'room_type_id' => $roomId,
'date_from' => reset($dates),
'date_to' => reset($dates),
'availability' => $currentValue['availability']
];
}
$valueKey++;
}
}
}
$xmlResponse = new \SimpleXMLElement('
' . print_r($roomAvailability, true) . '' ]; //$this->mailer->onQueue('logMail', new LogMail($mailParams)); //$logMessage $channelManagerPushedIds[] = $roomAvailability['id']; continue; } $channelManagerRoomRate = $channelManagerPropertyRoomRateMappingCollect->where('property_room_rate_mapping.room_id', $roomAvailability['property_room_id'])->first(); if (empty($channelManagerRoomRate)) { //None Mapping! //$channelManagerPushedIds[] = $roomAvailability['id']; $channelManagerNoneMappingIds[] = $roomAvailability['id']; continue; } $roomAvailabilityQueueForUpdateIdList[$roomAvailability['date']][] = $roomAvailability['id']; $roomAvailabilityQueueForUpdate[$roomAvailability['date']] [$channelManagerRoomRate['channel_manager_room_id']] = [ 'id' => $roomAvailability['id'], 'idList' => $roomAvailabilityQueueForUpdateIdList[$roomAvailability['date']], 'date' => $roomAvailability['date'], 'availability' => $roomAvailability['stop_sell'] == 1 ? 0 : $roomAvailability['availability'] ]; } $roomAvailabilityUpdateRequestMultiParam = $this->availabilityUpdateRequestMultiParam($channelManagerPropertyMapping['channel_manager_property_id'], $channelManagerPropertyMapping['channel_manager_room_rate'], $roomAvailabilityQueueForUpdate); $request = $this->inventoryRoomRateUpdate('webservice_updater.apro', $roomAvailabilityUpdateRequestMultiParam['payload']); if ($request['status']) { $channelManagerPushedIds = array_merge($channelManagerPushedIds, $roomAvailabilityUpdateRequestMultiParam['idList']); $channelManagerPushConfirmCode[] = $request['data']['confirmId']; } else { //$logMessage $mailParams = [ 'title' => $this->channelManagerName . ' - RoomAvailabilityPushService Error - InventoryRoomRateAvailabilityUpdate Error', 'logMessage' => '
' . print_r(array_merge($request, $roomAvailabilityUpdateRequestMultiParam), true) . '' ]; $this->mailer->onQueue('logMail', new LogMail($mailParams)); //$logMessage throw new ApiErrorException($request['message']); } } catch (ApiErrorException $e) { $message = $this->channelManagerName . ' - ' . $e->getFile() . " " . $e->getLine() . " " . implode(', ', $e->getMessageArr()); Log::error($message); //$response['message'] = $this->channelManagerName . ' - ' . implode(', ', $e->getMessageArr()); } catch (Exception $e) { $message = $this->channelManagerName . ' - ' . $e->getFile() . " " . $e->getLine() . " " . $e->getMessage(); Log::error($message); //$response['message'] = $e->getMessage(); } } else { $channelManagerNoneMappingIds = collect($bulkQueueData)->pluck('id')->toArray(); } $channelManagerPushedIds = array_unique($channelManagerPushedIds); asort($channelManagerPushedIds); $channelManagerPushedIds = array_values($channelManagerPushedIds); $response = [ 'status' => true, 'data' => [ 'confirmCode' => $channelManagerPushConfirmCode, 'channelManagerPushedIds' => $channelManagerPushedIds, 'channelManagerNoneMappingIds' => $channelManagerNoneMappingIds ] ]; return $response; } public function roomRateUpdateRequestMultiParam($propertyId, $params) { $idList = []; ksort($params); $dataByRoomRate = []; foreach ($params as $currentDate => $rooms) { foreach ($rooms as $roomId => $rates) { foreach ($rates as $rateId => $value) { $uniqueKey = $value['amount'] . '|' . $value['stop_sell'] . '|' . $value['min_stay']; $dataByRoomRate[$rateId][$uniqueKey][$currentDate] = $value; } } } //Log::debug(json_encode($dataByRoomRate)); $requestParam = []; $valueKey = 0; foreach ($dataByRoomRate as $rateId => $rates) { $valueKey = 0; foreach ($rates as $uniqueKey => $uniqueValues) { $uniqueValueDate = array_keys($uniqueValues); $dateKey = 0; $dateGroup = []; for ($i = 0; $i < count($uniqueValueDate); $i++) { $dateGroup[$dateKey][] = $uniqueValueDate[$i]; if ($i + 1 < count($uniqueValueDate)) { if (Carbon::parse($uniqueValueDate[$i])->diffInDays(Carbon::parse($uniqueValueDate[$i + 1])) > 1) { $dateKey++; } } } foreach ($uniqueValues as $uniqueValue) { $idList = array_merge($idList, $uniqueValue['idList']); } $currentValue = reset($uniqueValues); foreach ($dateGroup as $dates) { $requestParam[$rateId][$valueKey] = [ 'property_id' => $propertyId, 'rate_plan_id' => $rateId, 'date_from' => reset($dates), 'date_to' => last($dates), 'amount' => moneyDoubleFormatDecimal($currentValue['amount']), 'stop_sell' => fillOnUndefined($currentValue, 'stop_sell', 0), 'min_stay' => fillOnUndefined($currentValue, 'min_stay', 1) != 0 ? $currentValue['min_stay'] : 1, 'currency' => $currentValue['currency'], ]; if ($requestParam[$rateId][$valueKey]['amount'] == 0) { $requestParam[$rateId][$valueKey]['stop_sell'] = 1; } $valueKey++; } } } $xmlResponse = new \SimpleXMLElement('
' . print_r($roomRatePrice, true) . '' ]; //$this->mailer->onQueue('logMail', new LogMail($mailParams)); //$logMessage $channelManagerPushedIds[] = $roomRatePrice['id']; continue; } $channelManagerRoomRate = $channelManagerPropertyRoomRateMappingCollect->where('property_room_rate_mapping_id', $roomRatePrice['room_rate_mapping_id'])->first(); if (empty($channelManagerRoomRate)) { //None Mapping! //$channelManagerPushedIds[] = $roomAvailability['id']; $channelManagerNoneMappingIds[] = $roomRatePrice['id']; //TODO: Burada oda eşleşmiyorsa süreç devam ediyor ama uyarı maili atılabilir. continue; } $roomRatePriceQueueForUpdateIdList [$channelManagerPropertyMapping['channel_manager_property_id']] [$roomRatePrice['date']][$channelManagerRoomRate['channel_manager_room_id']] [$channelManagerRoomRate['channel_manager_room_rate_id']][] = $roomRatePrice['id']; $roomRatePriceQueueForUpdate [$channelManagerPropertyMapping['channel_manager_property_id']] [$roomRatePrice['date']] [$channelManagerRoomRate['channel_manager_room_id']] [$channelManagerRoomRate['channel_manager_room_rate_id']] = [ 'id' => $roomRatePrice['id'], 'idList' => $roomRatePriceQueueForUpdateIdList[$channelManagerPropertyMapping['channel_manager_property_id']][$roomRatePrice['date']][$channelManagerRoomRate['channel_manager_room_id']][$channelManagerRoomRate['channel_manager_room_rate_id']], 'date' => $roomRatePrice['date'], 'amount' => $roomRatePrice['amount'], 'currency' => $roomRatePrice['currency'], 'stop_sell' => $roomRatePrice['stop_sell'], 'min_stay' => $roomRatePrice['min_stay'], ]; } foreach ($roomRatePriceQueueForUpdate as $channelManagerPropertyId => $channelManagerProperty) { if (empty($channelManagerProperty)) { throw new ApiErrorException('$channelManagerProperty is Empty!'); } $roomRateUpdateRequestMultiParam = $this->roomRateUpdateRequestMultiParam($channelManagerPropertyId, $channelManagerProperty); $request = $this->inventoryRoomRateUpdate('webservice_updater.apro', $roomRateUpdateRequestMultiParam['payload']); if ($request['status']) { $channelManagerPushedIds = array_merge($channelManagerPushedIds, $roomRateUpdateRequestMultiParam['idList']); $channelManagerPushConfirmCode = $request['data']['confirmId']; } else { //$logMessage $mailParams = [ 'title' => $this->channelManagerName . ' - RoomAvailabilityPushService Error - InventoryRoomRateUpdate Error', 'logMessage' => '
' . print_r($request, true) . '' ]; $this->mailer->onQueue('logMail', new LogMail($mailParams)); //$logMessage throw new ApiErrorException($request['message']); } } } $channelManagerPushedIds = array_unique($channelManagerPushedIds); asort($channelManagerPushedIds); $channelManagerPushedIds = array_values($channelManagerPushedIds); $response = [ 'status' => true, 'data' => [ 'confirmCode' => $channelManagerPushConfirmCode, 'channelManagerPushedIds' => $channelManagerPushedIds, 'channelManagerNoneMappingIds' => $channelManagerNoneMappingIds ] ]; } catch (ApiErrorException $e) { $response['message'] = $this->channelManagerName . ' - ' . implode(', ', $e->getMessageArr()); } catch (Exception $e) { $message = $this->channelManagerName . ' - ' . $e->getFile() . " " . $e->getLine() . " " . $e->getMessage(); Log::error($message); $response['message'] = $e->getMessage(); } return $response; } */ }