From 0152c8c4d62300e52e02d1d841e1d59ee36add99 Mon Sep 17 00:00:00 2001 From: Jared Norman Date: Wed, 30 Jan 2019 13:05:02 -0800 Subject: Introduce class to handle param generation This leaves the API class to just be resonsible for calling the right methods on the TaxJar gem. --- lib/super_good/solidus_taxjar/api_params.rb | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lib/super_good/solidus_taxjar/api_params.rb (limited to 'lib/super_good/solidus_taxjar/api_params.rb') 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 -- cgit v1.2.3