Skip to content

Commit 28d239a

Browse files
committed
Merge remote-tracking branch 'act4/ACP2E-3340' into PR_13_OCT_2024
2 parents 7939bd0 + 1814910 commit 28d239a

File tree

5 files changed

+140
-13
lines changed

5 files changed

+140
-13
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,18 @@ protected function _getAccessToken(): string|null
934934
$apiKey = $this->getConfigData('api_key') ?? null;
935935
$secretKey = $this->getConfigData('secret_key') ?? null;
936936

937+
return $this->retrieveAccessToken($apiKey, $secretKey);
938+
}
939+
940+
/**
941+
* Make the call to get the access token
942+
*
943+
* @param string|null $apiKey
944+
* @param string|null $secretKey
945+
* @return string|null
946+
*/
947+
private function retrieveAccessToken(?string $apiKey, ?string $secretKey): string|null
948+
{
937949
if (!$apiKey || !$secretKey) {
938950
$this->_debug(__('Authentication keys are missing.'));
939951
return null;
@@ -955,9 +967,23 @@ protected function _getAccessToken(): string|null
955967
} elseif (!empty($response['access_token'])) {
956968
$accessToken = $response['access_token'];
957969
}
970+
958971
return $accessToken;
959972
}
960973

974+
/**
975+
* Get Access Token for Tracking Rest API
976+
*
977+
* @return string|null
978+
*/
979+
private function getTrackingApiAccessToken(): string|null
980+
{
981+
$trackingApiKey = $this->getConfigData('tracking_api_key') ?? null;
982+
$trackingSecretKey = $this->getConfigData('tracking_api_secret_key') ?? null;
983+
984+
return $this->retrieveAccessToken($trackingApiKey, $trackingSecretKey);
985+
}
986+
961987
/**
962988
* Send Curl Request
963989
*
@@ -1007,7 +1033,12 @@ protected function sendRequest($endpoint, $request, $accessToken = null): array|
10071033
*/
10081034
protected function _getTrackingInformation($tracking): void
10091035
{
1010-
$accessToken = $this->_getAccessToken();
1036+
if ($this->getConfigData('enabled_tracking_api')) {
1037+
$accessToken = $this->getTrackingApiAccessToken();
1038+
} else {
1039+
$accessToken = $this->_getAccessToken();
1040+
}
1041+
10111042
if (!empty($accessToken)) {
10121043

10131044
$trackRequest = [

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

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,6 @@ protected function setUp(): void
143143
->disableOriginalConstructor()
144144
->getMockForAbstractClass();
145145

146-
$this->scope->expects($this->any())
147-
->method('getValue')
148-
->willReturnCallback([$this, 'scopeConfigGetValue']);
149-
150146
$countryFactory = $this->getCountryFactory();
151147
$rateFactory = $this->getRateFactory();
152148
$storeManager = $this->getStoreManager();
@@ -254,6 +250,7 @@ public function testRequestToShipmentExceptionNoPackages(): void
254250
/**
255251
* @return void
256252
* @throws LocalizedException
253+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
257254
*/
258255
public function testRequestToShipmentSuccess(): void
259256
{
@@ -293,6 +290,11 @@ public function testRequestToShipmentSuccess(): void
293290
];
294291
$storeId = 1;
295292
$phoneNumber = '1234567890';
293+
294+
$this->scope->expects($this->any())
295+
->method('getValue')
296+
->willReturnCallback([$this, 'scopeConfigGetValue']);
297+
296298
$request->expects($this->once())->method('getPackages')->willReturn($packages);
297299
$request->expects($this->exactly(3))->method('getStoreId')->willReturn($storeId);
298300
$request->expects($this->once())->method('setPackageId');
@@ -355,6 +357,10 @@ public function testRequestToShipmentSuccess(): void
355357

356358
public function testSetRequestWithoutCity(): void
357359
{
360+
$this->scope->expects($this->any())
361+
->method('getValue')
362+
->willReturnCallback([$this, 'scopeConfigGetValue']);
363+
358364
$request = $this->getMockBuilder(RateRequest::class)
359365
->disableOriginalConstructor()
360366
->addMethods(['getDestCity'])
@@ -367,6 +373,10 @@ public function testSetRequestWithoutCity(): void
367373

368374
public function testSetRequestWithCity(): void
369375
{
376+
$this->scope->expects($this->any())
377+
->method('getValue')
378+
->willReturnCallback([$this, 'scopeConfigGetValue']);
379+
370380
$request = $this->getMockBuilder(RateRequest::class)
371381
->disableOriginalConstructor()
372382
->addMethods(['getDestCity'])
@@ -391,8 +401,33 @@ public function scopeConfigGetValue(string $path): int|string|null
391401
'carriers/fedex/debug' => 1,
392402
'carriers/fedex/api_key' => 'TestApiKey',
393403
'carriers/fedex/secret_key' => 'TestSecretKey',
404+
'carriers/fedex/enabled_tracking_api' => 0,
405+
'carriers/fedex/rest_sandbox_webservices_url' => 'https://rest.sandbox.url/',
406+
'carriers/fedex/rest_production_webservices_url' => 'https://rest.production.url/'
407+
];
408+
409+
return $pathMap[$path] ?? null;
410+
}
411+
412+
/**
413+
* Callback function, emulates getValue function for tracking api flow
414+
*
415+
* @param string $path
416+
* @return int|string|null
417+
*/
418+
public function scopeConfigGetValueForTracking(string $path): int|string|null
419+
{
420+
$pathMap = [
421+
'carriers/fedex/showmethod' => 1,
422+
'carriers/fedex/allowed_methods' => 'ServiceType',
423+
'carriers/fedex/debug' => 1,
424+
'carriers/fedex/api_key' => 'TestApiKey',
425+
'carriers/fedex/secret_key' => 'TestSecretKey',
426+
'carriers/fedex/enabled_tracking_api' => 1,
427+
'carriers/fedex/tracking_api_key' => 'TestTrackingApiKey',
428+
'carriers/fedex/tracking_api_secret_key' => 'TestTrackingSecretKey',
394429
'carriers/fedex/rest_sandbox_webservices_url' => 'https://rest.sandbox.url/',
395-
'carriers/fedex/rest_production_webservices_url' => 'https://rest.production.url/',
430+
'carriers/fedex/rest_production_webservices_url' => 'https://rest.production.url/'
396431
];
397432

398433
return $pathMap[$path] ?? null;
@@ -417,6 +452,10 @@ public function testCollectRatesRateAmountOriginBased(
417452
->method('isSetFlag')
418453
->willReturn(true);
419454

455+
$this->scope->expects($this->any())
456+
->method('getValue')
457+
->willReturnCallback([$this, 'scopeConfigGetValue']);
458+
420459
$accessTokenResponse = $this->getAccessToken();
421460
$rateResponse = $this->getRateResponse($amount, $currencyCode, $rateType);
422461

@@ -515,6 +554,10 @@ public function testCollectRatesErrorMessage(): void
515554
->method('isSetFlag')
516555
->willReturn(false);
517556

557+
$this->scope->expects($this->any())
558+
->method('getValue')
559+
->willReturnCallback([$this, 'scopeConfigGetValue']);
560+
518561
$this->error->expects($this->once())
519562
->method('setCarrier')
520563
->with('fedex');
@@ -893,14 +936,25 @@ public function getAccessToken(): array
893936
* @param string $shipTimeStamp
894937
* @param string $expectedDate
895938
* @param string $expectedTime
939+
* @param bool $trackingEnabled
896940
* @dataProvider shipDateDataProvider
897941
*/
898-
public function testGetTracking($tracking, $shipTimeStamp, $expectedDate, $expectedTime): void
942+
public function testGetTracking($tracking, $shipTimeStamp, $expectedDate, $expectedTime, $trackingEnabled): void
899943
{
900944
$trackRequest = $this->getTrackRequest($tracking);
901945
$trackResponse = $this->getTrackResponse($shipTimeStamp, $expectedDate, $expectedTime);
902946
$accessTokenResponse = $this->getAccessToken();
903947

948+
if ($trackingEnabled) {
949+
$this->scope->expects($this->atLeast(1))
950+
->method('getValue')
951+
->willReturnCallback([$this, 'scopeConfigGetValueForTracking']);
952+
} else {
953+
$this->scope->expects($this->any())
954+
->method('getValue')
955+
->willReturnCallback([$this, 'scopeConfigGetValue']);
956+
}
957+
904958
$this->serializer->method('serialize')->willReturn(json_encode($trackRequest));
905959
$this->serializer->expects($this->any())
906960
->method('unserialize')
@@ -954,50 +1008,67 @@ public static function shipDateDataProvider(): array
9541008
'shipTimeStamp' => '2020-08-15T02:06:35+03:00',
9551009
'expectedDate' => '2014-01-09',
9561010
'18:31:00',
957-
0,
1011+
false,
1012+
0
9581013
],
9591014
'tracking1-again' => [
9601015
'tracking1',
9611016
'shipTimeStamp' => '2014-01-09T02:06:35+03:00',
9621017
'expectedDate' => '2014-01-09',
9631018
'18:31:00',
964-
0,
1019+
false,
1020+
0
9651021
],
9661022
'tracking2' => [
9671023
'tracking2',
9681024
'shipTimeStamp' => '2014-01-09T02:06:35+03:00',
9691025
'expectedDate' => '2014-01-09',
9701026
'23:06:35',
1027+
false,
1028+
0
9711029
],
9721030
'tracking3' => [
9731031
'tracking3',
9741032
'shipTimeStamp' => '2014-01-09T14:06:35',
9751033
'expectedDate' => '2014-01-09',
9761034
'18:31:00',
1035+
false,
1036+
0
9771037
],
9781038
'tracking4' => [
9791039
'tracking4',
9801040
'shipTimeStamp' => '2016-08-05 14:06:35',
9811041
'expectedDate' => null,
982-
null,
1042+
'',
1043+
false,
9831044
],
9841045
'tracking5' => [
9851046
'tracking5',
9861047
'shipTimeStamp' => '2016-08-05 14:06:35+00:00',
9871048
'expectedDate' => null,
988-
null,
1049+
'',
1050+
false,
9891051
],
9901052
'tracking6' => [
9911053
'tracking6',
9921054
'shipTimeStamp' => '2016-08-05',
9931055
'expectedDate' => null,
994-
null,
1056+
'',
1057+
false,
9951058
],
9961059
'tracking7' => [
9971060
'tracking7',
9981061
'shipTimeStamp' => '2016/08/05',
9991062
'expectedDate' => null,
1000-
null
1063+
'',
1064+
false,
1065+
],
1066+
'tracking8' => [
1067+
'tracking8',
1068+
'shipTimestamp' => '2024-09-19T02:06:35+03:00',
1069+
'expectedDate' => '2024-09-21',
1070+
'18:31:00',
1071+
true
10011072
],
10021073
];
10031074
}

app/code/Magento/Fedex/etc/adminhtml/system.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@
3131
<label>Secret Key</label>
3232
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
3333
</field>
34+
<field id="enabled_tracking_api" translate="label" type="select" sortOrder="61" showInDefault="1" showInWebsite="1" canRestore="1">
35+
<label>Enable Tracking API credentials</label>
36+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
37+
</field>
38+
<field id="tracking_api_key" translate="label" type="obscure" sortOrder="62" showInDefault="1" showInWebsite="1">
39+
<label>Tracking API Key</label>
40+
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
41+
<depends>
42+
<field id="enabled_tracking_api">1</field>
43+
</depends>
44+
</field>
45+
<field id="tracking_api_secret_key" translate="label" type="obscure" sortOrder="63" showInDefault="1" showInWebsite="1">
46+
<label>Tracking API Secret Key</label>
47+
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
48+
<depends>
49+
<field id="enabled_tracking_api">1</field>
50+
</depends>
51+
</field>
3452
<field id="sandbox_mode" translate="label" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" canRestore="1">
3553
<label>Sandbox Mode</label>
3654
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>

app/code/Magento/Fedex/etc/config.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<account backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
1313
<api_key backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
1414
<secret_key backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
15+
<tracking_api_key backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
16+
<tracking_api_secret_key backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
1517
<sandbox_mode>0</sandbox_mode>
1618
<production_webservices_url><![CDATA[https://apis.fedex.com/]]></production_webservices_url>
1719
<sandbox_webservices_url><![CDATA[https://apis-sandbox.fedex.com/]]></sandbox_webservices_url>

app/code/Magento/Fedex/i18n/en_US.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,8 @@ Debug,Debug
8888
"Package Return Program","Package Return Program"
8989
"Regular Stop","Regular Stop"
9090
"Tag","Tag"
91+
"Api Key","Api Key"
92+
"Secret Key","Secret Key"
93+
"Enable Tracking API credentials","Enable Tracking API credentials"
94+
"Tracking API Key","Tracking API Key"
95+
"Tracking API Secret Key","Tracking API Secret Key"

0 commit comments

Comments
 (0)