diff options
-rw-r--r-- | lib/super_good/solidus_taxjar/api.rb | 4 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar/api_params.rb | 12 | ||||
-rw-r--r-- | spec/super_good/solidus_taxjar/api_params_spec.rb | 30 | ||||
-rw-r--r-- | spec/super_good/solidus_taxjar/api_spec.rb | 22 |
4 files changed, 65 insertions, 3 deletions
diff --git a/lib/super_good/solidus_taxjar/api.rb b/lib/super_good/solidus_taxjar/api.rb index d52a4a4..301fba4 100644 --- a/lib/super_good/solidus_taxjar/api.rb +++ b/lib/super_good/solidus_taxjar/api.rb @@ -20,6 +20,10 @@ module SuperGood taxjar_client.rates_for_location(*APIParams.address_params(address)) end + def create_transaction_for(order) + taxjar_client.create_order APIParams.transaction_params(order) + end + private attr_reader :taxjar_client diff --git a/lib/super_good/solidus_taxjar/api_params.rb b/lib/super_good/solidus_taxjar/api_params.rb index 4e61253..f75713c 100644 --- a/lib/super_good/solidus_taxjar/api_params.rb +++ b/lib/super_good/solidus_taxjar/api_params.rb @@ -21,6 +21,18 @@ module SuperGood ] end + def transaction_params(order) + {} + .merge(order_address_params(order.tax_address)) + .merge( + transaction_id: order.number, + transaction_date: order.completed_at.to_formatted_s(:iso8601), + amount: order.total - order.additional_tax_total, + shipping: order.shipment_total, + sales_tax: order.additional_tax_total + ) + end + private def order_address_params(address) diff --git a/spec/super_good/solidus_taxjar/api_params_spec.rb b/spec/super_good/solidus_taxjar/api_params_spec.rb index 8677153..cb70804 100644 --- a/spec/super_good/solidus_taxjar/api_params_spec.rb +++ b/spec/super_good/solidus_taxjar/api_params_spec.rb @@ -3,12 +3,17 @@ require 'spec_helper' RSpec.describe SuperGood::SolidusTaxJar::APIParams do let(:order) do Spree::Order.create!( - item_total: 28.00, - shipment_total: 3.01, + number: "R111222333", + total: BigDecimal("123.45"), + shipment_total: BigDecimal("3.01"), + additional_tax_total: BigDecimal("9.87"), + item_total: BigDecimal("28.00"), store: store, ship_address: ship_address, line_items: [line_item] - ) + ).tap do |order| + order.update! completed_at: DateTime.new(2018, 3, 6, 12, 10, 33) + end end let(:store) do @@ -138,4 +143,23 @@ RSpec.describe SuperGood::SolidusTaxJar::APIParams do ]) end end + + describe "#transaction_params" do + subject { described_class.transaction_params(order) } + + it "returns params for creating/updating an order transaction" do + expect(subject).to eq({ + amount: BigDecimal("113.58"), + sales_tax: BigDecimal("9.87"), + shipping: BigDecimal("3.01"), + to_city: "Los Angeles", + to_country: "US", + to_state: "CA", + to_street: "475 N Beverly Dr", + to_zip: "90210", + transaction_date: "2018-03-06T12:10:33Z", + transaction_id: "R111222333", + }) + end + end end diff --git a/spec/super_good/solidus_taxjar/api_spec.rb b/spec/super_good/solidus_taxjar/api_spec.rb index 1b6defd..0bfb67a 100644 --- a/spec/super_good/solidus_taxjar/api_spec.rb +++ b/spec/super_good/solidus_taxjar/api_spec.rb @@ -44,4 +44,26 @@ RSpec.describe SuperGood::SolidusTaxJar::API do it { is_expected.to eq({ some_kind_of: "response" }) } end + + describe "create_transaction_for" do + subject { api.create_transaction_for order } + + let(:api) { described_class.new(taxjar_client: dummy_client) } + let(:dummy_client) { instance_double ::Taxjar::Client } + let(:order) { Spree::Order.new } + + before do + allow(SuperGood::SolidusTaxJar::APIParams) + .to receive(:transaction_params) + .with(order) + .and_return({ transaction: "params" }) + + allow(dummy_client) + .to receive(:create_order) + .with({ transaction: "params" }) + .and_return({ some_kind_of: "response" }) + end + + it { is_expected.to eq({ some_kind_of: "response" }) } + end end |