Version Notes
Updated API to v4
Download this release
Release Info
Developer | aftership |
Extension | aftership |
Version | 0.3.4 |
Comparing to | |
See all releases |
Code changes from version 0.3.3 to 0.3.4
app/code/community/Aftership/Track/Model/Observer.php
CHANGED
@@ -1,396 +1,337 @@
|
|
1 |
<?php
|
2 |
|
3 |
class Aftership_Track_Model_Observer {
|
4 |
-
const ENDPOINT_TRACKING = 'https://api.aftership.com/v1/trackings';
|
5 |
-
const ENDPOINT_CONFIG = 'https://api.aftership.com/v1/users/authenticate';
|
6 |
|
|
|
|
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
public function salesOrderShipmentTrackSaveAfter(Varien_Event_Observer $observer) {
|
9 |
ob_start();
|
10 |
|
11 |
-
$
|
12 |
-
$track_data = $track->getData();
|
13 |
-
$order_data = $track->getShipment()->getOrder()->getData();
|
14 |
|
15 |
-
$
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
}
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
//1.6.2.0 or later
|
27 |
-
$track_no = trim($track_data["track_number"]);
|
28 |
-
} else {
|
29 |
-
//1.5.1.0
|
30 |
-
$track_no = trim($track_data["number"]);
|
31 |
}
|
32 |
|
33 |
-
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
|
41 |
-
|
42 |
-
$exist_track_data = Mage::getModel('track/track')
|
43 |
-
->getCollection()
|
44 |
-
->addFieldToFilter('tracking_number', array('eq' => $track_no))
|
45 |
-
->addFieldToFilter('order_id', array('eq' => $order_id))
|
46 |
-
->getData();
|
47 |
|
48 |
-
|
49 |
-
$track = Mage::getModel('track/track');
|
50 |
|
51 |
-
|
52 |
-
$track->setShipCompCode($track_data["carrier_code"]);
|
53 |
-
$track->setTitle($order_data["increment_id"]);
|
54 |
-
$track->setOrderId($order_data["increment_id"]);
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
|
65 |
-
if (array_key_exists("status", $config) && $config["status"]) {
|
66 |
-
$track->setPosted(0);
|
67 |
|
68 |
-
$
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
$track->setPosted(1);
|
74 |
}
|
75 |
-
} else {
|
76 |
-
$track->setPosted(2);
|
77 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
80 |
}
|
|
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
85 |
|
86 |
-
$
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
->
|
|
|
|
|
|
|
93 |
|
94 |
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
-
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
$tracking_data = Mage::getResourceModel('sales/order_shipment_track_collection')->setOrderFilter($order);
|
101 |
|
102 |
-
|
|
|
|
|
103 |
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
break;
|
108 |
-
}
|
109 |
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
120 |
}
|
121 |
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
}
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
);
|
138 |
|
139 |
-
$url_params = http_build_query($url_params);
|
140 |
-
|
141 |
$ch = curl_init();
|
142 |
-
curl_setopt($ch, CURLOPT_URL,
|
143 |
curl_setopt($ch, CURLOPT_POST, true);
|
144 |
-
curl_setopt($ch, CURLOPT_POSTFIELDS, $
|
145 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
146 |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
|
147 |
curl_setopt($ch, CURLOPT_HEADER, 0);
|
148 |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
149 |
-
|
|
|
|
|
150 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //the SSL is not correct
|
151 |
-
curl_setopt($ch, CURLOPT_HTTPHEADER,
|
152 |
|
153 |
$response = curl_exec($ch);
|
154 |
|
155 |
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
156 |
-
$error = curl_error($ch);
|
157 |
curl_close($ch);
|
158 |
|
159 |
-
$response_obj = json_decode($response, true);
|
160 |
-
|
161 |
return $http_status;
|
162 |
}
|
163 |
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
//$website = Mage::app()->getRequest()->getParam('website');
|
169 |
-
//$scope = "websites";
|
170 |
-
//$score_id = (int)Mage::getConfig()->getNode('websites/'.$website.'/system/website/id');
|
171 |
-
|
172 |
-
if (!isset($post_data["groups"]["messages"]["fields"]["api_key"]['inherit']) || $post_data["groups"]["messages"]["fields"]["api_key"]['inherit'] != 1) {
|
173 |
-
$api_key = $post_data["groups"]["messages"]["fields"]["api_key"]["value"];
|
174 |
-
|
175 |
-
$url_params = array(
|
176 |
-
"api_key" => $api_key
|
177 |
-
);
|
178 |
-
$url = self::ENDPOINT_CONFIG;
|
179 |
-
|
180 |
-
$ch = curl_init();
|
181 |
-
curl_setopt($ch, CURLOPT_URL, $url);
|
182 |
-
curl_setopt($ch, CURLOPT_POST, true);
|
183 |
-
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($url_params));
|
184 |
-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
185 |
-
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
|
186 |
-
curl_setopt($ch, CURLOPT_HEADER, 0);
|
187 |
-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
188 |
-
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //handle SSL certificate problem: unable to get local issuer certificate issue
|
189 |
-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //the SSL is not correct
|
190 |
-
|
191 |
-
$response = curl_exec($ch);
|
192 |
-
$error = curl_error($ch);
|
193 |
-
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
194 |
-
curl_close($ch);
|
195 |
-
$response_obj = json_decode($response, true);
|
196 |
-
|
197 |
-
if ($http_status != "200" && $http_status != "401") {
|
198 |
-
Mage::throwException(Mage::helper('adminhtml')->__("Connection error, please try again later."));
|
199 |
-
} else {
|
200 |
-
if (!$response_obj["success"]) {
|
201 |
-
Mage::throwException(Mage::helper('adminhtml')->__("Incorrect API Key"));
|
202 |
-
}
|
203 |
-
}
|
204 |
-
}
|
205 |
-
}
|
206 |
-
|
207 |
-
/*Mage::getModel('core/config')->saveConfig('aftership_options/messages/status', 0);
|
208 |
-
Mage::getModel('core/config_data')
|
209 |
-
->setScope($scope)
|
210 |
-
->setScopeId($score_id)
|
211 |
-
->setPath('aftership_options/messages/status')
|
212 |
-
->setValue(0)
|
213 |
-
->save();*/
|
214 |
-
|
215 |
-
/*
|
216 |
-
*
|
217 |
-
* Cron
|
218 |
-
*
|
219 |
*/
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
$exist_track_data = Mage::getModel('track/track')
|
226 |
-
->getCollection()
|
227 |
-
->addFieldToFilter('tracking_number', array('eq' => $tracking_number))
|
228 |
-
->addFieldToFilter('order_id', array('eq' => $order_id))
|
229 |
-
->getData();
|
230 |
-
|
231 |
-
if (!$exist_track_data) {
|
232 |
-
$track = Mage::getModel('track/track')
|
233 |
-
->setTrackingNumber($tracking_number)
|
234 |
-
->setShipCompCode($carrier_code)
|
235 |
-
->setTitle($title)
|
236 |
-
->setOrderId($order_id)
|
237 |
-
->setEmail($email)
|
238 |
-
->setTelephone($telephone)
|
239 |
-
->setPosted(0)
|
240 |
-
->save();
|
241 |
-
|
242 |
-
$track_id = $track->getTrackId();
|
243 |
-
} else {
|
244 |
-
$track = $exist_track_data[0];
|
245 |
-
$track_id = $track['track_id'];
|
246 |
-
}
|
247 |
-
|
248 |
-
//send
|
249 |
-
$url = self::ENDPOINT_TRACKING;
|
250 |
-
|
251 |
-
$url_params = array();
|
252 |
-
|
253 |
-
$url_params["api_key"] = (string)$api_key;
|
254 |
-
$url_params["tracking_number"] = $tracking_number;
|
255 |
-
$url_params["smses[]"] = $telephone;
|
256 |
-
$url_params["emails[]"] = $email;
|
257 |
-
$url_params["title"] = $title;
|
258 |
-
$url_params["order_id"] = $order_id;
|
259 |
-
$url_params["customer_name"] = $customer_name;
|
260 |
-
$url_params["source"] = "magento";
|
261 |
|
262 |
$ch = curl_init();
|
263 |
-
curl_setopt($ch, CURLOPT_URL,
|
264 |
-
curl_setopt($ch, CURLOPT_POST,
|
265 |
-
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($url_params));
|
266 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
267 |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
|
268 |
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
269 |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
|
|
|
|
270 |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //the SSL is not correct
|
271 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //the SSL is not correct
|
272 |
-
curl_setopt($ch, CURLOPT_HTTPHEADER,
|
273 |
|
274 |
$response = curl_exec($ch);
|
275 |
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
276 |
-
$error = curl_error($ch);
|
277 |
curl_close($ch);
|
278 |
|
279 |
-
|
280 |
-
|
281 |
-
//save, 422: repeated
|
282 |
-
if ($http_status == "201" || $http_status == "422") {
|
283 |
-
Mage::getModel('track/track')
|
284 |
-
->load($track_id)
|
285 |
-
->setPosted(1)
|
286 |
-
->save();
|
287 |
-
}
|
288 |
}
|
289 |
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
$website_config = array();
|
300 |
-
|
301 |
-
//if there if only 1 store
|
302 |
-
if (count(Mage::app()->getWebsites()) == 1) {
|
303 |
-
$website_config[1] = $this->getDefaultConfig();
|
304 |
-
} else {
|
305 |
-
foreach (Mage::app()->getWebsites() as $website) {
|
306 |
-
$website_config[$website->getId()] = $this->getConfigByWebsiteId($website->getId());
|
307 |
-
}
|
308 |
}
|
309 |
|
310 |
-
$
|
311 |
-
|
312 |
|
313 |
-
|
|
|
|
|
|
|
|
|
|
|
314 |
|
315 |
-
if ($
|
316 |
-
|
317 |
-
$
|
318 |
} else {
|
319 |
-
|
320 |
-
$
|
321 |
-
}
|
322 |
-
|
323 |
-
//echo "from: ".$from.", to: ".$to."\n";
|
324 |
-
|
325 |
-
$track_collection = Mage::getResourceModel('sales/order_shipment_track_collection')
|
326 |
-
->addAttributeToFilter('created_at', array(
|
327 |
-
'from' => $from,
|
328 |
-
'to' => $to,
|
329 |
-
))
|
330 |
-
->addAttributeToSort('created_at', 'asc');
|
331 |
-
|
332 |
-
foreach ($track_collection as $track) {
|
333 |
-
//echo "order id: ".$track->getOrderId().", tracking number: ".$track->getTrackNumber().", created at: ".$track->getCreatedAt()."\n";
|
334 |
-
|
335 |
-
$order_shipment = $track->getShipment();
|
336 |
-
|
337 |
-
$store_id = $order_shipment->getStoreId();
|
338 |
-
|
339 |
-
//if website enabled
|
340 |
-
if (isset($website_config[$store_id]['status']) && $website_config[$store_id]['status'] == 1 &&
|
341 |
-
isset($website_config[$store_id]['cron_job_enable']) && $website_config[$store_id]['cron_job_enable'] == 1) {
|
342 |
-
//check with existing as_track table and see if sent
|
343 |
-
$post_tracks = Mage::getModel('track/track')
|
344 |
-
->getCollection()
|
345 |
-
->addFieldToFilter('posted', array('eq' => 1))
|
346 |
-
->addFieldToFilter('tracking_number', array('eq' => $track->getTrackNumber()))
|
347 |
-
->getData();
|
348 |
-
|
349 |
-
//if not sent
|
350 |
-
if (count($post_tracks) == 0) {
|
351 |
-
$order = $order_shipment->getOrder();
|
352 |
-
$shipping_address = $order->getShippingAddress();
|
353 |
-
|
354 |
-
$api_key = $website_config[$store_id]['api_key'];
|
355 |
-
$telephone = $shipping_address->getTelephone();
|
356 |
-
$carrier_code = $track->getCarrierCode();
|
357 |
-
$email = $order->getCustomerEmail();
|
358 |
-
$title = $order->getIncrementId();
|
359 |
-
$order_id = $order->getIncrementId();
|
360 |
-
$customer_name = $shipping_address->getFirstname() . " " . $shipping_address->getLastname();
|
361 |
-
|
362 |
-
$this->sendTracking($api_key, $track->getTrackNumber(), $carrier_code, $telephone, $email, $title, $order_id, $customer_name);
|
363 |
-
}
|
364 |
-
}
|
365 |
-
}
|
366 |
-
|
367 |
-
//update time ONLY if success
|
368 |
-
Mage::getModel('core/config')->saveConfig('aftership_options/messages/last_update', time());
|
369 |
-
|
370 |
-
foreach (Mage::app()->getWebsites() as $website) {
|
371 |
-
$website_id = $website->getId();
|
372 |
-
$scope = "websites";
|
373 |
-
$scope_id = (int)Mage::getConfig()->getNode('websites/' . Mage::app()->getWebsite($website_id)->getCode() . '/system/website/id');
|
374 |
-
|
375 |
-
Mage::getModel('core/config_data')
|
376 |
-
->setScope($scope)
|
377 |
-
->setScopeId($scope_id)
|
378 |
-
->setPath('aftership_options/messages/last_update')
|
379 |
-
->setValue(time())
|
380 |
-
->save();
|
381 |
}
|
382 |
-
}
|
383 |
|
384 |
-
|
385 |
-
return Mage::getStoreConfig('aftership_options/messages');
|
386 |
}
|
387 |
|
388 |
-
private function getConfigByWebsiteId($website_id) {
|
389 |
-
$config_obj = Mage::app()->getWebsite($website_id)->getConfig('aftership_options');
|
390 |
-
$config = array();
|
391 |
-
$config['api_key'] = $config_obj['messages']->api_key.'';
|
392 |
-
$config['status'] = $config_obj['messages']->status.'';
|
393 |
-
$config['cron_job_enable'] = $config_obj['messages']->cron_job_enable.'';
|
394 |
-
return $config;
|
395 |
-
}
|
396 |
}
|
1 |
<?php
|
2 |
|
3 |
class Aftership_Track_Model_Observer {
|
|
|
|
|
4 |
|
5 |
+
const ENDPOINT_TRACKING = 'https://api.aftership.com/v4/trackings';
|
6 |
+
const ENDPOINT_AUTHENTICATE = 'https://api.aftership.com/v4/couriers';
|
7 |
|
8 |
+
const POSTED_NOT_YET = 0;
|
9 |
+
const POSTED_DONE = 1;
|
10 |
+
const POSTED_DISABLED = 2;
|
11 |
+
|
12 |
+
private $_configs = array();
|
13 |
+
|
14 |
+
/**
|
15 |
+
* @param Varien_Event_Observer $observer
|
16 |
+
*/
|
17 |
public function salesOrderShipmentTrackSaveAfter(Varien_Event_Observer $observer) {
|
18 |
ob_start();
|
19 |
|
20 |
+
$magento_track = $observer->getEvent()->getTrack();
|
|
|
|
|
21 |
|
22 |
+
$magento_order = $magento_track->getShipment()->getOrder();
|
23 |
+
$website_config = $this->_getWebsiteConfig($magento_order);
|
24 |
|
25 |
+
$tracks = Mage::getModel('track/track')
|
26 |
+
->getCollection()
|
27 |
+
->addFieldToFilter('tracking_number', array('eq' => $this->_getTrackNo($magento_track)))
|
28 |
+
->addFieldToFilter('order_id', array('eq' => $magento_order->getIncrementId()))
|
29 |
+
->getItems();
|
30 |
+
|
31 |
+
if (empty($tracks)) {
|
32 |
+
$track = $this->_saveTrack($magento_track);
|
33 |
+
}
|
34 |
+
else {
|
35 |
+
$track = reset($tracks);
|
36 |
}
|
37 |
|
38 |
+
if ($website_config->status) {
|
39 |
+
$this->_sendTrack($track);
|
|
|
|
|
|
|
|
|
|
|
40 |
}
|
41 |
|
42 |
+
ob_end_clean();
|
43 |
+
}
|
44 |
|
45 |
+
/**
|
46 |
+
* Cron to sync trackings
|
47 |
+
*/
|
48 |
+
public function cron() {
|
49 |
+
set_time_limit(0);
|
50 |
|
51 |
+
$global_config = Mage::getStoreConfig('aftership_options/messages');
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
$last_update = $global_config['last_update'];
|
|
|
54 |
|
55 |
+
$debug_range = 1;
|
|
|
|
|
|
|
56 |
|
57 |
+
if ($last_update == '0' || !$last_update) {
|
58 |
+
$from = gmdate('Y-m-d H:i:s', time() - 3 * 60 * 60 * $debug_range); //past 3 hours
|
59 |
+
$to = gmdate('Y-m-d H:i:s');
|
60 |
+
} else {
|
61 |
+
$from = gmdate('Y-m-d H:i:s', $last_update);
|
62 |
+
$to = gmdate('Y-m-d H:i:s');
|
63 |
+
}
|
64 |
|
65 |
+
$track_collection = Mage::getResourceModel('sales/order_shipment_track_collection')
|
66 |
+
->addAttributeToFilter('created_at', array(
|
67 |
+
'from' => $from,
|
68 |
+
'to' => $to,
|
69 |
+
))
|
70 |
+
->addAttributeToSort('created_at', 'asc');
|
71 |
+
|
72 |
+
foreach ($track_collection as $magento_track) {
|
73 |
+
$order = Mage::getModel('sales/order')->load($magento_track->getOrderId(), 'increment_id');
|
74 |
+
$website_config = $this->_getWebsiteConfig($order);
|
75 |
+
|
76 |
+
if ($website_config->cron_job_enable && $website_config->status) {
|
77 |
+
$tracks = Mage::getModel('track/track')
|
78 |
+
->getCollection()
|
79 |
+
->addFieldToFilter('tracking_number', array('eq' => $this->_getTrackNo($magento_track)))
|
80 |
+
->getItems();
|
81 |
|
82 |
+
$is_send = false;
|
|
|
|
|
83 |
|
84 |
+
if (empty($tracks)) {
|
85 |
+
// for the case that salesOrderShipmentTrackSaveAfter() is bypassed/crashed in shipment creation
|
86 |
+
$track = $this->_saveTrack($magento_track);
|
87 |
+
$is_send = true;
|
88 |
+
}
|
89 |
+
else {
|
90 |
+
$track = reset($tracks);
|
91 |
+
if ($track->getPosted() == self::POSTED_NOT_YET) {
|
92 |
+
// for the case that the tracking was somehow failed to send to aftership
|
93 |
+
$is_send = true;
|
94 |
+
|
95 |
+
}
|
96 |
+
// else its done or disabled, do nothing
|
97 |
+
}
|
98 |
|
99 |
+
if ($is_send) {
|
100 |
+
$this->_sendTrack($track);
|
|
|
101 |
}
|
|
|
|
|
102 |
}
|
103 |
+
}
|
104 |
+
|
105 |
+
//update time ONLY if success
|
106 |
+
Mage::getModel('core/config')->saveConfig('aftership_options/messages/last_update', time());
|
107 |
+
|
108 |
+
foreach (Mage::app()->getWebsites() as $website) {
|
109 |
+
$website_id = $website->getId();
|
110 |
+
$scope = 'websites';
|
111 |
+
$scope_id = (int)Mage::getConfig()->getNode('websites/' . Mage::app()->getWebsite($website_id)->getCode() . '/system/website/id');
|
112 |
|
113 |
+
Mage::getModel('core/config_data')
|
114 |
+
->setScope($scope)
|
115 |
+
->setScopeId($scope_id)
|
116 |
+
->setPath('aftership_options/messages/last_update')
|
117 |
+
->setValue(time())
|
118 |
+
->save();
|
119 |
}
|
120 |
+
}
|
121 |
|
122 |
+
/**
|
123 |
+
* @param Varien_Event_Observer $observer
|
124 |
+
*/
|
125 |
+
public function adminSystemConfigChangedSectionAftership(Varien_Event_Observer $observer)
|
126 |
+
{
|
127 |
+
$post_data = Mage::app()->getRequest()->getPost();
|
128 |
|
129 |
+
if (
|
130 |
+
!isset($post_data['groups']['messages']['fields']['api_key']['inherit']) ||
|
131 |
+
$post_data['groups']['messages']['fields']['api_key']['inherit'] != 1
|
132 |
+
) {
|
133 |
+
$api_key = $post_data['groups']['messages']['fields']['api_key']['value'];
|
134 |
|
135 |
+
$http_status = $this->_callApiAuthenticate($api_key);
|
136 |
|
137 |
+
if ($http_status == '401') {
|
138 |
+
Mage::throwException(Mage::helper('adminhtml')->__('Incorrect API Key'));
|
139 |
+
}
|
140 |
+
else if ($http_status != '200') {
|
141 |
+
Mage::throwException(Mage::helper('adminhtml')->__('Connection error, please try again later.'));
|
142 |
+
}
|
143 |
+
}
|
144 |
+
}
|
145 |
|
146 |
|
147 |
+
/**
|
148 |
+
* @param Mage_Sales_Model_Order_Shipment_Track $magento_track
|
149 |
+
* @return mixed
|
150 |
+
*/
|
151 |
+
private function _saveTrack(Mage_Sales_Model_Order_Shipment_Track $magento_track) {
|
152 |
+
$magento_track_data = $magento_track->getData();
|
153 |
+
$magento_order_data = $magento_track->getShipment()->getOrder()->getData();
|
154 |
+
$magento_shipping_address_data = $magento_track->getShipment()->getOrder()->getShippingAddress()->getData();
|
155 |
|
156 |
+
$track = Mage::getModel('track/track');
|
157 |
+
$track->setTrackingNumber($this->_getTrackNo($magento_track));
|
158 |
+
$track->setShipCompCode($magento_track_data['carrier_code']);
|
159 |
+
$track->setTitle($magento_order_data['increment_id']);
|
160 |
|
161 |
+
$track->setOrderId($magento_order_data['increment_id']);
|
|
|
162 |
|
163 |
+
if ($magento_order_data['customer_email'] && $magento_order_data['customer_email'] != '') {
|
164 |
+
$track->setEmail($magento_order_data['customer_email']);
|
165 |
+
}
|
166 |
|
167 |
+
if ($magento_shipping_address_data['telephone'] && $magento_shipping_address_data['telephone'] != '') {
|
168 |
+
$track->setTelephone($magento_shipping_address_data['telephone']);
|
169 |
+
}
|
|
|
|
|
170 |
|
171 |
+
$config = $this->_getWebsiteConfig($magento_track->getShipment()->getOrder());
|
172 |
+
if ($config->status) {
|
173 |
+
$track->setPosted(self::POSTED_NOT_YET);
|
174 |
+
}
|
175 |
+
else {
|
176 |
+
// mark it as disabled so that cron will not touch it
|
177 |
+
$track->setPosted(self::POSTED_DISABLED);
|
178 |
+
}
|
179 |
|
180 |
+
$track->save();
|
181 |
+
|
182 |
+
return $track;
|
183 |
+
}
|
184 |
+
|
185 |
+
/**
|
186 |
+
* @param Aftership_Track_Model_Track $track
|
187 |
+
* @return bool|mixed
|
188 |
+
*/
|
189 |
+
private function _sendTrack(Aftership_Track_Model_Track $track) {
|
190 |
+
if ($track->getPosted() != self::POSTED_NOT_YET) {
|
191 |
+
return false;
|
192 |
}
|
193 |
|
194 |
+
$order = Mage::getModel('sales/order')->load($track->getOrderId(), 'increment_id');
|
195 |
+
$website_config = $this->_getWebsiteConfig($order);
|
196 |
+
|
197 |
+
$shipping_address = $order->getShippingAddress();
|
198 |
+
|
199 |
+
$api_key = $website_config->api_key;
|
200 |
+
$country_id = $shipping_address->getCountryId();
|
201 |
+
$telephone = $shipping_address->getTelephone();
|
202 |
+
$carrier_code = $track->getCarrierCode();
|
203 |
+
$email = $order->getCustomerEmail();
|
204 |
+
$title = $track->getOrderId();
|
205 |
+
$order_id = $track->getOrderId();
|
206 |
+
$customer_name = $shipping_address->getFirstname() . ' ' . $shipping_address->getLastname();
|
207 |
+
|
208 |
+
$http_status = $this->_callApiCreateTracking($api_key, $track->getTrackingNumber(), $carrier_code, $country_id, $telephone, $email, $title, $order_id, $customer_name);
|
209 |
+
|
210 |
+
//save, 422: repeated
|
211 |
+
if ($http_status == '201' || $http_status == '422') {
|
212 |
+
$track->setPosted(self::POSTED_DONE)->save();
|
213 |
+
}
|
214 |
+
|
215 |
+
return $http_status;
|
216 |
}
|
217 |
|
218 |
+
/**
|
219 |
+
* Call API to create tracking
|
220 |
+
* @param $api_key
|
221 |
+
* @param $tracking_number
|
222 |
+
* @param $carrier_code (NOT USING CURRENTLY)
|
223 |
+
* @param $country_id
|
224 |
+
* @param $telephone
|
225 |
+
* @param $email
|
226 |
+
* @param $title
|
227 |
+
* @param $order_id
|
228 |
+
* @param $customer_name
|
229 |
+
* @return mixed
|
230 |
+
*/
|
231 |
+
private function _callApiCreateTracking($api_key, $tracking_number, $carrier_code, $country_id, $telephone, $email, $title, $order_id, $customer_name) {
|
232 |
+
$url_params = array('tracking' => array(
|
233 |
+
'tracking_number' => $tracking_number,
|
234 |
+
'destination_country_iso3' => $country_id,
|
235 |
+
'smses' => $telephone,
|
236 |
+
'emails' => $email,
|
237 |
+
'title' => $title,
|
238 |
+
'order_id' => $order_id,
|
239 |
+
'customer_name' => $customer_name,
|
240 |
+
'source' => 'magento'
|
241 |
+
));
|
242 |
+
|
243 |
+
$json_params = json_encode($url_params);
|
244 |
+
|
245 |
+
$headers = array(
|
246 |
+
'aftership-api-key: ' . $api_key,
|
247 |
+
'Content-Type: application/json',
|
248 |
+
'Content-Length: ' . strlen($json_params)
|
249 |
);
|
250 |
|
|
|
|
|
251 |
$ch = curl_init();
|
252 |
+
curl_setopt($ch, CURLOPT_URL, self::ENDPOINT_TRACKING);
|
253 |
curl_setopt($ch, CURLOPT_POST, true);
|
254 |
+
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_params);
|
255 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
256 |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
|
257 |
curl_setopt($ch, CURLOPT_HEADER, 0);
|
258 |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
259 |
+
|
260 |
+
//handle SSL certificate problem: unable to get local issuer certificate issue
|
261 |
+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //the SSL is not correct
|
262 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //the SSL is not correct
|
263 |
+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
264 |
|
265 |
$response = curl_exec($ch);
|
266 |
|
267 |
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
268 |
curl_close($ch);
|
269 |
|
|
|
|
|
270 |
return $http_status;
|
271 |
}
|
272 |
|
273 |
+
/**
|
274 |
+
* Call API to authenticate
|
275 |
+
* @param $api_key
|
276 |
+
* @return HTTP status code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
*/
|
278 |
+
private function _callApiAuthenticate($api_key) {
|
279 |
+
$headers = array(
|
280 |
+
'aftership-api-key: ' . $api_key,
|
281 |
+
'Content-Type: application/json',
|
282 |
+
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
|
284 |
$ch = curl_init();
|
285 |
+
curl_setopt($ch, CURLOPT_URL, self::ENDPOINT_AUTHENTICATE);
|
286 |
+
curl_setopt($ch, CURLOPT_POST, false);
|
|
|
287 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
288 |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
|
289 |
curl_setopt($ch, CURLOPT_HEADER, 0);
|
290 |
+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
291 |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
292 |
+
|
293 |
+
//handle SSL certificate problem: unable to get local issuer certificate issue
|
294 |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //the SSL is not correct
|
295 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //the SSL is not correct
|
296 |
+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
297 |
|
298 |
$response = curl_exec($ch);
|
299 |
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
300 |
curl_close($ch);
|
301 |
|
302 |
+
return $http_status;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
303 |
}
|
304 |
|
305 |
+
/**
|
306 |
+
* @param Mage_Sales_Model_Order $order
|
307 |
+
* @return mixed
|
308 |
+
*/
|
309 |
+
private function _getWebsiteConfig(Mage_Sales_Model_Order $order) {
|
310 |
+
if (!isset($this->_configs[$order->getStore()->getWebsiteId()])) {
|
311 |
+
$config = Mage::app()->getWebsite($order->getStore()->getWebsiteId())->getConfig('aftership_options');
|
312 |
+
// object conversion to avoid config element object for easy comparing
|
313 |
+
$this->_configs[$order->getStore()->getWebsiteId()] = (object)((array)$config['messages']);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
314 |
}
|
315 |
|
316 |
+
return $this->_configs[$order->getStore()->getWebsiteId()];
|
317 |
+
}
|
318 |
|
319 |
+
/**
|
320 |
+
* @param $magento_track
|
321 |
+
* @return string
|
322 |
+
*/
|
323 |
+
private function _getTrackNo($magento_track) {
|
324 |
+
$track_data = $magento_track->getData();
|
325 |
|
326 |
+
if (strlen(trim($track_data['track_number'])) > 0) {
|
327 |
+
//1.6.2.0 or later
|
328 |
+
$track_no = trim($track_data['track_number']);
|
329 |
} else {
|
330 |
+
//1.5.1.0
|
331 |
+
$track_no = trim($track_data['number']);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
332 |
}
|
|
|
333 |
|
334 |
+
return $track_no;
|
|
|
335 |
}
|
336 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
337 |
}
|
app/code/community/Aftership/Track/etc/config.xml
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
<config>
|
3 |
<modules>
|
4 |
<Aftership_Track>
|
5 |
-
<version>0.3.
|
6 |
</Aftership_Track>
|
7 |
</modules>
|
8 |
|
2 |
<config>
|
3 |
<modules>
|
4 |
<Aftership_Track>
|
5 |
+
<version>0.3.4</version>
|
6 |
</Aftership_Track>
|
7 |
</modules>
|
8 |
|
package.xml
CHANGED
@@ -1,19 +1,18 @@
|
|
1 |
<?xml version="1.0"?>
|
2 |
<package>
|
3 |
<name>aftership</name>
|
4 |
-
<version>0.3.
|
5 |
<stability>stable</stability>
|
6 |
<license uri="http://www.opensource.org/licenses/gpl-license.php">GPL</license>
|
7 |
<channel>community</channel>
|
8 |
<extends/>
|
9 |
-
<summary>AfterShip magento extension interface. Tracking number will be added to the aftership.com
|
10 |
-
<description>Users can simply link their online stores to Aftership and view all packages in one place. Online stores can also automatically inform their customers of the latest delivery status through SMS, email and Twitter. All the messages are white-labeled and can be customized for any marketing opportunities. Aftership also provides online stores a reporting tool so that they can easily find out any shipping problems like delayed delivery and solve immediately. Aftership currently supports over
|
11 |
-
<notes>
|
12 |
-
Fixed wrong order info issue</notes>
|
13 |
<authors><author><name>aftership</name><user>aftership</user><email>support@aftership.com</email></author></authors>
|
14 |
-
<date>
|
15 |
-
<time>
|
16 |
-
<contents><target name="magecommunity"><dir name="Aftership"><dir name="Track"><dir name="Helper"><file name="Data.php" hash="854baf793cb348f80c94703b585691b9"/></dir><dir name="Model"><file name="FrequencyWords.php" hash="91aad25bcba1c0f8433ab0f297f50614"/><file name="Methods.php" hash="ee6ccd73ab94405e8b0ab4a59990a0fa"/><dir name="Mysql4"><dir name="Track"><file name="Collection.php" hash="b359845af660063396556b37920675ad"/></dir><file name="Track.php" hash="e7799ea4fef0ff7e4217ae0d3a3ed50e"/></dir><file name="Observer.php" hash="
|
17 |
<compatible/>
|
18 |
-
<dependencies><required><php><min>5.2.13</min><max>5.6.
|
19 |
</package>
|
1 |
<?xml version="1.0"?>
|
2 |
<package>
|
3 |
<name>aftership</name>
|
4 |
+
<version>0.3.4</version>
|
5 |
<stability>stable</stability>
|
6 |
<license uri="http://www.opensource.org/licenses/gpl-license.php">GPL</license>
|
7 |
<channel>community</channel>
|
8 |
<extends/>
|
9 |
+
<summary>AfterShip magento extension interface. Tracking number will be added to the aftership.com https://www.aftership.com/ account at the same time when input in magento shipment. Please go to aftership.com https://www.aftership.com/ to change the notification setting and value.</summary>
|
10 |
+
<description>Users can simply link their online stores to Aftership and view all packages in one place. Online stores can also automatically inform their customers of the latest delivery status through SMS, email and Twitter. All the messages are white-labeled and can be customized for any marketing opportunities. Aftership also provides online stores a reporting tool so that they can easily find out any shipping problems like delayed delivery and solve immediately. Aftership currently supports over 200 shipping companies worldwide.</description>
|
11 |
+
<notes>Updated API to v4</notes>
|
|
|
12 |
<authors><author><name>aftership</name><user>aftership</user><email>support@aftership.com</email></author></authors>
|
13 |
+
<date>2015-02-24</date>
|
14 |
+
<time>03:15:41</time>
|
15 |
+
<contents><target name="magecommunity"><dir name="Aftership"><dir name="Track"><dir name="Helper"><file name="Data.php" hash="854baf793cb348f80c94703b585691b9"/></dir><dir name="Model"><file name="FrequencyWords.php" hash="91aad25bcba1c0f8433ab0f297f50614"/><file name="Methods.php" hash="ee6ccd73ab94405e8b0ab4a59990a0fa"/><dir name="Mysql4"><dir name="Track"><file name="Collection.php" hash="b359845af660063396556b37920675ad"/></dir><file name="Track.php" hash="e7799ea4fef0ff7e4217ae0d3a3ed50e"/></dir><file name="Observer.php" hash="b200f9bdca4a179464e4e9d19e95facb"/><dir name="Resource"><dir name="Mysql4"><file name="Setup.php" hash="9a1fee597d5f7219cd3f68f38bb1ed97"/></dir></dir><file name="Track.php" hash="61bae429d01ace637296e11994203fe5"/><file name="Words.php" hash="40d412b8ea57998ae6429219b14709db"/></dir><dir name="controllers"><file name="IndexController.php" hash="7f5c1ae52f625152ca3e9ffe15a93028"/></dir><dir name="etc"><file name="adminhtml.xml" hash="cadc113478c285edf9f43c7506f6ba89"/><file name="config.xml" hash="0a217c95d63a5f9be1a92dfd9d2672f4"/><file name="system.xml" hash="bfd83204df3be87132971d15fbef8607"/></dir><dir name="sql"><dir name="track_setup"><file name="mysql4-install-0.3.2.php" hash="7639b6303191813f78441a6d7297e0d1"/><file name="mysql4-upgrade-0.2.9-0.3.2.php" hash="7639b6303191813f78441a6d7297e0d1"/></dir></dir></dir></dir></target><target name="mage"><dir name="app"><dir name="etc"><dir name="modules"><file name="Aftership_Track.xml" hash="25367ce0ec0484973a73e5548199dea0"/></dir></dir></dir></target><target name="mageskin"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="aftership"><dir name="css"><file name="style.css" hash="bd5df03e68aa767303eb392df394ce52"/></dir><dir name="images"><file name="logo.png" hash="3e8d8f9d5e1747c18fcd1a810ddc5f2a"/><file name=".DS_Store" hash="194577a7e20bdcc7afbb718f502c134c"/></dir><file name=".DS_Store" hash="e8931b980e8ec084f41ef4f99eeef0c3"/></dir></dir></dir></dir></target><target name="magedesign"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="layout"><file name="aftership.xml" hash="263c8bb1e2ba9acbf017a2a44fb11439"/></dir></dir></dir></dir></target></contents>
|
16 |
<compatible/>
|
17 |
+
<dependencies><required><php><min>5.2.13</min><max>5.6.6</max></php></required></dependencies>
|
18 |
</package>
|