summaryrefslogtreecommitdiff
path: root/lib/super_good/solidus_taxjar/api_params.rb
diff options
context:
space:
mode:
authorJared Norman <jared@super.gd>2019-01-30 13:05:02 -0800
committerJared Norman <jared@super.gd>2019-01-30 13:05:02 -0800
commit0152c8c4d62300e52e02d1d841e1d59ee36add99 (patch)
tree8e7073c18839240cb307531dbbedd337f6e936fa /lib/super_good/solidus_taxjar/api_params.rb
parentccc941df00cc8cfc3360e7c70152e7cb4b0b2855 (diff)
Introduce class to handle param generation
This leaves the API class to just be resonsible for calling the right methods on the TaxJar gem.
Diffstat (limited to 'lib/super_good/solidus_taxjar/api_params.rb')
-rw-r--r--lib/super_good/solidus_taxjar/api_params.rb44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/super_good/solidus_taxjar/api_params.rb b/lib/super_good/solidus_taxjar/api_params.rb
new file mode 100644
index 0000000..eba0947
--- /dev/null
+++ b/lib/super_good/solidus_taxjar/api_params.rb
@@ -0,0 +1,44 @@
+module SuperGood
+ module SolidusTaxJar
+ module APIParams
+ class << self
+ def order_params(order)
+ {}
+ .merge(address_params(order.tax_address))
+ .merge(line_items_params(order.line_items))
+ .merge(shipping: order.shipment_total)
+ end
+
+ private
+
+ def address_params(address)
+ {
+ to_country: address.country.iso,
+ to_zip: address.zipcode,
+ to_city: address.city,
+ to_state: address&.state&.abbr || address.state_name,
+ to_street: address.address1,
+ }
+ end
+
+ def line_items_params(line_items)
+ {
+ line_items: line_items.map do |line_item|
+ {
+ id: line_item.id,
+ quantity: line_item.quantity,
+ unit_price: line_item.price,
+ discount: discount(line_item),
+ product_tax_code: line_item.tax_category&.tax_code
+ }
+ end
+ }
+ end
+
+ def discount(line_item)
+ ::SuperGood::SolidusTaxJar.discount_calculator.new(line_item).discount
+ end
+ end
+ end
+ end
+end