Skip to content

Commit 3906daf

Browse files
AC-13257::Migrate USPS integration from outdated Web Tools APIs to new RESTful USPS APIs
1 parent d346303 commit 3906daf

File tree

4 files changed

+33
-47
lines changed

4 files changed

+33
-47
lines changed

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,18 +1020,23 @@ public function getCode($type, $code = '')
10201020
],
10211021
],
10221022
'from_us' => [
1023-
'method' => ['INT_16', 'INT_24', 'PRIORITY_MAIL_INTERNATIONAL_MACHINABLE_ISC_SMALL_FLAT_RATE_BOX'],
1023+
'method' => ['INT_16', 'INT_24',
1024+
'PRIORITY_MAIL_INTERNATIONAL_MACHINABLE_ISC_SMALL_FLAT_RATE_BOX'
1025+
],
10241026
],
10251027
]
10261028
],
10271029
[
10281030
'containers' => ['MD FLAT RATE BOX'],
10291031
'filters' => [
10301032
'within_us' => [
1031-
'method' => ['17', '57', 'PRIORITY_MAIL_MACHINABLE_MEDIUM_FLAT_RATE_BOX'],
1033+
'method' => ['17', '57', 'PRIORITY_MAIL_MACHINABLE_MEDIUM_FLAT_RATE_BOX'
1034+
],
10321035
],
10331036
'from_us' => [
1034-
'method' => ['INT_9', 'INT_24', 'PRIORITY_MAIL_INTERNATIONAL_MACHINABLE_ISC_MEDIUM_FLAT_RATE_BOX'],
1037+
'method' => ['INT_9', 'INT_24',
1038+
'PRIORITY_MAIL_INTERNATIONAL_MACHINABLE_ISC_MEDIUM_FLAT_RATE_BOX'
1039+
],
10351040
],
10361041
]
10371042
],
@@ -1042,7 +1047,9 @@ public function getCode($type, $code = '')
10421047
'method' => ['22', '57', 'PRIORITY_MAIL_MACHINABLE_LARGE_FLAT_RATE_BOX'],
10431048
],
10441049
'from_us' => [
1045-
'method' => ['INT_11', 'INT_24', 'INT_25', 'PRIORITY_MAIL_INTERNATIONAL_MACHINABLE_ISC_LARGE_FLAT_RATE_BOX'],
1050+
'method' => ['INT_11', 'INT_24', 'INT_25',
1051+
'PRIORITY_MAIL_INTERNATIONAL_MACHINABLE_ISC_LARGE_FLAT_RATE_BOX'
1052+
],
10461053
],
10471054
]
10481055
],
@@ -1086,7 +1093,7 @@ public function getCode($type, $code = '')
10861093
'method' => ['62', '63', '64', '46', '29',
10871094
'PRIORITY_MAIL_EXPRESS_PADDED_FLAT_RATE_ENVELOPE',
10881095
'PRIORITY_MAIL_PADDED_FLAT_RATE_ENVELOPE'
1089-
],
1096+
],
10901097
],
10911098
'from_us' => [
10921099
'method' => ['INT_27', 'INT_23',
@@ -1144,7 +1151,8 @@ public function getCode($type, $code = '')
11441151
],
11451152
],
11461153
'from_us' => [
1147-
'method' => ['INT_12', 'INT_1', 'INT_2', 'INT_15', 'INT_13', 'INT_14', 'INT_16', 'INT_20', 'INT_21', 'INT_22', 'INT_23', 'INT_24', 'INT_25', 'INT_27',
1154+
'method' => ['INT_12', 'INT_1', 'INT_2', 'INT_15', 'INT_13', 'INT_14', 'INT_16', 'INT_20',
1155+
'INT_21', 'INT_22', 'INT_23', 'INT_24', 'INT_25', 'INT_27',
11481156
'FIRST-CLASS_PACKAGE_INTERNATIONAL_SERVICE_MACHINABLE_ISC_SINGLE-PIECE',
11491157
'PRIORITY_MAIL_INTERNATIONAL_ISC_SINGLE-PIECE',
11501158
'PRIORITY_MAIL_EXPRESS_INTERNATIONAL_ISC_SINGLE-PIECE'
@@ -1165,7 +1173,12 @@ public function getCode($type, $code = '')
11651173
],
11661174
],
11671175
'from_us' => [
1168-
'method' => ['INT_4', 'INT_1', 'INT_2', 'INT_15', 'INT_13', 'INT_14', 'INT_16', 'INT_20', 'INT_21', 'INT_22', 'INT_23', 'INT_24', 'INT_25', 'INT_27', 'INT_28', 'INT_29', 'INT_30', 'INT_31', 'INT_32', 'INT_33', 'INT_34', 'INT_35', 'INT_36', 'INT_37', 'INT_38', 'INT_39', 'INT_40', 'INT_41', 'INT_42', 'INT_43', 'INT_44', 'INT_45', 'INT_46', 'INT_47', 'INT_48', 'INT_49', 'INT_50', 'INT_53', 'INT_58', 'INT_59', 'INT_61', 'INT_62', 'INT_63', 'INT_64',
1176+
'method' => ['INT_4', 'INT_1', 'INT_2', 'INT_15', 'INT_13', 'INT_14', 'INT_16', 'INT_20',
1177+
'INT_21', 'INT_22', 'INT_23', 'INT_24', 'INT_25', 'INT_27', 'INT_28', 'INT_29',
1178+
'INT_30', 'INT_31', 'INT_32', 'INT_33', 'INT_34', 'INT_35', 'INT_36', 'INT_37',
1179+
'INT_38', 'INT_39', 'INT_40', 'INT_41', 'INT_42', 'INT_43', 'INT_44', 'INT_45',
1180+
'INT_46', 'INT_47', 'INT_48', 'INT_49', 'INT_50', 'INT_53', 'INT_58', 'INT_59',
1181+
'INT_61', 'INT_62', 'INT_63', 'INT_64',
11691182
'FIRST-CLASS_PACKAGE_INTERNATIONAL_SERVICE_MACHINABLE_ISC_SINGLE-PIECE',
11701183
'PRIORITY_MAIL_INTERNATIONAL_ISC_SINGLE-PIECE',
11711184
'PRIORITY_MAIL_EXPRESS_INTERNATIONAL_ISC_SINGLE-PIECE',

app/code/Magento/Usps/Model/ShipmentService.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ public function _doShipmentRequestRest(DataObject $request): DataObject
539539
$accessToken = $this->carrierModel->getOauthAccessRequest();
540540

541541
if (!$accessToken) {
542-
throw new LocalizedException(__('We couldn\'t connect to USPS at the moment. Please try again shortly.'));
542+
throw new LocalizedException(__('We couldn\'t connect to USPS at the moment.
543+
Please try again shortly.'));
543544
}
544545

545546
$paymentToken = $this->getLabelPaymentTokenRequest($accessToken);
@@ -597,7 +598,8 @@ public function _doShipmentRequestRest(DataObject $request): DataObject
597598
} else {
598599
$errorMsg = $this->handleErrorResponse($response);
599600
if (empty($errorMsg)) {
600-
$errorMsg[] = $response['error']['message'] ?? __('An error occurred while processing your request.');
601+
$errorMsg[] = $response['error']['message']
602+
?? __('An error occurred while processing your request.');
601603
}
602604
$debugData['result'] = [
603605
'error' => $errorMsg,
@@ -638,7 +640,8 @@ public function setPackageRequest(?DataObject $request): DataObject
638640
// Handle girth for non-rectangular containers
639641
$container = $this->carrierModel->getConfigData('container');
640642
if (in_array($container, ['NONRECTANGULAR', 'VARIABLE'], true)) {
641-
$girth = (int)($request->getGirth() ?: $this->carrierModel->getConfigData('girth') ?: $defaultDimension);
643+
$girth = (int)($request->getGirth()
644+
?: $this->carrierModel->getConfigData('girth') ?: $defaultDimension);
642645
$request->setPackageGirth($girth);
643646
}
644647
} else {
@@ -647,8 +650,9 @@ public function setPackageRequest(?DataObject $request): DataObject
647650
$request->setPackageLength($length);
648651
}
649652

650-
if ($this->shippingMethodManager->getPackageType($request->getShippingMethod()) === 'FLAT_RATE_ENVELOPE') {
651-
$packageDimension = $this->shippingMethodManager->getMethodMinDimensions($request->getShippingMethod());
653+
// Apply minimum dimensions if they exist for the shipping method
654+
$packageDimension = $this->shippingMethodManager->getMethodMinDimensions($request->getShippingMethod());
655+
if ($packageDimension) {
652656
$request->setPackageHeight(($packageDimension['height'] ?? $height));
653657
$request->setPackageWidth(($packageDimension['width'] ?? $width));
654658
$request->setPackageLength(($packageDimension['length'] ?? $length));
@@ -816,8 +820,8 @@ private function prepareCustomFormForIntl($request, $packageParams, &$requestPar
816820
<a href="%1" target="_blank">Schedule B Export Codes</a>
817821
and the shipment must not require an export license. If any item exceeds this value,
818822
an export license is required. A shipment (regardless of value) is going to Canada and does not
819-
require an export license. Users may enter <b>%2</b> in the AESITN field if the shipment value meets the exemption criteria.
820-
Please contact USPS for more information.',
823+
require an export license. Users may enter <b>%2</b> in the AESITN field if the shipment value
824+
meets the exemption criteria. Please contact USPS for more information.',
821825
'www.census.gov/foreign-trade/schedules/b',
822826
"'NO EEI 30.37(a)'"
823827
)

app/code/Magento/Usps/Model/ShippingMethodManager.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ class ShippingMethodManager
188188
'mail_class' => "PRIORITY_MAIL_INTERNATIONAL",
189189
'processing_category' => "FLATS",
190190
'destination_entry_facility_type' => "INTERNATIONAL_SERVICE_CENTER",
191-
'package_type' => "FLAT_RATE_ENVELOPE",
192191
'min_dimension' => [
193192
'length' => 12.5,
194193
'height' => 9.5,
@@ -202,7 +201,6 @@ class ShippingMethodManager
202201
'mail_class' => "PRIORITY_MAIL_INTERNATIONAL",
203202
'processing_category' => "MACHINABLE",
204203
'destination_entry_facility_type' => "INTERNATIONAL_SERVICE_CENTER",
205-
'package_type' => "FLAT_RATE_ENVELOPE",
206204
'min_dimension' => [
207205
'length' => 11.25,
208206
'height' => 8.75,
@@ -216,7 +214,6 @@ class ShippingMethodManager
216214
'mail_class' => "PRIORITY_MAIL_INTERNATIONAL",
217215
'processing_category' => "MACHINABLE",
218216
'destination_entry_facility_type' => "INTERNATIONAL_SERVICE_CENTER",
219-
'package_type' => "FLAT_RATE_ENVELOPE",
220217
'min_dimension' => [
221218
'length' => 12.25,
222219
'height' => 12.25,
@@ -230,7 +227,6 @@ class ShippingMethodManager
230227
'mail_class' => "PRIORITY_MAIL_INTERNATIONAL",
231228
'processing_category' => "MACHINABLE",
232229
'destination_entry_facility_type' => "INTERNATIONAL_SERVICE_CENTER",
233-
'package_type' => "FLAT_RATE_ENVELOPE",
234230
'min_dimension' => [
235231
'length' => 8.6875,
236232
'height' => 5.4375,
@@ -244,7 +240,6 @@ class ShippingMethodManager
244240
'mail_class' => "PRIORITY_MAIL_INTERNATIONAL",
245241
'processing_category' => "FLATS",
246242
'destination_entry_facility_type' => "INTERNATIONAL_SERVICE_CENTER",
247-
'package_type' => "FLAT_RATE_ENVELOPE",
248243
'min_dimension' => [
249244
'length' => 15,
250245
'height' => 9.5,
@@ -258,7 +253,6 @@ class ShippingMethodManager
258253
'mail_class' => "PRIORITY_MAIL_INTERNATIONAL",
259254
'processing_category' => "MACHINABLE",
260255
'destination_entry_facility_type' => "INTERNATIONAL_SERVICE_CENTER",
261-
'package_type' => "FLAT_RATE_ENVELOPE",
262256
'min_dimension' => [
263257
'length' => 12.5,
264258
'height' => 9.5,
@@ -272,7 +266,6 @@ class ShippingMethodManager
272266
'mail_class' => "PRIORITY_MAIL_EXPRESS_INTERNATIONAL",
273267
'processing_category' => "MACHINABLE",
274268
'destination_entry_facility_type' => "INTERNATIONAL_SERVICE_CENTER",
275-
'package_type' => "FLAT_RATE_ENVELOPE",
276269
'min_dimension' => [
277270
'length' => 12.5,
278271
'height' => 9.5,
@@ -286,7 +279,6 @@ class ShippingMethodManager
286279
'mail_class' => "PRIORITY_MAIL_EXPRESS_INTERNATIONAL",
287280
'processing_category' => "FLATS",
288281
'destination_entry_facility_type' => "INTERNATIONAL_SERVICE_CENTER",
289-
'package_type' => "FLAT_RATE_ENVELOPE",
290282
'min_dimension' => [
291283
'length' => 15,
292284
'height' => 9.5,
@@ -300,7 +292,6 @@ class ShippingMethodManager
300292
'mail_class' => "PRIORITY_MAIL_EXPRESS_INTERNATIONAL",
301293
'processing_category' => "FLATS",
302294
'destination_entry_facility_type' => "INTERNATIONAL_SERVICE_CENTER",
303-
'package_type' => "FLAT_RATE_ENVELOPE",
304295
'min_dimension' => [
305296
'length' => 12.5,
306297
'height' => 9.5,
@@ -421,16 +412,4 @@ public function getMethodMaxDimensions(string $methodCode): ?array
421412
$methodCode = strtoupper($methodCode);
422413
return $this->shippingMethods[$methodCode]['max_dimension'] ?? null;
423414
}
424-
425-
/**
426-
* Get the rate indicator for a specific method code
427-
*
428-
* @param string $methodCode The shipping method code
429-
* @return string|null The rate indicator or null if not found
430-
*/
431-
public function getPackageType(string $methodCode): ?string
432-
{
433-
$methodCode = strtoupper($methodCode);
434-
return $this->shippingMethods[$methodCode]['package_type'] ?? null;
435-
}
436415
}

app/code/Magento/Usps/Test/Unit/Model/ShipmentServiceTest.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ public function testPreparePackageDimensionsWithGenericException(): void
528528
}
529529

530530
/**
531-
* Test setPackageRequest for large packages
531+
* Test setPackageRequest for large package
532532
*/
533533
public function testSetPackageRequestLargePackage(): void
534534
{
@@ -546,11 +546,6 @@ public function testSetPackageRequestLargePackage(): void
546546
->with('container')
547547
->willReturn('NONRECTANGULAR');
548548

549-
$this->shippingMethodManagerMock->expects($this->once())
550-
->method('getPackageType')
551-
->with('Priority Mail')
552-
->willReturn('RECTANGULAR');
553-
554549
$result = $this->shipmentService->setPackageRequest($request);
555550

556551
$this->assertEquals('LARGE', $result->getPackageSize());
@@ -584,11 +579,6 @@ public function testSetPackageRequestFlatRateEnvelope(): void
584579
return $map[$key] ?? null;
585580
});
586581

587-
$this->shippingMethodManagerMock->expects($this->once())
588-
->method('getPackageType')
589-
->with('Priority Mail Express Flat Rate Envelope')
590-
->willReturn('FLAT_RATE_ENVELOPE');
591-
592582
$this->shippingMethodManagerMock->expects($this->once())
593583
->method('getMethodMinDimensions')
594584
->with('Priority Mail Express Flat Rate Envelope')
@@ -975,7 +965,7 @@ public function testPrepareIntlShipmentLabelRestRequestHighValueException(): voi
975965
// The method should return a DataObject with errors instead of throwing an exception
976966
$this->assertInstanceOf(DataObject::class, $result);
977967
$this->assertNotEmpty($result->getErrors());
978-
$this->assertStringContainsString('valued at $2,500 or less', implode(' ', $result->getErrors()));
968+
$this->assertStringContainsString('Schedule B Export Codes', implode(' ', $result->getErrors()));
979969
}
980970

981971
/**

0 commit comments

Comments
 (0)