Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
policy.php
1<?php
2/* NOT FOR RELEASE*/
4
12
13class Policy
14{
15 protected $http;
16 protected $authToken = "";
17 protected $siteId = "";
18
19 const TYPE_RETURN = 0;
20 const TYPE_PAYMENT = 1;
21 const TYPE_SHIPPING = 2;
22 const URL = "https://svcs.ebay.com/services/selling/v1/SellerProfilesManagementService";
23
24 public function __construct($authToken, $siteId)
25 {
26 if($authToken == '')
27 throw new ArgumentNullException("authToken");
28
29 if($siteId == '')
30 throw new ArgumentNullException("siteId");
31
32 $this->authToken = $authToken;
33 $this->siteId = $siteId;
34
35 $this->http = new HttpClient(array(
36 "version" => "1.1",
37 "socketTimeout" => 30,
38 "streamTimeout" => 30,
39 "redirect" => true,
40 "redirectMax" => 5,
41 ));
42 }
43
44 public function getItemsList()
45 {
46 static $result = null;
47
48 if($result === null)
49 $result = \Bitrix\Sale\TradingPlatform\Xml2Array::convert($this->getItems());
50
51 return $result;
52 }
53
58 public function getPoliciesNames($type)
59 {
60 $policiesList = $this->getItemsList();
61
62 if(empty($policiesList))
63 return array();
64
65 if($type == self::TYPE_RETURN)
66 $policyBranch = $policiesList["returnPolicyProfileList"]["ReturnPolicyProfile"];
67 elseif($type == self::TYPE_PAYMENT)
68 $policyBranch = $policiesList["paymentProfileList"]["PaymentProfile"];
69 elseif($type == self::TYPE_SHIPPING)
70 $policyBranch = $policiesList["shippingPolicyProfile"]["ShippingPolicyProfile"];
71 else
72 throw new ArgumentOutOfRangeException("type");
73
74 if(empty($policyBranch) || !is_array($policyBranch))
75 return array();
76
77 $result = array();
78 $policies = Xml2Array::normalize($policyBranch);
79
80 foreach($policies as $policy)
81 {
82 if(
83 isset($policy["profileName"])
84 && $policy["profileName"] <> ''
85 && isset($policy["profileId"])
86 && $policy["profileId"] <> ''
87 )
88 {
89 $result[$policy["profileId"]] = $policy["profileName"];
90 }
91 }
92
93 return $result;
94 }
95
96 protected function getItems()
97 {
98 $data = '<?xml version="1.0" encoding="utf-8"?>
99 <getSellerProfilesRequest xmlns="http://www.ebay.com/marketplace/sellings">
100 </getSellerProfilesRequest>';
101
102 return $this->sendRequest("getSellerProfiles", $data);
103 }
104
105 protected function sendRequest($operationName, $data)
106 {
107 $this->http->setHeader("X-EBAY-SOA-CONTENT-TYPE", "text/xml");
108 $this->http->setHeader("X-EBAY-SOA-GLOBAL-ID", "EBAY-RU");
109 $this->http->setHeader("X-EBAY-SOA-SERVICE-NAME", "SellerProfilesManagementService");
110 $this->http->setHeader("X-EBAY-SOA-OPERATION-NAME", $operationName); //addSellerProfile getSellerProfiles
111 $this->http->setHeader("X-EBAY-SOA-REQUEST-DATA-FORMAT", "XML");
112 $this->http->setHeader("X-EBAY-SOA-RESPONSE-DATA-FORMAT", "XML");
113 $this->http->setHeader("X-EBAY-SOA-SECURITY-TOKEN", $this->authToken);
114
115 if(mb_strtolower(SITE_CHARSET) != 'utf-8')
116 $data = Encoding::convertEncodingArray($data, SITE_CHARSET, 'UTF-8');
117
118 $result = $this->http->post(self::URL, $data);
119 $errors = $this->http->getError();
120
121 if (!$result && !empty($errors))
122 {
123 $strError = "";
124
125 foreach($errors as $errorCode => $errMes)
126 $strError .= $errorCode.": ".$errMes;
127
128 Ebay::log(Logger::LOG_LEVEL_INFO, "EBAY_POLICY_REQUEST_ERROR", $operationName, $strError, $this->siteId);
129 }
130 else
131 {
132 $status = $this->http->getStatus();
133
134 if ($status != 200)
135 Ebay::log(Logger::LOG_LEVEL_INFO, "EBAY_POLICY_REQUEST_HTTP_ERROR", $operationName, 'HTTP error code: '.$status, $this->siteId);
136
137 if(mb_strtolower(SITE_CHARSET) != 'utf-8')
138 $result = Encoding::convertEncodingArray($result, 'UTF-8', SITE_CHARSET);
139 }
140
141 return $result;
142 }
143}
static log($level, $type, $itemId, $description, $siteId)
Definition ebay.php:147
sendRequest($operationName, $data)
Definition policy.php:105
__construct($authToken, $siteId)
Definition policy.php:24
static normalize(array $branch)
Definition xml2array.php:75