Exemple #1
0
 /**
  * Form array with appropriate structure for shipment request
  *
  * @param \Magento\Framework\DataObject $request
  * @return array
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 protected function _formShipmentRequest(\Magento\Framework\DataObject $request)
 {
     if ($request->getReferenceData()) {
         $referenceData = $request->getReferenceData() . $request->getPackageId();
     } else {
         $referenceData = 'Order #' . $request->getOrderShipment()->getOrder()->getIncrementId() . ' P' . $request->getPackageId();
     }
     $packageParams = $request->getPackageParams();
     $customsValue = $packageParams->getCustomsValue();
     $height = $packageParams->getHeight();
     $width = $packageParams->getWidth();
     $length = $packageParams->getLength();
     $weightUnits = $packageParams->getWeightUnits() == \Zend_Measure_Weight::POUND ? 'LB' : 'KG';
     $dimensionsUnits = $packageParams->getDimensionUnits() == \Zend_Measure_Length::INCH ? 'IN' : 'CM';
     $unitPrice = 0;
     $itemsQty = 0;
     $itemsDesc = [];
     $countriesOfManufacture = [];
     $productIds = [];
     $packageItems = $request->getPackageItems();
     foreach ($packageItems as $itemShipment) {
         $item = new \Magento\Framework\DataObject();
         $item->setData($itemShipment);
         $unitPrice += $item->getPrice();
         $itemsQty += $item->getQty();
         $itemsDesc[] = $item->getName();
         $productIds[] = $item->getProductId();
     }
     // get countries of manufacture
     $productCollection = $this->_productCollectionFactory->create()->addStoreFilter($request->getStoreId())->addFieldToFilter('entity_id', ['in' => $productIds])->addAttributeToSelect('country_of_manufacture');
     foreach ($productCollection as $product) {
         $countriesOfManufacture[] = $product->getCountryOfManufacture();
     }
     $paymentType = $request->getIsReturn() ? 'RECIPIENT' : 'SENDER';
     $optionType = $request->getShippingMethod() == self::RATE_REQUEST_SMARTPOST ? 'SERVICE_DEFAULT' : $packageParams->getDeliveryConfirmation();
     $requestClient = ['RequestedShipment' => ['ShipTimestamp' => time(), 'DropoffType' => $this->getConfigData('dropoff'), 'PackagingType' => $request->getPackagingType(), 'ServiceType' => $request->getShippingMethod(), 'Shipper' => ['Contact' => ['PersonName' => $request->getShipperContactPersonName(), 'CompanyName' => $request->getShipperContactCompanyName(), 'PhoneNumber' => $request->getShipperContactPhoneNumber()], 'Address' => ['StreetLines' => [$request->getShipperAddressStreet()], 'City' => $request->getShipperAddressCity(), 'StateOrProvinceCode' => $request->getShipperAddressStateOrProvinceCode(), 'PostalCode' => $request->getShipperAddressPostalCode(), 'CountryCode' => $request->getShipperAddressCountryCode()]], 'Recipient' => ['Contact' => ['PersonName' => $request->getRecipientContactPersonName(), 'CompanyName' => $request->getRecipientContactCompanyName(), 'PhoneNumber' => $request->getRecipientContactPhoneNumber()], 'Address' => ['StreetLines' => [$request->getRecipientAddressStreet()], 'City' => $request->getRecipientAddressCity(), 'StateOrProvinceCode' => $request->getRecipientAddressStateOrProvinceCode(), 'PostalCode' => $request->getRecipientAddressPostalCode(), 'CountryCode' => $request->getRecipientAddressCountryCode(), 'Residential' => (bool) $this->getConfigData('residence_delivery')]], 'ShippingChargesPayment' => ['PaymentType' => $paymentType, 'Payor' => ['AccountNumber' => $this->getConfigData('account'), 'CountryCode' => $this->_scopeConfig->getValue(\Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $request->getStoreId())]], 'LabelSpecification' => ['LabelFormatType' => 'COMMON2D', 'ImageType' => 'PNG', 'LabelStockType' => 'PAPER_8.5X11_TOP_HALF_LABEL'], 'RateRequestTypes' => ['ACCOUNT'], 'PackageCount' => 1, 'RequestedPackageLineItems' => ['SequenceNumber' => '1', 'Weight' => ['Units' => $weightUnits, 'Value' => $request->getPackageWeight()], 'CustomerReferences' => ['CustomerReferenceType' => 'CUSTOMER_REFERENCE', 'Value' => $referenceData], 'SpecialServicesRequested' => ['SpecialServiceTypes' => 'SIGNATURE_OPTION', 'SignatureOptionDetail' => ['OptionType' => $optionType]]]]];
     // for international shipping
     if ($request->getShipperAddressCountryCode() != $request->getRecipientAddressCountryCode()) {
         $requestClient['RequestedShipment']['CustomsClearanceDetail'] = ['CustomsValue' => ['Currency' => $request->getBaseCurrencyCode(), 'Amount' => $customsValue], 'DutiesPayment' => ['PaymentType' => $paymentType, 'Payor' => ['AccountNumber' => $this->getConfigData('account'), 'CountryCode' => $this->_scopeConfig->getValue(\Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $request->getStoreId())]], 'Commodities' => ['Weight' => ['Units' => $weightUnits, 'Value' => $request->getPackageWeight()], 'NumberOfPieces' => 1, 'CountryOfManufacture' => implode(',', array_unique($countriesOfManufacture)), 'Description' => implode(', ', $itemsDesc), 'Quantity' => ceil($itemsQty), 'QuantityUnits' => 'pcs', 'UnitPrice' => ['Currency' => $request->getBaseCurrencyCode(), 'Amount' => $unitPrice], 'CustomsValue' => ['Currency' => $request->getBaseCurrencyCode(), 'Amount' => $customsValue]]];
     }
     if ($request->getMasterTrackingId()) {
         $requestClient['RequestedShipment']['MasterTrackingId'] = $request->getMasterTrackingId();
     }
     if ($request->getShippingMethod() == self::RATE_REQUEST_SMARTPOST) {
         $requestClient['RequestedShipment']['SmartPostDetail'] = ['Indicia' => (double) $request->getPackageWeight() >= 1 ? 'PARCEL_SELECT' : 'PRESORTED_STANDARD', 'HubId' => $this->getConfigData('smartpost_hubid')];
     }
     // set dimensions
     if ($length || $width || $height) {
         $requestClient['RequestedShipment']['RequestedPackageLineItems']['Dimensions'] = [];
         $dimenssions =& $requestClient['RequestedShipment']['RequestedPackageLineItems']['Dimensions'];
         $dimenssions['Length'] = $length;
         $dimenssions['Width'] = $width;
         $dimenssions['Height'] = $height;
         $dimenssions['Units'] = $dimensionsUnits;
     }
     return $this->_getAuthDetails() + $requestClient;
 }
 /**
  * Form XML for US Signature Confirmation request
  * As integration guide it is important to follow appropriate sequence for tags e.g.: <FromLastName /> must be
  * after <FromFirstName />
  *
  * @param \Magento\Framework\DataObject $request
  * @param string $serviceType
  * @return string
  * @throws \Exception
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function _formUsSignatureConfirmationShipmentRequest(\Magento\Framework\DataObject $request, $serviceType)
 {
     switch ($serviceType) {
         case 'PRIORITY':
         case 'Priority':
             $serviceType = 'Priority';
             break;
         case 'FIRST CLASS':
         case 'First Class':
             $serviceType = 'First Class';
             break;
         case 'STANDARD':
         case 'Standard Post':
         case 'Retail Ground':
             $serviceType = 'Retail Ground';
             break;
         case 'MEDIA':
         case 'Media':
             $serviceType = 'Media Mail';
             break;
         case 'LIBRARY':
         case 'Library':
             $serviceType = 'Library Mail';
             break;
         default:
             throw new \Exception(__('Service type does not match'));
     }
     $packageParams = $request->getPackageParams();
     $packageWeight = $request->getPackageWeight();
     if ($packageParams->getWeightUnits() != \Zend_Measure_Weight::OUNCE) {
         $packageWeight = round($this->_carrierHelper->convertMeasureWeight($request->getPackageWeight(), $packageParams->getWeightUnits(), \Zend_Measure_Weight::OUNCE));
     }
     list($fromZip5, $fromZip4) = $this->_parseZip($request->getShipperAddressPostalCode());
     list($toZip5, $toZip4) = $this->_parseZip($request->getRecipientAddressPostalCode(), true);
     if ($this->getConfigData('mode')) {
         $rootNode = 'SignatureConfirmationV3.0Request';
     } else {
         $rootNode = 'SigConfirmCertifyV3.0Request';
     }
     // the wrap node needs for remove xml declaration above
     $xmlWrap = $this->_xmlElFactory->create(['data' => '<?xml version = "1.0" encoding = "UTF-8"?><wrap/>']);
     $xml = $xmlWrap->addChild($rootNode);
     $xml->addAttribute('USERID', $this->getConfigData('userid'));
     $xml->addChild('Option', 1);
     $xml->addChild('ImageParameters');
     $xml->addChild('FromName', $request->getShipperContactPersonName());
     $xml->addChild('FromFirm', $request->getShipperContactCompanyName());
     $xml->addChild('FromAddress1', $request->getShipperAddressStreet2());
     $xml->addChild('FromAddress2', $request->getShipperAddressStreet1());
     $xml->addChild('FromCity', $request->getShipperAddressCity());
     $xml->addChild('FromState', $request->getShipperAddressStateOrProvinceCode());
     $xml->addChild('FromZip5', $fromZip5);
     $xml->addChild('FromZip4', $fromZip4);
     $xml->addChild('ToName', $request->getRecipientContactPersonName());
     $xml->addChild('ToFirm', $request->getRecipientContactCompanyName());
     $xml->addChild('ToAddress1', $request->getRecipientAddressStreet2());
     $xml->addChild('ToAddress2', $request->getRecipientAddressStreet1());
     $xml->addChild('ToCity', $request->getRecipientAddressCity());
     $xml->addChild('ToState', $request->getRecipientAddressStateOrProvinceCode());
     $xml->addChild('ToZip5', $toZip5);
     $xml->addChild('ToZip4', $toZip4);
     $xml->addChild('WeightInOunces', $packageWeight);
     $xml->addChild('ServiceType', $serviceType);
     $xml->addChild('WaiverOfSignature', $packageParams->getDeliveryConfirmation());
     $xml->addChild('ImageType', 'PDF');
     $xml = $xmlWrap->{$rootNode}->asXML();
     return $xml;
 }
Exemple #3
0
 /**
  * Form XML for shipment request
  *
  * @param \Magento\Framework\DataObject $request
  * @return string
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 protected function _formShipmentRequest(\Magento\Framework\DataObject $request)
 {
     $packageParams = $request->getPackageParams();
     $height = $packageParams->getHeight();
     $width = $packageParams->getWidth();
     $length = $packageParams->getLength();
     $weightUnits = $packageParams->getWeightUnits() == \Zend_Measure_Weight::POUND ? 'LBS' : 'KGS';
     $dimensionsUnits = $packageParams->getDimensionUnits() == \Zend_Measure_Length::INCH ? 'IN' : 'CM';
     $itemsDesc = [];
     $itemsShipment = $request->getPackageItems();
     foreach ($itemsShipment as $itemShipment) {
         $item = new \Magento\Framework\DataObject();
         $item->setData($itemShipment);
         $itemsDesc[] = $item->getName();
     }
     $xmlRequest = $this->_xmlElFactory->create(['data' => '<?xml version = "1.0" ?><ShipmentConfirmRequest xml:lang="en-US"/>']);
     $requestPart = $xmlRequest->addChild('Request');
     $requestPart->addChild('RequestAction', 'ShipConfirm');
     $requestPart->addChild('RequestOption', 'nonvalidate');
     $shipmentPart = $xmlRequest->addChild('Shipment');
     if ($request->getIsReturn()) {
         $returnPart = $shipmentPart->addChild('ReturnService');
         // UPS Print Return Label
         $returnPart->addChild('Code', '9');
     }
     $shipmentPart->addChild('Description', substr(implode(' ', $itemsDesc), 0, 35));
     //empirical
     $shipperPart = $shipmentPart->addChild('Shipper');
     if ($request->getIsReturn()) {
         $shipperPart->addChild('Name', $request->getRecipientContactCompanyName());
         $shipperPart->addChild('AttentionName', $request->getRecipientContactPersonName());
         $shipperPart->addChild('ShipperNumber', $this->getConfigData('shipper_number'));
         $shipperPart->addChild('PhoneNumber', $request->getRecipientContactPhoneNumber());
         $addressPart = $shipperPart->addChild('Address');
         $addressPart->addChild('AddressLine1', $request->getRecipientAddressStreet());
         $addressPart->addChild('AddressLine2', $request->getRecipientAddressStreet2());
         $addressPart->addChild('City', $request->getRecipientAddressCity());
         $addressPart->addChild('CountryCode', $request->getRecipientAddressCountryCode());
         $addressPart->addChild('PostalCode', $request->getRecipientAddressPostalCode());
         if ($request->getRecipientAddressStateOrProvinceCode()) {
             $addressPart->addChild('StateProvinceCode', $request->getRecipientAddressStateOrProvinceCode());
         }
     } else {
         $shipperPart->addChild('Name', $request->getShipperContactCompanyName());
         $shipperPart->addChild('AttentionName', $request->getShipperContactPersonName());
         $shipperPart->addChild('ShipperNumber', $this->getConfigData('shipper_number'));
         $shipperPart->addChild('PhoneNumber', $request->getShipperContactPhoneNumber());
         $addressPart = $shipperPart->addChild('Address');
         $addressPart->addChild('AddressLine1', $request->getShipperAddressStreet());
         $addressPart->addChild('AddressLine2', $request->getShipperAddressStreet2());
         $addressPart->addChild('City', $request->getShipperAddressCity());
         $addressPart->addChild('CountryCode', $request->getShipperAddressCountryCode());
         $addressPart->addChild('PostalCode', $request->getShipperAddressPostalCode());
         if ($request->getShipperAddressStateOrProvinceCode()) {
             $addressPart->addChild('StateProvinceCode', $request->getShipperAddressStateOrProvinceCode());
         }
     }
     $shipToPart = $shipmentPart->addChild('ShipTo');
     $shipToPart->addChild('AttentionName', $request->getRecipientContactPersonName());
     $shipToPart->addChild('CompanyName', $request->getRecipientContactCompanyName() ? $request->getRecipientContactCompanyName() : 'N/A');
     $shipToPart->addChild('PhoneNumber', $request->getRecipientContactPhoneNumber());
     $addressPart = $shipToPart->addChild('Address');
     $addressPart->addChild('AddressLine1', $request->getRecipientAddressStreet1());
     $addressPart->addChild('AddressLine2', $request->getRecipientAddressStreet2());
     $addressPart->addChild('City', $request->getRecipientAddressCity());
     $addressPart->addChild('CountryCode', $request->getRecipientAddressCountryCode());
     $addressPart->addChild('PostalCode', $request->getRecipientAddressPostalCode());
     if ($request->getRecipientAddressStateOrProvinceCode()) {
         $addressPart->addChild('StateProvinceCode', $request->getRecipientAddressRegionCode());
     }
     if ($this->getConfigData('dest_type') == 'RES') {
         $addressPart->addChild('ResidentialAddress');
     }
     if ($request->getIsReturn()) {
         $shipFromPart = $shipmentPart->addChild('ShipFrom');
         $shipFromPart->addChild('AttentionName', $request->getShipperContactPersonName());
         $shipFromPart->addChild('CompanyName', $request->getShipperContactCompanyName() ? $request->getShipperContactCompanyName() : $request->getShipperContactPersonName());
         $shipFromAddress = $shipFromPart->addChild('Address');
         $shipFromAddress->addChild('AddressLine1', $request->getShipperAddressStreet1());
         $shipFromAddress->addChild('AddressLine2', $request->getShipperAddressStreet2());
         $shipFromAddress->addChild('City', $request->getShipperAddressCity());
         $shipFromAddress->addChild('CountryCode', $request->getShipperAddressCountryCode());
         $shipFromAddress->addChild('PostalCode', $request->getShipperAddressPostalCode());
         if ($request->getShipperAddressStateOrProvinceCode()) {
             $shipFromAddress->addChild('StateProvinceCode', $request->getShipperAddressStateOrProvinceCode());
         }
         $addressPart = $shipToPart->addChild('Address');
         $addressPart->addChild('AddressLine1', $request->getShipperAddressStreet1());
         $addressPart->addChild('AddressLine2', $request->getShipperAddressStreet2());
         $addressPart->addChild('City', $request->getShipperAddressCity());
         $addressPart->addChild('CountryCode', $request->getShipperAddressCountryCode());
         $addressPart->addChild('PostalCode', $request->getShipperAddressPostalCode());
         if ($request->getShipperAddressStateOrProvinceCode()) {
             $addressPart->addChild('StateProvinceCode', $request->getShipperAddressStateOrProvinceCode());
         }
         if ($this->getConfigData('dest_type') == 'RES') {
             $addressPart->addChild('ResidentialAddress');
         }
     }
     $servicePart = $shipmentPart->addChild('Service');
     $servicePart->addChild('Code', $request->getShippingMethod());
     $packagePart = $shipmentPart->addChild('Package');
     $packagePart->addChild('Description', substr(implode(' ', $itemsDesc), 0, 35));
     //empirical
     $packagePart->addChild('PackagingType')->addChild('Code', $request->getPackagingType());
     $packageWeight = $packagePart->addChild('PackageWeight');
     $packageWeight->addChild('Weight', $request->getPackageWeight());
     $packageWeight->addChild('UnitOfMeasurement')->addChild('Code', $weightUnits);
     // set dimensions
     if ($length || $width || $height) {
         $packageDimensions = $packagePart->addChild('Dimensions');
         $packageDimensions->addChild('UnitOfMeasurement')->addChild('Code', $dimensionsUnits);
         $packageDimensions->addChild('Length', $length);
         $packageDimensions->addChild('Width', $width);
         $packageDimensions->addChild('Height', $height);
     }
     // ups support reference number only for domestic service
     if ($this->_isUSCountry($request->getRecipientAddressCountryCode()) && $this->_isUSCountry($request->getShipperAddressCountryCode())) {
         if ($request->getReferenceData()) {
             $referenceData = $request->getReferenceData() . $request->getPackageId();
         } else {
             $referenceData = 'Order #' . $request->getOrderShipment()->getOrder()->getIncrementId() . ' P' . $request->getPackageId();
         }
         $referencePart = $packagePart->addChild('ReferenceNumber');
         $referencePart->addChild('Code', '02');
         $referencePart->addChild('Value', $referenceData);
     }
     $deliveryConfirmation = $packageParams->getDeliveryConfirmation();
     if ($deliveryConfirmation) {
         /** @var $serviceOptionsNode Element */
         $serviceOptionsNode = null;
         switch ($this->_getDeliveryConfirmationLevel($request->getRecipientAddressCountryCode())) {
             case self::DELIVERY_CONFIRMATION_PACKAGE:
                 $serviceOptionsNode = $packagePart->addChild('PackageServiceOptions');
                 break;
             case self::DELIVERY_CONFIRMATION_SHIPMENT:
                 $serviceOptionsNode = $shipmentPart->addChild('ShipmentServiceOptions');
                 break;
             default:
                 break;
         }
         if (!is_null($serviceOptionsNode)) {
             $serviceOptionsNode->addChild('DeliveryConfirmation')->addChild('DCISType', $packageParams->getDeliveryConfirmation());
         }
     }
     $shipmentPart->addChild('PaymentInformation')->addChild('Prepaid')->addChild('BillShipper')->addChild('AccountNumber', $this->getConfigData('shipper_number'));
     if ($request->getPackagingType() != $this->configHelper->getCode('container', 'ULE') && $request->getShipperAddressCountryCode() == self::USA_COUNTRY_ID && ($request->getRecipientAddressCountryCode() == 'CA' || $request->getRecipientAddressCountryCode() == 'PR')) {
         $invoiceLineTotalPart = $shipmentPart->addChild('InvoiceLineTotal');
         $invoiceLineTotalPart->addChild('CurrencyCode', $request->getBaseCurrencyCode());
         $invoiceLineTotalPart->addChild('MonetaryValue', ceil($packageParams->getCustomsValue()));
     }
     $labelPart = $xmlRequest->addChild('LabelSpecification');
     $labelPart->addChild('LabelPrintMethod')->addChild('Code', 'GIF');
     $labelPart->addChild('LabelImageFormat')->addChild('Code', 'GIF');
     $this->setXMLAccessRequest();
     $xmlRequest = $this->_xmlAccessRequest . $xmlRequest->asXml();
     return $xmlRequest;
 }