Example #1
0
 /**
  * Parse xml response and return result
  *
  * @param string $response
  * @return Mage_Shipping_Model_Rate_Result|Varien_Object
  */
 protected function _parseXmlResponse($response)
 {
     $r = $this->_rawRequest;
     $costArr = array();
     $priceArr = array();
     $errorTitle = 'Unable to retrieve quotes';
     $tr = get_html_translation_table(HTML_ENTITIES);
     unset($tr['<'], $tr['>'], $tr['"']);
     $response = str_replace(array_keys($tr), array_values($tr), $response);
     if (strlen(trim($response)) > 0) {
         if (strpos(trim($response), '<?xml') === 0) {
             $xml = simplexml_load_string($response);
             if (is_object($xml)) {
                 if (is_object($xml->Faults) && is_object($xml->Faults->Fault) && is_object($xml->Faults->Fault->Code) && is_object($xml->Faults->Fault->Description) && is_object($xml->Faults->Fault->Context)) {
                     $code = (string) $xml->Faults->Fault->Code;
                     $description = $xml->Faults->Fault->Description;
                     $context = $xml->Faults->Fault->Context;
                     $this->_errors[$code] = Mage::helper('usa')->__('Error #%s : %s (%s)', $code, $description, $context);
                 } else {
                     if ($r->getDestCountryId() == self::USA_COUNTRY_ID) {
                         if ($xml->Shipment) {
                             foreach ($xml->Shipment as $shipXml) {
                                 $this->_parseXmlObject($shipXml);
                             }
                         } else {
                             $this->_errors[] = Mage::helper('usa')->__('Shipment is not available.');
                         }
                     } else {
                         $shipXml = $xml->IntlShipment;
                         $this->_parseXmlObject($shipXml);
                     }
                     $shipXml = $r->getDestCountryId() == self::USA_COUNTRY_ID ? $xml->Shipment : $xml->IntlShipment;
                 }
             }
         } else {
             $this->_errors[] = Mage::helper('usa')->__('The response is in wrong format.');
         }
     }
     if ($this->_rawRequest->getAction() == 'GenerateLabel') {
         $result = new Varien_Object();
         if (!empty($this->_errors)) {
             $result->setErrors(implode($this->_errors, '; '));
         } else {
             if ($xml !== false) {
                 if ($r->getDestCountryId() == self::USA_COUNTRY_ID) {
                     $shippingLabelContent = base64_decode((string) $xml->Shipment->Label->Image);
                     $trackingNumber = (string) $xml->Shipment->ShipmentDetail->AirbillNbr;
                 } else {
                     $shippingLabelContent = base64_decode((string) $xml->IntlShipment->Label->Image);
                     $trackingNumber = (string) $xml->IntlShipment->ShipmentDetail->AirbillNbr;
                 }
             }
             $result->setShippingLabelContent($shippingLabelContent);
             $result->setTrackingNumber($trackingNumber);
         }
         return $result;
     } else {
         $result = Mage::getModel('shipping/rate_result');
         if ($this->_dhlRates) {
             foreach ($this->_dhlRates as $rate) {
                 $method = $rate['service'];
                 $data = $rate['data'];
                 $rate = Mage::getModel('shipping/rate_result_method');
                 $rate->setCarrier('dhl');
                 $rate->setCarrierTitle($this->getConfigData('title'));
                 $rate->setMethod($method);
                 $rate->setMethodTitle($data['term']);
                 $rate->setCost($data['price_total']);
                 $rate->setPrice($data['price_total']);
                 $result->append($rate);
             }
         } else {
             if (!empty($this->_errors)) {
                 $error = Mage::getModel('shipping/rate_result_error');
                 $error->setCarrier('dhl');
                 $error->setCarrierTitle($this->getConfigData('title'));
                 $error->setErrorMessage($this->getConfigData('specificerrmsg'));
                 $result->append($error);
             }
         }
         return $result;
     }
 }
Example #2
0
 /**
  * Do shipment request to carrier web service, obtain Print Shipping Labels and process errors in response
  *
  * @param Varien_Object $request
  * @return Varien_Object
  */
 protected function _doShipmentRequest(Varien_Object $request)
 {
     $this->_prepareShipmentRequest($request);
     $result = new Varien_Object();
     $client = $this->_createShipSoapClient();
     $requestClient = $this->_formShipmentRequest($request);
     $response = $client->processShipment($requestClient);
     if ($response->HighestSeverity != 'FAILURE' && $response->HighestSeverity != 'ERROR') {
         $shippingLabelContent = $response->CompletedShipmentDetail->CompletedPackageDetails->Label->Parts->Image;
         $trackingNumber = $response->CompletedShipmentDetail->CompletedPackageDetails->TrackingIds->TrackingNumber;
         $result->setShippingLabelContent($shippingLabelContent);
         $result->setTrackingNumber($trackingNumber);
         $debugData = array('request' => $client->__getLastRequest(), 'result' => $client->__getLastResponse());
         $this->_debug($debugData);
     } else {
         $debugData = array('request' => $client->__getLastRequest(), 'result' => array('error' => '', 'code' => '', 'xml' => $client->__getLastResponse()));
         if (is_array($response->Notifications)) {
             foreach ($response->Notifications as $notification) {
                 $debugData['result']['code'] .= $notification->Code . '; ';
                 $debugData['result']['error'] .= $notification->Message . '; ';
             }
         } else {
             $debugData['result']['code'] = $response->Notifications->Code . ' ';
             $debugData['result']['error'] = $response->Notifications->Message . ' ';
         }
         $this->_debug($debugData);
         $result->setErrors($debugData['result']['error']);
     }
     $result->setGatewayResponse($client->__getLastResponse());
     return $result;
 }
Example #3
0
 /**
  * Do shipment request to carrier web service, obtain Print Shipping Labels and process errors in response
  *
  * @param Varien_Object $request
  * @return Varien_Object
  */
 protected function _doShipmentRequest(Varien_Object $request)
 {
     $this->_prepareShipmentRequest($request);
     $result = new Varien_Object();
     $service = $this->getCode('service_to_code', $request->getShippingMethod());
     $recipientUSCountry = $this->_isUSCountry($request->getRecipientAddressCountryCode());
     if ($recipientUSCountry && $service == 'Priority Express') {
         $requestXml = $this->_formUsExpressShipmentRequest($request);
         $api = 'ExpressMailLabel';
     } else {
         if ($recipientUSCountry) {
             $requestXml = $this->_formUsSignatureConfirmationShipmentRequest($request, $service);
             if ($this->getConfigData('mode')) {
                 $api = 'SignatureConfirmationV3';
             } else {
                 $api = 'SignatureConfirmationCertifyV3';
             }
         } else {
             if ($service == 'First Class') {
                 $requestXml = $this->_formIntlShipmentRequest($request);
                 $api = 'FirstClassMailIntl';
             } else {
                 if ($service == 'Priority') {
                     $requestXml = $this->_formIntlShipmentRequest($request);
                     $api = 'PriorityMailIntl';
                 } else {
                     $requestXml = $this->_formIntlShipmentRequest($request);
                     $api = 'ExpressMailIntl';
                 }
             }
         }
     }
     $debugData = array('request' => $requestXml);
     $url = $this->getConfigData('gateway_secure_url');
     if (!$url) {
         $url = $this->_defaultGatewayUrl;
     }
     $client = new Zend_Http_Client();
     $client->setUri($url);
     $client->setConfig(array('maxredirects' => 0, 'timeout' => 30));
     $client->setParameterGet('API', $api);
     $client->setParameterGet('XML', $requestXml);
     $response = $client->request()->getBody();
     $response = simplexml_load_string($response);
     if ($response === false || $response->getName() == 'Error') {
         $debugData['result'] = array('error' => $response->Description, 'code' => $response->Number, 'xml' => $response->asXML());
         $this->_debug($debugData);
         $result->setErrors($debugData['result']['error']);
     } else {
         if ($recipientUSCountry && $service == 'Priority Express') {
             $labelContent = base64_decode((string) $response->EMLabel);
             $trackingNumber = (string) $response->EMConfirmationNumber;
         } else {
             if ($recipientUSCountry) {
                 $labelContent = base64_decode((string) $response->SignatureConfirmationLabel);
                 $trackingNumber = (string) $response->SignatureConfirmationNumber;
             } else {
                 $labelContent = base64_decode((string) $response->LabelImage);
                 $trackingNumber = (string) $response->BarcodeNumber;
             }
         }
         $result->setShippingLabelContent($labelContent);
         $result->setTrackingNumber($trackingNumber);
     }
     $result->setGatewayResponse($response);
     $debugData['result'] = $response;
     $this->_debug($debugData);
     return $result;
 }
Example #4
0
 /**
  * Parse buyRequest into options values used by product
  *
  * @param  Varien_Object $buyRequest
  * @return Varien_Object
  */
 public function processBuyRequest(Varien_Object $buyRequest)
 {
     $options = new Varien_Object();
     /* add product custom options data */
     $customOptions = $buyRequest->getOptions();
     if (is_array($customOptions)) {
         foreach ($customOptions as $key => $value) {
             if ($value === '') {
                 unset($customOptions[$key]);
             }
         }
         $options->setOptions($customOptions);
     }
     /* add product type selected options data */
     $type = $this->getTypeInstance(true);
     $typeSpecificOptions = $type->processBuyRequest($this, $buyRequest);
     $options->addData($typeSpecificOptions);
     /* check correctness of product's options */
     $options->setErrors($type->checkProductConfiguration($this, $buyRequest));
     return $options;
 }
Example #5
0
 /**
  * Do shipment request to carrier web service, obtain Print Shipping Labels and process errors in response
  *
  * @param Varien_Object $request
  * @return Varien_Object
  */
 protected function _doShipmentRequest(Varien_Object $request)
 {
     $this->_prepareShipmentRequest($request);
     $result = new Varien_Object();
     $xmlRequest = $this->_formShipmentRequest($request);
     $xmlResponse = $this->_getCachedQuotes($xmlRequest);
     if ($xmlResponse === null) {
         $url = $this->getConfigData('url');
         if (!$url) {
             $url = $this->_defaultUrls['ShipConfirm'];
         }
         $debugData = array('request' => $xmlRequest);
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_HEADER, 0);
         curl_setopt($ch, CURLOPT_POST, 1);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequest);
         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (bool) $this->getConfigFlag('mode_xml'));
         $xmlResponse = curl_exec($ch);
         if ($xmlResponse === false) {
             throw new Exception(curl_error($ch));
         } else {
             $debugData['result'] = $xmlResponse;
             $this->_setCachedQuotes($xmlRequest, $xmlResponse);
         }
     }
     try {
         $response = new SimpleXMLElement($xmlResponse);
     } catch (Exception $e) {
         $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
         $result->setErrors($e->getMessage());
     }
     if (isset($response->Response->Error) && in_array($response->Response->Error->ErrorSeverity, array('Hard', 'Transient'))) {
         $result->setErrors((string) $response->Response->Error->ErrorDescription);
     }
     $this->_debug($debugData);
     if ($result->hasErrors() || empty($response)) {
         return $result;
     } else {
         return $this->_sendShipmentAcceptRequest($response);
     }
 }
Example #6
0
 /**
  * Do request to RMA shipment
  *
  * @param $request
  * @return array
  */
 public function returnOfShipment($request)
 {
     $request->setIsReturn(true);
     $packages = $request->getPackages();
     if (!is_array($packages) || !$packages) {
         Mage::throwException(Mage::helper('usa')->__('No packages for request'));
     }
     if ($request->getStoreId() != null) {
         $this->setStore($request->getStoreId());
     }
     $data = array();
     foreach ($packages as $packageId => $package) {
         $request->setPackageId($packageId);
         $request->setPackagingType($package['params']['container']);
         $request->setPackageWeight($package['params']['weight']);
         $request->setPackageParams(new Varien_Object($package['params']));
         $request->setPackageItems($package['items']);
         $result = $this->_doShipmentRequest($request);
         if ($result->hasErrors()) {
             $this->rollBack($data);
             break;
         } else {
             $data[] = array('tracking_number' => $result->getTrackingNumber(), 'label_content' => $result->getShippingLabelContent());
         }
         if (!isset($isFirstRequest)) {
             $request->setMasterTrackingId($result->getTrackingNumber());
             $isFirstRequest = false;
         }
     }
     $response = new Varien_Object(array('info' => $data));
     if ($result->getErrors()) {
         $response->setErrors($result->getErrors());
     }
     return $response;
 }
 /**
  * validate module before saving
  * @access public 
  * @return void
  * @author Marius Strajeru <*****@*****.**>
  */
 public function validateAction()
 {
     $errors = array();
     $response = new Varien_Object();
     $response->setError(false);
     $data = $this->getRequest()->getPost();
     $entities = $this->getRequest()->getPost('entity');
     $settings = $this->getRequest()->getPost('settings');
     if (empty($settings['namespace'])) {
         $error = new Varien_Object();
         $error->setField('settings_namespace');
         $error->setMessage(Mage::helper('modulecreator')->__('Fill in the Namespace'));
         $errors[] = $error->toArray();
     }
     if (empty($settings['module_name'])) {
         $error = new Varien_Object();
         $error->setField('settings_module_name');
         $error->setMessage(Mage::helper('modulecreator')->__('Fill in the Module name'));
         $errors[] = $error->toArray();
     } else {
         $validModule = Mage::helper('modulecreator')->validateModuleName($settings['module_name'], $settings['current_extension']);
         if (is_string($validModule)) {
             $error = new Varien_Object();
             $error->setMessage($validModule);
             $error->setField('settings_module_name');
             $errors[] = $error->toArray();
         }
     }
     $validExtension = Mage::helper('modulecreator')->validateExtensionName($settings['namespace'], $settings['module_name'], $settings['current_extension']);
     if (is_string($validExtension)) {
         $error = new Varien_Object();
         $error->setMessage($validExtension);
         $errors[] = $error->toArray();
     }
     if (empty($entities)) {
         $error = new Varien_Object();
         $error->setMessage(Mage::helper('modulecreator')->__('Add at least one entity'));
         $errors[] = $error->toArray();
     } else {
         //validate each entity
         $noAttributeEntities = false;
         $noNameEntities = false;
         foreach ($entities as $key => $entity) {
             if (empty($entity['name_singular'])) {
                 $error = new Varien_Object();
                 $error->setMessage(Mage::helper('modulecreator')->__('This is a required field.'));
                 $error->setField('entity_' . $key . '_name_singular');
                 $errors[] = $error->toArray();
             }
             if (empty($entity['name_plural'])) {
                 $error = new Varien_Object();
                 $error->setMessage(Mage::helper('modulecreator')->__('This is a required field.'));
                 $error->setField('entity_' . $key . '_name_plural');
                 $errors[] = $error->toArray();
             }
             if (!isset($entity['attributes']) && !$noAttributeEntities) {
                 $error = new Varien_Object();
                 $error->setMessage(Mage::helper('modulecreator')->__('There are entities without attribtues. Add attributes or remove them before continuing.'));
                 $errors[] = $error->toArray();
                 $noAttributeEntities = true;
             } elseif (isset($entity['attributes'])) {
                 //validate attributes
                 foreach ($entity['attributes'] as $attrKey => $attribute) {
                     if (is_numeric($attrKey)) {
                         $validAttribute = Mage::helper('modulecreator')->validateAttributeName($attribute['code']);
                         if (is_string($validAttribute)) {
                             $error = new Varien_Object();
                             $error->setMessage($validAttribute);
                             $error->setField('attribute_' . $key . '_' . $attrKey . '_code');
                             $errors[] = $error->toArray();
                         }
                     }
                 }
             }
             if ((!isset($entity['attributes']) || is_null($entity['attributes']['is_name'])) && !$noNameEntities) {
                 $error = new Varien_Object();
                 $error->setMessage(Mage::helper('modulecreator')->__('Each entity must have an attribute set to behave as "Name"'));
                 $errors[] = $error->toArray();
                 $noNameEntities = true;
             }
             $validEntity = Mage::helper('modulecreator')->validateEntityName($settings['module_name'], @$entity['name_singular'], $settings['current_extension']);
             if (is_string($validEntity)) {
                 $error = new Varien_Object();
                 $error->setMessage($validEntity);
                 $error->setField('entity_' . $key . '_name_singular');
                 $errors[] = $error->toArray();
             }
         }
     }
     try {
         $module = $this->_initModuleFromData($data);
     } catch (Exception $e) {
         $error = new Varien_Object();
         $error->setMessage($e->getMessage());
         $errors[] = $error->toArray();
     }
     if (count($errors) > 0) {
         $response->setError(true);
         $response->setErrors($errors);
     }
     $this->getResponse()->setBody($response->toJson());
 }
Example #8
0
 /**
  * Do shipment request to carrier web service, obtain Print Shipping Labels and process errors in response
  *
  * @param Varien_Object $request
  * @return Varien_Object
  */
 protected function _doShipmentRequest(Varien_Object $request)
 {
     $this->_prepareShipmentRequest($request);
     $result = new Varien_Object();
     try {
         $client = $this->_createLabelSoapClient();
         $requestShip = $this->_formShipmentRequest($request);
         $response = $client->GetPostageLabel($requestShip);
         $debugData['request_sent'] = $requestShip;
         //response includes string of PDF label - too big to log
         $debugString = "Status: " . $response->LabelRequestResponse->Status;
         if (isset($response->LabelRequestResponse->TrackingNumber)) {
             $debugString .= ' Tracking ID: ' . $response->LabelRequestResponse->TrackingNumber . ' Final Postage: ' . $response->LabelRequestResponse->FinalPostage;
         }
         $debugData['response'] = $debugString;
     } catch (Exception $e) {
         $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
         Mage::logException($e);
         $result->setErrors((string) 'an issue: ' . $e->getMessage());
     }
     if (is_object($response)) {
         if ($response->LabelRequestResponse->Status != 0) {
             $result->setErrors((string) $response->LabelRequestResponse->ErrorMessage);
             $debugData['response'] = $response->LabelRequestResponse->ErrorMessage;
         } else {
             $result->setShippingLabelContent(base64_decode($response->LabelRequestResponse->Base64LabelImage));
             $result->setTrackingNumber($response->LabelRequestResponse->TrackingNumber);
             $this->_recordCostToOrder($request->getOrderShipment()->getOrder(), $response->LabelRequestResponse->FinalPostage);
         }
     }
     $result->setGatewayResponse($client->__getLastResponse());
     if ($this->_debug) {
         Mage::helper('wsalogger/log')->postInfo('endicia', 'Request XML', $client->__getLastRequest());
         Mage::helper('wsalogger/log')->postInfo('endicia', 'Response', $debugData);
     }
     return $result;
 }
Example #9
0
 /**
  * Parse buyRequest into options values used by product
  *
  * @param  Varien_Object $buyRequest
  * @return Varien_Object
  */
 public function processBuyRequest(Varien_Object $buyRequest)
 {
     $options = new Varien_Object();
     /* add product custom options data */
     $customOptions = $buyRequest->getOptions();
     if (is_array($customOptions)) {
         $options->setOptions(array_diff($buyRequest->getOptions(), array('')));
     }
     /* add product type selected options data */
     $type = $this->getTypeInstance();
     $typeSpecificOptions = $type->processBuyRequest($this, $buyRequest);
     $options->addData($typeSpecificOptions);
     /* check correctness of product's options */
     $options->setErrors($type->checkProductConfiguration($this, $buyRequest));
     return $options;
 }