summaryrefslogtreecommitdiff
path: root/lib/super_good
diff options
context:
space:
mode:
authorJared Norman <jared@super.gd>2020-01-15 15:24:22 -0800
committerGitHub <noreply@github.com>2020-01-15 15:24:22 -0800
commit48a68fcb692d83e211794ba064fd4448158a0c92 (patch)
tree01f32803ea75c3c5f9c1920ce523b0eca2bded0d /lib/super_good
parent98677696e6f43432c05eb581dcaec292beeafaaa (diff)
parentdc109637def501b965c4c59112eb625188484b6a (diff)
Merge pull request #17 from spaghetticode/spaghetticode/tax-rate-calculator
Add tax rate calculator
Diffstat (limited to 'lib/super_good')
-rw-r--r--lib/super_good/solidus_taxjar.rb7
-rw-r--r--lib/super_good/solidus_taxjar/api.rb4
-rw-r--r--lib/super_good/solidus_taxjar/api_params.rb7
-rw-r--r--lib/super_good/solidus_taxjar/calculator_helper.rb44
-rw-r--r--lib/super_good/solidus_taxjar/tax_calculator.rb35
-rw-r--r--lib/super_good/solidus_taxjar/tax_rate_calculator.rb36
6 files changed, 98 insertions, 35 deletions
diff --git a/lib/super_good/solidus_taxjar.rb b/lib/super_good/solidus_taxjar.rb
index 9318181..1b1a4ac 100644
--- a/lib/super_good/solidus_taxjar.rb
+++ b/lib/super_good/solidus_taxjar.rb
@@ -5,7 +5,9 @@ require 'taxjar'
require "super_good/solidus_taxjar/version"
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
@@ -25,7 +27,10 @@ module SuperGood
end
self.cache_duration = 3.hours
- self.cache_key = ->(order) { APIParams.order_params(order).to_json }
+ self.cache_key = ->(record) {
+ record_type = record.class.name.demodulize.underscore
+ APIParams.send("#{record_type}_params", record).to_json
+ }
self.discount_calculator = ::SuperGood::SolidusTaxJar::DiscountCalculator
self.exception_handler = ->(e) {
Rails.logger.error "An error occurred while fetching TaxJar tax rates - #{e}: #{e.message}"
diff --git a/lib/super_good/solidus_taxjar/api.rb b/lib/super_good/solidus_taxjar/api.rb
index 0d182ce..601a2ae 100644
--- a/lib/super_good/solidus_taxjar/api.rb
+++ b/lib/super_good/solidus_taxjar/api.rb
@@ -22,6 +22,10 @@ module SuperGood
end
end
+ def tax_rate_for(address)
+ taxjar_client.tax_for_order(APIParams.tax_rate_address_params(address)).rate
+ end
+
def tax_rates_for(address)
taxjar_client.rates_for_location(*APIParams.address_params(address))
end
diff --git a/lib/super_good/solidus_taxjar/api_params.rb b/lib/super_good/solidus_taxjar/api_params.rb
index b8ef896..3e5bcb4 100644
--- a/lib/super_good/solidus_taxjar/api_params.rb
+++ b/lib/super_good/solidus_taxjar/api_params.rb
@@ -29,6 +29,13 @@ module SuperGood
]
end
+ def tax_rate_address_params(address)
+ {
+ amount: 100,
+ shipping: 0,
+ }.merge(order_address_params(address))
+ end
+
def transaction_params(order)
{}
.merge(customer_params(order))
diff --git a/lib/super_good/solidus_taxjar/calculator_helper.rb b/lib/super_good/solidus_taxjar/calculator_helper.rb
new file mode 100644
index 0000000..89dedc0
--- /dev/null
+++ b/lib/super_good/solidus_taxjar/calculator_helper.rb
@@ -0,0 +1,44 @@
+module SuperGood
+ module SolidusTaxJar
+ module CalculatorHelper
+ extend ActiveSupport::Concern
+
+ class_methods do
+ def default_api
+ ::SuperGood::SolidusTaxJar::API.new
+ end
+ end
+
+ def incomplete_address?(address)
+ return true if address.is_a?(Spree::Tax::TaxLocation)
+
+ [
+ address.address1,
+ address.city,
+ address&.state&.abbr || address.state_name,
+ address.zipcode,
+ address.country.iso
+ ].any?(&:blank?)
+ end
+
+ def taxable_address?(address)
+ SuperGood::SolidusTaxJar.taxable_address_check.(address)
+ end
+
+ def cache
+ if !Rails.env.test?
+ Rails.cache.fetch(
+ cache_key,
+ expires_in: SuperGood::SolidusTaxJar.cache_duration
+ ) { yield }
+ else
+ yield
+ end
+ end
+
+ def exception_handler
+ SuperGood::SolidusTaxJar.exception_handler
+ end
+ end
+ end
+end
diff --git a/lib/super_good/solidus_taxjar/tax_calculator.rb b/lib/super_good/solidus_taxjar/tax_calculator.rb
index c7d44a9..7fac1bd 100644
--- a/lib/super_good/solidus_taxjar/tax_calculator.rb
+++ b/lib/super_good/solidus_taxjar/tax_calculator.rb
@@ -1,9 +1,7 @@
module SuperGood
module SolidusTaxJar
class TaxCalculator
- def self.default_api
- ::SuperGood::SolidusTaxJar::API.new
- end
+ include CalculatorHelper
def initialize(order, api: self.class.default_api)
@order = order
@@ -115,33 +113,14 @@ module SuperGood
Spree::TaxRate.find_by(name: "Sales Tax")
end
- def cache
- if !Rails.env.test?
- Rails.cache.fetch(
- cache_key,
- expires_in: SuperGood::SolidusTaxJar.cache_duration
- ) { yield }
- else
- yield
- end
- end
-
def cache_key
SuperGood::SolidusTaxJar.cache_key.(order)
end
- def exception_handler
- SuperGood::SolidusTaxJar.exception_handler
- end
-
def taxable_order?(order)
SuperGood::SolidusTaxJar.taxable_order_check.(order)
end
- def taxable_address?(address)
- SuperGood::SolidusTaxJar.taxable_address_check.(address)
- end
-
def shipping_tax_label(shipment, shipping_tax)
SuperGood::SolidusTaxJar.shipping_tax_label_maker.(
shipment,
@@ -152,18 +131,6 @@ module SuperGood
def line_item_tax_label(taxjar_line_item, spree_line_item)
SuperGood::SolidusTaxJar.line_item_tax_label_maker.(taxjar_line_item, spree_line_item)
end
-
- def incomplete_address?(tax_address)
- return true if tax_address.is_a?(Spree::Tax::TaxLocation)
-
- [
- tax_address.address1,
- tax_address.city,
- tax_address&.state&.abbr || tax_address.state_name,
- tax_address.zipcode,
- tax_address.country.iso
- ].any?(&:blank?)
- end
end
end
end
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