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 ++++ spec/super_good/solidus_taxjar/api_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) 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/spec/super_good/solidus_taxjar/api_spec.rb b/spec/super_good/solidus_taxjar/api_spec.rb index 0f890ca..8b0a5db 100644 --- a/spec/super_good/solidus_taxjar/api_spec.rb +++ b/spec/super_good/solidus_taxjar/api_spec.rb @@ -23,6 +23,30 @@ RSpec.describe SuperGood::SolidusTaxJar::API do it { is_expected.to eq({ some_kind_of: "response" }) } end + describe "tax_rate_for" do + subject { api.tax_rate_for address } + + let(:api) { described_class.new(taxjar_client: dummy_client) } + let(:dummy_client) { instance_double ::Taxjar::Client } + let(:address) { Spree::Address.new } + let(:tax_rate) { 0.04 } + let(:response) { double(rate: tax_rate) } + + before do + allow(SuperGood::SolidusTaxJar::APIParams) + .to receive(:tax_rate_address_params) + .with(address) + .and_return({ address: "params" }) + + allow(dummy_client) + .to receive(:tax_for_order) + .with({ address: "params" }) + .and_return(response) + end + + it { is_expected.to eq(tax_rate) } + end + describe "#tax_rates_for" do subject { api.tax_rates_for address } -- cgit v1.2.3