Skip to content

Commit ad98770

Browse files
author
Jack Chen
authored
Update CarrierService update and create method to account UPS endpoints (#341)
1 parent 525d2c6 commit ad98770

File tree

8 files changed

+724
-88
lines changed

8 files changed

+724
-88
lines changed

CHANGELOG.md

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

3+
## Next Release
4+
5+
- Routes `UpsAccount`, `UpsMailInnovationsAccount`, and `UpsSurepostAccount` create/update requests to the new `/ups_oauth_registrations` endpoint
6+
- 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
7+
38
## v7.2.0 (2024-04-10)
49

510
- Fix payment method funding and deletion failures due to undetermined payment method type

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ docs:
2323
curl -LJs https://github.com/phpDocumentor/phpDocumentor/releases/download/v3.3.1/phpDocumentor.phar -o phpDocumentor.phar
2424
php phpDocumentor.phar -d lib -t docs
2525

26+
## init-examples-submodule - Initialize the examples submodule
27+
init-examples-submodule:
28+
git submodule init
29+
git submodule update
30+
2631
## install - Install dependencies
27-
install: | update-examples-submodule
32+
install: | init-examples-submodule
2833
composer install --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
2934

3035
## lint - Lint the project

lib/EasyPost/Constant/Constants.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ abstract class Constants
1717
// Validation
1818
const CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS = [
1919
'FedexAccount',
20-
'UpsAccount',
2120
'FedexSmartpostAccount'
2221
];
2322

23+
const UPS_OAUTH_ACCOUNT_TYPES = [
24+
'UpsAccount',
25+
'UpsMailInnovationsAccount',
26+
'UpsSurepostAccount'
27+
];
28+
2429
// Exception messages (many of these are intended to be used with `sprintf()`)
2530
const ARRAY_REQUIRED_ERROR = 'You must pass an array as the first argument to EasyPost API method calls.';
2631
const COMMUNICATION_ERROR = 'Unexpected error communicating with %s. If this problem persists please let us know at ' . self::SUPPORT_EMAIL . '. %s'; // phpcs:ignore

lib/EasyPost/Service/CarrierAccountService.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@ public function all(mixed $params = null): mixed
4343
*/
4444
public function update(string $id, mixed $params): mixed
4545
{
46-
return self::updateResource(self::serviceModelClassName(self::class), $id, $params);
46+
$carrierAccount = self::retrieve($id);
47+
$carrierAccountType = $carrierAccount['type'];
48+
if (in_array($carrierAccountType, Constants::UPS_OAUTH_ACCOUNT_TYPES, true)) {
49+
$className = 'UpsOauthRegistration';
50+
$params = [self::selectTopLayerKey($carrierAccountType) => $params];
51+
} else {
52+
$className = 'CarrierAccount';
53+
$params = [self::selectTopLayerKey($carrierAccountType) => $params];
54+
}
55+
return self::updateResource($className, $id, $params);
4756
}
4857

4958
/**
@@ -67,17 +76,13 @@ public function delete(string $id, mixed $params = null): void
6776
*/
6877
public function create(mixed $params = null): mixed
6978
{
70-
if (!isset($params['carrier_account']) || !is_array($params['carrier_account'])) {
71-
$clone = $params;
72-
unset($params);
73-
$params['carrier_account'] = $clone;
74-
}
75-
76-
if (!isset($params['carrier_account']['type'])) {
79+
if (!isset($params['type'])) {
7780
throw new MissingParameterException(sprintf(Constants::MISSING_PARAMETER_ERROR, 'type'));
7881
}
7982

80-
$url = self::selectCarrierAccountCreationEndpoint($params['carrier_account']['type']);
83+
$carrierAccountType = $params['type'];
84+
$params = [self::selectTopLayerKey($carrierAccountType) => $params];
85+
$url = self::selectCarrierAccountCreationEndpoint($carrierAccountType);
8186
$response = Requestor::request($this->client, 'post', $url, $params);
8287

8388
return InternalUtil::convertToEasyPostObject($this->client, $response);
@@ -106,8 +111,23 @@ private function selectCarrierAccountCreationEndpoint(string $carrierAccountType
106111
{
107112
if (in_array($carrierAccountType, Constants::CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOWS, true)) {
108113
return '/carrier_accounts/register';
114+
} else if (in_array($carrierAccountType, Constants::UPS_OAUTH_ACCOUNT_TYPES, true)) {
115+
return '/ups_oauth_registrations';
109116
}
110117

111118
return '/carrier_accounts';
112119
}
120+
121+
/**
122+
* Select the top-key layer for creating/updating a carrier account based on the type of carrier account.
123+
*
124+
* @param string $carrierAccountType The type of carrier account to create.
125+
* @return string The top-layer key for creating/updating a carrier account.
126+
*/
127+
private function selectTopLayerKey(string $carrierAccountType): string
128+
{
129+
return in_array($carrierAccountType, Constants::UPS_OAUTH_ACCOUNT_TYPES, true)
130+
? 'ups_oauth_registrations'
131+
: 'carrier_account';
132+
}
113133
}

test/EasyPost/CarrierAccountTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ public function testCreate(): void
4747
self::$client->carrierAccount->delete($carrierAccount->id);
4848
}
4949

50+
/**
51+
* Test creating an UPS account.
52+
*/
53+
public function testCreateUps(): void
54+
{
55+
TestUtil::setupCassette('carrier_accounts/create_ups.yml');
56+
57+
self::$client = new EasyPostClient(getenv('EASYPOST_PROD_API_KEY'));
58+
59+
$upsAccount = self::$client->carrierAccount->create([
60+
'type' => 'UpsAccount',
61+
'account_number' => '123456789'
62+
]);
63+
64+
$this->assertEquals('UpsAccount', $upsAccount->type);
65+
$this->assertInstanceOf(CarrierAccount::class, $upsAccount);
66+
$this->assertStringMatchesFormat('ca_%s', $upsAccount->id);
67+
68+
// Delete the carrier account once it's done being tested.
69+
self::$client->carrierAccount->delete($upsAccount->id);
70+
}
71+
5072
/**
5173
* Test creating a carrier account.
5274
*/
@@ -138,6 +160,30 @@ public function testAll(): void
138160
$this->assertContainsOnlyInstancesOf(CarrierAccount::class, $carrierAccounts);
139161
}
140162

163+
/**
164+
* Test updating an UPS account.
165+
*/
166+
public function testUpdateUps(): void
167+
{
168+
TestUtil::setupCassette('carrier_accounts/update_ups.yml');
169+
170+
self::$client = new EasyPostClient(getenv('EASYPOST_PROD_API_KEY'));
171+
172+
$upsAccount = self::$client->carrierAccount->create([
173+
'type' => 'UpsAccount',
174+
'account_number' => '123456789'
175+
]);
176+
177+
$updatedUpsAccount = self::$client->carrierAccount->update($upsAccount->id, ['account_number' => '987654321']);
178+
179+
$this->assertInstanceOf(CarrierAccount::class, $updatedUpsAccount);
180+
$this->assertStringMatchesFormat('ca_%s', $updatedUpsAccount->id);
181+
$this->assertEquals('UpsAccount', $updatedUpsAccount->type);
182+
183+
// Delete the carrier account once it's done being tested.
184+
self::$client->carrierAccount->delete($updatedUpsAccount->id);
185+
}
186+
141187
/**
142188
* Test updating a carrier account.
143189
*/

test/cassettes/carrier_accounts/create_ups.yml

Lines changed: 157 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)