Skip to content

Commit cffe5ac

Browse files
authored
Merge pull request #371 from EasyPost/SHPE-594_sessions
feat: portal and embeddable sessions
2 parents 2e5273c + aefe879 commit cffe5ac

File tree

18 files changed

+527
-37
lines changed

18 files changed

+527
-37
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## Next Release
4+
5+
- Adds the following functions:
6+
- `embeddable.createSession`
7+
- `customerPortal.createAccountLink`
8+
39
## v8.3.0 (2025-11-10)
410

511
- Adds support for `UspsShipAccount`

lib/EasyPost/EasyPostClient.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
use EasyPost\Service\CarrierAccountService;
1717
use EasyPost\Service\CarrierMetadataService;
1818
use EasyPost\Service\ClaimService;
19+
use EasyPost\Service\CustomerPortalService;
1920
use EasyPost\Service\CustomsInfoService;
2021
use EasyPost\Service\CustomsItemService;
22+
use EasyPost\Service\EmbeddableService;
2123
use EasyPost\Service\EndShipperService;
2224
use EasyPost\Service\EventService;
2325
use EasyPost\Service\InsuranceService;
@@ -50,8 +52,10 @@
5052
* @property CarrierAccountService $carrierAccount
5153
* @property CarrierMetadataService $carrierMetadata
5254
* @property ClaimService $claim
55+
* @property CustomerPortalService $customerPortal
5356
* @property CustomsInfoService $customsInfo
5457
* @property CustomsItemService $customsItem
58+
* @property EmbeddableService $embeddable
5559
* @property EndShipperService $endShipper
5660
* @property EventService $event
5761
* @property InsuranceService $insurance
@@ -124,8 +128,10 @@ public function __get(string $serviceName)
124128
'carrierAccount' => CarrierAccountService::class,
125129
'carrierMetadata' => CarrierMetadataService::class,
126130
'claim' => ClaimService::class,
131+
'customerPortal' => CustomerPortalService::class,
127132
'customsInfo' => CustomsInfoService::class,
128133
'customsItem' => CustomsItemService::class,
134+
'embeddable' => EmbeddableService::class,
129135
'endShipper' => EndShipperService::class,
130136
'event' => EventService::class,
131137
'insurance' => InsuranceService::class,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace EasyPost\Service;
4+
5+
use EasyPost\Http\Requestor;
6+
use EasyPost\Util\InternalUtil;
7+
8+
/**
9+
* CustomerPortal service containing all the logic to make API calls.
10+
*/
11+
class CustomerPortalService extends BaseService
12+
{
13+
/**
14+
* Create a Portal Session.
15+
*
16+
* @param mixed $params
17+
* @return mixed
18+
*/
19+
public function createAccountLink(mixed $params = null): mixed
20+
{
21+
$response = Requestor::request($this->client, 'post', '/customer_portal/account_link', $params);
22+
23+
return InternalUtil::convertToEasyPostObject($this->client, $response);
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace EasyPost\Service;
4+
5+
use EasyPost\Http\Requestor;
6+
use EasyPost\Util\InternalUtil;
7+
8+
/**
9+
* Embeddable service containing all the logic to make API calls.
10+
*/
11+
class EmbeddableService extends BaseService
12+
{
13+
/**
14+
* Create an Embeddable Session.
15+
*
16+
* @param mixed $params
17+
* @return mixed
18+
*/
19+
public function createSession(mixed $params = null): mixed
20+
{
21+
$response = Requestor::request($this->client, 'post', '/embeddables/session', $params);
22+
23+
return InternalUtil::convertToEasyPostObject($this->client, $response);
24+
}
25+
}

lib/EasyPost/Util/InternalUtil.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,15 @@ public static function convertToEasyPostObject(EasyPostClient|null $client, mixe
181181
* @param array<string> $carriers
182182
* @param array<string> $services
183183
* @param string|null $ratesKey
184-
* @return Rate|PickupRate
184+
* @return object
185185
* @throws EasyPostException
186186
*/
187187
public static function getLowestObjectRate(
188188
?EasyPostObject $easypostObject,
189189
?array $carriers = [],
190190
?array $services = [],
191191
?string $ratesKey = 'rates'
192-
): Rate|PickupRate {
192+
): object {
193193
$lowestRate = false;
194194
$carriersInclude = [];
195195
$carriersExclude = [];
@@ -235,8 +235,11 @@ public static function getLowestObjectRate(
235235
continue;
236236
}
237237

238-
if (!$lowestRate || floatval($easypostObject->$ratesKey[$i]->rate) < floatval($lowestRate->rate)) {
239-
$lowestRate = clone ($easypostObject->$ratesKey[$i]);
238+
if (
239+
!$lowestRate ||
240+
floatval($easypostObject->$ratesKey[$i]->rate) < floatval($lowestRate->rate) // @phpstan-ignore-line
241+
) {
242+
$lowestRate = clone($easypostObject->$ratesKey[$i]);
240243
}
241244
}
242245

lib/easypost.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@
9898
require_once(dirname(__FILE__) . '/EasyPost/Service/BillingService.php');
9999
require_once(dirname(__FILE__) . '/EasyPost/Service/CarrierAccountService.php');
100100
require_once(dirname(__FILE__) . '/EasyPost/Service/ClaimService.php');
101+
require_once(dirname(__FILE__) . '/EasyPost/Service/CustomerPortalService.php');
101102
require_once(dirname(__FILE__) . '/EasyPost/Service/CustomsInfoService.php');
102103
require_once(dirname(__FILE__) . '/EasyPost/Service/CustomsItemService.php');
104+
require_once(dirname(__FILE__) . '/EasyPost/Service/EmbeddableService.php');
103105
require_once(dirname(__FILE__) . '/EasyPost/Service/EndShipperService.php');
104106
require_once(dirname(__FILE__) . '/EasyPost/Service/EventService.php');
105107
require_once(dirname(__FILE__) . '/EasyPost/Service/InsuranceService.php');
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace EasyPost\Test;
4+
5+
use EasyPost\EasyPostClient;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class CustomerPortalTest extends TestCase
9+
{
10+
private static EasyPostClient $client;
11+
12+
/**
13+
* Setup the testing environment for this file.
14+
*/
15+
public static function setUpBeforeClass(): void
16+
{
17+
TestUtil::setupVcrTests();
18+
self::$client = new EasyPostClient((string)getenv('EASYPOST_PROD_API_KEY'));
19+
}
20+
21+
/**
22+
* Cleanup the testing environment once finished.
23+
*/
24+
public static function tearDownAfterClass(): void
25+
{
26+
TestUtil::teardownVcrTests();
27+
}
28+
29+
/**
30+
* Test creating an account link
31+
*/
32+
public function testCreateAccountLink(): void
33+
{
34+
TestUtil::setupCassette('customer_portal/createAccountLink.yml');
35+
36+
$user = self::$client->user->allChildren()['children'][0];
37+
38+
$accountLink = self::$client->customerPortal->createAccountLink(
39+
[
40+
'session_type' => 'account_onboarding',
41+
'user_id' => $user->id,
42+
'refresh_url' => 'https://example.com/refresh',
43+
'return_url' => 'https://example.com/return',
44+
]
45+
);
46+
47+
$this->assertEquals('CustomerPortalAccountLink', $accountLink->object);
48+
}
49+
}

test/EasyPost/EmbeddableTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace EasyPost\Test;
4+
5+
use EasyPost\EasyPostClient;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class EmbeddableTest extends TestCase
9+
{
10+
private static EasyPostClient $client;
11+
12+
/**
13+
* Setup the testing environment for this file.
14+
*/
15+
public static function setUpBeforeClass(): void
16+
{
17+
TestUtil::setupVcrTests();
18+
self::$client = new EasyPostClient((string)getenv('EASYPOST_PROD_API_KEY'));
19+
}
20+
21+
/**
22+
* Cleanup the testing environment once finished.
23+
*/
24+
public static function tearDownAfterClass(): void
25+
{
26+
TestUtil::teardownVcrTests();
27+
}
28+
29+
/**
30+
* Test creating an account link
31+
*/
32+
public function testCreateSession(): void
33+
{
34+
TestUtil::setupCassette('embeddables/createSession.yml');
35+
36+
$user = self::$client->user->allChildren()['children'][0];
37+
38+
$accountLink = self::$client->embeddable->createSession(
39+
[
40+
'origin_host' => 'https://example.com',
41+
'user_id' => $user->id,
42+
]
43+
);
44+
45+
$this->assertEquals('EmbeddablesSession', $accountLink->object);
46+
}
47+
}

test/EasyPost/Fixture.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,12 @@ public static function lumaPlannedShipDate(): string
296296
{
297297
return '2025-06-12';
298298
}
299+
300+
/**
301+
* @return array<mixed>
302+
*/
303+
public static function referralUser(): array
304+
{
305+
return self::readFixtureData()['users']['referral'];
306+
}
299307
}

test/EasyPost/ReferralCustomerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public function testCreate(): void
4343
TestUtil::setupCassette('referral_customers/create.yml');
4444

4545
$referral = self::$client->referralCustomer->create([
46-
'name' => 'Test Referral',
47-
'email' => '[email protected]',
48-
'phone' => '8888888888'
46+
'name' => Fixture::referralUser()['name'],
47+
'email' => Fixture::referralUser()['email'],
48+
'phone' => Fixture::referralUser()['phone'],
4949
]);
5050

5151
$this->assertInstanceOf(User::class, $referral);

0 commit comments

Comments
 (0)