Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ebay.php
1<?php
2
4
8
9Loc::loadMessages(__FILE__);
10
11class Ebay extends Platform
12{
13 //todo: check if token for sftp is expired
14
15 const TRADING_PLATFORM_CODE = "ebay";
16 const API_URL = "https://api.ebay.com/ws/api.dll";
17
22 public static function getInstance()
23 {
24 return parent::getInstanceByCode(self::TRADING_PLATFORM_CODE);
25 }
26
27 public static function getSftpTokenUrl($accountName)
28 {
29 return "http://".self::getServiceHost()."/buy_tmp/ebay/".
30 "?action=OAUTH_AUTH".
31 "&LICENCE_HASH=".self::getLicenseHash().
32 "&ACCOUNT_NAME=".htmlspecialcharsbx($accountName).
33 "&BACK_URL=".urlencode((\CMain::IsHTTPS() ? "https://" : "http://").$_SERVER['HTTP_HOST']);
34 }
35
36 public static function getApiTokenUrl()
37 {
38 return "http://".self::getServiceHost()."/buy_tmp/ebay/".
39 "?action=GET_AUTH_URL&LICENCE_HASH=".self::getLicenseHash().
40 "&BACK_URL=".urlencode((\CMain::IsHTTPS() ? "https://" : "http://").$_SERVER['HTTP_HOST']);
41 }
42
43 protected static function getLicenseHash()
44 {
45 return md5(defined("LICENSE_KEY") ? LICENSE_KEY : "DEMO");
46 }
47
48 protected static function getServiceHost()
49 {
50 return defined('SALE_EBAY_SERVICE_HOST') ? SALE_EBAY_SERVICE_HOST : 'www.1c-bitrix.ru';
51 }
52
58 public function setActive()
59 {
60 if($this->isActive())
61 return true;
62
63 RegisterModuleDependences("sale", "OnSaleDeductOrder", "sale", '\Bitrix\Sale\TradingPlatform\Ebay\Helper', "onSaleDeductOrder", 100);
64 return parent::setActive();
65 }
66
71 public function unsetActive()
72 {
73 if(!$this->isActive())
74 return true;
75
76 UnRegisterModuleDependences("sale", "OnSaleDeductOrder", "sale", '\Bitrix\Sale\TradingPlatform\Ebay\Helper', "onSaleDeductOrder", 100);
77 return parent::unsetActive();
78 }
79
84 public function install()
85 {
86 RegisterModuleDependences('main', 'OnEventLogGetAuditTypes', 'sale', '\Bitrix\Sale\TradingPlatform\Ebay\Helper', 'OnEventLogGetAuditTypes');
87
88 $tptAddRes = \Bitrix\Sale\TradingPlatformTable::add(array(
89 "CODE" => $this->getCode(),
90 "ACTIVE" => "N",
91 "NAME" => Loc::getMessage("SALE_EBAY_NAME"),
92 "DESCRIPTION" => Loc::getMessage("SALE_EBAY_DESCRIPTION"),
93 "CATALOG_SECTION_TAB_CLASS_NAME" => '\Bitrix\Sale\TradingPlatform\Ebay\CatalogSectionTabHandler',
94 "CLASS" => '\Bitrix\Sale\TradingPlatform\Ebay\Ebay'
95 ));
96
97 $ebay = Ebay::getInstance();
98 $catMapEntRes = \Bitrix\Sale\TradingPlatform\MapEntityTable::add(array(
99 "TRADING_PLATFORM_ID" => $ebay->getId(),
100 "CODE" => "CATEGORY"
101 ));
102
103 $eventRes = Helper::installEvents();
105
106 return $tptAddRes->isSuccess()
107 && $catMapEntRes->isSuccess()
108 && $eventRes
109 && $fsRes;
110 }
111
119 public function sendErrorMail($type, $details, $siteId)
120 {
121 if(!isset($this->settings[$siteId]["EMAIL_ERRORS"]) || $this->settings[$siteId]["EMAIL_ERRORS"] == '')
122 return false;
123
124 $loggerTypes = Helper::OnEventLogGetAuditTypes();
125 $errorType = isset($loggerTypes[$type]) ? $loggerTypes[$type] : $type;
126
127 $fields = array(
128 "EMAIL_TO" => $this->settings[$siteId]["EMAIL_ERRORS"],
129 "ERROR_TYPE" => $errorType,
130 "ERROR_DETAILS" => $details
131 );
132
133 $event = new \CEvent;
134
135 return $event->Send("SALE_EBAY_ERROR", $siteId, $fields, "N");
136 }
137
147 public static function log($level, $type, $itemId, $description, $siteId)
148 {
149 static $ebay = null;
150
151 if($ebay === null)
152 {
153 $ebay = self::getInstance();
154 $settings = $ebay->getSettings();
155
156 if(isset($settings[$siteId]["LOG_LEVEL"]))
157 {
158 $logLevel = $settings[$siteId]["LOG_LEVEL"];
159 $ebay->logger->setLevel($logLevel);
160 }
161 }
162
163 if($level == Logger::LOG_LEVEL_ERROR)
164 $ebay->sendErrorMail($type, $description, $siteId);
165
166 return $ebay->addLogRecord($level, $type, $itemId, $description);
167 }
168
169 public static function onAfterUpdateShipment(\Bitrix\Main\Event $event, array $additional)
170 {
171 $data = array();
172 $ebay = self::getInstance();
173 $settings = $ebay->getSettings();
174 $deliveryName = "Other";
175
176 if(
177 !empty($settings[$additional["SITE_ID"]]["MAPS"]["SHIPMENT"])
178 && is_array($settings[$additional["SITE_ID"]]["MAPS"]["SHIPMENT"])
179 )
180 {
181 $map = array_flip($settings[$additional["SITE_ID"]]["MAPS"]["SHIPMENT"]);
182
183 if(isset($map[$additional['DELIVERY_ID']]))
184 {
185 $deliveryName = $map[$additional['DELIVERY_ID']];
186
187 if(mb_substr($deliveryName, 0, 3) == "RU_")
188 $deliveryName = mb_substr($deliveryName, 3);
189 }
190 }
191
192 if(
193 !empty($additional["PARAMS"]["ORDER_LINES"])
194 && is_array($additional["PARAMS"]["ORDER_LINES"])
195 && !empty($additional["PARAMS"]["ORDER_ID"])
196 )
197 {
198 foreach($additional["PARAMS"]["ORDER_LINES"] as $lineId)
199 {
200 $data[] = array(
201 "ORDER_ID" => $additional["PARAMS"]["ORDER_ID"],
202 "ORDER_LINE_ITEM_ID" => $lineId,
203 "DELIVERY_NAME" => $deliveryName,
204 "TRACKING_NUMBER" => $additional['TRACKING_NUMBER']
205 );
206 }
207 }
208
209 if(!empty($data))
210 {
211 $ebayFeed = \Bitrix\Sale\TradingPlatform\Ebay\Feed\Manager::createFeed("SHIPMENT", $additional["SITE_ID"]);
212 $ebayFeed->setSourceData(array($data));
213 $ebayFeed->processData();
214 \Bitrix\Sale\TradingPlatform\Ebay\Agent::add('SHIPMENT', $additional["SITE_ID"], 1, true);
215 }
216 }
217
218 public static function getApiUrl()
219 {
220 return static::API_URL;
221 }
222}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static onAfterUpdateShipment(\Bitrix\Main\Event $event, array $additional)
Definition ebay.php:169
sendErrorMail($type, $details, $siteId)
Definition ebay.php:119
static getSftpTokenUrl($accountName)
Definition ebay.php:27
static log($level, $type, $itemId, $description, $siteId)
Definition ebay.php:147