Skip to content

Commit 64af0d9

Browse files
authored
Merge pull request #15 from magento-mpi/MAGETWO-69898
Magetwo 69898
2 parents 27f9de4 + 09bbedb commit 64af0d9

File tree

19 files changed

+575
-80
lines changed

19 files changed

+575
-80
lines changed

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

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
use Magento\Quote\Model\Quote\Address\RateRequest;
1414
use Magento\Shipping\Model\Carrier\AbstractCarrierOnline;
1515
use Magento\Shipping\Model\Rate\Result;
16-
use Magento\Shipping\Model\Tracking\Result as TrackingResult;
1716

1817
/**
1918
* Fedex shipping implementation
2019
*
21-
* @author Magento Core Team <core@magentocommerce.com>
20+
* @author Magento Core Team <core@magentocommerce.com>
2221
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
2322
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2423
*/
@@ -78,7 +77,7 @@ class Carrier extends AbstractCarrierOnline implements \Magento\Shipping\Model\C
7877
/**
7978
* Rate result data
8079
*
81-
* @var Result|TrackingResult
80+
* @var Result|null
8281
*/
8382
protected $_result = null;
8483

@@ -124,7 +123,7 @@ class Carrier extends AbstractCarrierOnline implements \Magento\Shipping\Model\C
124123
* @inheritdoc
125124
*/
126125
protected $_debugReplacePrivateDataKeys = [
127-
'Key', 'Password', 'MeterNumber'
126+
'Key', 'Password', 'MeterNumber',
128127
];
129128

130129
/**
@@ -380,12 +379,12 @@ public function setRequest(RateRequest $request)
380379
/**
381380
* Get result of request
382381
*
383-
* @return Result|TrackingResult
382+
* @return Result|null
384383
*/
385384
public function getResult()
386385
{
387386
if (!$this->_result) {
388-
$this->_result = $this->_rateFactory->create();
387+
$this->_result = $this->_trackFactory->create();
389388
}
390389
return $this->_result;
391390
}
@@ -748,7 +747,7 @@ protected function _parseXmlResponse($response)
748747
$priceArr = [];
749748

750749
if (strlen(trim($response)) > 0) {
751-
$xml = $this->parseXml($response, 'Magento\Shipping\Model\Simplexml\Element');
750+
$xml = $this->parseXml($response, \Magento\Shipping\Model\Simplexml\Element::class);
752751
if (is_object($xml)) {
753752
if (is_object($xml->Error) && is_object($xml->Error->Message)) {
754753
$errorTitle = (string)$xml->Error->Message;
@@ -893,14 +892,14 @@ public function getCode($type, $code = '')
893892
'from_us' => [
894893
'method' => ['INTERNATIONAL_FIRST', 'INTERNATIONAL_ECONOMY', 'INTERNATIONAL_PRIORITY'],
895894
],
896-
]
895+
],
897896
],
898897
[
899898
'containers' => ['FEDEX_10KG_BOX', 'FEDEX_25KG_BOX'],
900899
'filters' => [
901900
'within_us' => [],
902901
'from_us' => ['method' => ['INTERNATIONAL_PRIORITY']],
903-
]
902+
],
904903
],
905904
[
906905
'containers' => ['YOUR_PACKAGING'],
@@ -938,7 +937,7 @@ public function getCode($type, $code = '')
938937
'INTERNATIONAL_PRIORITY_FREIGHT',
939938
],
940939
],
941-
]
940+
],
942941
],
943942
],
944943
'delivery_confirmation_types' => [
@@ -1039,7 +1038,6 @@ protected function setTrackingReqeust()
10391038
*/
10401039
protected function _getXMLTracking($tracking)
10411040
{
1042-
$this->_result = $this->_trackFactory->create();
10431041
$trackRequest = [
10441042
'WebAuthenticationDetail' => [
10451043
'UserCredential' => [
@@ -1096,10 +1094,10 @@ protected function _parseTrackingResponse($trackingValue, $response)
10961094
if (!is_object($response) || empty($response->HighestSeverity)) {
10971095
$this->appendTrackingError($trackingValue, __('Invalid response from carrier'));
10981096
return;
1099-
} elseif (in_array($response->HighestSeverity, self::$trackingErrors)) {
1097+
} else if (in_array($response->HighestSeverity, self::$trackingErrors)) {
11001098
$this->appendTrackingError($trackingValue, (string) $response->Notifications->Message);
11011099
return;
1102-
} elseif (empty($response->CompletedTrackDetails) || empty($response->CompletedTrackDetails->TrackDetails)) {
1100+
} else if (empty($response->CompletedTrackDetails) || empty($response->CompletedTrackDetails->TrackDetails)) {
11031101
$this->appendTrackingError($trackingValue, __('No available tracking items'));
11041102
return;
11051103
}
@@ -1197,7 +1195,7 @@ protected function _getAuthDetails()
11971195
'TransactionDetail' => [
11981196
'CustomerTransactionId' => '*** Express Domestic Shipping Request v9 using PHP ***',
11991197
],
1200-
'Version' => ['ServiceId' => 'ship', 'Major' => '10', 'Intermediate' => '0', 'Minor' => '0']
1198+
'Version' => ['ServiceId' => 'ship', 'Major' => '10', 'Intermediate' => '0', 'Minor' => '0'],
12011199
];
12021200
}
12031201

@@ -1372,7 +1370,7 @@ protected function _formShipmentRequest(\Magento\Framework\DataObject $request)
13721370
'Length' => $length,
13731371
'Width' => $width,
13741372
'Height' => $height,
1375-
'Units' => $packageParams->getDimensionUnits() == \Zend_Measure_Length::INCH ? 'IN' : 'CM'
1373+
'Units' => $packageParams->getDimensionUnits() == \Zend_Measure_Length::INCH ? 'IN' : 'CM',
13761374
];
13771375
}
13781376

@@ -1428,9 +1426,10 @@ protected function _doShipmentRequest(\Magento\Framework\DataObject $request)
14281426
* @param array|object $trackingIds
14291427
* @return string
14301428
*/
1431-
private function getTrackingNumber($trackingIds) {
1429+
private function getTrackingNumber($trackingIds)
1430+
{
14321431
return is_array($trackingIds) ? array_map(
1433-
function($val) {
1432+
function ($val) {
14341433
return $val->TrackingNumber;
14351434
},
14361435
$trackingIds
@@ -1558,7 +1557,7 @@ protected function filterDebugData($data)
15581557
* @param \stdClass $trackInfo
15591558
* @return array
15601559
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
1561-
+ @SuppressWarnings(PHPMD.NPathComplexity)
1560+
* @SuppressWarnings(PHPMD.NPathComplexity)
15621561
*/
15631562
private function processTrackingDetails(\stdClass $trackInfo)
15641563
{
@@ -1571,10 +1570,9 @@ private function processTrackingDetails(\stdClass $trackInfo)
15711570
'progressdetail' => [],
15721571
];
15731572

1574-
if (!empty($trackInfo->ShipTimestamp) &&
1575-
($datetime = \DateTime::createFromFormat(\DateTime::ISO8601, $trackInfo->ShipTimestamp)) !== false
1576-
) {
1577-
$result['shippeddate'] = $datetime->format('Y-m-d');
1573+
$datetime = $this->parseDate(!empty($trackInfo->ShipTimestamp) ? $trackInfo->ShipTimestamp : null);
1574+
if ($datetime) {
1575+
$result['shippeddate'] = gmdate('Y-m-d', $datetime->getTimestamp());
15781576
}
15791577

15801578
$result['signedby'] = !empty($trackInfo->DeliverySignatureName) ?
@@ -1590,8 +1588,8 @@ private function processTrackingDetails(\stdClass $trackInfo)
15901588

15911589
$datetime = $this->getDeliveryDateTime($trackInfo);
15921590
if ($datetime) {
1593-
$result['deliverydate'] = $datetime->format('Y-m-d');
1594-
$result['deliverytime'] = $datetime->format('H:i:s');
1591+
$result['deliverydate'] = gmdate('Y-m-d', $datetime->getTimestamp());
1592+
$result['deliverytime'] = gmdate('H:i:s', $datetime->getTimestamp());
15951593
}
15961594

15971595
$address = null;
@@ -1638,15 +1636,15 @@ private function getDeliveryDateTime(\stdClass $trackInfo)
16381636
$timestamp = $trackInfo->ActualDeliveryTimestamp;
16391637
}
16401638

1641-
return $timestamp ? \DateTime::createFromFormat(\DateTime::ISO8601, $timestamp) : null;
1639+
return $timestamp ? $this->parseDate($timestamp) : null;
16421640
}
16431641

16441642
/**
16451643
* Get delivery address details in string representation
16461644
* Return City, State, Country Code
16471645
*
16481646
* @param \stdClass $address
1649-
* @return string
1647+
* @return \Magento\Framework\Phrase|string
16501648
*/
16511649
private function getDeliveryAddress(\stdClass $address)
16521650
{
@@ -1687,10 +1685,10 @@ private function processTrackDetailsEvents(array $events)
16871685
'deliverylocation' => null
16881686
];
16891687

1690-
if (!empty($event->Timestamp)) {
1691-
$datetime = \DateTime::createFromFormat(\DateTime::ISO8601, $event->Timestamp);
1692-
$item['deliverydate'] = $datetime->format('Y-m-d');
1693-
$item['deliverytime'] = $datetime->format('H:i:s');
1688+
$datetime = $this->parseDate(!empty($event->Timestamp) ? $event->Timestamp : null);
1689+
if ($datetime) {
1690+
$item['deliverydate'] = gmdate('Y-m-d', $datetime->getTimestamp());
1691+
$item['deliverytime'] = gmdate('H:i:s', $datetime->getTimestamp());
16941692
}
16951693

16961694
if (!empty($event->Address)) {
@@ -1707,16 +1705,41 @@ private function processTrackDetailsEvents(array $events)
17071705
* Append error message to rate result instance
17081706
* @param string $trackingValue
17091707
* @param string $errorMessage
1710-
* @return void
17111708
*/
17121709
private function appendTrackingError($trackingValue, $errorMessage)
17131710
{
17141711
$error = $this->_trackErrorFactory->create();
1715-
$error->setCarrier(self::CODE);
1712+
$error->setCarrier('fedex');
17161713
$error->setCarrierTitle($this->getConfigData('title'));
17171714
$error->setTracking($trackingValue);
17181715
$error->setErrorMessage($errorMessage);
17191716
$result = $this->getResult();
17201717
$result->append($error);
17211718
}
1719+
1720+
/**
1721+
* Parses datetime string from FedEx response.
1722+
* According to FedEx API, datetime string should be in \DateTime::ATOM format, but
1723+
* sometimes FedEx returns datetime without timezone and in that case timezone will be set as UTC.
1724+
*
1725+
* @param string $timestamp
1726+
* @return bool|\DateTime
1727+
*/
1728+
private function parseDate($timestamp)
1729+
{
1730+
if ($timestamp === null) {
1731+
return false;
1732+
}
1733+
$formats = [\DateTime::ATOM, 'Y-m-d\TH:i:s'];
1734+
foreach ($formats as $format) {
1735+
// set UTC timezone for a case if timestamp does not contain any timezone
1736+
$utcTimezone = new \DateTimeZone('UTC');
1737+
$dateTime = \DateTime::createFromFormat($format, $timestamp, $utcTimezone);
1738+
if ($dateTime !== false) {
1739+
return $dateTime;
1740+
}
1741+
}
1742+
1743+
return false;
1744+
}
17221745
}

0 commit comments

Comments
 (0)