27 if(!isset($params[
"SITE_ID"]) || $params[
"SITE_ID"] ==
'')
30 $this->siteId = $params[
"SITE_ID"];
38 foreach($data as $order)
42 if(intval($this->processOrder($order)) > 0)
46 "EBAY_DATA_PROCESSOR_ORDER_PROCESSED",
49 "SALE_TP_EBAY_FDPO_ORDER_PROCESSED",
50 array(
"#ORDER_ID" => $order[
"OrderID"])
59 "EBAY_DATA_PROCESSOR_ORDER_ERROR",
62 "SALE_TP_EBAY_FDPO_ORDER_ERROR",
63 array(
"#ORDER_ID" => $order[
"OrderID"])
73 "EBAY_DATA_PROCESSOR_ORDER_ERROR",
76 "SALE_TP_EBAY_FDPO_ORDER_ERROR",
77 array(
"#ORDER_ID" => $order[
"OrderID"])
78 ).
".".$e->getMessage(),
84 \Bitrix\Sale\TradingPlatform\Ebay\Agent::add(
'ORDER_ACK', $this->siteId, 1,
true);
91 $sku = explode(
"_", $ebaySku);
93 if(isset($sku[1]) && $sku[1] <>
'')
103 $sku = explode(
"_", $ebaySku);
105 if(isset($sku[2]) && $sku[2] <>
'')
113 foreach($transactArray[
"Transaction"] as $key => $transaction)
115 if(intval($key) !== $key)
116 $transactArray[
"Transaction"] = array($transactArray[
"Transaction"]);
121 return $transactArray[
"Transaction"];
124 public function processOrder($orderEbay)
126 Ebay::log(
Logger::LOG_LEVEL_DEBUG,
"EBAY_DATA_PROCESSOR_ORDER_PROCESSING", $orderEbay[
"ExtendedOrderID"], print_r($orderEbay,
true), $this->siteId);
132 if($orderEbay[
"OrderStatus"]!=
"Completed"
133 || !isset($orderEbay[
"CheckoutStatus"][
"eBayPaymentStatus"])
134 || $orderEbay[
"CheckoutStatus"][
"eBayPaymentStatus"] !=
"NoPaymentFailure"
140 "EBAY_DATA_PROCESSOR_ORDER_SKIPPED",
141 $orderEbay[
"ExtendedOrderID"],
143 "SALE_TP_EBAY_FDPO_ORDER_SKIPPED",
144 array(
"#ORDER_ID#" => $orderEbay[
"ExtendedOrderID"])
151 $ebay = \Bitrix\Sale\TradingPlatform\Ebay\Ebay::getInstance();
152 $settings = $ebay->getSettings();
154 if(!isset($settings[$this->siteId][
"ORDER_PROPS"]) || !is_array($settings[$this->siteId][
"ORDER_PROPS"]))
157 $propsMap = $settings[$this->siteId][
"ORDER_PROPS"];
161 "TRADING_PLATFORM_ID" => $ebay->getId(),
162 "EXTERNAL_ORDER_ID" => $orderEbay[
"ExtendedOrderID"]
166 if($orderCorrespondence = $dbRes->fetch())
170 "EBAY_DATA_PROCESSOR_ORDER_ALREADY_EXIST",
171 $orderEbay[
"ExtendedOrderID"],
173 "SALE_TP_EBAY_FDPO_ORDER_SKIPPED_EXIST",
174 array(
"#ORDER_ID#" => $orderEbay[
"ExtendedOrderID"])
184 $orderClass = $registry->getOrderClassName();
187 $order = $orderClass::create($this->siteId);
188 $order->setPersonTypeId($settings[$this->siteId][
"PERSON_TYPE"]);
189 $propsCollection = $order->getPropertyCollection();
192 if(intval($propsMap[
"FIO"]) > 0)
194 $prop = $propsCollection->getItemByOrderPropertyId($propsMap[
"FIO"]);
195 $prop->setValue($orderEbay[
"ShippingAddress"][
"Name"]);
198 if(intval($propsMap[
"CITY"]) > 0)
200 $prop = $propsCollection->getItemByOrderPropertyId($propsMap[
"CITY"]);
201 $prop->setValue($orderEbay[
"ShippingAddress"][
"CityName"]);
204 if(intval($propsMap[
"PHONE"]) > 0)
206 $prop = $propsCollection->getItemByOrderPropertyId($propsMap[
"PHONE"]);
207 $prop->setValue($orderEbay[
"ShippingAddress"][
"Phone"]);
210 if(intval($propsMap[
"ZIP"]) > 0)
212 $prop = $propsCollection->getItemByOrderPropertyId($propsMap[
"ZIP"]);
213 $prop->setValue($orderEbay[
"ShippingAddress"][
"PostalCode"]);
216 if(intval($propsMap[
"ADDRESS"]) > 0)
218 $prop = $propsCollection->getItemByOrderPropertyId($propsMap[
"ADDRESS"]);
220 $orderEbay[
"ShippingAddress"][
"CountryName"].
" ".
221 $orderEbay[
"ShippingAddress"][
"CityName"].
" ".
222 $orderEbay[
"ShippingAddress"][
"Street1"].
" ".
223 (!empty($orderEbay[
"ShippingAddress"][
"Street2"]) ? $orderEbay[
"ShippingAddress"][
"Street2"].
" " :
"")
230 $orderLineItemsIds = array();
231 $transactionsArray = $this->normalizeTransactionsArray($orderEbay[
"TransactionArray"]);
233 foreach($transactionsArray as $transaction)
238 if(intval($propsMap[
"EMAIL"]) > 0 && !empty($transaction[
"Buyer"][
"Email"]))
240 $prop = $propsCollection->getItemByOrderPropertyId($propsMap[
"EMAIL"]);
241 $prop->setValue($transaction[
"Buyer"][
"Email"]);
243 $userId = $this->createUser(
244 $transaction[
"Buyer"][
"Email"],
246 "NAME" => $transaction[
"Buyer"][
"UserFirstName"],
247 "LAST_NAME" => $transaction[
"Buyer"][
"UserLastName"]
253 $userId = \CSaleUser::GetAnonymousUserID();
256 if(intval($userId > 0))
257 $order->setFieldNoDemand(
"USER_ID", $userId);
261 if ($order->getUserId() > 0)
267 $basketClass = $registry->getBasketClassName();
268 $basket = $basketClass::create($this->siteId);
269 $basket->setFUserId($fUserId);
273 $isVariation =
false;
275 if(!empty($transaction[
"Item"]))
279 elseif(!empty($transaction[
"Variation"]))
287 Ebay::log(
Logger::LOG_LEVEL_INFO,
"EBAY_DATA_PROCESSOR_ORDER_PROCESSING_TRANSACTION_ITEM_NOT_FOUND", $transaction[
"OrderLineItemID"], print_r($transaction,
true), $this->siteId);
291 foreach($items as $transactionItem)
293 $ebaySku = $isVariation ? $this->
getSkuVariation($transactionItem[
"SKU"]) : $this->
getSku($transactionItem[
"SKU"]);
297 Ebay::log(
Logger::LOG_LEVEL_INFO,
"EBAY_DATA_PROCESSOR_ORDER_PROCESSING_TRANSACTION_ITEM_SKU_NOT_FOUND", $transaction[
"OrderLineItemID"], print_r($transaction,
true), $this->siteId);
303 "PRODUCT_ID" => $ebaySku,
304 "QUANTITY" => floatval($transaction[
"QuantityPurchased"]),
305 "CUSTOM_PRICE" =>
"Y",
306 "PRICE" => floatval($transaction[
"TransactionPrice"]),
307 "QUANTITY" => floatval($transaction[
"QuantityPurchased"]),
308 "NAME" => !empty($transactionItem[
"VariationTitle"]) ? $transactionItem[
"VariationTitle"] : $transactionItem[
"Title"],
312 'SITE_ID' => $this->siteId,
316 if ($order->getUserId() > 0)
318 $context[
'USER_ID'] = $order->getUserId();
322 $res = Catalog\Product\Basket::addProductToBasket($basket, $itemData, $context);
323 $resultData = $res->getData();
324 if (!empty($resultData[
'BASKET_ITEM']))
327 $item = $resultData[
'BASKET_ITEM'];
330 if (!$res->isSuccess())
337 $itemData[
'MODULE'] =
'';
338 $itemData[
'PRODUCT_PROVIDER_CLASS'] =
'';
340 $res = Catalog\Product\Basket::addProductToBasket($basket, $itemData, $context);
343 $res = $item->setFields($itemData);
345 if($res->isSuccess())
347 $orderLineItemsIds[] = $transaction[
"OrderLineItemID"];
351 foreach($res->getErrors() as $error)
352 Ebay::log(
Logger::LOG_LEVEL_ERROR,
"EBAY_DATA_PROCESSOR_ORDER_TRANSACTION_ITEM_CREATE_ERROR", $transaction[
"OrderLineItemID"], $error->getMessage(), $this->siteId);
357 $res = $order->setBasket($basket);
359 if(!$res->isSuccess())
360 foreach($res->getErrors() as $error)
361 Ebay::log(
Logger::LOG_LEVEL_ERROR,
"EBAY_DATA_PROCESSOR_ORDER_CREATE_ERROR_SET_BASKET", $orderEbay[
"ExtendedOrderID"], $error->getMessage(), $this->siteId);
364 if(intval($settings[$this->siteId][
"MAPS"][
"PAYMENT"][
"PayPal"]) > 0)
366 $payments = $order->getPaymentCollection();
369 if($payments->count() > 0)
371 foreach ($payments as $payment)
373 if($payment->isPaid())
374 $payment->setPaid(
"N");
380 $payment = $payments->createItem();
381 $payment->setField(
'PAY_SYSTEM_ID', $settings[$this->siteId][
"MAPS"][
"PAYMENT"][
"PayPal"]);
382 $payment->setField(
'PAY_SYSTEM_NAME',
"PayPal via Ebay");
384 if($orderEbay[
"CheckoutStatus"][
"eBayPaymentStatus"] ==
"NoPaymentFailure"
385 && $orderEbay[
"MonetaryDetails"][
"Payments"][
"Payment"][
"PaymentStatus"]
386 && $orderEbay[
"MonetaryDetails"][
"Payments"][
"Payment"][
"PaymentAmount"] == $orderEbay[
"Total"]
389 $payment->setField(
"SUM", $orderEbay[
"AmountPaid"]);
390 $payment->setPaid(
"Y");
395 if(intval($settings[$this->siteId][
"MAPS"][
"SHIPMENT"][$orderEbay[
"ShippingServiceSelected"][
"ShippingService"]]) > 0)
397 $shipments = $order->getShipmentCollection();
400 if($shipments->count() > 0)
401 foreach ($shipments as $shipment)
402 if(!$shipment->isSystem())
405 $shipment = $shipments->createItem();
406 $shipment->setField(
'DELIVERY_ID', $settings[$this->siteId][
"MAPS"][
"SHIPMENT"][$orderEbay[
"ShippingServiceSelected"][
"ShippingService"]]);
407 $shipment->setField(
'CUSTOM_PRICE_DELIVERY',
"Y");
408 $shipment->setField(
'BASE_PRICE_DELIVERY', $orderEbay[
"ShippingServiceSelected"][
"ShippingServiceCost"]);
409 $basket = $order->getBasket();
413 $shipmentItemCollection = $shipment->getShipmentItemCollection();
414 $basketItems = $basket->getBasketItems();
416 foreach ($basketItems as $basketItem)
418 $shipmentItem = $shipmentItemCollection->createItem($basketItem);
419 $shipmentItem->setQuantity($basketItem->getField(
'QUANTITY'));
423 $shipment->setField(
'BASE_PRICE_DELIVERY', $orderEbay[
"ShippingServiceSelected"][
"ShippingServiceCost"]);
429 "EBAY_DATA_PROCESSOR_ORDER_SHIPPING_ERROR",
430 $orderEbay[
"ExtendedOrderID"],
432 "SALE_TP_EBAY_FDPO_NOT_MAPPED_SHIPPING",
434 "#ORDER_ID#" => $orderEbay[
"ExtendedOrderID"],
435 "#EBAY_SHIPPING#" => $orderEbay[
"ShippingServiceSelected"][
"ShippingService"]
445 if($settings[$this->siteId][
"STATUS_MAP"][$orderEbay[
"OrderStatus"]] <>
'')
447 switch($settings[$this->siteId][
"STATUS_MAP"][$orderEbay[
"OrderStatus"]])
452 if(!$order->setField(
"CANCELED",
"Y"))
456 "EBAY_DATA_PROCESSOR_ORDER_CANCELING_ERROR",
457 $orderEbay[
"ExtendedOrderID"],
459 "SALE_TP_EBAY_FDPO_ORDER_CANCEL_ERROR",
461 "#ORDER_ID#" => $orderEbay[
"ExtendedOrderID"]
471 $payments = $order->getPaymentCollection();
473 foreach ($payments as $payment)
474 $payment->setPaid(
"Y");
478 case "ALLOW_DELIVERY":
480 $shipments = $order->getShipmentCollection();
482 foreach ($shipments as $shipment)
484 if(!$shipment->isSystem())
486 if(!$shipment->allowDelivery())
490 "EBAY_DATA_PROCESSOR_ORDER_ALLOW_DELIVERY_ERROR",
491 $orderEbay[
"ExtendedOrderID"],
493 "SALE_TP_EBAY_FDPO_ORDER_ALLOW_DELIVERY_ERROR",
495 "#ORDER_ID#" => $orderEbay[
"ExtendedOrderID"]
507 $shipments = $order->getShipmentCollection();
509 foreach ($shipments as $shipment)
511 if(!$shipment->isSystem())
513 if(!$shipment->setField(
'DEDUCTED',
'Y'))
517 "EBAY_DATA_PROCESSOR_ORDER_DEDUCTIOING_ERROR",
518 $orderEbay[
"ExtendedOrderID"],
520 "SALE_TP_EBAY_FDPO_ORDER_DEDUCT_ERROR",
521 array(
"#ORDER_ID#" => $orderEbay[
"ExtendedOrderID"])
533 $res = $order->setField(
"STATUS_ID", $settings[$this->siteId][
"STATUS_MAP"][$orderEbay[
"OrderStatus"]]);
536 if(!$res->isSuccess())
540 "EBAY_DATA_PROCESSOR_ORDER_CHANGE_STATUS_ERROR",
541 $orderEbay[
"OrderLineItemID"],
543 "SALE_TP_EBAY_FDPO_ORDER_SET_STATUS_ERROR",
545 "#ORDER_ID#" => $orderEbay[
"ExtendedOrderID"],
546 "#STATUS#" => $orderEbay[
"OrderStatus"]
555 $order->setField(
"PRICE", $orderEbay[
"Total"]);
556 $order->setField(
"XML_ID", Ebay::TRADING_PLATFORM_CODE.
"_".$orderEbay[
"ExtendedOrderID"]);
558 $tradeCollection = $order->getTradeBindingCollection();
561 $entity = TradeBindingEntity::create($tradeCollection);
563 "TRADING_PLATFORM_ID" => $ebay->getId(),
564 "EXTERNAL_ORDER_ID" => $orderEbay[
"ExtendedOrderID"],
566 "ORDER_LINES" => $orderLineItemsIds,
567 "ORDER_ID" => $orderEbay[
"OrderID"]
571 $tradeCollection->addItem($entity);
573 $res = $order->save();
575 if(!$res->isSuccess())
577 foreach($res->getErrors() as $error)
578 Ebay::log(
Logger::LOG_LEVEL_ERROR,
"EBAY_DATA_PROCESSOR_ORDER_SAVE_ERROR", $orderEbay[
"ExtendedOrderID"], print_r($error->getMessage(),
true), $this->siteId);
584 "EBAY_DATA_PROCESSOR_ORDER_CREATED",
587 "SALE_TP_EBAY_FDPO_ORDER_SAVED",
588 array(
"#ORDER_ID#" => $bitrixOrderId)
593 \CSaleMobileOrderPush::send(
"ORDER_CREATED", array(
"ORDER_ID" => $order->getId()));
597 if($bitrixOrderId > 0 && !empty($orderLineItemsIds))
599 $ebayFeed = \Bitrix\Sale\TradingPlatform\Ebay\Feed\Manager::createFeed(
"ORDER_ACK", $this->siteId);
600 $sourceData = array();
602 foreach($orderLineItemsIds as $id)
603 $sourceData[] = array(
"ORDER_ID" => $orderEbay[
"OrderID"],
"ORDER_LINE_ITEM_ID" => $id);
605 $ebayFeed->setSourceData(array($sourceData));
606 $ebayFeed->processData();
608 return $bitrixOrderId;
615 $userId = \CSaleUser::DoAutoRegisterUser(
625 foreach($errors as $val)
626 $errorMessage .= $val[
"TEXT"];