diff options
author | Jared Norman <jared@super.gd> | 2019-01-09 13:11:09 -0800 |
---|---|---|
committer | Jared Norman <jared@super.gd> | 2019-01-09 13:11:09 -0800 |
commit | f0de9ce17896903d1436b122d52fc4b185c0b477 (patch) | |
tree | 11b9c6b90b985ed7019f5539a6f9833fae51be66 /lib/super_good | |
parent | bc51a8c65a1853a4652c264e8a01f4c756b1f432 (diff) |
Communicate with TaxJar API
This allows us to calculate the tax for a Solidus order using the TaxJar
API. It's currently hardcoded to use the sandbox, and provides no
caching.
Diffstat (limited to 'lib/super_good')
-rw-r--r-- | lib/super_good/solidus_taxjar.rb | 1 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar/api.rb | 49 |
2 files changed, 50 insertions, 0 deletions
diff --git a/lib/super_good/solidus_taxjar.rb b/lib/super_good/solidus_taxjar.rb index 6553213..40df3ec 100644 --- a/lib/super_good/solidus_taxjar.rb +++ b/lib/super_good/solidus_taxjar.rb @@ -8,4 +8,5 @@ module SuperGood end require "super_good/solidus_taxjar/version" +require "super_good/solidus_taxjar/api" require "super_good/solidus_taxjar/tax_calculator" diff --git a/lib/super_good/solidus_taxjar/api.rb b/lib/super_good/solidus_taxjar/api.rb new file mode 100644 index 0000000..5434aab --- /dev/null +++ b/lib/super_good/solidus_taxjar/api.rb @@ -0,0 +1,49 @@ +module SuperGood + module SolidusTaxJar + class API + def self.default_taxjar_client + ::Taxjar::Client.new( + api_key: ENV.fetch("TAXJAR_API_KEY"), + api_url: 'https://api.sandbox.taxjar.com' + ) + end + + def initialize(taxjar_client: Client.default_taxjar_client) + @taxjar_client = taxjar_client + end + + def order_tax_for(order) + taxjar_client.tax_for_order order_params(order) + end + + private + + attr_reader :taxjar_client + + def order_params(order) + ship_address = order.ship_address + + { + to_country: ship_address.country.iso, + to_zip: ship_address.zipcode, + to_city: ship_address.city, + to_state: ship_address&.state&.abbr || ship_address.state_name, + to_street: ship_address.address1, + + amount: order.item_total, + 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.adjustment_total, + product_tax_code: line_item.tax_category&.tax_code + } + end + } + end + end + end +end |