Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- `betaReferralCustomer.createBankAccountClientSecret`
- `referralCustomer.addCreditCardFromStripe`
- `referralCustomer.addBankAccountFromStripe`
- Routes `AmazonShippingAccount` to the correct endpoint
- Corrects wrapping payload for update webhook endpoint
- Corrects various type hints throughout project
- Fixes error handling
Expand Down
4 changes: 4 additions & 0 deletions lib/EasyPost/Constant/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ abstract class Constants
'UpsSurepostAccount'
];

const CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH = [
'AmazonShippingAccount',
];

// Exception messages (many of these are intended to be used with `sprintf()`)
const ARRAY_REQUIRED_ERROR = 'You must pass an array as the first argument to EasyPost API method calls.';
const COMMUNICATION_ERROR = 'Unexpected error communicating with %s. If this problem persists please let us know at ' . self::SUPPORT_EMAIL . '. %s'; // phpcs:ignore
Expand Down
12 changes: 9 additions & 3 deletions lib/EasyPost/Service/CarrierAccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ private function selectCarrierAccountCreationEndpoint(string $carrierAccountType
return '/carrier_accounts/register';
} else if (in_array($carrierAccountType, Constants::UPS_OAUTH_ACCOUNT_TYPES, true)) {
return '/ups_oauth_registrations';
} else if (in_array($carrierAccountType, Constants::CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH, true)) {
return '/carrier_accounts/register_oauth';
}

return '/carrier_accounts';
Expand All @@ -126,8 +128,12 @@ private function selectCarrierAccountCreationEndpoint(string $carrierAccountType
*/
private function selectTopLayerKey(string $carrierAccountType): string
{
return in_array($carrierAccountType, Constants::UPS_OAUTH_ACCOUNT_TYPES, true)
? 'ups_oauth_registrations'
: 'carrier_account';
if (in_array($carrierAccountType, Constants::UPS_OAUTH_ACCOUNT_TYPES, true)) {
return 'ups_oauth_registrations';
} else if (in_array($carrierAccountType, Constants::CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH, true)) {
return 'carrier_account_oauth_registrations';
} else {
return 'carrier_account';
}
}
}
48 changes: 36 additions & 12 deletions test/EasyPost/CarrierAccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,46 @@ public function testCreate(): void
}

/**
* Test creating an UPS account.
* Test creating a UPS account.
*/
public function testCreateUps(): void
{
TestUtil::setupCassette('carrier_accounts/create_ups.yml');

self::$client = new EasyPostClient((string)getenv('EASYPOST_PROD_API_KEY'));

$upsAccount = self::$client->carrierAccount->create([
$carrierAccount = self::$client->carrierAccount->create([
'type' => 'UpsAccount',
'account_number' => '123456789'
]);

$this->assertEquals('UpsAccount', $upsAccount->type);
$this->assertInstanceOf(CarrierAccount::class, $upsAccount);
$this->assertStringMatchesFormat('ca_%s', $upsAccount->id);
$this->assertEquals('UpsAccount', $carrierAccount->type);
$this->assertInstanceOf(CarrierAccount::class, $carrierAccount);
$this->assertStringMatchesFormat('ca_%s', $carrierAccount->id);

// Delete the carrier account once it's done being tested.
self::$client->carrierAccount->delete($upsAccount->id);
self::$client->carrierAccount->delete($carrierAccount->id);
}

/**
* Test creating an Amazon account.
*/
public function testCreateAmazon(): void
{
TestUtil::setupCassette('carrier_accounts/create_amazon.yml');

self::$client = new EasyPostClient((string)getenv('EASYPOST_PROD_API_KEY'));

$carrierAccount = self::$client->carrierAccount->create([
'type' => 'AmazonShippingAccount',
]);

$this->assertEquals('AmazonShippingAccount', $carrierAccount->type);
$this->assertInstanceOf(CarrierAccount::class, $carrierAccount);
$this->assertStringMatchesFormat('ca_%s', $carrierAccount->id);

// Delete the carrier account once it's done being tested.
self::$client->carrierAccount->delete($carrierAccount->id);
}

/**
Expand Down Expand Up @@ -169,19 +190,22 @@ public function testUpdateUps(): void

self::$client = new EasyPostClient((string)getenv('EASYPOST_PROD_API_KEY'));

$upsAccount = self::$client->carrierAccount->create([
$carrierAccount = self::$client->carrierAccount->create([
'type' => 'UpsAccount',
'account_number' => '123456789'
]);

$updatedUpsAccount = self::$client->carrierAccount->update($upsAccount->id, ['account_number' => '987654321']);
$updatedCarrierAccount = self::$client->carrierAccount->update(
$carrierAccount->id,
['account_number' => '987654321']
);

$this->assertInstanceOf(CarrierAccount::class, $updatedUpsAccount);
$this->assertStringMatchesFormat('ca_%s', $updatedUpsAccount->id);
$this->assertEquals('UpsAccount', $updatedUpsAccount->type);
$this->assertInstanceOf(CarrierAccount::class, $updatedCarrierAccount);
$this->assertStringMatchesFormat('ca_%s', $updatedCarrierAccount->id);
$this->assertEquals('UpsAccount', $updatedCarrierAccount->type);

// Delete the carrier account once it's done being tested.
self::$client->carrierAccount->delete($updatedUpsAccount->id);
self::$client->carrierAccount->delete($updatedCarrierAccount->id);
}

/**
Expand Down
157 changes: 157 additions & 0 deletions test/cassettes/carrier_accounts/create_amazon.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading