Skip to content

Commit

Permalink
take country into account when determining state fallback rate
Browse files Browse the repository at this point in the history
  • Loading branch information
jcain3389 committed Jan 17, 2025
1 parent 4ad3119 commit 9b1c9d1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
15 changes: 5 additions & 10 deletions lib/vertex_client/responses/quotation_fallback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,18 @@ def product_for_line_item(product)

# see lib/vertex_client/rates.rb for hard-coded fallback rates
def tax_amount(price, country, state)
if known_us_state?(state)
if domestic?(country) && state.present? && RATES['US'].has_key?(state)
price * BigDecimal(RATES['US'][state])
elsif known_non_us_country?(country)
elsif !domestic?(country) && country.present? && RATES.has_key?(country)
price * BigDecimal(RATES[country])
else
BigDecimal('0.0')
end
end

# state is in the United States and we have an explicit fallback
def known_us_state?(state)
state.present? && RATES['US'].has_key?(state)
end

# we have an explicit fallback for the country
def known_non_us_country?(country)
country.present? && country != 'US' && RATES.has_key?(country)
def domestic?(country)
# we assume a country-less customer is from the US
country.nil? || country == 'US'
end

def tax_for_line_item(line_item)
Expand Down
22 changes: 22 additions & 0 deletions test/responses/quotation_fallback_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@
assert_equal 0.0, response.total_tax.to_f
end
end

describe 'for a country with a state code that collides with a US state code' do
let(:params) do
working_quote_params.tap do |wqp|
wqp[:customer][:address_1] = 'Miguel Angel Blanco 2 4C'
wqp[:customer][:city] = 'Valladolid'
wqp[:customer][:state] = 'VA'
wqp[:customer][:postal_code] = '47014'
wqp[:customer][:country] = 'ES'
wqp[:line_items][1][:customer][:address_1] = 'Miguel Angel Blanco 2 4C'
wqp[:line_items][1][:customer][:city] = 'Valladolid'
wqp[:line_items][1][:customer][:state] = 'VA'
wqp[:line_items][1][:customer][:postal_code] = '47014'
wqp[:line_items][1][:customer][:country] = 'ES'
end
end
let(:payload) { VertexClient::Payload::QuotationFallback.new(params) }

it 'does not use the US rates' do
assert_equal 0.0, response.total_tax.to_f
end
end
end

describe 'total' do
Expand Down

0 comments on commit 9b1c9d1

Please sign in to comment.