From f024aff45d8e46a5bed1ce9c7c8d637dbccd8785 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Wed, 16 Oct 2024 11:21:10 +1100 Subject: [PATCH 1/2] Add Bugsnag notification if we reach tax rate refund code The original Spree code allow for a tax adjustment to be considered a refund in a specific scenario: - instance is using inclusive tax - instance that applies different tax rate in different tax zones This scenario should not happen with how our instances are configured More info: https://github.com/openfoodfoundation/openfoodnetwork/pull/6565#discussion_r566535431 --- app/models/spree/tax_rate.rb | 9 +++++++++ spec/models/spree/tax_rate_spec.rb | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/app/models/spree/tax_rate.rb b/app/models/spree/tax_rate.rb index 868b5e3a9f7..2a2e649a4e6 100644 --- a/app/models/spree/tax_rate.rb +++ b/app/models/spree/tax_rate.rb @@ -105,6 +105,15 @@ def compute_amount(item) if default_zone_or_zone_match?(item.order) calculator.compute(item) else + # Tax refund should not be possible with the way our production server are configured + Bugsnag.notify( + "Notice: Tax refund should not be possible, please check the default zone and " \ + "the tax rate zone configuration" + ) do |payload| + payload.add_metadata :order_tax_zone, order.tax_zone + payload.add_metadata :tax_rate_zone, zone + payload.add_metadata :default_zone, Zone.default_tax + end # In this case, it's a refund. calculator.compute(item) * - 1 end diff --git a/spec/models/spree/tax_rate_spec.rb b/spec/models/spree/tax_rate_spec.rb index 9d58cd024bc..564c92c141e 100644 --- a/spec/models/spree/tax_rate_spec.rb +++ b/spec/models/spree/tax_rate_spec.rb @@ -386,6 +386,16 @@ module Spree Spree::TaxRate.adjust(order, order.line_items) expect(line_item.adjustments.credit.count).to eq 2 end + + it "notifies bugsnag" do + # there are two tax rate + expect(Bugsnag).to receive(:notify).with( + "Notice: Tax refund should not be possible, please check the default zone and " \ + "the tax rate zone configuration" + ).twice + + Spree::TaxRate.adjust(order, order.line_items) + end end end From 3f22e8cca7edb504a022b63f31868a02119c7f90 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Mon, 21 Oct 2024 11:13:42 +1100 Subject: [PATCH 2/2] Fix Bugsnag payload data --- app/models/spree/tax_rate.rb | 2 +- spec/models/spree/tax_rate_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/spree/tax_rate.rb b/app/models/spree/tax_rate.rb index 2a2e649a4e6..588fe919852 100644 --- a/app/models/spree/tax_rate.rb +++ b/app/models/spree/tax_rate.rb @@ -110,7 +110,7 @@ def compute_amount(item) "Notice: Tax refund should not be possible, please check the default zone and " \ "the tax rate zone configuration" ) do |payload| - payload.add_metadata :order_tax_zone, order.tax_zone + payload.add_metadata :order_tax_zone, item.order.tax_zone payload.add_metadata :tax_rate_zone, zone payload.add_metadata :default_zone, Zone.default_tax end diff --git a/spec/models/spree/tax_rate_spec.rb b/spec/models/spree/tax_rate_spec.rb index 564c92c141e..ae12b8109d5 100644 --- a/spec/models/spree/tax_rate_spec.rb +++ b/spec/models/spree/tax_rate_spec.rb @@ -392,7 +392,7 @@ module Spree expect(Bugsnag).to receive(:notify).with( "Notice: Tax refund should not be possible, please check the default zone and " \ "the tax rate zone configuration" - ).twice + ).twice.and_call_original Spree::TaxRate.adjust(order, order.line_items) end