From 37120896954c4f765c047c0ab0528dde1c41ae09 Mon Sep 17 00:00:00 2001 From: andrea longhi Date: Tue, 7 Jan 2020 16:48:54 +0100 Subject: Add CalculatorHelper for sharing logic This module includes helper methods that can be shared among different calculators. --- lib/super_good/solidus_taxjar/calculator_helper.rb | 44 ++++++++++++++++++++++ lib/super_good/solidus_taxjar/tax_calculator.rb | 35 +---------------- 2 files changed, 45 insertions(+), 34 deletions(-) create mode 100644 lib/super_good/solidus_taxjar/calculator_helper.rb (limited to 'lib/super_good/solidus_taxjar') 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 -- cgit v1.2.3 From 134be24f677be142b5ddeb889c45e80a19fd58c8 Mon Sep 17 00:00:00 2001 From: andrea longhi Date: Mon, 13 Jan 2020 20:12:43 +0100 Subject: Add SolidusTaxJar::APIParams.tax_rate_address_params This helper converts a `Spree::Address` to address params that can be used to retrieve the tax rate for that location. --- lib/super_good/solidus_taxjar/api_params.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'lib/super_good/solidus_taxjar') 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)) -- cgit v1.2.3 From 044bb66e25a440388431de27c67d437fe6cebfb6 Mon Sep 17 00:00:00 2001 From: andrea longhi Date: Mon, 13 Jan 2020 20:17:08 +0100 Subject: Add SolidusTaxJar::API#tax_rate_for When calculating tax rates, the recommended endpoint from TaxJar support for live calculations is `/v2/taxes`, the same used for calculating order taxes, as it accounts for all factors like sourcing or nexus, while the `/v2/rates` endpoint will return the full combined rate for the queried location. --- lib/super_good/solidus_taxjar/api.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/super_good/solidus_taxjar') 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 -- cgit v1.2.3 From c7bcc30f9aa142694e84195063a6093f428950b4 Mon Sep 17 00:00:00 2001 From: andrea longhi Date: Mon, 13 Jan 2020 20:31:47 +0100 Subject: Add tax rate calculator class This new calculator allows to retrieve tax rate information starting from a `Spree::Address` model. It shares some logic with the tax calculator, for example the ability to handle exceptions using `SolidusTaxJar.exception_handler` lambda. --- .../solidus_taxjar/tax_rate_calculator.rb | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 lib/super_good/solidus_taxjar/tax_rate_calculator.rb (limited to 'lib/super_good/solidus_taxjar') 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 -- cgit v1.2.3