3namespace Sale\Handlers\Delivery\YandexTaxi\Api;
5use Bitrix\Main\Localization\Loc;
8use Bitrix\Main\Web\HttpClient;
9use Sale\Handlers\Delivery\YandexTaxi\Api\ApiResult\CancelInfoResult;
10use Sale\Handlers\Delivery\YandexTaxi\Api\ApiResult\Journal\EventBuilder;
11use Sale\Handlers\Delivery\YandexTaxi\Api\ApiResult\PhoneResult;
12use Sale\Handlers\Delivery\YandexTaxi\Api\ApiResult\PriceResult;
13use Sale\Handlers\Delivery\YandexTaxi\Api\ApiResult\SingleClaimResult;
14use Sale\Handlers\Delivery\YandexTaxi\Api\ApiResult\Tariff;
15use Sale\Handlers\Delivery\YandexTaxi\Api\ApiResult\TariffsResult;
16use Sale\Handlers\Delivery\YandexTaxi\Api\ClaimReader\ClaimReader;
17use Sale\Handlers\Delivery\YandexTaxi\Api\RequestEntity\Claim;
18use Sale\Handlers\Delivery\YandexTaxi\Api\RequestEntity\Estimation;
19use Sale\Handlers\Delivery\YandexTaxi\Api\RequestEntity\OfferEstimation;
20use Sale\Handlers\Delivery\YandexTaxi\Api\RequestEntity\TariffsOptions;
21use Sale\Handlers\Delivery\YandexTaxi\Api\Transport;
22use Sale\Handlers\Delivery\YandexTaxi\Common\Logger;
31 private const SINGLE_POINT_API_VERSION = 1;
32 private const MULTI_POINT_API_VERSION = 2;
34 private const LOG_SOURCE =
'api';
43 private $eventBuilder;
55 public function __construct(
56 Transport\Client $transport,
57 ClaimReader $claimReader,
58 EventBuilder $eventBuilder,
62 $this->transport = $transport;
63 $this->claimReader = $claimReader;
64 $this->eventBuilder = $eventBuilder;
65 $this->logger = $logger;
72 public function checkPrice(Estimation $estimation): PriceResult
79 self::SINGLE_POINT_API_VERSION,
80 HttpClient::HTTP_POST,
86 catch (Transport\Exception $requestException)
88 return $this->respondTransportError(
$result);
94 if ($statusCode !== 200)
96 $this->logger->log(static::LOG_SOURCE,
'check_price',
$response->toString());
97 return $this->respondStatusError(
$result, $statusCode,
'check_price');
101 !isset($body[
'price'])
102 || !isset($body[
'currency_rules'][
'code'])
103 || (
float)$body[
'price'] <= 0
106 return $result->addError(
new Error(Loc::getMessage(
'SALE_YANDEX_TAXI_RATE_CALCULATE_ERROR')));
110 ->setPrice((
float)$body[
'price'])
111 ->setCurrency((
string)$body[
'currency_rules'][
'code'])
114 if (isset($body[
'eta']))
116 $result->setEta((
int)$body[
'eta']);
126 public function offersCalculate(OfferEstimation $estimation): Result
133 self::MULTI_POINT_API_VERSION,
134 HttpClient::HTTP_POST,
140 catch (Transport\Exception $requestException)
142 return $this->respondTransportError(
$result);
148 if ($statusCode !== 200)
150 $this->logger->log(static::LOG_SOURCE,
'offers/calculate',
$response->toString());
151 return $this->respondStatusError(
$result, $statusCode,
'offers/calculate');
155 !isset($body[
'offers'])
156 || !is_array($body[
'offers'])
159 return $result->addError(
new Error(Loc::getMessage(
'SALE_YANDEX_TAXI_RATE_CALCULATE_ERROR')));
162 $maxOfferByPrice =
null;
164 foreach ($body[
'offers'] as $offer)
167 !isset($offer[
'price'][
'total_price_with_vat'])
168 || !isset($offer[
'price'][
'currency'])
169 || !isset($offer[
'payload'])
170 || (
float)$offer[
'price'][
'total_price_with_vat'] < 0
176 if (is_null($maxOfferByPrice))
178 $maxOfferByPrice = $offer;
183 if ((
float)$offer[
'price'][
'total_price_with_vat'] > (
float)$maxOfferByPrice[
'price'][
'total_price_with_vat'])
185 $maxOfferByPrice = $offer;
189 if (is_null($maxOfferByPrice))
191 return $result->addError(
new Error(Loc::getMessage(
'SALE_YANDEX_TAXI_RATE_CALCULATE_ERROR')));
195 ->setPrice((
float)$maxOfferByPrice[
'price'][
'total_price_with_vat'])
196 ->setCurrency((
string)$maxOfferByPrice[
'price'][
'currency'])
197 ->setData([
'offerPayload' => (
string)$maxOfferByPrice[
'payload']])
207 public function getTariffs(TariffsOptions $tariffsOptions): TariffsResult
214 self::SINGLE_POINT_API_VERSION,
215 HttpClient::HTTP_POST,
221 catch (Transport\Exception $requestException)
223 return $this->respondTransportError(
$result);
229 if ($statusCode !== 200)
231 $this->logger->log(static::LOG_SOURCE,
'tariffs',
$response->toString());
232 return $this->respondStatusError(
$result, $statusCode,
'tariffs');
235 if (isset($body[
'available_tariffs']) && is_array($body[
'available_tariffs']))
237 foreach ($body[
'available_tariffs'] as $tariff)
239 if (!isset($tariff[
'name']))
244 $tariffObject =
new Tariff($tariff[
'name']);
246 if (isset($tariff[
'supported_requirements']) && is_array($tariff[
'supported_requirements']))
248 foreach ($tariff[
'supported_requirements'] as $supportedRequirement)
250 if (!isset($supportedRequirement[
'name']))
255 if ($supportedRequirement[
'name'] ===
'cargo_options')
257 if (isset($supportedRequirement[
'options']) && is_array($supportedRequirement[
'options']))
259 foreach ($supportedRequirement[
'options'] as
$option)
263 $tariffObject->addSupportedRequirement(
$option[
'value']);
270 $tariffObject->addSupportedRequirement($supportedRequirement[
'name']);
275 $result->addTariff($tariffObject);
286 public function createClaim(Claim $claim): SingleClaimResult
288 $result =
new SingleClaimResult();
293 self::MULTI_POINT_API_VERSION,
294 HttpClient::HTTP_POST,
296 [
'request_id' => uniqid(
'',
true)],
300 catch (Transport\Exception $requestException)
302 return $this->respondTransportError(
$result);
308 if ($statusCode !== 200)
310 $this->logger->log(static::LOG_SOURCE,
'create_claim',
$response->toString());
311 return $this->respondStatusError(
$result, $statusCode,
'create');
314 $claimReadResult = $this->claimReader->readFromArray($body);
315 if ($claimReadResult->isSuccess())
317 return $result->setClaim($claimReadResult->getClaim());
328 public function acceptClaim(
string $claimId,
int $version): Result
335 self::SINGLE_POINT_API_VERSION,
336 HttpClient::HTTP_POST,
338 [
'claim_id' => $claimId],
339 [
'version' => $version]
342 catch (Transport\Exception $requestException)
344 return $this->respondTransportError(
$result);
349 if ($statusCode !== 200)
351 $this->logger->log(static::LOG_SOURCE,
'accept_claim',
$response->toString());
352 return $this->respondStatusError(
$result, $statusCode,
'accept');
364 public function cancelClaim(
string $claimId,
int $version,
string $cancelState): Result
371 self::SINGLE_POINT_API_VERSION,
372 HttpClient::HTTP_POST,
374 [
'claim_id' => $claimId],
376 'version' => $version,
377 'cancel_state' => $cancelState,
381 catch (Transport\Exception $requestException)
383 return $this->respondTransportError(
$result);
388 if ($statusCode !== 200)
390 $this->logger->log(static::LOG_SOURCE,
'cancel_claim',
$response->toString());
391 return $this->respondStatusError(
$result, $statusCode,
'cancel');
401 public function getCancelInfo(
string $claimId): CancelInfoResult
403 $result =
new CancelInfoResult();
408 self::MULTI_POINT_API_VERSION,
409 HttpClient::HTTP_POST,
410 'claims/cancel-info',
411 [
'claim_id' => $claimId]
414 catch (Transport\Exception $requestException)
416 return $this->respondTransportError(
$result);
422 if ($statusCode !== 200)
424 $this->logger->log(static::LOG_SOURCE,
'cancel_info',
$response->toString());
425 return $this->respondStatusError(
$result, $statusCode,
'cancel_info');
428 if (empty($body[
'cancel_state']))
430 return $result->addError(
new Error(Loc::getMessage(
'SALE_YANDEX_TAXI_CANCELLATION_FATAL_ERROR')));
433 return $result->setCancelState($body[
'cancel_state']);
440 public function getClaim(
string $claimId): SingleClaimResult
442 $result =
new SingleClaimResult();
447 self::MULTI_POINT_API_VERSION,
448 HttpClient::HTTP_POST,
450 [
'claim_id' => $claimId]
453 catch (Transport\Exception $requestException)
455 return $this->respondTransportError(
$result);
461 if ($statusCode !== 200)
463 $this->logger->log(static::LOG_SOURCE,
'get_claim',
$response->toString());
464 return $this->respondStatusError(
$result, $statusCode,
'info');
467 $claimReadResult = $this->claimReader->readFromArray($body);
468 if ($claimReadResult->isSuccess())
470 return $result->setClaim($claimReadResult->getClaim());
480 public function getPhone(
string $claimId): PhoneResult
487 self::MULTI_POINT_API_VERSION,
488 HttpClient::HTTP_POST,
489 'driver-voiceforwarding',
491 [
'claim_id' => $claimId]
494 catch (Transport\Exception $requestException)
496 return $this->respondTransportError(
$result);
502 if ($statusCode !== 200)
504 $this->logger->log(static::LOG_SOURCE,
'get_phone',
$response->toString());
505 return $this->respondStatusError(
$result, $statusCode,
'driver-voiceforwarding');
508 if (isset($body[
'phone']))
510 $result->setPhone($body[
'phone']);
512 if (isset($body[
'ext']))
516 if (isset($body[
'ttl_seconds']))
518 $result->setTtlSeconds($body[
'ttl_seconds']);
528 public function getJournalRecords($cursor): ApiResult\Journal\Result
530 $result =
new ApiResult\Journal\Result();
535 self::MULTI_POINT_API_VERSION,
536 HttpClient::HTTP_POST,
539 is_null($cursor) ?
new \stdClass() : [
'cursor' => $cursor]
542 catch (Transport\Exception $requestException)
544 return $this->respondTransportError(
$result);
550 if ($statusCode !== 200)
552 $this->logger->log(static::LOG_SOURCE,
'get_journal_records_1',
$response->toString());
553 return $this->respondStatusError(
$result, $statusCode,
'journal');
556 if (!is_array($body) || !isset($body[
'cursor']) || !isset($body[
'events']))
558 $this->logger->log(static::LOG_SOURCE,
'get_journal_records_2',
$response->toString());
560 return $result->addError(
new Error(Loc::getMessage(
'SALE_YANDEX_TAXI_NETWORK_ERROR_UNEXPECTED_ERROR')));
563 $result->setCursor($body[
'cursor']);
565 if (is_array($body[
'events']))
567 foreach ($body[
'events'] as
$event)
588 private function respondStatusError(
$result,
int $statusCode,
string $method)
590 $error = Loc::getMessage(
'SALE_YANDEX_TAXI_NETWORK_ERROR_UNEXPECTED_ERROR');
592 if ($statusCode == 401)
594 $error = Loc::getMessage(
'SALE_YANDEX_TAXI_AUTHENTICATION_ERROR');
596 elseif ($statusCode == 404)
598 $error = Loc::getMessage(
'SALE_YANDEX_TAXI_ORDER_NOT_FOUND_ERROR');
600 elseif ($statusCode == 409)
602 $error = Loc::getMessage(
'SALE_YANDEX_TAXI_OPERATION_REJECTED');
612 private function respondTransportError(
$result)
614 return $result->addError(
new Error(Loc::getMessage(
'SALE_YANDEX_TAXI_NETWORK_ERROR')));
620 public function getTransport(): Transport\Client
622 return $this->transport;
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)