1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32:
33:
34: 35: 36: 37: 38: 39: 40:
41: class Eabi_DpdEE_Model_Api extends Varien_Object {
42:
43:
44:
45: 46: 47: 48: 49: 50:
51: public function getConfigData($field) {
52: if (!$this->getCode()) {
53: return false;
54: }
55: $path = 'carriers/'.$this->getCode().'/'.$field;
56: return Mage::getStoreConfig($path, $this->getStore());
57: }
58:
59:
60: 61: 62: 63: 64: 65:
66: public function getOfficeList() {
67: $body = @$this->_getRequest();
68: return $body;
69: }
70:
71:
72: 73: 74: 75: 76:
77: public function getCourierCollectionTimes(array $requestData = array()) {
78: $details = array(
79: 'op' => 'date',
80: 'Po_postal' => $this->getConfigData('return_postcode'),
81: 'Po_country' => strtolower($this->getConfigData('return_country')),
82: 'Po_type' => isset($requestData['Po_type'])?strtolower($requestData['Po_type']):'po',
83: );
84: if ($details['Po_country'] == 'ee') {
85:
86:
87:
88: $details['Po_country'] = 'eesti';
89: }
90: foreach ($details as $key => $detail) {
91: $requestData[$key] = $detail;
92: }
93: $requestResult = $this->_getRequest($requestData);
94: return $requestResult;
95:
96: }
97:
98: 99: 100: 101: 102:
103: public function autoSendData(array $requestData) {
104: $returnDetails = array(
105: 'op' => 'order',
106: 'Po_name' => $this->getConfigData('return_name'),
107: 'Po_company' => $this->getConfigData('return_company'),
108: 'Po_street' => $this->getConfigData('return_street'),
109: 'Po_postal' => $this->getConfigData('return_postcode'),
110: 'Po_country' => strtolower($this->getConfigData('return_country')),
111: 'Po_city' => $this->getConfigData('return_citycounty'),
112: 'Po_contact' => $this->getConfigData('return_name'),
113: 'Po_phone' => $this->getConfigData('return_phone'),
114:
115: 'Po_email' => $this->getConfigData('return_email'),
116: 'Po_show_on_label' => $this->getConfigData('po_show_on_label')?'true':'false',
117: 'Po_save_address' => $this->getConfigData('po_save_address')?'true':'false',
118:
119: 'LabelsPosition' => $this->getConfigData('label_position'),
120:
121: );
122:
123: foreach ($returnDetails as $key => $returnDetail) {
124: $requestData[$key] = $returnDetail;
125: }
126: if (!isset($requestData['Po_type'])) {
127: $requestData['Po_type'] = $this->getConfigData('senddata_service');
128: }
129:
130: $requestResult = $this->_getRequest($requestData);
131: return $requestResult;
132: }
133:
134:
135: 136: 137: 138: 139: 140: 141: 142: 143: 144:
145: public function isCourierComing() {
146: $pickupTime = $this->getConfigData('courier_pickup_time');
147: $time = time();
148: if ($pickupTime) {
149: $pickupTime = explode(',', $pickupTime);
150: }
151: if ($pickupTime[0] >= $time) {
152: return $pickupTime;
153: }
154: return false;
155: }
156:
157:
158:
159: 160: 161: 162: 163: 164: 165: 166:
167: protected function _getRequest($params = array('op' => 'pudo'), $url = null) {
168: if (!$url) {
169: $url = $this->getConfigData('api_url');
170: }
171: if (isset($params['op']) && $params['op'] != 'pudo') {
172: $params['User_login'] = $this->getConfigData('sendpackage_username');
173: $params['User_password'] = $this->getConfigData('sendpackage_password');
174: }
175: $client = new Zend_Http_Client($url);
176: $options = array(
177: 'timeout' => $this->getConfigData('http_request_timeout')>10?$this->getConfigData('http_request_timeout'):10,
178: );
179: $client->setConfig($options);
180: $client->setParameterPost($params);
181: $resp = $client->request(Zend_Http_Client::POST);
182: $dataToLog = array(
183: 'url' => $url,
184: 'method' => Zend_Http_Client::POST,
185: 'params' => $this->_debugReplace($params),
186: );
187:
188:
189: $decodeResult = @json_decode($resp->getBody(), true);
190: if (!is_array($decodeResult) || !isset($decodeResult['Error_code'])
191: || $decodeResult['Error_code'] !== 0) {
192: if (!$decodeResult) {
193: $dataToLog['response'] = $resp;
194: } else {
195: $dataToLog['response'] = $decodeResult;
196: }
197: if ($this->_isLogEnabled()) {
198: Mage::log($dataToLog, Zend_Log::ERR, __CLASS__ . '.log');
199: }
200:
201: Mage::throwException($this->_getDpdHelper()->__('DPD request failed with response: %s', print_r($decodeResult, true)));
202: }
203: $dataToLog['response'] = $decodeResult;
204:
205: if ($this->_isLogEnabled()) {
206: Mage::log($dataToLog, Zend_Log::DEBUG, __CLASS__ . '.log');
207: }
208:
209: return $decodeResult;
210: }
211:
212:
213: protected function _debugReplace($params) {
214: $res = $params;
215: $toReplace = array(
216: 'User_login',
217: 'User_password',
218: );
219: foreach ($toReplace as $replace) {
220: if (isset($res[$replace])) {
221: $res[$replace] = '***';
222: }
223: }
224: return $res;
225: }
226:
227:
228: protected function _isLogEnabled() {
229: return $this->getConfigData('enable_log');
230: }
231:
232: 233: 234: 235:
236: protected function _getDpdHelper() {
237: return Mage::helper('eabi_dpdee');
238: }
239:
240:
241:
242: 243: 244: 245:
246: protected function _getOfficeHelper() {
247: return Mage::helper('eabi_postoffice');
248: }
249:
250:
251: }
252: