From 4f7bd65748a251e881cfdf54bab1b7513ada24da Mon Sep 17 00:00:00 2001 From: Jared Norman Date: Tue, 5 Feb 2019 10:51:08 -0800 Subject: Introduce configurable taxable address check --- CHANGELOG.md | 4 ++++ lib/super_good/solidus_taxjar.rb | 2 ++ lib/super_good/solidus_taxjar/tax_calculator.rb | 5 +++++ spec/super_good/solidus_taxjar/tax_calculator_spec.rb | 14 ++++++++++++++ 4 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 229a1a1..8471e12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,3 +3,7 @@ ## v0.5.0 - Moved exception handler configuration to `SuperGood::SolidusTaxJar.exception_handler` from `SuperGood::SolidusTaxJar::TaxCalculator.exception_handler`. Now all the configuration options are in the same place. +- Added `SuperGood::SolidusTaxJar.taxable_address_check` option which can be set to a block that receives the address and will prevent actual tax calculator from occurring if it returns false. If your app has introduced a method like `Spree::Address#us?`, you could avoid trying to compute taxes on non-US orders by doing the following in an initializer: + ```ruby + SuperGood::SolidusTaxJar.taxable_address_check = ->(address) { address.us? } + ``` diff --git a/lib/super_good/solidus_taxjar.rb b/lib/super_good/solidus_taxjar.rb index 663b3d8..1ab40ec 100644 --- a/lib/super_good/solidus_taxjar.rb +++ b/lib/super_good/solidus_taxjar.rb @@ -14,6 +14,7 @@ module SuperGood attr_accessor :discount_calculator attr_accessor :test_mode attr_accessor :exception_handler + attr_accessor :taxable_address_check end self.discount_calculator = ::SuperGood::SolidusTaxJar::DiscountCalculator @@ -21,5 +22,6 @@ module SuperGood self.exception_handler = ->(e) { Rails.logger.error "An error occurred while fetching TaxJar tax rates - #{e}: #{e.message}" } + self.taxable_address_check = ->(address) { true } end end diff --git a/lib/super_good/solidus_taxjar/tax_calculator.rb b/lib/super_good/solidus_taxjar/tax_calculator.rb index a3c0690..d2b7b74 100644 --- a/lib/super_good/solidus_taxjar/tax_calculator.rb +++ b/lib/super_good/solidus_taxjar/tax_calculator.rb @@ -13,6 +13,7 @@ module SuperGood def calculate return no_tax if SuperGood::SolidusTaxJar.test_mode return no_tax if order.tax_address.empty? || order.line_items.none? + return no_tax unless taxable_address? order.tax_address cache do next no_tax unless taxjar_breakdown @@ -128,6 +129,10 @@ module SuperGood def exception_handler SuperGood::SolidusTaxJar.exception_handler end + + def taxable_address?(address) + SuperGood::SolidusTaxJar.taxable_address_check.(address) + end end end end diff --git a/spec/super_good/solidus_taxjar/tax_calculator_spec.rb b/spec/super_good/solidus_taxjar/tax_calculator_spec.rb index e5c0a01..f7a7929 100644 --- a/spec/super_good/solidus_taxjar/tax_calculator_spec.rb +++ b/spec/super_good/solidus_taxjar/tax_calculator_spec.rb @@ -173,6 +173,20 @@ RSpec.describe ::SuperGood::SolidusTaxJar::TaxCalculator do end end + context "but the taxable address check returns false" do + before do + allow(SuperGood::SolidusTaxJar.taxable_address_check) + .to receive(:call).with(address) + .and_return(false) + end + + it "returns no taxes" do + expect(subject.order_id).to eq order.id + expect(subject.shipment_taxes).to be_empty + expect(subject.line_item_taxes).to be_empty + end + end + context "when there are shipping taxes" do let(:shipping_tax_breakdown) do instance_double ::Taxjar::Shipping, tax_collectable: 10.00 -- cgit v1.2.3