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 @@ -5,6 +5,7 @@
- Removes the deprecated, unusable `add_credit_card` function
- Stripe has disabled the ability to pass plain credit card details over the wire and now requires using [Stripe.js/Elements/Checkout](https://support.stripe.com/questions/card-tokenization-restrictions-using-publishable-keys). Follow the [Decentralized (EasyPost-Manage Billing) Guide](https://docs.easypost.com/guides/get-started-with-forge/easypost-managed-billing-guide#referralcustomer-billing-management) for more details on the new flow to use.
- Makes `referral_customer.retrieve_easypost_stripe_api_key` public to help facilitate adding credit cards using Stripe.js
- Adds support for `FedexDefaultAccount`, `GofoWalletAccount`, `UpsDapAccount`, `UniUniWalletAccount`, and `VehoWalletAccount` by properly routing carrier-account create requests to the `/carrier_accounts/open` endpoint.

## v7.7.0 (2026-06-25)

Expand Down
51 changes: 34 additions & 17 deletions lib/easypost/services/carrier_account.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
# frozen_string_literal: true

class EasyPost::Services::CarrierAccount < EasyPost::Services::Service
CUSTOM_WORKFLOW_CARRIER_TYPES = %w[FedexAccount FedexSmartpostAccount].freeze
CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH = %w[
AmazonShippingAccount UpsAccount UpsMailInnovationsAccount
UpsSurepostAccount UspsShipAccount
].freeze
CARRIER_ACCOUNT_CREATE_ENDPOINT_TYPES = {
'carrier_accounts/open' => %w[
FedexDefaultAccount
GofoWalletAccount
UpsDapAccount
UniUniWalletAccount
VehoWalletAccount
].freeze,
'carrier_accounts/register' => %w[
FedexAccount
FedexSmartpostAccount
].freeze,
'carrier_accounts/register_oauth' => %w[
AmazonShippingAccount
UpsAccount
UpsMailInnovationsAccount
UpsSurepostAccount
UspsShipAccount
].freeze,
}.freeze
MODEL_CLASS = EasyPost::Models::CarrierAccount # :nodoc:

# Create a carrier account
def create(params = {})
carrier_account_type = params[:type]
wrapped_params = { select_top_layer_key(carrier_account_type).to_sym => params }
create_url = select_carrier_account_creation_endpoint(carrier_account_type)
wrapped_params = { select_top_layer_key(create_url).to_sym => params }

# For UPS and FedEx the endpoint is different
create_url = if CUSTOM_WORKFLOW_CARRIER_TYPES.include?(carrier_account_type)
'carrier_accounts/register'
elsif CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH.include?(carrier_account_type)
'carrier_accounts/register_oauth'
else
'carrier_accounts'
end
response = @client.make_request(:post, create_url, wrapped_params)

EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
Expand Down Expand Up @@ -57,12 +65,21 @@ def delete(id)

private

# Select the top-layer key for the carrier account creation/update request based on the carrier type.
def select_top_layer_key(carrier_account_type)
if CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH.include?(carrier_account_type)
# Select the top-layer key for the carrier account creation/update request based on the create endpoint.
def select_top_layer_key(create_url)
if create_url == 'carrier_accounts/register_oauth'
'carrier_account_oauth_registrations'
else
'carrier_account'
end
end

# Determines which API endpoint to use for the creation call.
def select_carrier_account_creation_endpoint(carrier_account_type)
CARRIER_ACCOUNT_CREATE_ENDPOINT_TYPES.each do |endpoint, carrier_account_types|
return endpoint if carrier_account_types.include?(carrier_account_type)
end

'carrier_accounts'
end
end
86 changes: 40 additions & 46 deletions spec/carrier_account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,46 @@
client.carrier_account.delete(carrier_account.id)
end

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)
expect(carrier_account.id).to match('ca_')
expect(carrier_account.type).to eq('UpsAccount')

# 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

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

it 'creates an Amazon account' do
carrier_account = client.carrier_account.create({ type: 'AmazonShippingAccount', account_number: '123456789' })

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)
EasyPost::Services::CarrierAccount::CARRIER_ACCOUNT_CREATE_ENDPOINT_TYPES.each do |expected_endpoint,
carrier_account_types|
carrier_account_types.each do |carrier_account_type|
it "routes #{carrier_account_type} account creation" do
params = { type: carrier_account_type }
if %w[carrier_accounts/open carrier_accounts/register].include?(expected_endpoint)
params[:registration_data] = {}
end
captured_request = {}

allow(client).to receive(:make_request).and_wrap_original do |original, *args|
method, endpoint, request_params = args
if captured_request[:endpoint].nil? && endpoint.start_with?('carrier_accounts')
captured_request[:method] = method
captured_request[:endpoint] = endpoint
captured_request[:params] = request_params
end
original.call(*args)
end

begin
carrier_account = client.carrier_account.create(params)
rescue EasyPost::Errors::ApiError => e
expect(e.status_code).not_to eq(404)
expect(e.status_code).not_to eq(500)
else
expect(carrier_account).to be_an_instance_of(EasyPost::Models::CarrierAccount)
expect(carrier_account.id).to match('ca_')
client.carrier_account.delete(carrier_account.id)
end

expect(captured_request[:method]).to eq(:post)
expect(captured_request[:endpoint]).to eq(expected_endpoint)
if expected_endpoint == 'carrier_accounts/register_oauth'
expect(captured_request[:params]).to have_key(:carrier_account_oauth_registrations)
else
expect(captured_request[:params]).to have_key(:carrier_account)
end
end
end
end
end

Expand Down Expand Up @@ -89,19 +96,6 @@
# 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

it 'updates an ups account' do
ups_account = client.carrier_account.create({ type: 'UpsAccount', account_number: '123456789' })

updated_ups_account = client.carrier_account.update(ups_account.id, account_number: '987654321')

expect(updated_ups_account).to be_an_instance_of(EasyPost::Models::CarrierAccount)
expect(updated_ups_account.id).to match('ca_')
expect(updated_ups_account.type).to eq('UpsAccount')

# Remove the carrier account once we have tested it so we don't pollute the account with test accounts
client.carrier_account.delete(ups_account.id)
end
end

describe '.delete' do
Expand Down

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

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

Loading
Loading