1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
requesthandler.php
См. документацию.
1<?php
2
3namespace Sale\Handlers\Delivery\YandexTaxi;
4
5use Bitrix\Main\Error;
6use Bitrix\Main\Localization\Loc;
7use Bitrix\Main\Type\DateTime;
8use Bitrix\Main\Web\Json;
9use Bitrix\Sale\Delivery\Requests\HandlerBase;
10use Bitrix\Sale\Delivery\Requests\Manager;
11use Bitrix\Sale\Delivery\Requests\Message;
12use Bitrix\Sale\Delivery\Requests\RequestResult;
13use Bitrix\Sale\Delivery\Requests\RequestTable;
14use Bitrix\Sale\Delivery\Requests\Result;
15use Bitrix\Sale\Delivery\Requests\ShipmentResult;
16use Bitrix\Sale\Delivery\Services\Base;
17use Bitrix\Sale\Repository\ShipmentRepository;
18use Sale\Handlers\Delivery\YandexTaxi\Api\Api;
19use Sale\Handlers\Delivery\YandexTaxi\Api\RequestEntity\Claim;
20use Sale\Handlers\Delivery\YandexTaxi\ClaimBuilder\ClaimBuilder;
21use Sale\Handlers\Delivery\YandexTaxi\EventJournal\JournalProcessor;
22use Sale\Handlers\Delivery\YandexTaxi\Internals\ClaimsTable;
23
28class RequestHandler extends HandlerBase
29{
31 private $api;
32
34 private $claimBuilder;
35
37 private $journalProcessor;
38
42 public function __construct(Base $deliveryService)
43 {
44 parent::__construct($deliveryService);
45
46 $this->api = ServiceContainer::getApi();
47 $this->claimBuilder = ServiceContainer::getClaimBuilder();
48 $this->journalProcessor = ServiceContainer::getJournalProcessor();
49 }
50
54 public function create(array $shipmentIds, array $additional = array())
55 {
56 $result = new Result();
57
58 $isShipmentError = false;
59 if (empty($shipmentIds) || count($shipmentIds) !== 1)
60 {
61 $isShipmentError = true;
62 }
63 else
64 {
65 $shipment = ShipmentRepository::getInstance()->getById((int)$shipmentIds[0]);
66 if (is_null($shipment))
67 {
68 $isShipmentError = true;
69 }
70 }
71 if ($isShipmentError)
72 {
73 return $result->addErrors([Loc::getMessage('SALE_YANDEX_TAXI_REQUEST_HANDLER_SHIPMENT_ERROR')]);
74 }
75
76 $claimBuildingResult = $this->claimBuilder->build($shipment);
77 if (!$claimBuildingResult->isSuccess())
78 {
79 return $result->addErrors($claimBuildingResult->getErrors());
80 }
81
83 $claim = $claimBuildingResult->getData()['RESULT'];
84
85 $taxiClass = $claim->getClientRequirements()->getTaxiClass();
86
87 if (ClaimBuilder::isOffersCalculateMethod($taxiClass))
88 {
89 $offerData = ServiceContainer::getRateCalculator()->calculateRate($shipment)->getData();
90
91 if (isset($offerData['offerPayload']))
92 {
93 $claim->setOfferPayload($offerData['offerPayload']);
94 }
95 }
96
97 $claimCreationResult = $this->api->createClaim($claim);
98
99 if (!$claimCreationResult->isSuccess())
100 {
101 return $result->addError(new Error(Loc::getMessage('SALE_YANDEX_TAXI_ORDER_CREATE_ERROR')));
102 }
103
104 $createdClaim = $claimCreationResult->getClaim();
105 if (is_null($createdClaim))
106 {
107 return $result->addError(new Error(Loc::getMessage('SALE_YANDEX_TAXI_ORDER_PERSIST_ERROR')));
108 }
109
110 $addResult = ClaimsTable::add([
111 'SHIPMENT_ID' => $shipment->getId(),
112 'CREATED_AT' => new DateTime(),
113 'UPDATED_AT' => new DateTime(),
114 'EXTERNAL_ID' => $createdClaim->getId(),
115 'EXTERNAL_STATUS' => $createdClaim->getStatus(),
116 'EXTERNAL_CREATED_TS' => $createdClaim->getCreatedTs(),
117 'EXTERNAL_UPDATED_TS' => $createdClaim->getUpdatedTs(),
118 'INITIAL_CLAIM' => Json::encode($createdClaim),
119 'IS_SANDBOX_ORDER' => $this->api->getTransport()->isTestEnvironment() ? 'Y' : 'N',
120 ]);
121 if (!$addResult->isSuccess())
122 {
123 return $result->addError(new Error(Loc::getMessage('SALE_YANDEX_TAXI_ORDER_PERSIST_ERROR')));
124 }
125
126 \CAgent::AddAgent(
127 $this->journalProcessor->getAgentName(
128 $this->deliveryService->getParentId()
129 ),
130 'sale',
131 'N',
132 30,
133 '',
134 'Y',
135 '',
136 100,
137 false,
138 false
139 );
140
141 $requestResult = new RequestResult();
142 $requestResult->setExternalId($createdClaim->getId());
143 $requestResult->addResult(new ShipmentResult($shipment->getId()));
144 $result->addResult($requestResult);
145 $result->setData([
146 'STATUS' => Loc::getMessage('SALE_YANDEX_TAXI_REQUEST_HANDLER_STATUS_SEARCHING_PERFORMER_DESCRIPTION'),
147 'STATUS_SEMANTIC' => Manager::EXTERNAL_STATUS_SEMANTIC_PROCESS,
148 ]);
149
150 return $result;
151 }
152
156 public function getActions($requestId)
157 {
158 return [
159 $this->getCancelActionCode() => $this->getCancelActionName()
160 ];
161 }
162
166 public function executeAction($requestId, $actionType, array $additional)
167 {
168 if ($actionType === $this->getCancelActionCode())
169 {
170 return $this->cancelRequest($requestId);
171 }
172
173 return parent::executeAction($requestId, $actionType, $additional);
174 }
175
180 public function cancelRequest($requestId): Result
181 {
182 $result = new Result();
183
184 $request = RequestTable::getById($requestId)->fetch();
185 if (!$request)
186 {
187 return $result->addError(
188 new Error(Loc::getMessage('SALE_YANDEX_TAXI_REQUEST_HANDLER_REQUEST_NOT_FOUND'))
189 );
190 }
191
192 $getClaimResult = $this->api->getClaim($request['EXTERNAL_ID']);
193 if (!$getClaimResult->isSuccess())
194 {
195 return $result->addErrors($getClaimResult->getErrors());
196 }
197
198 $claim = $getClaimResult->getClaim();
199 if (is_null($claim))
200 {
201 return $result->addError(
202 new Error(Loc::getMessage('SALE_YANDEX_TAXI_CANCELLATION_TMP_ERROR'))
203 );
204 }
205
206 $getCancelInfoResult = $this->api->getCancelInfo($request['EXTERNAL_ID']);
207 if (!$getCancelInfoResult->isSuccess())
208 {
209 return $result->addErrors($getCancelInfoResult->getErrors());
210 }
211 $availableCancelState = $getCancelInfoResult->getCancelState();
212
213 $cancellationResult = ServiceContainer::getApi()->cancelClaim(
214 $request['EXTERNAL_ID'],
215 $claim->getVersion(),
216 $availableCancelState
217 );
218
219 if (!$cancellationResult->isSuccess())
220 {
221 return $result->addError(
222 new Error(Loc::getMessage('SALE_YANDEX_TAXI_CANCELLATION_FATAL_ERROR'))
223 );
224 }
225
226 if ($availableCancelState === 'paid')
227 {
228 $result->addMessage(
229 new Message(Loc::getMessage(
230 'SALE_YANDEX_TAXI_DELIVERY_PAID_CANCELLATION',
231 [
232 '#SERVICE_NAME#' =>
233 $this->deliveryService->getParentService()
234 ? $this->deliveryService->getParentService()->getName()
235 : $this->deliveryService->getName()
236 ,
237 ]
238 ))
239 );
240 }
241 else
242 {
243 $result->addMessage(
244 new Message(
245 Loc::getMessage('SALE_YANDEX_TAXI_DELIVERY_FREE_CANCELLATION')
246 )
247 );
248 }
249
250 return $result;
251 }
252
256 public function delete($requestId)
257 {
258 return new Result();
259 }
260
264 public function hasCallbackTrackingSupport(): bool
265 {
266 return true;
267 }
268}
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
create(array $shipmentIds, array $additional=[])
Определения handlerbase.php:38
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
trait Error
Определения error.php:11
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936