public function get_shipment_label($order_id, $shipment_id) { $order = new WC_Order($order_id); $soapClient = new SoapClient(__DIR__ . '/Shipping.wsdl'); $params = array('ClientInfo' => array('AccountCountryCode' => $this->country, 'AccountEntity' => $this->entity, 'AccountNumber' => $this->account_num, 'AccountPin' => $this->pin, 'UserName' => $this->username, 'Password' => $this->password, 'Version' => 'v1.0'), 'Transaction' => array('Reference1' => $order_id, 'Reference2' => '', 'Reference3' => '', 'Reference4' => '', 'Reference5' => ''), 'ShipmentNumber' => $shipment_id, 'LabelInfo' => array('ReportID' => '9729', 'ReportType' => 'URL')); try { // update_post_meta($order_id,"shipment_label_request",json_encode($params)); $auth_call = $soapClient->PrintLabel($params); // update_post_meta($order_id,"shipment_label_response",json_encode($auth_call)); if (empty($auth_call->HasErrors) || $auth_call->HasErrors == 0) { $shipping_label_url = $auth_call->ShipmentLabel->LabelURL; $order->add_order_note("Aramex Shipment Label: <a href='" . $shipping_label_url . "' target='_blank'>Click Here</a>"); update_post_meta($order_id, 'shipping_label', $shipping_label_url); } else { $msg = "Aramex Shipment Label Request Failed due to the following error(s):<br>"; foreach ($auth_call->Notifications as $notification) { $msg .= "Error " . $notification->Code . ": " . $notification->Message . "<br>"; } $order->add_order_note($msg); if ($this->verbose_reporting == true) { wp_mail(get_bloginfo('admin_email'), 'Shipment label request failed. Order ID:' . $order_id, $msg); } } } catch (SoapFault $fault) { $order->add_order_note("Failed generating shipment label. Error:" . $fault->faultstring); $message = "The system was unable to create a shipment label request for Order ID" . $order_id . "/r/n The error we received from Aramex is as follows:/r/n" . $fault->faultstring . "/r/n"; wp_mail(get_bloginfo('admin_email'), 'Shipping request failed. Order ID:' . $order_id, $message); } return; }
public function printLabelAction() { $_order = Mage::getModel('sales/order')->load($this->getRequest()->getParam('order_id')); $previuosUrl = Mage::getSingleton('core/session')->getPreviousUrl(); if ($_order->getId()) { $baseUrl = Mage::helper('aramexshipment')->getWsdlPath(); $soapClient = new SoapClient($baseUrl . 'shipping.wsdl'); $clientInfo = Mage::helper('aramexshipment')->getClientInfo(); $commentTable = Mage::getSingleton('core/resource')->getTableName('sales/shipment_comment'); $shipments = Mage::getResourceModel('sales/order_shipment_collection')->addAttributeToSelect('*')->addFieldToFilter("order_id", $_order->getId())->join("sales/shipment_comment", 'main_table.entity_id=parent_id', 'comment')->addFieldToFilter('comment', array('like' => "%{$_order->getIncrementId()}%"))->load(); $awbno = ''; $orderHistory = Mage::getModel('sales/order_status_history')->getCollection()->addFieldToFilter('parent_id', $_order->getId())->setOrder('created_at', 'desc'); foreach ($orderHistory as $history) { $comments = $history->getComment(); if ($comments && preg_match('/Aramex Shipment Return Order AWB No. ([0-9]+)/', $comments, $cmatches)) { $awbno = $cmatches[1]; break; } } if ($shipments->count()) { if ($awbno == '') { foreach ($shipments as $key => $comment) { if (version_compare(PHP_VERSION, '5.3.0') <= 0) { $awbno = substr($comment->getComment(), 0, strpos($comment->getComment(), "- Order No")); } else { $awbno = strstr($comment->getComment(), "- Order No", true); } $awbno = trim($awbno, "AWB No."); break; } } $report_id = (int) Mage::getStoreConfig('aramexsettings/config/report_id'); if (!$report_id) { $report_id = 9729; } $params = array('ClientInfo' => $clientInfo, 'Transaction' => array('Reference1' => $_order->getIncrementId(), 'Reference2' => '', 'Reference3' => '', 'Reference4' => '', 'Reference5' => ''), 'LabelInfo' => array('ReportID' => $report_id, 'ReportType' => 'URL')); $params['ShipmentNumber'] = $awbno; try { $auth_call = $soapClient->PrintLabel($params); /* bof PDF demaged Fixes debug */ if ($auth_call->HasErrors) { if (count($auth_call->Notifications->Notification) > 1) { foreach ($auth_call->Notifications->Notification as $notify_error) { Mage::throwException($this->__('Aramex: ' . $notify_error->Code . ' - ' . $notify_error->Message)); } } else { Mage::throwException($this->__('Aramex: ' . $auth_call->Notifications->Notification->Code . ' - ' . $auth_call->Notifications->Notification->Message)); } } /* eof PDF demaged Fixes */ $filepath = $auth_call->ShipmentLabel->LabelURL; $name = "{$_order->getIncrementId()}-shipment-label.pdf"; header('Content-type: application/pdf'); header('Content-Disposition: attachment; filename="' . $name . '"'); readfile($filepath); exit; } catch (SoapFault $fault) { Mage::getSingleton('adminhtml/session')->addError('Error : ' . $fault->faultstring); $this->_redirectUrl($previuosUrl); } catch (Exception $e) { $aramex_errors = true; Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); $this->_redirectUrl($previuosUrl); } } else { Mage::getSingleton('adminhtml/session')->addError($this->__('Shipment is empty or not created yet.')); $this->_redirectUrl($previuosUrl); } } else { Mage::getSingleton('adminhtml/session')->addError($this->__('This order no longer exists.')); $this->_redirectUrl($previuosUrl); } }