From 5f5b5d1d63d7dbc66eba86c466b2b513e5ef7d5d Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Fri, 11 Apr 2025 11:02:29 -0600 Subject: [PATCH 1/2] feat: route AmazonShippingAccount on create --- CHANGELOG.md | 1 + lib/EasyPost/Constant/Constants.php | 4 + .../Service/CarrierAccountService.php | 12 +- test/EasyPost/CarrierAccountTest.php | 45 +++-- .../carrier_accounts/create_amazon.yml | 157 ++++++++++++++++++ 5 files changed, 204 insertions(+), 15 deletions(-) create mode 100644 test/cassettes/carrier_accounts/create_amazon.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index c1f709cf..220093e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/EasyPost/Constant/Constants.php b/lib/EasyPost/Constant/Constants.php index f5bf8cd0..67b39dd0 100644 --- a/lib/EasyPost/Constant/Constants.php +++ b/lib/EasyPost/Constant/Constants.php @@ -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 diff --git a/lib/EasyPost/Service/CarrierAccountService.php b/lib/EasyPost/Service/CarrierAccountService.php index bb4afa99..2ee91225 100644 --- a/lib/EasyPost/Service/CarrierAccountService.php +++ b/lib/EasyPost/Service/CarrierAccountService.php @@ -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'; @@ -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'; + } } } diff --git a/test/EasyPost/CarrierAccountTest.php b/test/EasyPost/CarrierAccountTest.php index 51e0f9fb..7b6f5aea 100644 --- a/test/EasyPost/CarrierAccountTest.php +++ b/test/EasyPost/CarrierAccountTest.php @@ -48,7 +48,7 @@ public function testCreate(): void } /** - * Test creating an UPS account. + * Test creating a UPS account. */ public function testCreateUps(): void { @@ -56,17 +56,38 @@ public function testCreateUps(): 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' ]); - $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); } /** @@ -169,19 +190,19 @@ 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); } /** diff --git a/test/cassettes/carrier_accounts/create_amazon.yml b/test/cassettes/carrier_accounts/create_amazon.yml new file mode 100644 index 00000000..d70eeb2a --- /dev/null +++ b/test/cassettes/carrier_accounts/create_amazon.yml @@ -0,0 +1,157 @@ + +- + request: + method: POST + url: 'https://api.easypost.com/v2/carrier_accounts/register_oauth' + headers: + Host: api.easypost.com + Expect: '' + Accept-Encoding: '' + Accept: application/json + Authorization: '' + Content-Type: application/json + User-Agent: '' + body: '{"carrier_account_oauth_registrations":{"type":"AmazonShippingAccount"}}' + response: + status: + code: 201 + message: Created + headers: + x-frame-options: SAMEORIGIN + x-xss-protection: '1; mode=block' + x-content-type-options: nosniff + x-download-options: noopen + x-permitted-cross-domain-policies: none + referrer-policy: strict-origin-when-cross-origin + x-ep-request-uuid: d574b4b967f94a4de2ca6e9300557166 + cache-control: 'private, no-cache, no-store' + pragma: no-cache + expires: '0' + content-type: 'application/json; charset=utf-8' + content-length: '1416' + x-runtime: '0.075333' + x-node: bigweb42nuq + x-version-label: easypost-202504102051-85835c857e-master + x-backend: easypost + x-proxied: ['intlb4nuq 284c5d344a', 'extlb2nuq 99aac35317'] + strict-transport-security: 'max-age=31536000; includeSubDomains; preload' + body: '{"id":"ca_61a8c4589fe146d68f7ccdca2543f799","object":"CarrierAccount","type":"AmazonShippingAccount","clone":false,"created_at":"2025-04-11T16:58:53Z","updated_at":"2025-04-11T16:58:53Z","description":null,"reference":null,"billing_type":"carrier","readable":"Amazon Shipping","logo":null,"fields":[],"credentials":[],"test_credentials":[]}' + curl_info: + url: 'https://api.easypost.com/v2/carrier_accounts/register_oauth' + content_type: 'application/json; charset=utf-8' + http_code: 201 + header_size: 694 + request_size: 401 + filetime: -1 + ssl_verify_result: 0 + redirect_count: 0 + total_time: 0.208204 + namelookup_time: 0.030903 + connect_time: 0.062197 + pretransfer_time: 0.097447 + size_upload: 72.0 + size_download: 1416.0 + speed_download: 6801.0 + speed_upload: 345.0 + download_content_length: 1416.0 + upload_content_length: 72.0 + starttransfer_time: 0.208157 + redirect_time: 0.0 + redirect_url: '' + primary_ip: 169.62.110.130 + certinfo: { } + primary_port: 443 + local_ip: 192.168.1.75 + local_port: 61084 + http_version: 2 + protocol: 2 + ssl_verifyresult: 0 + scheme: https + appconnect_time_us: 97079 + connect_time_us: 62197 + namelookup_time_us: 30903 + pretransfer_time_us: 97447 + redirect_time_us: 0 + starttransfer_time_us: 208157 + total_time_us: 208204 + effective_method: POST + capath: '' + cainfo: '' + index: 0 +- + request: + method: DELETE + url: 'https://api.easypost.com/v2/carrier_accounts/ca_61a8c4589fe146d68f7ccdca2543f799' + headers: + Host: api.easypost.com + Accept-Encoding: '' + Accept: application/json + Authorization: '' + Content-Type: application/json + User-Agent: '' + response: + status: + code: 200 + message: OK + headers: + x-frame-options: SAMEORIGIN + x-xss-protection: '1; mode=block' + x-content-type-options: nosniff + x-download-options: noopen + x-permitted-cross-domain-policies: none + referrer-policy: strict-origin-when-cross-origin + x-ep-request-uuid: d574b4b667f94a4de2ca6e94005571a8 + cache-control: 'private, no-cache, no-store' + pragma: no-cache + expires: '0' + content-type: 'application/json; charset=utf-8' + content-length: '2' + x-runtime: '0.053299' + x-node: bigweb33nuq + x-version-label: easypost-202504102051-85835c857e-master + x-backend: easypost + x-proxied: ['intlb4nuq 284c5d344a', 'extlb2nuq 99aac35317'] + strict-transport-security: 'max-age=31536000; includeSubDomains; preload' + body: '[]' + curl_info: + url: 'https://api.easypost.com/v2/carrier_accounts/ca_61a8c4589fe146d68f7ccdca2543f799' + content_type: 'application/json; charset=utf-8' + http_code: 200 + header_size: 686 + request_size: 332 + filetime: -1 + ssl_verify_result: 0 + redirect_count: 0 + total_time: 0.160275 + namelookup_time: 0.001693 + connect_time: 0.034594 + pretransfer_time: 0.070726 + size_upload: 0.0 + size_download: 2.0 + speed_download: 12.0 + speed_upload: 0.0 + download_content_length: 2.0 + upload_content_length: 0.0 + starttransfer_time: 0.160221 + redirect_time: 0.0 + redirect_url: '' + primary_ip: 169.62.110.130 + certinfo: { } + primary_port: 443 + local_ip: 192.168.1.75 + local_port: 61085 + http_version: 2 + protocol: 2 + ssl_verifyresult: 0 + scheme: https + appconnect_time_us: 70684 + connect_time_us: 34594 + namelookup_time_us: 1693 + pretransfer_time_us: 70726 + redirect_time_us: 0 + starttransfer_time_us: 160221 + total_time_us: 160275 + effective_method: DELETE + capath: '' + cainfo: '' + index: 0 From 8284307645005f11680be8f1f4f584da78facca4 Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Fri, 11 Apr 2025 11:16:02 -0600 Subject: [PATCH 2/2] fix: lint --- test/EasyPost/CarrierAccountTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/EasyPost/CarrierAccountTest.php b/test/EasyPost/CarrierAccountTest.php index 7b6f5aea..ddb96bb3 100644 --- a/test/EasyPost/CarrierAccountTest.php +++ b/test/EasyPost/CarrierAccountTest.php @@ -195,7 +195,10 @@ public function testUpdateUps(): void 'account_number' => '123456789' ]); - $updatedCarrierAccount = self::$client->carrierAccount->update($carrierAccount->id, ['account_number' => '987654321']); + $updatedCarrierAccount = self::$client->carrierAccount->update( + $carrierAccount->id, + ['account_number' => '987654321'] + ); $this->assertInstanceOf(CarrierAccount::class, $updatedCarrierAccount); $this->assertStringMatchesFormat('ca_%s', $updatedCarrierAccount->id);