Skip to content

Commit be13826

Browse files
authored
Add new smartrate endpoints functions (#342)
1 parent ad98770 commit be13826

12 files changed

+549
-57
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Next Release
44

5+
- Adds new `shipment.recommendShipDate`, `smartrate.recommendShipDate`, and `smartrate.estimateDeliveryDate` functions
56
- Routes `UpsAccount`, `UpsMailInnovationsAccount`, and `UpsSurepostAccount` create/update requests to the new `/ups_oauth_registrations` endpoint
67
- Starting `2024-08-05`, UPS accounts will require a new payload to register or update. See [UPS OAuth 2.0 Update](https://support.easypost.com/hc/en-us/articles/26635027512717-UPS-OAuth-2-0-Update?utm_medium=email&_hsenc=p2ANqtz-96MmFtWICOzy9sKRbbcZSiMovZSrY3MSX1_bgY9N3f9yLVfWQdLhjAGq-SmNcOnDIS6GYhZ0OApjDBrGkKyLLMx1z6_TFOVp6-wllhEFQINrkuRuc&_hsmi=313130292&utm_content=313130292&utm_source=hs_email) for more details
78

lib/EasyPost/EasyPostClient.php

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use EasyPost\Service\ReportService;
3030
use EasyPost\Service\ScanFormService;
3131
use EasyPost\Service\ShipmentService;
32+
use EasyPost\Service\SmartRateService;
3233
use EasyPost\Service\TrackerService;
3334
use EasyPost\Service\UserService;
3435
use EasyPost\Service\WebhookService;
@@ -60,6 +61,7 @@
6061
* @property ReportService $report
6162
* @property ScanFormService $scanForm
6263
* @property ShipmentService $shipment
64+
* @property SmartRateService $smartRate
6365
* @property TrackerService $tracker
6466
* @property UserService $user
6567
* @property WebhookService $webhook
@@ -131,6 +133,7 @@ public function __get(string $serviceName)
131133
'report' => ReportService::class,
132134
'scanForm' => ScanFormService::class,
133135
'shipment' => ShipmentService::class,
136+
'smartRate' => SmartRateService::class,
134137
'tracker' => TrackerService::class,
135138
'user' => UserService::class,
136139
'webhook' => WebhookService::class,

lib/EasyPost/Service/ShipmentService.php

+20
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,24 @@ public function retrieveEstimatedDeliveryDate(string $id, string $plannedShipDat
234234

235235
return InternalUtil::convertToEasyPostObject($this->client, $response['rates'] ?? []);
236236
}
237+
238+
/**
239+
* Retrieve a recommended ship date for an existing Shipment via the Precision Shipping API,
240+
* based on a specific desired delivery date.
241+
*
242+
* @param string $id
243+
* @param string $desiredDeliveryDate
244+
* @return mixed
245+
*/
246+
public function recommendShipDate(string $id, string $desiredDeliveryDate): mixed
247+
{
248+
$params = [
249+
'desired_delivery_date' => $desiredDeliveryDate,
250+
];
251+
252+
$url = $this->instanceUrl(self::serviceModelClassName(self::class), $id) . '/smartrate/precision_shipping';
253+
$response = Requestor::request($this->client, 'get', $url, $params);
254+
255+
return InternalUtil::convertToEasyPostObject($this->client, $response['rates'] ?? []);
256+
}
237257
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace EasyPost\Service;
4+
5+
use EasyPost\Exception\General\EndOfPaginationException;
6+
use EasyPost\Http\Requestor;
7+
use EasyPost\Util\InternalUtil;
8+
9+
/**
10+
* SmartRate service containing all the details of SmartRate requests.
11+
*/
12+
class SmartRateService extends BaseService
13+
{
14+
/**
15+
* Retrieve a recommended ship date for each carrier-service level combination via the
16+
* Smart Deliver On API, based on a specific delivery date and origin-destination postal code pair.
17+
*
18+
* @param mixed $params
19+
* @return mixed
20+
*/
21+
public function recommendShipDate(mixed $params = null): mixed
22+
{
23+
$response = Requestor::request($this->client, 'post', '/smartrate/deliver_on', $params);
24+
25+
return InternalUtil::convertToEasyPostObject($this->client, $response);
26+
}
27+
28+
/**
29+
* Retrieve the estimated delivery date of each carrier-service level combination via the
30+
* Smart Deliver By API, based on a specific ship date and origin-destination postal code pair.
31+
*
32+
* @param mixed $params
33+
* @return mixed
34+
*/
35+
public function estimateDeliveryDate(mixed $params = null): mixed
36+
{
37+
$response = Requestor::request($this->client, 'post', '/smartrate/deliver_by', $params);
38+
39+
return InternalUtil::convertToEasyPostObject($this->client, $response);
40+
}
41+
}

lib/easypost.php

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
require_once(dirname(__FILE__) . '/EasyPost/Service/ReportService.php');
111111
require_once(dirname(__FILE__) . '/EasyPost/Service/ScanFormService.php');
112112
require_once(dirname(__FILE__) . '/EasyPost/Service/ShipmentService.php');
113+
require_once(dirname(__FILE__) . '/EasyPost/Service/SmartRateService.php');
113114
require_once(dirname(__FILE__) . '/EasyPost/Service/TrackerService.php');
114115
require_once(dirname(__FILE__) . '/EasyPost/Service/UserService.php');
115116
require_once(dirname(__FILE__) . '/EasyPost/Service/WebhookService.php');

test/EasyPost/Fixture.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ public static function rmaFormOtions(): array
231231

232232
public static function plannedShipDate(): string
233233
{
234-
return '2023-11-24';
234+
return '2024-07-16';
235+
}
236+
237+
public static function desiredDeliveryDate(): string
238+
{
239+
return '2024-07-16';
235240
}
236241
}

test/EasyPost/ShipmentTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -516,4 +516,23 @@ public function testRetrieveEstimatedDeliveryDate(): void
516516
$this->assertNotNull($entry->easypost_time_in_transit_data);
517517
}
518518
}
519+
520+
/**
521+
* Tests that we retrieve the recommend ship date of a Shipment.
522+
*/
523+
public function testRetrieveRecommendDate(): void
524+
{
525+
TestUtil::setupCassette('shipments/recommendShipDate.yml');
526+
527+
$shipment = self::$client->shipment->create(Fixture::basicShipment());
528+
529+
$rates = self::$client->shipment->recommendShipDate(
530+
$shipment->id,
531+
Fixture::desiredDeliveryDate(),
532+
);
533+
534+
foreach ($rates as $entry) {
535+
$this->assertNotNull($entry->easypost_time_in_transit_data);
536+
}
537+
}
519538
}

test/EasyPost/SmartRateTest.php

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace EasyPost\Test;
4+
5+
use EasyPost\EasyPostClient;
6+
use EasyPost\Exception\General\EndOfPaginationException;
7+
use Exception;
8+
9+
class SmartRateTest extends \PHPUnit\Framework\TestCase
10+
{
11+
private static EasyPostClient $client;
12+
13+
/**
14+
* Setup the testing environment for this file.
15+
*/
16+
public static function setUpBeforeClass(): void
17+
{
18+
TestUtil::setupVcrTests();
19+
self::$client = new EasyPostClient(getenv('EASYPOST_TEST_API_KEY'));
20+
}
21+
22+
/**
23+
* Cleanup the testing environment once finished.
24+
*/
25+
public static function tearDownAfterClass(): void
26+
{
27+
TestUtil::teardownVcrTests();
28+
}
29+
30+
/**
31+
* Test that we retrieve SmartRates when provided a from/to zip and planned ship date.
32+
*/
33+
public function testRetrieveRecommendDate(): void
34+
{
35+
TestUtil::setupCassette('smartrate/recommendShipDate.yml');
36+
37+
$params = [
38+
'from_zip' => Fixture::caAddress1()['zip'],
39+
'to_zip' => Fixture::caAddress2()['zip'],
40+
'desired_delivery_date' => Fixture::desiredDeliveryDate(),
41+
'carriers' => [Fixture::usps()],
42+
];
43+
44+
$rates = self::$client->smartRate->recommendShipDate($params);
45+
46+
foreach ($rates['results'] as $entry) {
47+
$this->assertTrue(
48+
isset($entry['easypost_time_in_transit_data']),
49+
'Assertion failed: easypost_time_in_transit_data is not set.'
50+
);
51+
}
52+
}
53+
54+
/**
55+
* Test that we retrieve SmartRates when provided a from/to zip and planned ship date.
56+
*/
57+
public function testRetrieveEstimatedDeliveryDate(): void
58+
{
59+
TestUtil::setupCassette('smartrate/estimatedDeliveryDate.yml');
60+
61+
$params = [
62+
'from_zip' => Fixture::caAddress1()['zip'],
63+
'to_zip' => Fixture::caAddress2()['zip'],
64+
'planned_ship_date' => Fixture::plannedShipDate(),
65+
'carriers' => [Fixture::usps()],
66+
];
67+
68+
$rates = self::$client->smartRate->estimateDeliveryDate($params);
69+
70+
foreach ($rates['results'] as $entry) {
71+
$this->assertTrue(
72+
isset($entry['easypost_time_in_transit_data']),
73+
'Assertion failed: easypost_time_in_transit_data is not set.'
74+
);
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)