88 'ShipConfirm' =>
'https://wwwcie.ups.com/ups.app/xml/ShipConfirm',
89 'ShipAccept' =>
'https://wwwcie.ups.com/ups.app/xml/ShipAccept',
98 'ShipConfirm' =>
'https://onlinetools.ups.com/ups.app/xml/ShipConfirm',
99 'ShipAccept' =>
'https://onlinetools.ups.com/ups.app/xml/ShipAccept',
128 'UserId',
'Password',
'AccessLicenseNumber' 134 private $httpClientFactory;
161 \
Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
162 \Psr\Log\LoggerInterface
$logger,
164 \
Magento\Shipping\Model\Simplexml\ElementFactory $xmlElFactory,
165 \
Magento\Shipping\Model\Rate\ResultFactory $rateFactory,
166 \
Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
167 \
Magento\Shipping\Model\Tracking\ResultFactory $trackFactory,
168 \
Magento\Shipping\Model\Tracking\
Result\ErrorFactory $trackErrorFactory,
169 \
Magento\Shipping\Model\Tracking\
Result\StatusFactory $trackStatusFactory,
170 \
Magento\Directory\Model\RegionFactory $regionFactory,
171 \
Magento\Directory\Model\CountryFactory $countryFactory,
172 \
Magento\Directory\Model\CurrencyFactory $currencyFactory,
173 \
Magento\Directory\Helper\Data $directoryData,
175 \
Magento\Framework\Locale\FormatInterface $localeFormat,
198 $this->httpClientFactory = $httpClientFactory;
199 $this->_localeFormat = $localeFormat;
236 $rowRequest = new \Magento\Framework\DataObject();
239 $rowRequest->setAction($this->configHelper->getCode(
'action',
'single'));
240 $rowRequest->setProduct(
$request->getLimitMethod());
242 $rowRequest->setAction($this->configHelper->getCode(
'action',
'all'));
243 $rowRequest->setProduct(
'GND' . $this->
getConfigData(
'dest_type'));
246 if ($request->getUpsPickup()) {
251 $rowRequest->setPickup($this->configHelper->getCode(
'pickup', $pickup));
254 $container =
$request->getUpsContainer();
258 $rowRequest->setContainer($this->configHelper->getCode(
'container', $container));
261 $destType =
$request->getUpsDestType();
265 $rowRequest->setDestType($this->configHelper->getCode(
'dest_type', $destType));
268 $origCountry =
$request->getOrigCountry();
270 $origCountry = $this->_scopeConfig->getValue(
271 \
Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID,
272 \
Magento\Store\Model\ScopeInterface::SCOPE_STORE,
277 $rowRequest->setOrigCountry($this->_countryFactory->create()->load($origCountry)->getData(
'iso2_code'));
279 if (
$request->getOrigRegionCode()) {
280 $origRegionCode =
$request->getOrigRegionCode();
282 $origRegionCode = $this->_scopeConfig->getValue(
283 \
Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_REGION_ID,
284 \
Magento\Store\Model\ScopeInterface::SCOPE_STORE,
288 if (is_numeric($origRegionCode)) {
289 $origRegionCode = $this->_regionFactory->create()->load($origRegionCode)->getCode();
291 $rowRequest->setOrigRegionCode($origRegionCode);
294 $rowRequest->setOrigPostal(
$request->getOrigPostcode());
296 $rowRequest->setOrigPostal(
297 $this->_scopeConfig->getValue(
298 \
Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_ZIP,
299 \
Magento\Store\Model\ScopeInterface::SCOPE_STORE,
306 $rowRequest->setOrigCity(
$request->getOrigCity());
308 $rowRequest->setOrigCity(
309 $this->_scopeConfig->getValue(
310 \
Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_CITY,
311 \
Magento\Store\Model\ScopeInterface::SCOPE_STORE,
318 $destCountry =
$request->getDestCountryId();
324 if ($destCountry == self::USA_COUNTRY_ID && (
$request->getDestPostcode() ==
'00912' ||
335 $country = $this->_countryFactory->create()->load($destCountry);
336 $rowRequest->setDestCountry($country->getData(
'iso2_code') ?: $destCountry);
338 $rowRequest->setDestRegionCode(
$request->getDestRegionCode());
341 $rowRequest->setDestPostal(
$request->getDestPostcode());
348 $rowRequest->setWeight($weight);
350 $rowRequest->setFreeMethodWeight(
$request->getFreeMethodWeight());
353 $rowRequest->setValue(
$request->getPackageValue());
354 $rowRequest->setValueWithDiscount(
$request->getPackageValueWithDiscount());
356 if (
$request->getUpsUnitMeasure()) {
357 $unit =
$request->getUpsUnitMeasure();
361 $rowRequest->setUnitMeasure($unit);
362 $rowRequest->setIsReturn(
$request->getIsReturn());
363 $rowRequest->setBaseSubtotalInclTax(
$request->getBaseSubtotalInclTax());
365 $this->_rawRequest = $rowRequest;
384 if ($weight < $minWeight) {
385 $weight = $minWeight;
389 $weight = ceil($weight * 10) / 10;
435 $r->setWeight($weight);
436 $r->setAction($this->configHelper->getCode(
'action',
'single'));
437 $r->setProduct($freeMethod);
448 if (self::USA_COUNTRY_ID == $rowRequest->getDestCountry()) {
449 $destPostal = substr($rowRequest->getDestPostal(), 0, 5);
451 $destPostal = $rowRequest->getDestPostal();
455 'accept_UPS_license_agreement' =>
'yes',
456 '10_action' => $rowRequest->getAction(),
457 '13_product' => $rowRequest->getProduct(),
458 '14_origCountry' => $rowRequest->getOrigCountry(),
459 '15_origPostal' => $rowRequest->getOrigPostal(),
460 'origCity' => $rowRequest->getOrigCity(),
461 '19_destPostal' => $destPostal,
462 '22_destCountry' => $rowRequest->getDestCountry(),
463 '23_weight' => $rowRequest->getWeight(),
464 '47_rate_chart' => $rowRequest->getPickup(),
465 '48_container' => $rowRequest->getContainer(),
466 '49_residential' => $rowRequest->getDestType(),
467 'weight_std' => strtolower($rowRequest->getUnitMeasure()),
472 if ($responseBody ===
null) {
473 $debugData = [
'request' =>
$params];
479 $client = new \Zend_Http_Client();
480 $client->setUri(
$url);
481 $client->setConfig([
'maxredirects' => 0,
'timeout' => 30]);
482 $client->setParameterGet(
$params);
486 $debugData[
'result'] = $responseBody;
488 }
catch (\Throwable $e) {
489 $debugData[
'result'] = [
'error' => $e->getMessage(),
'code' => $e->getCode()];
492 $this->
_debug($debugData);
507 if ($origin ===
null) {
510 $arr = $this->configHelper->getCode(
'originShipment', $origin);
511 if (isset($arr[
$code])) {
531 $allowedMethods = explode(
",", $this->
getConfigData(
'allowed_methods'));
532 foreach ($rRows as $rRow) {
533 $row = explode(
'%', $rRow);
534 switch (substr(
$row[0], -1)) {
537 if (in_array(
$row[1], $allowedMethods)) {
538 $responsePrice = $this->_localeFormat->getNumber(
$row[8]);
539 $costArr[
$row[1]] = $responsePrice;
544 $errorTitle =
$row[1];
546 'Sorry, something went wrong. Please try again or contact us and we\'ll try to help.' 548 $this->_logger->debug(
$message .
': ' . $errorTitle);
551 if (in_array(
$row[3], $allowedMethods)) {
552 $responsePrice = $this->_localeFormat->getNumber(
$row[10]);
553 $costArr[
$row[3]] = $responsePrice;
564 $result = $this->_rateFactory->create();
566 if (empty($priceArr)) {
567 $error = $this->_rateErrorFactory->create();
568 $error->setCarrier(
'ups');
570 $error->setErrorMessage($this->
getConfigData(
'specificerrmsg'));
574 $rate = $this->_rateMethodFactory->create();
575 $rate->setCarrier(
'ups');
578 $methodArray = $this->configHelper->getCode(
'method',
$method);
579 $rate->setMethodTitle($methodArray);
606 if (self::USA_COUNTRY_ID == $rowRequest->getDestCountry()) {
607 $destPostal = substr($rowRequest->getDestPostal(), 0, 5);
609 $destPostal = $rowRequest->getDestPostal();
612 'accept_UPS_license_agreement' =>
'yes',
613 '10_action' => $rowRequest->getAction(),
614 '13_product' => $rowRequest->getProduct(),
615 '14_origCountry' => $rowRequest->getOrigCountry(),
616 '15_origPostal' => $rowRequest->getOrigPostal(),
617 'origCity' => $rowRequest->getOrigCity(),
618 'origRegionCode' => $rowRequest->getOrigRegionCode(),
619 '19_destPostal' => $destPostal,
620 '22_destCountry' => $rowRequest->getDestCountry(),
621 'destRegionCode' => $rowRequest->getDestRegionCode(),
622 '23_weight' => $rowRequest->getWeight(),
623 '47_rate_chart' => $rowRequest->getPickup(),
624 '48_container' => $rowRequest->getContainer(),
625 '49_residential' => $rowRequest->getDestType(),
628 if (
$params[
'10_action'] ==
'4') {
633 $serviceCode = $rowRequest->getProduct() ? $rowRequest->getProduct() :
null;
635 $serviceDescription = $serviceCode ? $this->
getShipmentByCode($serviceCode) :
'';
637 $xmlParams = <<<XMLRequest
638 <?xml version=
"1.0"?>
639 <RatingServiceSelectionRequest xml:lang=
"en-US">
641 <TransactionReference>
642 <CustomerContext>Rating and Service</CustomerContext>
643 <XpciVersion>1.0</XpciVersion>
644 </TransactionReference>
645 <RequestAction>Rate</RequestAction>
646 <RequestOption>{
$params[
'10_action']}</RequestOption>
649 <Code>{
$params[
'47_rate_chart'][
'code']}</Code>
650 <Description>{
$params[
'47_rate_chart'][
'label']}</Description>
656 if ($serviceCode !==
null) {
657 $xmlParams .=
"<Service>" .
658 "<Code>{$serviceCode}</Code>" .
659 "<Description>{$serviceDescription}</Description>" .
663 $xmlParams .= <<<XMLRequest
668 $xmlParams .=
"<ShipperNumber>{$shipperNumber}</ShipperNumber>";
671 if ($rowRequest->getIsReturn()) {
673 $shipperPostalCode =
$params[
'19_destPostal'];
674 $shipperCountryCode =
$params[
'22_destCountry'];
675 $shipperStateProvince =
$params[
'destRegionCode'];
677 $shipperCity =
$params[
'origCity'];
678 $shipperPostalCode =
$params[
'15_origPostal'];
679 $shipperCountryCode =
$params[
'14_origCountry'];
680 $shipperStateProvince =
$params[
'origRegionCode'];
683 $xmlParams .= <<<XMLRequest
685 <City>{$shipperCity}</City>
686 <PostalCode>{$shipperPostalCode}</PostalCode>
687 <CountryCode>{$shipperCountryCode}</CountryCode>
688 <StateProvinceCode>{$shipperStateProvince}</StateProvinceCode>
694 <PostalCode>{
$params[
'19_destPostal']}</PostalCode>
695 <CountryCode>{
$params[
'22_destCountry']}</CountryCode>
696 <ResidentialAddress>{
$params[
'49_residential']}</ResidentialAddress>
697 <StateProvinceCode>{
$params[
'destRegionCode']}</StateProvinceCode>
700 if (
$params[
'49_residential'] ===
'01') {
701 $xmlParams .=
"<ResidentialAddressIndicator>{$params['49_residential']}</ResidentialAddressIndicator>";
704 $xmlParams .= <<<XMLRequest
710 <PostalCode>{
$params[
'15_origPostal']}</PostalCode>
711 <CountryCode>{
$params[
'14_origCountry']}</CountryCode>
712 <StateProvinceCode>{
$params[
'origRegionCode']}</StateProvinceCode>
718 <Code>{
$params[
'48_container']}</Code>
722 <Code>{$rowRequest->getUnitMeasure()}</Code>
724 <Weight>{
$params[
'23_weight']}</Weight>
730 $xmlParams .=
"<RateInformation><NegotiatedRatesIndicator/></RateInformation>";
733 $xmlParams .=
"<TaxInformationIndicator/>";
736 $xmlParams .= <<<XMLRequest
738 </RatingServiceSelectionRequest>
741 $xmlRequest .= $xmlParams;
744 if ($xmlResponse ===
null) {
745 $debugData[
'request'] = $xmlParams;
747 $client = $this->httpClientFactory->create();
748 $client->post(
$url, $xmlRequest);
749 $xmlResponse = $client->getBody();
750 $debugData[
'result'] = $xmlResponse;
752 }
catch (\Throwable $e) {
753 $debugData[
'result'] = [
'error' => $e->getMessage(),
'code' => $e->getCode()];
756 $this->
_debug($debugData);
770 if (!$this->_baseCurrencyRate) {
771 $this->_baseCurrencyRate = $this->_currencyFactory->create()->load(
774 $this->_request->getBaseCurrency()->getCode()
787 private function mapCurrencyCode(
$code)
794 return isset($currencyMapping[
$code]) ? $currencyMapping[
$code] :
$code;
810 if (strlen(trim($xmlResponse)) > 0) {
811 $xml = new \Magento\Framework\Simplexml\Config();
812 $xml->loadString($xmlResponse);
813 $arr = $xml->getXpath(
"//RatingServiceSelectionResponse/Response/ResponseStatusCode/text()");
814 $success = (int)$arr[0];
815 if ($success === 1) {
816 $arr = $xml->getXpath(
"//RatingServiceSelectionResponse/RatedShipment");
817 $allowedMethods = explode(
",", $this->
getConfigData(
'allowed_methods'));
820 $negotiatedArr = $xml->getXpath(
"//RatingServiceSelectionResponse/RatedShipment/NegotiatedRates");
821 $negotiatedActive = $this->
getConfigFlag(
'negotiated_active')
823 && !empty($negotiatedArr);
825 $allowedCurrencies = $this->_currencyFactory->create()->getConfigAllowCurrencies();
826 foreach ($arr as $shipElement) {
827 $code = (string)$shipElement->Service->Code;
828 if (in_array(
$code, $allowedMethods)) {
831 if ($negotiatedActive) {
832 $includeTaxesArr = $xml->getXpath(
833 "//RatingServiceSelectionResponse/RatedShipment/NegotiatedRates" 834 .
"/NetSummaryCharges/TotalChargesWithTaxes" 836 $includeTaxesActive = $this->
getConfigFlag(
'include_taxes') && !empty($includeTaxesArr);
837 if ($includeTaxesActive) {
838 $cost = $shipElement->NegotiatedRates
840 ->TotalChargesWithTaxes
843 $responseCurrencyCode = $this->mapCurrencyCode(
844 (
string)$shipElement->NegotiatedRates
846 ->TotalChargesWithTaxes
850 $cost = $shipElement->NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue;
851 $responseCurrencyCode = $this->mapCurrencyCode(
852 (
string)$shipElement->NegotiatedRates->NetSummaryCharges->GrandTotal->CurrencyCode
856 $includeTaxesArr = $xml->getXpath(
857 "//RatingServiceSelectionResponse/RatedShipment/TotalChargesWithTaxes" 859 $includeTaxesActive = $this->
getConfigFlag(
'include_taxes') && !empty($includeTaxesArr);
860 if ($includeTaxesActive) {
861 $cost = $shipElement->TotalChargesWithTaxes->MonetaryValue;
862 $responseCurrencyCode = $this->mapCurrencyCode(
863 (
string)$shipElement->TotalChargesWithTaxes->CurrencyCode
866 $cost = $shipElement->TotalCharges->MonetaryValue;
867 $responseCurrencyCode = $this->mapCurrencyCode(
868 (
string)$shipElement->TotalCharges->CurrencyCode
874 $successConversion =
true;
875 if ($responseCurrencyCode) {
876 if (in_array($responseCurrencyCode, $allowedCurrencies)) {
880 'We can\'t convert a rate from "%1-%2".',
881 $responseCurrencyCode,
882 $this->_request->getPackageCurrency()->getCode()
884 $error = $this->_rateErrorFactory->create();
885 $error->setCarrier(
'ups');
887 $error->setErrorMessage($errorTitle);
888 $successConversion =
false;
892 if ($successConversion) {
893 $costArr[
$code] = $cost;
899 $arr = $xml->getXpath(
"//RatingServiceSelectionResponse/Response/Error/ErrorDescription/text()");
900 $errorTitle = (string)$arr[0][0];
901 $error = $this->_rateErrorFactory->create();
902 $error->setCarrier(
'ups');
904 $error->setErrorMessage($this->
getConfigData(
'specificerrmsg'));
908 $result = $this->_rateFactory->create();
910 if (empty($priceArr)) {
911 $error = $this->_rateErrorFactory->create();
912 $error->setCarrier(
'ups');
917 if (!isset($errorTitle)) {
918 $errorTitle =
__(
'Cannot retrieve shipping rates');
920 $error->setErrorMessage($errorTitle);
924 $rate = $this->_rateMethodFactory->create();
925 $rate->setCarrier(
'ups');
929 $rate->setMethodTitle($methodArr);
947 if (!is_array($trackings)) {
948 $trackings = [$trackings];
972 $this->_xmlAccessRequest = <<<XMLAuth
973 <?xml version=
"1.0" ?>
974 <AccessRequest xml:lang=
"en-US">
975 <AccessLicenseNumber>$accessKey</AccessLicenseNumber>
976 <UserId>$userId</UserId>
977 <Password>$userIdPass</Password>
992 $result = $this->_trackFactory->create();
993 foreach ($trackings as $tracking) {
994 $status = $this->_trackStatusFactory->create();
997 $status->setTracking($tracking);
1000 "http://wwwapps.ups.com/WebTracking/processInputRequest?HTMLVersion=5.0&error_carried=true" .
1001 "&tracknums_displayed=5&TypeOfInquiryNumber=T&loc=en_US&InquiryNumber1={$tracking}" .
1002 "&AgreeToTermsAndConditions=yes" 1022 foreach ($trackings as $tracking) {
1026 $xmlRequest = <<<XMLAuth
1027 <?xml version=
"1.0" ?>
1028 <TrackRequest xml:lang=
"en-US">
1030 <RequestAction>Track</RequestAction>
1031 <RequestOption>1</RequestOption>
1033 <TrackingNumber>$tracking</TrackingNumber>
1034 <IncludeFreight>01</IncludeFreight>
1037 $debugData[
'request'] = $this->
filterDebugData($this->_xmlAccessRequest) . $xmlRequest;
1039 $client = $this->httpClientFactory->create();
1040 $client->post(
$url, $this->_xmlAccessRequest . $xmlRequest);
1041 $xmlResponse = $client->getBody();
1042 $debugData[
'result'] = $xmlResponse;
1043 }
catch (\Throwable $e) {
1044 $debugData[
'result'] = [
'error' => $e->getMessage(),
'code' => $e->getCode()];
1048 $this->
_debug($debugData);
1066 $errorTitle =
'For some reason we can\'t retrieve tracking info right now.';
1068 $packageProgress = [];
1071 $xml = new \Magento\Framework\Simplexml\Config();
1072 $xml->loadString($xmlResponse);
1073 $arr = $xml->getXpath(
"//TrackResponse/Response/ResponseStatusCode/text()");
1074 $success = (int)$arr[0][0];
1076 if ($success === 1) {
1077 $arr = $xml->getXpath(
"//TrackResponse/Shipment/Service/Description/text()");
1078 $resultArr[
'service'] = (string)$arr[0];
1080 $arr = $xml->getXpath(
"//TrackResponse/Shipment/PickupDate/text()");
1081 $resultArr[
'shippeddate'] = (string)$arr[0];
1083 $arr = $xml->getXpath(
"//TrackResponse/Shipment/Package/PackageWeight/Weight/text()");
1084 $weight = (string)$arr[0];
1086 $arr = $xml->getXpath(
"//TrackResponse/Shipment/Package/PackageWeight/UnitOfMeasurement/Code/text()");
1087 $unit = (string)$arr[0];
1089 $resultArr[
'weight'] =
"{$weight} {$unit}";
1091 $activityTags = $xml->getXpath(
"//TrackResponse/Shipment/Package/Activity");
1092 if ($activityTags) {
1094 foreach ($activityTags as $activityTag) {
1096 if (isset($activityTag->ActivityLocation->Address->City)) {
1097 $addressArr[] = (string)$activityTag->ActivityLocation->Address->City;
1099 if (isset($activityTag->ActivityLocation->Address->StateProvinceCode)) {
1100 $addressArr[] = (string)$activityTag->ActivityLocation->Address->StateProvinceCode;
1102 if (isset($activityTag->ActivityLocation->Address->CountryCode)) {
1103 $addressArr[] = (string)$activityTag->ActivityLocation->Address->CountryCode;
1106 $date = (string)$activityTag->Date;
1108 $dateArr[] = substr($date, 0, 4);
1109 $dateArr[] = substr($date, 4, 2);
1110 $dateArr[] = substr($date, -2, 2);
1113 $time = (string)$activityTag->Time;
1115 $timeArr[] = substr($time, 0, 2);
1116 $timeArr[] = substr($time, 2, 2);
1117 $timeArr[] = substr($time, -2, 2);
1120 $resultArr[
'status'] = (string)$activityTag->Status->StatusType->Description;
1121 $resultArr[
'deliverydate'] = implode(
'-', $dateArr);
1123 $resultArr[
'deliverytime'] = implode(
':', $timeArr);
1125 $resultArr[
'deliverylocation'] = (string)$activityTag->ActivityLocation->Description;
1126 $resultArr[
'signedby'] = (
string)$activityTag->ActivityLocation->SignedForByName;
1128 $resultArr[
'deliveryto'] = implode(
', ', $addressArr);
1132 $tempArr[
'activity'] = (string)$activityTag->Status->StatusType->Description;
1133 $tempArr[
'deliverydate'] = implode(
'-', $dateArr);
1135 $tempArr[
'deliverytime'] = implode(
':', $timeArr);
1138 $tempArr[
'deliverylocation'] = implode(
', ', $addressArr);
1140 $packageProgress[] = $tempArr;
1144 $resultArr[
'progressdetail'] = $packageProgress;
1147 $arr = $xml->getXpath(
"//TrackResponse/Response/Error/ErrorDescription/text()");
1148 $errorTitle = (string)$arr[0][0];
1152 if (!$this->_result) {
1153 $this->_result = $this->_trackFactory->create();
1157 $tracking = $this->_trackStatusFactory->create();
1158 $tracking->setCarrier(
'ups');
1160 $tracking->setTracking($trackingValue);
1161 $tracking->addData($resultArr);
1162 $this->_result->append($tracking);
1164 $error = $this->_trackErrorFactory->create();
1165 $error->setCarrier(
'ups');
1167 $error->setTracking($trackingValue);
1168 $error->setErrorMessage($errorTitle);
1169 $this->_result->append($error);
1183 if ($this->_result instanceof \
Magento\Shipping\Model\Tracking\
Result) {
1184 $trackings = $this->_result->getAllTrackings();
1186 foreach ($trackings as $tracking) {
1187 $data = $tracking->getAllData();
1189 if (isset(
$data[
'status'])) {
1190 $statuses .=
__(
$data[
'status']);
1192 $statuses .=
__(
$data[
'error_message']);
1198 if (empty($statuses)) {
1199 $statuses =
__(
'Empty response');
1212 $allowed = explode(
',', $this->
getConfigData(
'allowed_methods'));
1215 foreach ($allowed as
$code) {
1231 protected function _formShipmentRequest(\
Magento\Framework\DataObject
$request)
1233 $packageParams =
$request->getPackageParams();
1234 $height = $packageParams->getHeight();
1235 $width = $packageParams->getWidth();
1236 $length = $packageParams->getLength();
1241 $itemsShipment =
$request->getPackageItems();
1242 foreach ($itemsShipment as $itemShipment) {
1243 $item = new \Magento\Framework\DataObject();
1244 $item->setData($itemShipment);
1245 $itemsDesc[] =
$item->getName();
1248 $xmlRequest = $this->_xmlElFactory->create(
1249 [
'data' =>
'<?xml version = "1.0" ?><ShipmentConfirmRequest xml:lang="en-US"/>']
1251 $requestPart = $xmlRequest->addChild(
'Request');
1252 $requestPart->addChild(
'RequestAction',
'ShipConfirm');
1253 $requestPart->addChild(
'RequestOption',
'nonvalidate');
1255 $shipmentPart = $xmlRequest->addChild(
'Shipment');
1257 $returnPart = $shipmentPart->addChild(
'ReturnService');
1259 $returnPart->addChild(
'Code',
'9');
1261 $shipmentPart->addChild(
'Description', substr(implode(
' ', $itemsDesc), 0, 35));
1264 $shipperPart = $shipmentPart->addChild(
'Shipper');
1266 $shipperPart->addChild(
'Name',
$request->getRecipientContactCompanyName());
1267 $shipperPart->addChild(
'AttentionName',
$request->getRecipientContactPersonName());
1268 $shipperPart->addChild(
'ShipperNumber', $this->
getConfigData(
'shipper_number'));
1269 $shipperPart->addChild(
'PhoneNumber',
$request->getRecipientContactPhoneNumber());
1271 $addressPart = $shipperPart->addChild(
'Address');
1272 $addressPart->addChild(
'AddressLine1',
$request->getRecipientAddressStreet());
1273 $addressPart->addChild(
'AddressLine2',
$request->getRecipientAddressStreet2());
1274 $addressPart->addChild(
'City',
$request->getRecipientAddressCity());
1275 $addressPart->addChild(
'CountryCode',
$request->getRecipientAddressCountryCode());
1276 $addressPart->addChild(
'PostalCode',
$request->getRecipientAddressPostalCode());
1277 if (
$request->getRecipientAddressStateOrProvinceCode()) {
1278 $addressPart->addChild(
'StateProvinceCode',
$request->getRecipientAddressStateOrProvinceCode());
1281 $shipperPart->addChild(
'Name',
$request->getShipperContactCompanyName());
1282 $shipperPart->addChild(
'AttentionName',
$request->getShipperContactPersonName());
1283 $shipperPart->addChild(
'ShipperNumber', $this->
getConfigData(
'shipper_number'));
1284 $shipperPart->addChild(
'PhoneNumber',
$request->getShipperContactPhoneNumber());
1286 $addressPart = $shipperPart->addChild(
'Address');
1287 $addressPart->addChild(
'AddressLine1',
$request->getShipperAddressStreet());
1288 $addressPart->addChild(
'AddressLine2',
$request->getShipperAddressStreet2());
1289 $addressPart->addChild(
'City',
$request->getShipperAddressCity());
1290 $addressPart->addChild(
'CountryCode',
$request->getShipperAddressCountryCode());
1291 $addressPart->addChild(
'PostalCode',
$request->getShipperAddressPostalCode());
1292 if (
$request->getShipperAddressStateOrProvinceCode()) {
1293 $addressPart->addChild(
'StateProvinceCode',
$request->getShipperAddressStateOrProvinceCode());
1297 $shipToPart = $shipmentPart->addChild(
'ShipTo');
1298 $shipToPart->addChild(
'AttentionName',
$request->getRecipientContactPersonName());
1299 $shipToPart->addChild(
1301 $request->getRecipientContactCompanyName() ?
$request->getRecipientContactCompanyName() :
'N/A' 1303 $shipToPart->addChild(
'PhoneNumber',
$request->getRecipientContactPhoneNumber());
1305 $addressPart = $shipToPart->addChild(
'Address');
1306 $addressPart->addChild(
'AddressLine1',
$request->getRecipientAddressStreet1());
1307 $addressPart->addChild(
'AddressLine2',
$request->getRecipientAddressStreet2());
1308 $addressPart->addChild(
'City',
$request->getRecipientAddressCity());
1309 $addressPart->addChild(
'CountryCode',
$request->getRecipientAddressCountryCode());
1310 $addressPart->addChild(
'PostalCode',
$request->getRecipientAddressPostalCode());
1311 if (
$request->getRecipientAddressStateOrProvinceCode()) {
1312 $addressPart->addChild(
'StateProvinceCode',
$request->getRecipientAddressRegionCode());
1315 $addressPart->addChild(
'ResidentialAddress');
1319 $shipFromPart = $shipmentPart->addChild(
'ShipFrom');
1320 $shipFromPart->addChild(
'AttentionName',
$request->getShipperContactPersonName());
1321 $shipFromPart->addChild(
1324 ->getShipperContactCompanyName() :
$request 1325 ->getShipperContactPersonName()
1327 $shipFromAddress = $shipFromPart->addChild(
'Address');
1328 $shipFromAddress->addChild(
'AddressLine1',
$request->getShipperAddressStreet1());
1329 $shipFromAddress->addChild(
'AddressLine2',
$request->getShipperAddressStreet2());
1330 $shipFromAddress->addChild(
'City',
$request->getShipperAddressCity());
1331 $shipFromAddress->addChild(
'CountryCode',
$request->getShipperAddressCountryCode());
1332 $shipFromAddress->addChild(
'PostalCode',
$request->getShipperAddressPostalCode());
1333 if (
$request->getShipperAddressStateOrProvinceCode()) {
1334 $shipFromAddress->addChild(
'StateProvinceCode',
$request->getShipperAddressStateOrProvinceCode());
1337 $addressPart = $shipToPart->addChild(
'Address');
1338 $addressPart->addChild(
'AddressLine1',
$request->getShipperAddressStreet1());
1339 $addressPart->addChild(
'AddressLine2',
$request->getShipperAddressStreet2());
1340 $addressPart->addChild(
'City',
$request->getShipperAddressCity());
1341 $addressPart->addChild(
'CountryCode',
$request->getShipperAddressCountryCode());
1342 $addressPart->addChild(
'PostalCode',
$request->getShipperAddressPostalCode());
1343 if (
$request->getShipperAddressStateOrProvinceCode()) {
1344 $addressPart->addChild(
'StateProvinceCode',
$request->getShipperAddressStateOrProvinceCode());
1347 $addressPart->addChild(
'ResidentialAddress');
1351 $servicePart = $shipmentPart->addChild(
'Service');
1352 $servicePart->addChild(
'Code',
$request->getShippingMethod());
1353 $packagePart = $shipmentPart->addChild(
'Package');
1354 $packagePart->addChild(
'Description', substr(implode(
' ', $itemsDesc), 0, 35));
1356 $packagePart->addChild(
'PackagingType')->addChild(
'Code',
$request->getPackagingType());
1357 $packageWeight = $packagePart->addChild(
'PackageWeight');
1358 $packageWeight->addChild(
'Weight',
$request->getPackageWeight());
1359 $packageWeight->addChild(
'UnitOfMeasurement')->addChild(
'Code', $weightUnits);
1362 if ($length || $width || $height) {
1363 $packageDimensions = $packagePart->addChild(
'Dimensions');
1364 $packageDimensions->addChild(
'UnitOfMeasurement')->addChild(
'Code', $dimensionsUnits);
1365 $packageDimensions->addChild(
'Length', $length);
1366 $packageDimensions->addChild(
'Width', $width);
1367 $packageDimensions->addChild(
'Height', $height);
1371 if ($this->
_isUSCountry($request->getRecipientAddressCountryCode())
1372 && $this->
_isUSCountry($request->getShipperAddressCountryCode())
1374 if (
$request->getReferenceData()) {
1377 $referenceData =
'Order #' .
1378 $request->getOrderShipment()->getOrder()->getIncrementId() .
1382 $referencePart = $packagePart->addChild(
'ReferenceNumber');
1383 $referencePart->addChild(
'Code',
'02');
1384 $referencePart->addChild(
'Value', $referenceData);
1387 $deliveryConfirmation = $packageParams->getDeliveryConfirmation();
1388 if ($deliveryConfirmation) {
1390 $serviceOptionsNode =
null;
1393 $serviceOptionsNode = $packagePart->addChild(
'PackageServiceOptions');
1396 $serviceOptionsNode = $shipmentPart->addChild(
'ShipmentServiceOptions');
1401 if ($serviceOptionsNode !==
null) {
1402 $serviceOptionsNode->addChild(
1403 'DeliveryConfirmation' 1406 $packageParams->getDeliveryConfirmation()
1411 $shipmentPart->addChild(
'PaymentInformation')
1412 ->addChild(
'Prepaid')
1413 ->addChild(
'BillShipper')
1414 ->addChild(
'AccountNumber', $this->
getConfigData(
'shipper_number'));
1416 if ($request->getPackagingType() != $this->configHelper->getCode(
'container',
'ULE')
1417 &&
$request->getShipperAddressCountryCode() == self::USA_COUNTRY_ID
1418 && (
$request->getRecipientAddressCountryCode() ==
'CA' 1419 ||
$request->getRecipientAddressCountryCode() ==
'PR')
1421 $invoiceLineTotalPart = $shipmentPart->addChild(
'InvoiceLineTotal');
1422 $invoiceLineTotalPart->addChild(
'CurrencyCode',
$request->getBaseCurrencyCode());
1423 $invoiceLineTotalPart->addChild(
'MonetaryValue', ceil($packageParams->getCustomsValue()));
1426 $labelPart = $xmlRequest->addChild(
'LabelSpecification');
1427 $labelPart->addChild(
'LabelPrintMethod')->addChild(
'Code',
'GIF');
1428 $labelPart->addChild(
'LabelImageFormat')->addChild(
'Code',
'GIF');
1430 return $xmlRequest->asXml();
1441 $xmlRequest = $this->_xmlElFactory->create(
1442 [
'data' =>
'<?xml version = "1.0" ?><ShipmentAcceptRequest/>']
1444 $request = $xmlRequest->addChild(
'Request');
1445 $request->addChild(
'RequestAction',
'ShipAccept');
1446 $xmlRequest->addChild(
'ShipmentDigest', $shipmentConfirmResponse->ShipmentDigest);
1447 $debugData = [
'request' => $this->
filterDebugData($this->_xmlAccessRequest) . $xmlRequest->asXML()];
1450 $client = $this->httpClientFactory->create();
1451 $client->post($this->
getShipAcceptUrl(), $this->_xmlAccessRequest . $xmlRequest->asXML());
1452 $xmlResponse = $client->getBody();
1453 $debugData[
'result'] = $xmlResponse;
1455 }
catch (\Throwable $e) {
1456 $debugData[
'result'] = [
'error' => $e->getMessage(),
'code' => $e->getCode()];
1461 $response = $this->_xmlElFactory->create([
'data' => $xmlResponse]);
1462 }
catch (\Throwable $e) {
1463 $debugData[
'result'] = [
'error' => $e->getMessage(),
'code' => $e->getCode()];
1466 $result = new \Magento\Framework\DataObject();
1470 $shippingLabelContent = (string)
$response->ShipmentResults->PackageResults->LabelImage->GraphicImage;
1471 $trackingNumber = (
string)
$response->ShipmentResults->PackageResults->TrackingNumber;
1473 $result->setShippingLabelContent(base64_decode($shippingLabelContent));
1474 $result->setTrackingNumber($trackingNumber);
1477 $this->
_debug($debugData);
1490 $url = $this->_liveUrls[
'ShipAccept'];
1492 $url = $this->_defaultUrls[
'ShipAccept'];
1507 $result = new \Magento\Framework\DataObject();
1508 $rawXmlRequest = $this->_formShipmentRequest(
$request);
1510 $xmlRequest = $this->_xmlAccessRequest . $rawXmlRequest;
1513 if ($xmlResponse ===
null) {
1514 $debugData[
'request'] = $this->
filterDebugData($this->_xmlAccessRequest) . $rawXmlRequest;
1516 $client = $this->httpClientFactory->create();
1518 $client->post(
$url, $xmlRequest);
1519 $xmlResponse = $client->getBody();
1520 $debugData[
'result'] = $xmlResponse;
1522 }
catch (\Throwable $e) {
1523 $debugData[
'result'] = [
'code' => $e->getCode(),
'error' => $e->getMessage()];
1528 $response = $this->_xmlElFactory->create([
'data' => $xmlResponse]);
1529 }
catch (\Throwable $e) {
1530 $debugData[
'result'] = [
'error' => $e->getMessage(),
'code' => $e->getCode()];
1531 $result->setErrors($e->getMessage());
1535 && in_array(
$response->Response->Error->ErrorSeverity, [
'Hard',
'Transient'])
1540 $this->
_debug($debugData);
1559 $url = $this->_liveUrls[
'ShipConfirm'];
1563 $url = $this->_defaultUrls[
'ShipConfirm'];
1585 $countryShipper =
$params->getCountryShipper();
1586 $countryRecipient =
$params->getCountryRecipient();
1588 if ($countryShipper == self::USA_COUNTRY_ID && $countryRecipient == self::CANADA_COUNTRY_ID ||
1589 $countryShipper == self::CANADA_COUNTRY_ID && $countryRecipient == self::USA_COUNTRY_ID ||
1590 $countryShipper == self::MEXICO_COUNTRY_ID && $countryRecipient == self::USA_COUNTRY_ID &&
$method ==
'11' 1592 $containerTypes = [];
1597 '01' =>
__(
'UPS Letter Envelope'),
1598 '24' =>
__(
'UPS Worldwide 25 kilo'),
1599 '25' =>
__(
'UPS Worldwide 10 kilo'),
1602 $containerTypes = $containerTypes + [
1603 '03' =>
__(
'UPS Tube'),
1605 '2a' =>
__(
'Small Express Box'),
1606 '2b' =>
__(
'Medium Express Box'),
1607 '2c' =>
__(
'Large Express Box'),
1611 return [
'00' =>
__(
'Customer Packaging')] + $containerTypes;
1612 }
elseif ($countryShipper == self::USA_COUNTRY_ID &&
1613 $countryRecipient == self::PUERTORICO_COUNTRY_ID &&
1619 $params->setCountryRecipient(self::USA_COUNTRY_ID);
1621 $params->setCountryRecipient($countryRecipient);
1623 return $containerTypes;
1636 $codes = $this->configHelper->getCode(
'container');
1637 $descriptions = $this->configHelper->getCode(
'container_description');
1639 foreach ($codes as $key => &
$code) {
1653 return $this->configHelper->getCode(
'containers_filter');
1664 $countryRecipient =
$params !=
null ?
$params->getCountryRecipient() :
null;
1665 $deliveryConfirmationTypes = [];
1668 $deliveryConfirmationTypes = [
1669 1 =>
__(
'Delivery Confirmation'),
1670 2 =>
__(
'Signature Required'),
1671 3 =>
__(
'Adult Signature Required'),
1675 $deliveryConfirmationTypes = [1 =>
__(
'Signature Required'), 2 =>
__(
'Adult Signature Required')];
1680 array_unshift($deliveryConfirmationTypes,
__(
'Not Required'));
1682 return $deliveryConfirmationTypes;
1693 $containerTypes = $this->configHelper->getCode(
'container');
1694 foreach (parent::getCustomizableContainerTypes() as $containerType) {
1695 $result[$containerType] = $containerTypes[$containerType];
1710 if ($countyDestination ===
null) {
1714 if ($countyDestination == self::USA_COUNTRY_ID) {
_prepareShipmentRequest(\Magento\Framework\DataObject $request)
_getCorrectWeight($weight)
if(!defined( 'PHP_VERSION_ID')||!(PHP_VERSION_ID===70002||PHP_VERSION_ID===70004||PHP_VERSION_ID >=70006))
getCustomizableContainerTypes()
elseif(isset( $params[ 'redirect_parent']))
const PUERTORICO_COUNTRY_ID
_parseXmlResponse($xmlResponse)
getTotalNumOfBoxes($weight)
$_debugReplacePrivateDataKeys
const DELIVERY_CONFIRMATION_PACKAGE
_sendShipmentAcceptRequest(Element $shipmentConfirmResponse)
getMethodPrice($cost, $method='')
const DELIVERY_CONFIRMATION_SHIPMENT
_getCachedQuotes($requestParams)
_getBaseCurrencyRate($code)
_setCachedQuotes($requestParams, $response)
_getCgiTracking($trackings)
getContainerTypesFilter()
setRequest(RateRequest $request)
_parseXmlTrackingResponse($trackingValue, $xmlResponse)
getDeliveryConfirmationTypes(\Magento\Framework\DataObject $params=null)
_getAllowedContainers(\Magento\Framework\DataObject $params=null)
_parseCgiResponse($response)
getShipmentByCode($code, $origin=null)
_getDeliveryConfirmationLevel($countyDestination=null)
_updateFreeMethodQuote($request)
_getXmlTracking($trackings)
$_customizableContainerTypes
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
_doShipmentRequest(\Magento\Framework\DataObject $request)
_setFreeMethodRequest($freeMethod)
collectRates(RateRequest $request)
__construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory, \Psr\Log\LoggerInterface $logger, Security $xmlSecurity, \Magento\Shipping\Model\Simplexml\ElementFactory $xmlElFactory, \Magento\Shipping\Model\Rate\ResultFactory $rateFactory, \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory, \Magento\Shipping\Model\Tracking\ResultFactory $trackFactory, \Magento\Shipping\Model\Tracking\Result\ErrorFactory $trackErrorFactory, \Magento\Shipping\Model\Tracking\Result\StatusFactory $trackStatusFactory, \Magento\Directory\Model\RegionFactory $regionFactory, \Magento\Directory\Model\CountryFactory $countryFactory, \Magento\Directory\Model\CurrencyFactory $currencyFactory, \Magento\Directory\Helper\Data $directoryData, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, \Magento\Framework\Locale\FormatInterface $localeFormat, Config $configHelper, ClientFactory $httpClientFactory, array $data=[])
getContainerTypes(\Magento\Framework\DataObject $params=null)