diff options
author | Jared Norman <jared@super.gd> | 2019-04-11 13:49:55 +1200 |
---|---|---|
committer | Jared Norman <jared@super.gd> | 2019-04-11 13:53:14 +1200 |
commit | cb740a35890e9aad7f5206f4c4759c497cbff115 (patch) | |
tree | 4a19b79ebc091b26ef1a2d99506e22d12ffde899 | |
parent | 3ad0d4d02a6a5c284c695d82e3064be33168be6e (diff) |
Avoid sending 0 quantity line items
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar/api_params.rb | 12 | ||||
-rw-r--r-- | spec/super_good/solidus_taxjar/api_params_spec.rb | 54 |
3 files changed, 66 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 50c2d0c..7d842a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## master +- Avoid sending 0 quantity line items. TaxJar doesn't like them. + ## v0.10.0 - Make shipping amounts configurable to make it easier to support order-level adjustments. diff --git a/lib/super_good/solidus_taxjar/api_params.rb b/lib/super_good/solidus_taxjar/api_params.rb index 8596856..25f8317 100644 --- a/lib/super_good/solidus_taxjar/api_params.rb +++ b/lib/super_good/solidus_taxjar/api_params.rb @@ -63,7 +63,7 @@ module SuperGood def line_items_params(line_items) { - line_items: line_items.map do |line_item| + line_items: valid_line_items(line_items).map do |line_item| { id: line_item.id, quantity: line_item.quantity, @@ -77,7 +77,7 @@ module SuperGood def transaction_line_items_params(line_items) { - line_items: line_items.map do |line_item| + line_items: valid_line_items(line_items).map do |line_item| { id: line_item.id, quantity: line_item.quantity, @@ -91,6 +91,14 @@ module SuperGood } end + def valid_line_items(line_items) + # The API appears to error when sent line items with no quantity... + # but why would you do that anyway. + line_items.reject do |line_item| + line_item.quantity.zero? + end + end + def discount(line_item) ::SuperGood::SolidusTaxJar.discount_calculator.new(line_item).discount end diff --git a/spec/super_good/solidus_taxjar/api_params_spec.rb b/spec/super_good/solidus_taxjar/api_params_spec.rb index 3688d2f..9c8696f 100644 --- a/spec/super_good/solidus_taxjar/api_params_spec.rb +++ b/spec/super_good/solidus_taxjar/api_params_spec.rb @@ -140,6 +140,32 @@ RSpec.describe SuperGood::SolidusTaxJar::APIParams do }] ) end + + context "when the line item has zero quantity" do + let(:line_item) do + Spree::LineItem.new( + variant: variant, + price: 10, + quantity: 0, + promo_total: -2, + additional_tax_total: 4 + ) + end + + it "excludes the line item" do + expect(subject).to eq( + to_country: "US", + to_zip: "90210", + to_city: "Los Angeles", + to_state: "CA", + to_street: "475 N Beverly Dr", + + shipping: 3.01, + + line_items: [] + ) + end + end end describe "#address_params" do @@ -184,6 +210,34 @@ RSpec.describe SuperGood::SolidusTaxJar::APIParams do }] }) end + + context "when the line item has 0 quantity" do + let(:line_item) do + Spree::LineItem.new( + variant: variant, + price: 10, + quantity: 0, + promo_total: -2, + additional_tax_total: 4 + ) + end + + it "excludes the line item" 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", + line_items: [] + }) + end + end end describe "#refund_params" do |