/**
  * 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;
 }
Example #2
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;
 }
Example #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
  * @throws \Exception
  */
 protected function _doShipmentRequest(\Magento\Framework\DataObject $request)
 {
     $this->_prepareShipmentRequest($request);
     $result = new \Magento\Framework\DataObject();
     $xmlRequest = $this->_formShipmentRequest($request);
     $xmlResponse = $this->_getCachedQuotes($xmlRequest);
     if ($xmlResponse === null) {
         $url = $this->getShipConfirmUrl();
         $debugData = ['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 = $this->_xmlElFactory->create(['data' => $xmlResponse]);
     } catch (\Exception $e) {
         $debugData['result'] = ['error' => $e->getMessage(), 'code' => $e->getCode()];
         $result->setErrors($e->getMessage());
     }
     if (isset($response->Response->Error) && in_array($response->Response->Error->ErrorSeverity, ['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 #4
0
 /**
  * Parse buyRequest into options values used by product
  *
  * @param  \Magento\Framework\DataObject $buyRequest
  * @return \Magento\Framework\DataObject
  */
 public function processBuyRequest(\Magento\Framework\DataObject $buyRequest)
 {
     $options = new \Magento\Framework\DataObject();
     /* add product custom options data */
     $customOptions = $buyRequest->getOptions();
     if (is_array($customOptions)) {
         array_filter($customOptions, function ($value) {
             return $value !== '';
         });
         $options->setOptions($customOptions);
     }
     /* 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;
 }
 /**
  * Do request to RMA shipment
  *
  * @param Request $request
  * @return \Magento\Framework\DataObject
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function returnOfShipment($request)
 {
     $request->setIsReturn(true);
     $packages = $request->getPackages();
     if (!is_array($packages) || !$packages) {
         throw new LocalizedException(__('No packages for request'));
     }
     if ($request->getStoreId() != null) {
         $this->setStore($request->getStoreId());
     }
     $data = [];
     foreach ($packages as $packageId => $package) {
         $request->setPackageId($packageId);
         $request->setPackagingType($package['params']['container']);
         $request->setPackageWeight($package['params']['weight']);
         $request->setPackageParams(new \Magento\Framework\DataObject($package['params']));
         $request->setPackageItems($package['items']);
         $result = $this->_doShipmentRequest($request);
         if ($result->hasErrors()) {
             $this->rollBack($data);
             break;
         } else {
             $data[] = ['tracking_number' => $result->getTrackingNumber(), 'label_content' => $result->getShippingLabelContent()];
         }
         if (!isset($isFirstRequest)) {
             $request->setMasterTrackingId($result->getTrackingNumber());
             $isFirstRequest = false;
         }
     }
     $response = new \Magento\Framework\DataObject(['info' => $data]);
     if ($result->getErrors()) {
         $response->setErrors($result->getErrors());
     }
     return $response;
 }