summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJared Norman <jared@super.gd>2019-01-24 18:09:32 -0800
committerJared Norman <jared@super.gd>2019-01-28 10:35:05 -0800
commitc1bf02cf6586b537535956a98c3864d05ecdecc6 (patch)
tree8de541c411b7eb55adffc1ac7d6bf56cce9c04ff /lib
parent462aa5b334c44e7cef9536ebf0db2e98875031da (diff)
Use DiscountCalculator and fix duplication
There's no reason for both the TaxCalculator and the API to have nearly the same logic... so I unified that while was in there.
Diffstat (limited to 'lib')
-rw-r--r--lib/super_good/solidus_taxjar/api.rb14
-rw-r--r--lib/super_good/solidus_taxjar/tax_calculator.rb28
2 files changed, 16 insertions, 26 deletions
diff --git a/lib/super_good/solidus_taxjar/api.rb b/lib/super_good/solidus_taxjar/api.rb
index ffefd5f..2504216 100644
--- a/lib/super_good/solidus_taxjar/api.rb
+++ b/lib/super_good/solidus_taxjar/api.rb
@@ -26,10 +26,6 @@ module SuperGood
)
end
- private
-
- attr_reader :taxjar_client
-
def order_params(order)
tax_address = order.tax_address
@@ -47,12 +43,20 @@ module SuperGood
id: line_item.id,
quantity: line_item.quantity,
unit_price: line_item.price,
- discount: -line_item.promo_total,
+ discount: discount(line_item),
product_tax_code: line_item.tax_category&.tax_code
}
end
}
end
+
+ private
+
+ attr_reader :taxjar_client
+
+ def discount(line_item)
+ ::SuperGood::SolidusTaxJar.discount_calculator.new(line_item).discount
+ 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 4a46953..54aace4 100644
--- a/lib/super_good/solidus_taxjar/tax_calculator.rb
+++ b/lib/super_good/solidus_taxjar/tax_calculator.rb
@@ -120,27 +120,13 @@ module SuperGood
end
def cache_key
- tax_address = order.tax_address
-
- {
- to_country: tax_address.country.iso,
- to_zip: tax_address.zipcode,
- to_city: tax_address.city,
- to_state: tax_address&.state&.abbr || tax_address.state_name,
- to_street: tax_address.address1,
-
- shipping: order.shipment_total,
-
- line_items: order.line_items.map do |line_item|
- {
- id: line_item.id,
- quantity: line_item.quantity,
- unit_price: line_item.price,
- discount: -line_item.promo_total,
- product_tax_code: line_item.tax_category&.tax_code
- }
- end.hash
- }
+ api.order_params(order).transform_values do |value|
+ case value
+ when Array, Hash then value.hash
+ else
+ value
+ end
+ end
end
end
end