/**
  * Do shipment request to carrier web service, obtain Print Shipping Labels and process errors in response
  *
  * @param \Magento\Framework\DataObject $request
  * @return \Magento\Framework\DataObject
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function _doShipmentRequest(\Magento\Framework\DataObject $request)
 {
     $this->_prepareShipmentRequest($request);
     $result = new \Magento\Framework\DataObject();
     $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 = ['request' => $requestXml];
     $url = $this->getConfigData('gateway_secure_url');
     if (!$url) {
         $url = $this->_defaultGatewayUrl;
     }
     $client = $this->_httpClientFactory->create();
     $client->setUri($url);
     $client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
     $client->setParameterGet('API', $api);
     $client->setParameterGet('XML', $requestXml);
     $response = $client->request()->getBody();
     $response = $this->parseXml($response);
     if ($response === false || $response->getName() == 'Error') {
         $debugData['result'] = ['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;
         } elseif ($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;
 }
Пример #2
0
 /**
  * Send and process shipment accept request
  *
  * @param Element $shipmentConfirmResponse
  * @return \Magento\Framework\DataObject
  */
 protected function _sendShipmentAcceptRequest(Element $shipmentConfirmResponse)
 {
     $xmlRequest = $this->_xmlElFactory->create(['data' => '<?xml version = "1.0" ?><ShipmentAcceptRequest/>']);
     $request = $xmlRequest->addChild('Request');
     $request->addChild('RequestAction', 'ShipAccept');
     $xmlRequest->addChild('ShipmentDigest', $shipmentConfirmResponse->ShipmentDigest);
     $debugData = ['request' => $xmlRequest->asXML()];
     try {
         $ch = curl_init($this->getShipAcceptUrl());
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_HEADER, 0);
         curl_setopt($ch, CURLOPT_POST, 1);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_xmlAccessRequest . $xmlRequest->asXML());
         curl_setopt($ch, CURLOPT_TIMEOUT, 30);
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (bool) $this->getConfigFlag('mode_xml'));
         $xmlResponse = curl_exec($ch);
         $debugData['result'] = $xmlResponse;
         $this->_setCachedQuotes($xmlRequest, $xmlResponse);
     } catch (\Exception $e) {
         $debugData['result'] = ['error' => $e->getMessage(), 'code' => $e->getCode()];
         $xmlResponse = '';
     }
     try {
         $response = $this->_xmlElFactory->create(['data' => $xmlResponse]);
     } catch (\Exception $e) {
         $debugData['result'] = ['error' => $e->getMessage(), 'code' => $e->getCode()];
     }
     $result = new \Magento\Framework\DataObject();
     if (isset($response->Error)) {
         $result->setErrors((string) $response->Error->ErrorDescription);
     } else {
         $shippingLabelContent = (string) $response->ShipmentResults->PackageResults->LabelImage->GraphicImage;
         $trackingNumber = (string) $response->ShipmentResults->PackageResults->TrackingNumber;
         $result->setShippingLabelContent(base64_decode($shippingLabelContent));
         $result->setTrackingNumber($trackingNumber);
     }
     $this->_debug($debugData);
     return $result;
 }
Пример #3
0
 /**
  * Do shipment request to carrier web service, obtain Print Shipping Labels and process errors in response
  *
  * @param \Magento\Framework\DataObject $request
  * @return \Magento\Framework\DataObject
  */
 protected function _doShipmentRequest(\Magento\Framework\DataObject $request)
 {
     $this->_prepareShipmentRequest($request);
     $result = new \Magento\Framework\DataObject();
     $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 = $this->getTrackingNumber($response->CompletedShipmentDetail->CompletedPackageDetails->TrackingIds);
         $result->setShippingLabelContent($shippingLabelContent);
         $result->setTrackingNumber($trackingNumber);
         $debugData = ['request' => $client->__getLastRequest(), 'result' => $client->__getLastResponse()];
         $this->_debug($debugData);
     } else {
         $debugData = ['request' => $client->__getLastRequest(), 'result' => ['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;
 }
Пример #4
0
 /**
  * Prepare shipping label data
  *
  * @param \SimpleXMLElement $xml
  * @return \Magento\Framework\DataObject
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function _prepareShippingLabelContent(\SimpleXMLElement $xml)
 {
     $result = new \Magento\Framework\DataObject();
     try {
         if (!isset($xml->AirwayBillNumber) || !isset($xml->LabelImage->OutputImage)) {
             throw new \Magento\Framework\Exception\LocalizedException(__('Unable to retrieve shipping label'));
         }
         $result->setTrackingNumber((string) $xml->AirwayBillNumber);
         $labelContent = (string) $xml->LabelImage->OutputImage;
         $result->setShippingLabelContent(base64_decode($labelContent));
     } catch (\Exception $e) {
         throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()));
     }
     return $result;
 }