Skip to content

Commit 91f116d

Browse files
author
Aliaksei Yakimovich2
committed
MAGETWO-98947: UPS CGI url gateway to migrate from http to https
- Fixed static/unit test falls;
1 parent 27565bc commit 91f116d

File tree

1 file changed

+169
-120
lines changed

1 file changed

+169
-120
lines changed

app/code/Magento/Ups/Model/Carrier.php

Lines changed: 169 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ protected function _getCgiQuotes()
455455
{
456456
$rowRequest = $this->_rawRequest;
457457
if (self::USA_COUNTRY_ID == $rowRequest->getDestCountry()) {
458-
$destPostal = substr($rowRequest->getDestPostal(), 0, 5);
458+
$destPostal = substr((string)$rowRequest->getDestPostal(), 0, 5);
459459
} else {
460460
$destPostal = $rowRequest->getDestPostal();
461461
}
@@ -613,7 +613,7 @@ protected function _getXmlQuotes()
613613

614614
$rowRequest = $this->_rawRequest;
615615
if (self::USA_COUNTRY_ID == $rowRequest->getDestCountry()) {
616-
$destPostal = substr($rowRequest->getDestPostal(), 0, 5);
616+
$destPostal = substr((string)$rowRequest->getDestPostal(), 0, 5);
617617
} else {
618618
$destPostal = $rowRequest->getDestPostal();
619619
}
@@ -833,76 +833,15 @@ protected function _parseXmlResponse($xmlResponse)
833833

834834
$allowedCurrencies = $this->_currencyFactory->create()->getConfigAllowCurrencies();
835835
foreach ($arr as $shipElement) {
836-
$code = (string)$shipElement->Service->Code;
837-
if (in_array($code, $allowedMethods)) {
838-
//The location of tax information is in a different place
839-
// depending on whether we are using negotiated rates or not
840-
if ($negotiatedActive) {
841-
$includeTaxesArr = $xml->getXpath(
842-
"//RatingServiceSelectionResponse/RatedShipment/NegotiatedRates"
843-
. "/NetSummaryCharges/TotalChargesWithTaxes"
844-
);
845-
$includeTaxesActive = $this->getConfigFlag('include_taxes') && !empty($includeTaxesArr);
846-
if ($includeTaxesActive) {
847-
$cost = $shipElement->NegotiatedRates
848-
->NetSummaryCharges
849-
->TotalChargesWithTaxes
850-
->MonetaryValue;
851-
852-
$responseCurrencyCode = $this->mapCurrencyCode(
853-
(string)$shipElement->NegotiatedRates
854-
->NetSummaryCharges
855-
->TotalChargesWithTaxes
856-
->CurrencyCode
857-
);
858-
} else {
859-
$cost = $shipElement->NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue;
860-
$responseCurrencyCode = $this->mapCurrencyCode(
861-
(string)$shipElement->NegotiatedRates->NetSummaryCharges->GrandTotal->CurrencyCode
862-
);
863-
}
864-
} else {
865-
$includeTaxesArr = $xml->getXpath(
866-
"//RatingServiceSelectionResponse/RatedShipment/TotalChargesWithTaxes"
867-
);
868-
$includeTaxesActive = $this->getConfigFlag('include_taxes') && !empty($includeTaxesArr);
869-
if ($includeTaxesActive) {
870-
$cost = $shipElement->TotalChargesWithTaxes->MonetaryValue;
871-
$responseCurrencyCode = $this->mapCurrencyCode(
872-
(string)$shipElement->TotalChargesWithTaxes->CurrencyCode
873-
);
874-
} else {
875-
$cost = $shipElement->TotalCharges->MonetaryValue;
876-
$responseCurrencyCode = $this->mapCurrencyCode(
877-
(string)$shipElement->TotalCharges->CurrencyCode
878-
);
879-
}
880-
}
881-
882-
//convert price with Origin country currency code to base currency code
883-
$successConversion = true;
884-
if ($responseCurrencyCode) {
885-
if (in_array($responseCurrencyCode, $allowedCurrencies)) {
886-
$cost = (double)$cost * $this->_getBaseCurrencyRate($responseCurrencyCode);
887-
} else {
888-
$errorTitle = __(
889-
'We can\'t convert a rate from "%1-%2".',
890-
$responseCurrencyCode,
891-
$this->_request->getPackageCurrency()->getCode()
892-
);
893-
$error = $this->_rateErrorFactory->create();
894-
$error->setCarrier('ups');
895-
$error->setCarrierTitle($this->getConfigData('title'));
896-
$error->setErrorMessage($errorTitle);
897-
$successConversion = false;
898-
}
899-
}
900-
901-
if ($successConversion) {
902-
$costArr[$code] = $cost;
903-
$priceArr[$code] = $this->getMethodPrice((float)$cost, $code);
904-
}
905-
}
836+
$this->processShippingRateForItem(
837+
$shipElement,
838+
$allowedMethods,
839+
$allowedCurrencies,
840+
$costArr,
841+
$priceArr,
842+
$negotiatedActive,
843+
$xml
844+
);
906845
}
907846
} else {
908847
$arr = $xml->getXpath("//RatingServiceSelectionResponse/Response/Error/ErrorDescription/text()");
@@ -945,6 +884,99 @@ protected function _parseXmlResponse($xmlResponse)
945884
return $result;
946885
}
947886

887+
/**
888+
* Processing rate for ship element
889+
*
890+
* @param \Magento\Framework\Simplexml\Element $shipElement
891+
* @param array $allowedMethods
892+
* @param array $allowedCurrencies
893+
* @param array $costArr
894+
* @param array $priceArr
895+
* @param bool $negotiatedActive
896+
* @param \Magento\Framework\Simplexml\Config $xml
897+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
898+
*/
899+
private function processShippingRateForItem(
900+
\Magento\Framework\Simplexml\Element $shipElement,
901+
array $allowedMethods,
902+
array $allowedCurrencies,
903+
array &$costArr,
904+
array &$priceArr,
905+
bool $negotiatedActive,
906+
\Magento\Framework\Simplexml\Config $xml
907+
): void {
908+
$code = (string)$shipElement->Service->Code;
909+
if (in_array($code, $allowedMethods)) {
910+
//The location of tax information is in a different place
911+
// depending on whether we are using negotiated rates or not
912+
if ($negotiatedActive) {
913+
$includeTaxesArr = $xml->getXpath(
914+
"//RatingServiceSelectionResponse/RatedShipment/NegotiatedRates"
915+
. "/NetSummaryCharges/TotalChargesWithTaxes"
916+
);
917+
$includeTaxesActive = $this->getConfigFlag('include_taxes') && !empty($includeTaxesArr);
918+
if ($includeTaxesActive) {
919+
$cost = $shipElement->NegotiatedRates
920+
->NetSummaryCharges
921+
->TotalChargesWithTaxes
922+
->MonetaryValue;
923+
924+
$responseCurrencyCode = $this->mapCurrencyCode(
925+
(string)$shipElement->NegotiatedRates
926+
->NetSummaryCharges
927+
->TotalChargesWithTaxes
928+
->CurrencyCode
929+
);
930+
} else {
931+
$cost = $shipElement->NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue;
932+
$responseCurrencyCode = $this->mapCurrencyCode(
933+
(string)$shipElement->NegotiatedRates->NetSummaryCharges->GrandTotal->CurrencyCode
934+
);
935+
}
936+
} else {
937+
$includeTaxesArr = $xml->getXpath(
938+
"//RatingServiceSelectionResponse/RatedShipment/TotalChargesWithTaxes"
939+
);
940+
$includeTaxesActive = $this->getConfigFlag('include_taxes') && !empty($includeTaxesArr);
941+
if ($includeTaxesActive) {
942+
$cost = $shipElement->TotalChargesWithTaxes->MonetaryValue;
943+
$responseCurrencyCode = $this->mapCurrencyCode(
944+
(string)$shipElement->TotalChargesWithTaxes->CurrencyCode
945+
);
946+
} else {
947+
$cost = $shipElement->TotalCharges->MonetaryValue;
948+
$responseCurrencyCode = $this->mapCurrencyCode(
949+
(string)$shipElement->TotalCharges->CurrencyCode
950+
);
951+
}
952+
}
953+
954+
//convert price with Origin country currency code to base currency code
955+
$successConversion = true;
956+
if ($responseCurrencyCode) {
957+
if (in_array($responseCurrencyCode, $allowedCurrencies)) {
958+
$cost = (double)$cost * $this->_getBaseCurrencyRate($responseCurrencyCode);
959+
} else {
960+
$errorTitle = __(
961+
'We can\'t convert a rate from "%1-%2".',
962+
$responseCurrencyCode,
963+
$this->_request->getPackageCurrency()->getCode()
964+
);
965+
$error = $this->_rateErrorFactory->create();
966+
$error->setCarrier('ups');
967+
$error->setCarrierTitle($this->getConfigData('title'));
968+
$error->setErrorMessage($errorTitle);
969+
$successConversion = false;
970+
}
971+
}
972+
973+
if ($successConversion) {
974+
$costArr[$code] = $cost;
975+
$priceArr[$code] = $this->getMethodPrice((float)$cost, $code);
976+
}
977+
}
978+
}
979+
948980
/**
949981
* Get tracking
950982
*
@@ -1101,54 +1133,7 @@ protected function _parseXmlTrackingResponse($trackingValue, $xmlResponse)
11011133
if ($activityTags) {
11021134
$index = 1;
11031135
foreach ($activityTags as $activityTag) {
1104-
$addressArr = [];
1105-
if (isset($activityTag->ActivityLocation->Address->City)) {
1106-
$addressArr[] = (string)$activityTag->ActivityLocation->Address->City;
1107-
}
1108-
if (isset($activityTag->ActivityLocation->Address->StateProvinceCode)) {
1109-
$addressArr[] = (string)$activityTag->ActivityLocation->Address->StateProvinceCode;
1110-
}
1111-
if (isset($activityTag->ActivityLocation->Address->CountryCode)) {
1112-
$addressArr[] = (string)$activityTag->ActivityLocation->Address->CountryCode;
1113-
}
1114-
$dateArr = [];
1115-
$date = (string)$activityTag->Date;
1116-
//YYYYMMDD
1117-
$dateArr[] = substr($date, 0, 4);
1118-
$dateArr[] = substr($date, 4, 2);
1119-
$dateArr[] = substr($date, -2, 2);
1120-
1121-
$timeArr = [];
1122-
$time = (string)$activityTag->Time;
1123-
//HHMMSS
1124-
$timeArr[] = substr($time, 0, 2);
1125-
$timeArr[] = substr($time, 2, 2);
1126-
$timeArr[] = substr($time, -2, 2);
1127-
1128-
if ($index === 1) {
1129-
$resultArr['status'] = (string)$activityTag->Status->StatusType->Description;
1130-
$resultArr['deliverydate'] = implode('-', $dateArr);
1131-
//YYYY-MM-DD
1132-
$resultArr['deliverytime'] = implode(':', $timeArr);
1133-
//HH:MM:SS
1134-
$resultArr['deliverylocation'] = (string)$activityTag->ActivityLocation->Description;
1135-
$resultArr['signedby'] = (string)$activityTag->ActivityLocation->SignedForByName;
1136-
if ($addressArr) {
1137-
$resultArr['deliveryto'] = implode(', ', $addressArr);
1138-
}
1139-
} else {
1140-
$tempArr = [];
1141-
$tempArr['activity'] = (string)$activityTag->Status->StatusType->Description;
1142-
$tempArr['deliverydate'] = implode('-', $dateArr);
1143-
//YYYY-MM-DD
1144-
$tempArr['deliverytime'] = implode(':', $timeArr);
1145-
//HH:MM:SS
1146-
if ($addressArr) {
1147-
$tempArr['deliverylocation'] = implode(', ', $addressArr);
1148-
}
1149-
$packageProgress[] = $tempArr;
1150-
}
1151-
$index++;
1136+
$this->processActivityTagInfo($activityTag, $index, $resultArr, $packageProgress);
11521137
}
11531138
$resultArr['progressdetail'] = $packageProgress;
11541139
}
@@ -1181,6 +1166,70 @@ protected function _parseXmlTrackingResponse($trackingValue, $xmlResponse)
11811166
return $this->_result;
11821167
}
11831168

1169+
/**
1170+
* Process tracking info from activity tag
1171+
*
1172+
* @param \Magento\Framework\Simplexml\Element $activityTag
1173+
* @param int $index
1174+
* @param array $resultArr
1175+
* @param array $packageProgress
1176+
*/
1177+
private function processActivityTagInfo(
1178+
\Magento\Framework\Simplexml\Element $activityTag,
1179+
int &$index,
1180+
array &$resultArr,
1181+
array &$packageProgress
1182+
) {
1183+
$addressArr = [];
1184+
if (isset($activityTag->ActivityLocation->Address->City)) {
1185+
$addressArr[] = (string)$activityTag->ActivityLocation->Address->City;
1186+
}
1187+
if (isset($activityTag->ActivityLocation->Address->StateProvinceCode)) {
1188+
$addressArr[] = (string)$activityTag->ActivityLocation->Address->StateProvinceCode;
1189+
}
1190+
if (isset($activityTag->ActivityLocation->Address->CountryCode)) {
1191+
$addressArr[] = (string)$activityTag->ActivityLocation->Address->CountryCode;
1192+
}
1193+
$dateArr = [];
1194+
$date = (string)$activityTag->Date;
1195+
//YYYYMMDD
1196+
$dateArr[] = substr($date, 0, 4);
1197+
$dateArr[] = substr($date, 4, 2);
1198+
$dateArr[] = substr($date, -2, 2);
1199+
1200+
$timeArr = [];
1201+
$time = (string)$activityTag->Time;
1202+
//HHMMSS
1203+
$timeArr[] = substr($time, 0, 2);
1204+
$timeArr[] = substr($time, 2, 2);
1205+
$timeArr[] = substr($time, -2, 2);
1206+
1207+
if ($index === 1) {
1208+
$resultArr['status'] = (string)$activityTag->Status->StatusType->Description;
1209+
$resultArr['deliverydate'] = implode('-', $dateArr);
1210+
//YYYY-MM-DD
1211+
$resultArr['deliverytime'] = implode(':', $timeArr);
1212+
//HH:MM:SS
1213+
$resultArr['deliverylocation'] = (string)$activityTag->ActivityLocation->Description;
1214+
$resultArr['signedby'] = (string)$activityTag->ActivityLocation->SignedForByName;
1215+
if ($addressArr) {
1216+
$resultArr['deliveryto'] = implode(', ', $addressArr);
1217+
}
1218+
} else {
1219+
$tempArr = [];
1220+
$tempArr['activity'] = (string)$activityTag->Status->StatusType->Description;
1221+
$tempArr['deliverydate'] = implode('-', $dateArr);
1222+
//YYYY-MM-DD
1223+
$tempArr['deliverytime'] = implode(':', $timeArr);
1224+
//HH:MM:SS
1225+
if ($addressArr) {
1226+
$tempArr['deliverylocation'] = implode(', ', $addressArr);
1227+
}
1228+
$packageProgress[] = $tempArr;
1229+
}
1230+
$index++;
1231+
}
1232+
11841233
/**
11851234
* Get tracking response
11861235
*

0 commit comments

Comments
 (0)