Skip to content

Commit 0c2836a

Browse files
committed
MAGETWO-63215: FedEx Shipment Tracking fails for valid tracking number
- Added an ability to parse date in `Y-m-d\TH:i:s` format for tracking response
1 parent cdd83d5 commit 0c2836a

File tree

4 files changed

+79
-29
lines changed

4 files changed

+79
-29
lines changed

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,9 +1570,8 @@ private function processTrackingDetails(\stdClass $trackInfo)
15701570
'progressdetail' => [],
15711571
];
15721572

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

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

15891588
$datetime = $this->getDeliveryDateTime($trackInfo);
15901589
if ($datetime) {
1591-
$result['deliverydate'] = $datetime->format('Y-m-d');
1592-
$result['deliverytime'] = $datetime->format('H:i:s');
1590+
$result['deliverydate'] = gmdate('Y-m-d', $datetime->getTimestamp());
1591+
$result['deliverytime'] = gmdate('H:i:s', $datetime->getTimestamp());
15931592
}
15941593

15951594
$address = null;
@@ -1636,7 +1635,7 @@ private function getDeliveryDateTime(\stdClass $trackInfo)
16361635
$timestamp = $trackInfo->ActualDeliveryTimestamp;
16371636
}
16381637

1639-
return $timestamp ? \DateTime::createFromFormat(\DateTime::ISO8601, $timestamp) : null;
1638+
return $timestamp ? $this->parseDate($timestamp) : null;
16401639
}
16411640

16421641
/**
@@ -1685,10 +1684,9 @@ private function processTrackDetailsEvents(array $events)
16851684
'deliverylocation' => null
16861685
];
16871686

1688-
if (!empty($event->Timestamp)) {
1689-
$datetime = \DateTime::createFromFormat(\DateTime::ISO8601, $event->Timestamp);
1690-
$item['deliverydate'] = $datetime->format('Y-m-d');
1691-
$item['deliverytime'] = $datetime->format('H:i:s');
1687+
if (!empty($event->Timestamp) && ($datetime = $this->parseDate($event->Timestamp)) !== false) {
1688+
$item['deliverydate'] = gmdate('Y-m-d', $datetime->getTimestamp());
1689+
$item['deliverytime'] = gmdate('H:i:s', $datetime->getTimestamp());
16921690
}
16931691

16941692
if (!empty($event->Address)) {
@@ -1716,4 +1714,31 @@ private function appendTrackingError($trackingValue, $errorMessage)
17161714
$result = $this->getResult();
17171715
$result->append($error);
17181716
}
1717+
1718+
/**
1719+
* Parses datetime string from FedEx response.
1720+
* According to FedEx API, datetime string should be in \DateTime::ATOM format, but
1721+
* sometimes FedEx returns datetime without timezone and in that case timezone will be set as UTC.
1722+
*
1723+
* @param string $timestamp
1724+
* @return bool|\DateTime
1725+
*/
1726+
private function parseDate($timestamp)
1727+
{
1728+
$formats = [\DateTime::ATOM, 'Y-m-d\TH:i:s'];
1729+
$tz = date_default_timezone_get();
1730+
1731+
foreach ($formats as $format) {
1732+
// set UTC timezone for a case if timestamp does not contain any timezone
1733+
date_default_timezone_set('UTC');
1734+
$dateTime = \DateTime::createFromFormat($format, $timestamp);
1735+
if ($dateTime !== false) {
1736+
date_default_timezone_set($tz);
1737+
return $dateTime;
1738+
}
1739+
}
1740+
1741+
date_default_timezone_set($tz);
1742+
return false;
1743+
}
17191744
}

app/code/Magento/Fedex/Test/Unit/Model/CarrierTest.php

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,12 @@ public function testGetTrackingErrorResponse()
393393

394394
/**
395395
* @covers \Magento\Fedex\Model\Carrier::getTracking
396+
* @param string $shipTimestamp
397+
* @param string $expectedDate
398+
* @param string $expectedDate
399+
* @dataProvider shipDateDataProvider
396400
*/
397-
public function testGetTracking()
401+
public function testGetTracking($shipTimeStamp, $expectedDate, $expectedTime)
398402
{
399403
$tracking = '123456789012';
400404

@@ -404,15 +408,15 @@ public function testGetTracking()
404408
$response->CompletedTrackDetails = new \stdClass();
405409

406410
$trackDetails = new \stdClass();
407-
$trackDetails->ShipTimestamp = '2016-08-05T14:06:35+00:00';
411+
$trackDetails->ShipTimestamp = $shipTimeStamp;
408412
$trackDetails->DeliverySignatureName = 'signature';
409413

410414
$trackDetails->StatusDetail = new \stdClass();
411415
$trackDetails->StatusDetail->Description = 'SUCCESS';
412416

413417
$trackDetails->Service = new \stdClass();
414418
$trackDetails->Service->Description = 'ground';
415-
$trackDetails->EstimatedDeliveryTimestamp = '2016-08-10T10:20:26+00:00';
419+
$trackDetails->EstimatedDeliveryTimestamp = $shipTimeStamp;
416420

417421
$trackDetails->EstimatedDeliveryAddress = new \stdClass();
418422
$trackDetails->EstimatedDeliveryAddress->City = 'Culver City';
@@ -444,25 +448,44 @@ public function testGetTracking()
444448
'signedby',
445449
'status',
446450
'service',
447-
'shippeddate',
448-
'deliverydate',
449-
'deliverytime',
450451
'deliverylocation',
451452
'weight',
452453
];
453454
array_walk($fields, function ($field) use ($current) {
454455
static::assertNotEmpty($current[$field]);
455456
});
456457

457-
static::assertEquals('2016-08-10', $current['deliverydate']);
458-
static::assertEquals('10:20:26', $current['deliverytime']);
459-
static::assertEquals('2016-08-05', $current['shippeddate']);
458+
static::assertEquals($expectedDate, $current['deliverydate']);
459+
static::assertEquals($expectedTime, $current['deliverytime']);
460+
static::assertEquals($expectedDate, $current['shippeddate']);
461+
}
462+
463+
/**
464+
* Gets list of variations for testing ship date.
465+
*
466+
* @return array
467+
*/
468+
public function shipDateDataProvider()
469+
{
470+
return [
471+
['shipTimestamp' => '2016-08-05T14:06:35+01:00', 'expectedDate' => '2016-08-05', '13:06:35'],
472+
['shipTimestamp' => '2016-08-05T02:06:35+03:00', 'expectedDate' => '2016-08-04', '23:06:35'],
473+
['shipTimestamp' => '2016-08-05T14:06:35', 'expectedDate' => '2016-08-05', '14:06:35'],
474+
['shipTimestamp' => '2016-08-05 14:06:35', 'expectedDate' => null, null],
475+
['shipTimestamp' => '2016-08-05 14:06:35+00:00', 'expectedDate' => null, null],
476+
['shipTimestamp' => '2016-08-05', 'expectedDate' => null, null],
477+
['shipTimestamp' => '2016/08/05', 'expectedDate' => null, null],
478+
];
460479
}
461480

462481
/**
463482
* @covers \Magento\Fedex\Model\Carrier::getTracking
483+
* @param string $shipTimestamp
484+
* @param string $expectedDate
485+
* @param string $expectedDate
486+
* @dataProvider shipDateDataProvider
464487
*/
465-
public function testGetTrackingWithEvents()
488+
public function testGetTrackingWithEvents($shipTimeStamp, $expectedDate, $expectedTime)
466489
{
467490
$tracking = '123456789012';
468491

@@ -473,7 +496,7 @@ public function testGetTrackingWithEvents()
473496

474497
$event = new \stdClass();
475498
$event->EventDescription = 'Test';
476-
$event->Timestamp = '2016-08-05T19:14:53+00:00';
499+
$event->Timestamp = $shipTimeStamp;
477500
$event->Address = new \stdClass();
478501

479502
$event->Address->City = 'Culver City';
@@ -504,12 +527,12 @@ public function testGetTrackingWithEvents()
504527
static::assertEquals(1, count($current['progressdetail']));
505528

506529
$event = $current['progressdetail'][0];
507-
$fields = ['activity', 'deliverydate', 'deliverytime', 'deliverylocation'];
530+
$fields = ['activity', 'deliverylocation'];
508531
array_walk($fields, function ($field) use ($event) {
509532
static::assertNotEmpty($event[$field]);
510533
});
511-
static::assertEquals('2016-08-05', $event['deliverydate']);
512-
static::assertEquals('19:14:53', $event['deliverytime']);
534+
static::assertEquals($expectedDate, $event['deliverydate']);
535+
static::assertEquals($expectedTime, $event['deliverytime']);
513536
}
514537

515538
/**

app/code/Magento/Shipping/view/frontend/templates/tracking/details.phtml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/** @var $block \Magento\Framework\View\Element\Template */
1010

11+
$parentBlock = $block->getParentBlock();
1112
$track = $block->getData('track');
1213
$email = $block->getData('storeSupportEmail');
1314
$fields = [
@@ -38,8 +39,8 @@ $fields = [
3839
<th class="col label" scope="row"><?php echo $block->escapeHtml(__('Error:')); ?></th>
3940
<td class="col error">
4041
<?php echo $block->escapeHtml(__('Tracking information is currently not available. Please ')); ?>
41-
<?php if ($block->getContactUsEnabled()) : ?>
42-
<a href="<?php echo $block->escapeUrl($block->getContactUs()); ?>" target="_blank"
42+
<?php if ($parentBlock->getContactUsEnabled()) : ?>
43+
<a href="<?php echo $block->escapeUrl($parentBlock->getContactUs()); ?>" target="_blank"
4344
title="<?php echo $block->escapeHtml(__('contact us')); ?>">
4445
<?php echo $block->escapeHtml(__('contact us')); ?>
4546
</a>
@@ -77,7 +78,7 @@ $fields = [
7778
<tr>
7879
<th class="col label" scope="row"><?php echo $block->escapeHtml(__('Delivered on:')); ?></th>
7980
<td class="col value">
80-
<?php /* @noEscape */ echo $block->formatDeliveryDateTime($track->getDeliverydate(), $track->getDeliverytime()); ?>
81+
<?php /* @noEscape */ echo $parentBlock->formatDeliveryDateTime($track->getDeliverydate(), $track->getDeliverytime()); ?>
8182
</td>
8283
</tr>
8384
<?php endif; ?>

app/code/Magento/Shipping/view/frontend/templates/tracking/progress.phtml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// @codingStandardsIgnoreFile
88

99
/** @var $block \Magento\Framework\View\Element\Template */
10+
$parentBlock = $block->getParentBlock();
1011
$track = $block->getData('track');
1112
?>
1213
<div class="table-wrapper">
@@ -22,8 +23,8 @@ $track = $block->getData('track');
2223
</thead>
2324
<tbody>
2425
<?php foreach ($track->getProgressdetail() as $detail): ?>
25-
<?php $detailDate = (!empty($detail['deliverydate']) ? $block->formatDeliveryDate($detail['deliverydate']) : ''); ?>
26-
<?php $detailTime = (!empty($detail['deliverytime']) ? $block->formatDeliveryTime($detail['deliverytime'], $detail['deliverydate']) : ''); ?>
26+
<?php $detailDate = (!empty($detail['deliverydate']) ? $parentBlock->formatDeliveryDate($detail['deliverydate']) : ''); ?>
27+
<?php $detailTime = (!empty($detail['deliverytime']) ? $parentBlock->formatDeliveryTime($detail['deliverytime'], $detail['deliverydate']) : ''); ?>
2728
<tr>
2829
<td data-th="<?php echo $block->escapeHtml(__('Location')); ?>" class="col location">
2930
<?php echo(!empty($detail['deliverylocation']) ? $block->escapeHtml($detail['deliverylocation']) : ''); ?>

0 commit comments

Comments
 (0)