diff --git a/CHANGELOG.md b/CHANGELOG.md index a3764069..9978aa8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - `beta_referral_customer.create_bank_account_client_secret` - `referral_customer.add_credit_card_from_stripe` - `referral_customer.add_bank_account_from_stripe` +- Routes `AmazonShippingAccount` to the proper create endpoint - Fixes error parsing - Allows for alternative format of `errors` field - Corrects available properties of an `EasyPostError` and `ApiError` (`code` and `field` removed from `EasyPostError`, `message` unfurled and explicitly added to `ApiError`) diff --git a/lib/easypost/services/carrier_account.rb b/lib/easypost/services/carrier_account.rb index 36a1bd01..00912555 100644 --- a/lib/easypost/services/carrier_account.rb +++ b/lib/easypost/services/carrier_account.rb @@ -3,6 +3,7 @@ class EasyPost::Services::CarrierAccount < EasyPost::Services::Service CUSTOM_WORKFLOW_CARRIER_TYPES = %w[FedexAccount FedexSmartpostAccount].freeze UPS_OAUTH_CARRIER_ACCOUNT_TYPES = %w[UpsAccount UpsMailInnovationsAccount UpsSurepostAccount].freeze + CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH = %w[AmazonShippingAccount].freeze MODEL_CLASS = EasyPost::Models::CarrierAccount # :nodoc: # Create a carrier account @@ -15,6 +16,8 @@ def create(params = {}) 'carrier_accounts/register' elsif UPS_OAUTH_CARRIER_ACCOUNT_TYPES.include?(carrier_account_type) 'ups_oauth_registrations' + elsif CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH.include?(carrier_account_type) + 'carrier_accounts/register_oauth' else 'carrier_accounts' end @@ -63,6 +66,8 @@ def delete(id) def select_top_layer_key(carrier_account_type) if UPS_OAUTH_CARRIER_ACCOUNT_TYPES.include?(carrier_account_type) 'ups_oauth_registrations' + elsif CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH.include?(carrier_account_type) + 'carrier_account_oauth_registrations' else 'carrier_account' end diff --git a/spec/carrier_account_spec.rb b/spec/carrier_account_spec.rb index 48c71b68..edf13a9f 100644 --- a/spec/carrier_account_spec.rb +++ b/spec/carrier_account_spec.rb @@ -17,7 +17,7 @@ client.carrier_account.delete(carrier_account.id) end - it 'creates an UPS account' do + it 'creates a UPS account' do carrier_account = client.carrier_account.create({ type: 'UpsAccount', account_number: '123456789' }) expect(carrier_account).to be_an_instance_of(EasyPost::Models::CarrierAccount) @@ -28,15 +28,28 @@ client.carrier_account.delete(carrier_account.id) end - it 'sends FedexAccount to the correct endpoint' do - allow(client).to receive(:make_request).with( - :post, 'carrier_accounts/register', - { carrier_account: { type: 'FedexAccount' } }, - ).and_return({ 'id' => 'ca_123' }) + it 'creates a FedEx account' do + expect { + client.carrier_account.create({ type: 'FedexAccount', registration_data: {} }) + }.to raise_error(EasyPost::Errors::ApiError) { |error| + expect(error.status_code).to eq(422) + expect( + error.errors.any? do |err| + err['field'] == 'account_number' && err['message'] == 'must be present and a string' + end, + ).to be true + } + end - response = client.carrier_account.create(type: 'FedexAccount') + it 'creates an Amazon account' do + carrier_account = client.carrier_account.create({ type: 'AmazonShippingAccount', account_number: '123456789' }) - expect(response['id']).to eq('ca_123') + expect(carrier_account).to be_an_instance_of(EasyPost::Models::CarrierAccount) + expect(carrier_account.id).to match('ca_') + expect(carrier_account.type).to eq('AmazonShippingAccount') + + # Remove the carrier account once we have tested it so we don't pollute the account with test accounts + client.carrier_account.delete(carrier_account.id) end end diff --git a/spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_a_FedEx_account.yml b/spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_a_FedEx_account.yml new file mode 100644 index 00000000..2a366150 --- /dev/null +++ b/spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_a_FedEx_account.yml @@ -0,0 +1,82 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.easypost.com/v2/carrier_accounts/register + body: + encoding: UTF-8 + string: '{"carrier_account":{"type":"FedexAccount","registration_data":{}}}' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: "" + Host: + - api.easypost.com + Content-Type: + - application/json + Authorization: "" + response: + status: + code: 422 + message: Unprocessable Entity + 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: + - d574b4b667f94839e2ca07b70052f8ac + Cache-Control: + - private, no-cache, no-store + Pragma: + - no-cache + Expires: + - '0' + Content-Type: + - application/json; charset=utf-8 + X-Runtime: + - '0.019799' + Transfer-Encoding: + - chunked + X-Node: + - bigweb54nuq + X-Version-Label: + - easypost-202504102051-85835c857e-master + X-Backend: + - easypost + X-Proxied: + - extlb2nuq 99aac35317 + - intlb4nuq 284c5d344a + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + body: + encoding: UTF-8 + string: '{"error":{"code":"UNPROCESSABLE_ENTITY","message":"The request was + understood, but cannot be processed.","errors":[{"field":"shipping_streets","message":"must + be present and a string"},{"field":"shipping_city","message":"must be present + and a string"},{"field":"shipping_state","message":"must be present and a + string"},{"field":"shipping_postal_code","message":"must be present and a + string"},{"field":"shipping_country_code","message":"must be present and a + string"},{"field":"corporate_first_name","message":"must be present and a + string"},{"field":"corporate_last_name","message":"must be present and a string"},{"field":"corporate_job_title","message":"must + be present and a string"},{"field":"corporate_company_name","message":"must + be present and a string"},{"field":"corporate_phone_number","message":"must + be present and a string"},{"field":"corporate_email_address","message":"must + be present and a string"},{"field":"corporate_streets","message":"must be + present and a string"},{"field":"corporate_city","message":"must be present + and a string"},{"field":"corporate_state","message":"must be present and a + string"},{"field":"corporate_postal_code","message":"must be present and a + string"},{"field":"corporate_country_code","message":"must be present and + a string"},{"field":"account_number","message":"must be present and a string"}]}}' + recorded_at: Fri, 11 Apr 2025 16:50:01 GMT +recorded_with: VCR 6.3.1 diff --git a/spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_an_UPS_account.yml b/spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_a_UPS_account.yml similarity index 78% rename from spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_an_UPS_account.yml rename to spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_a_UPS_account.yml index bc79c4a9..fb8df7c9 100644 --- a/spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_an_UPS_account.yml +++ b/spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_a_UPS_account.yml @@ -35,7 +35,7 @@ http_interactions: Referrer-Policy: - strict-origin-when-cross-origin X-Ep-Request-Uuid: - - d044fa7966a7da65e799d91f005987a1 + - d574b4b467f94771e2ca041900520f14 Cache-Control: - private, no-cache, no-store Pragma: @@ -45,29 +45,27 @@ http_interactions: Content-Type: - application/json; charset=utf-8 X-Runtime: - - '0.090809' + - '0.088219' Transfer-Encoding: - chunked X-Node: - - bigweb43nuq + - bigweb56nuq X-Version-Label: - - easypost-202407291746-57ea285141-master + - easypost-202504102051-85835c857e-master X-Backend: - easypost - X-Canary: - - direct X-Proxied: - - extlb1nuq fa152d4755 - - intlb4nuq c0f5e722d1 + - extlb2nuq 99aac35317 + - intlb3nuq 284c5d344a Strict-Transport-Security: - max-age=31536000; includeSubDomains; preload body: encoding: UTF-8 - string: '{"id":"ca_3d872c74658a4ebbba0bc771600eb6ff","object":"CarrierAccount","type":"UpsAccount","clone":false,"created_at":"2024-07-29T18:07:33Z","updated_at":"2024-07-29T18:07:33Z","description":null,"reference":null,"billing_type":"carrier","readable":"UPS","logo":null,"fields":[],"credentials":{},"test_credentials":{}}' - recorded_at: Mon, 29 Jul 2024 18:07:33 GMT + string: '{"id":"ca_67413142c8ba4766aeb04a08ca3b1526","object":"CarrierAccount","type":"UpsAccount","clone":false,"created_at":"2025-04-11T16:46:41Z","updated_at":"2025-04-11T16:46:41Z","description":null,"reference":null,"billing_type":"carrier","readable":"UPS","logo":null,"fields":[],"credentials":{},"test_credentials":{}}' + recorded_at: Fri, 11 Apr 2025 16:46:41 GMT - request: method: delete - uri: https://api.easypost.com/v2/carrier_accounts/ca_3d872c74658a4ebbba0bc771600eb6ff + uri: https://api.easypost.com/v2/carrier_accounts/ca_67413142c8ba4766aeb04a08ca3b1526 body: encoding: US-ASCII string: '' @@ -100,7 +98,7 @@ http_interactions: Referrer-Policy: - strict-origin-when-cross-origin X-Ep-Request-Uuid: - - d044fa7666a7da66e799d92000598833 + - d574b4b567f94771e2ca041c00520f57 Cache-Control: - private, no-cache, no-store Pragma: @@ -110,22 +108,22 @@ http_interactions: Content-Type: - application/json; charset=utf-8 X-Runtime: - - '0.079336' + - '0.144699' Transfer-Encoding: - chunked X-Node: - - bigweb38nuq + - bigweb58nuq X-Version-Label: - - easypost-202407291746-57ea285141-master + - easypost-202504102051-85835c857e-master X-Backend: - easypost X-Proxied: - - extlb1nuq fa152d4755 - - intlb3nuq c0f5e722d1 + - extlb2nuq 99aac35317 + - intlb3nuq 284c5d344a Strict-Transport-Security: - max-age=31536000; includeSubDomains; preload body: encoding: UTF-8 string: "{}" - recorded_at: Mon, 29 Jul 2024 18:07:34 GMT -recorded_with: VCR 6.1.0 + recorded_at: Fri, 11 Apr 2025 16:46:41 GMT +recorded_with: VCR 6.3.1 diff --git a/spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_an_Amazon_account.yml b/spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_an_Amazon_account.yml new file mode 100644 index 00000000..ad35f0a7 --- /dev/null +++ b/spec/cassettes/carrier_account/EasyPost_Services_CarrierAccount_create_creates_an_Amazon_account.yml @@ -0,0 +1,130 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.easypost.com/v2/carrier_accounts/register_oauth + body: + encoding: UTF-8 + string: '{"carrier_account_oauth_registrations":{"type":"AmazonShippingAccount","account_number":"123456789"}}' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: "" + Host: + - api.easypost.com + Content-Type: + - application/json + Authorization: "" + 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: + - d574b4b367f94771e2ca043700520fce + Cache-Control: + - private, no-cache, no-store + Pragma: + - no-cache + Expires: + - '0' + Content-Type: + - application/json; charset=utf-8 + X-Runtime: + - '0.082604' + Transfer-Encoding: + - chunked + X-Node: + - bigweb42nuq + X-Version-Label: + - easypost-202504102051-85835c857e-master + X-Backend: + - easypost + X-Proxied: + - extlb2nuq 99aac35317 + - intlb4nuq 284c5d344a + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + body: + encoding: UTF-8 + string: '{"id":"ca_557725c3d1c34fda9df04acf7ce6c5c3","object":"CarrierAccount","type":"AmazonShippingAccount","clone":false,"created_at":"2025-04-11T16:46:41Z","updated_at":"2025-04-11T16:46:41Z","description":null,"reference":null,"billing_type":"carrier","readable":"Amazon + Shipping","logo":null,"fields":[],"credentials":{},"test_credentials":{}}' + recorded_at: Fri, 11 Apr 2025 16:46:41 GMT +- request: + method: delete + uri: https://api.easypost.com/v2/carrier_accounts/ca_557725c3d1c34fda9df04acf7ce6c5c3 + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: "" + Host: + - api.easypost.com + Content-Type: + - application/json + Authorization: "" + 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: + - d574b4b667f94771e2ca04390052101b + Cache-Control: + - private, no-cache, no-store + Pragma: + - no-cache + Expires: + - '0' + Content-Type: + - application/json; charset=utf-8 + X-Runtime: + - '0.201251' + Transfer-Encoding: + - chunked + X-Node: + - bigweb38nuq + X-Version-Label: + - easypost-202504102051-85835c857e-master + X-Backend: + - easypost + X-Proxied: + - extlb2nuq 99aac35317 + - intlb3nuq 284c5d344a + Strict-Transport-Security: + - max-age=31536000; includeSubDomains; preload + body: + encoding: UTF-8 + string: "{}" + recorded_at: Fri, 11 Apr 2025 16:46:41 GMT +recorded_with: VCR 6.3.1