Skip to content

Commit 9b1c9d1

Browse files
committed
take country into account when determining state fallback rate
1 parent 4ad3119 commit 9b1c9d1

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

lib/vertex_client/responses/quotation_fallback.rb

+5-10
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,18 @@ def product_for_line_item(product)
2929

3030
# see lib/vertex_client/rates.rb for hard-coded fallback rates
3131
def tax_amount(price, country, state)
32-
if known_us_state?(state)
32+
if domestic?(country) && state.present? && RATES['US'].has_key?(state)
3333
price * BigDecimal(RATES['US'][state])
34-
elsif known_non_us_country?(country)
34+
elsif !domestic?(country) && country.present? && RATES.has_key?(country)
3535
price * BigDecimal(RATES[country])
3636
else
3737
BigDecimal('0.0')
3838
end
3939
end
4040

41-
# state is in the United States and we have an explicit fallback
42-
def known_us_state?(state)
43-
state.present? && RATES['US'].has_key?(state)
44-
end
45-
46-
# we have an explicit fallback for the country
47-
def known_non_us_country?(country)
48-
country.present? && country != 'US' && RATES.has_key?(country)
41+
def domestic?(country)
42+
# we assume a country-less customer is from the US
43+
country.nil? || country == 'US'
4944
end
5045

5146
def tax_for_line_item(line_item)

test/responses/quotation_fallback_test.rb

+22
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@
8080
assert_equal 0.0, response.total_tax.to_f
8181
end
8282
end
83+
84+
describe 'for a country with a state code that collides with a US state code' do
85+
let(:params) do
86+
working_quote_params.tap do |wqp|
87+
wqp[:customer][:address_1] = 'Miguel Angel Blanco 2 4C'
88+
wqp[:customer][:city] = 'Valladolid'
89+
wqp[:customer][:state] = 'VA'
90+
wqp[:customer][:postal_code] = '47014'
91+
wqp[:customer][:country] = 'ES'
92+
wqp[:line_items][1][:customer][:address_1] = 'Miguel Angel Blanco 2 4C'
93+
wqp[:line_items][1][:customer][:city] = 'Valladolid'
94+
wqp[:line_items][1][:customer][:state] = 'VA'
95+
wqp[:line_items][1][:customer][:postal_code] = '47014'
96+
wqp[:line_items][1][:customer][:country] = 'ES'
97+
end
98+
end
99+
let(:payload) { VertexClient::Payload::QuotationFallback.new(params) }
100+
101+
it 'does not use the US rates' do
102+
assert_equal 0.0, response.total_tax.to_f
103+
end
104+
end
83105
end
84106

85107
describe 'total' do

0 commit comments

Comments
 (0)