summaryrefslogtreecommitdiff
path: root/lib/super_good
diff options
context:
space:
mode:
Diffstat (limited to 'lib/super_good')
-rw-r--r--lib/super_good/solidus_taxjar.rb1
-rw-r--r--lib/super_good/solidus_taxjar/tax_rate_calculator.rb36
2 files changed, 37 insertions, 0 deletions
diff --git a/lib/super_good/solidus_taxjar.rb b/lib/super_good/solidus_taxjar.rb
index 35d9155..1b1a4ac 100644
--- a/lib/super_good/solidus_taxjar.rb
+++ b/lib/super_good/solidus_taxjar.rb
@@ -7,6 +7,7 @@ require "super_good/solidus_taxjar/api_params"
require "super_good/solidus_taxjar/api"
require "super_good/solidus_taxjar/calculator_helper"
require "super_good/solidus_taxjar/tax_calculator"
+require "super_good/solidus_taxjar/tax_rate_calculator"
require "super_good/solidus_taxjar/discount_calculator"
module SuperGood
diff --git a/lib/super_good/solidus_taxjar/tax_rate_calculator.rb b/lib/super_good/solidus_taxjar/tax_rate_calculator.rb
new file mode 100644
index 0000000..dc3d771
--- /dev/null
+++ b/lib/super_good/solidus_taxjar/tax_rate_calculator.rb
@@ -0,0 +1,36 @@
+module SuperGood
+ module SolidusTaxJar
+ class TaxRateCalculator
+ include CalculatorHelper
+ def initialize(address, api: self.class.default_api)
+ @address = address
+ @api = api
+ end
+
+ def calculate
+ return no_rate if SuperGood::SolidusTaxJar.test_mode
+ return no_rate if incomplete_address?(address)
+ return no_rate unless taxable_address?(address)
+ cache do
+ api.tax_rate_for(address).to_d
+ end
+
+ rescue StandardError => e
+ exception_handler.(e)
+ no_rate
+ end
+
+ private
+
+ attr_reader :address, :api
+
+ def no_rate
+ BigDecimal(0)
+ end
+
+ def cache_key
+ SuperGood::SolidusTaxJar.cache_key.(address)
+ end
+ end
+ end
+end