From 089d2b9c840c587652cd0ae9869cada57748d8f2 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Mon, 31 Jul 2023 12:05:52 +1000 Subject: [PATCH] Clear any existing payment and payment fees when adding a voucher If a user come back to checkout step 2 from step 3, the order will have payment and possibly payment fee existing. This can interfere with voucher calculation, so we clear them. The user can then select a payment method again if needed --- .../voucher_adjustments_controller.rb | 4 +++ spec/requests/voucher_adjustments_spec.rb | 25 ++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app/controllers/voucher_adjustments_controller.rb b/app/controllers/voucher_adjustments_controller.rb index 162a1c89d6..c55df05063 100644 --- a/app/controllers/voucher_adjustments_controller.rb +++ b/app/controllers/voucher_adjustments_controller.rb @@ -47,6 +47,10 @@ class VoucherAdjustmentsController < BaseController return false end + # Clear payments and payment_fee, to not affect voucher adjustment calculation + @order.all_adjustments.payment_fee.destroy_all + @order.payments.clear + VoucherAdjustmentsService.new(@order).update @order.update_totals_and_states diff --git a/spec/requests/voucher_adjustments_spec.rb b/spec/requests/voucher_adjustments_spec.rb index 5260996a4b..1bd92a1a94 100644 --- a/spec/requests/voucher_adjustments_spec.rb +++ b/spec/requests/voucher_adjustments_spec.rb @@ -8,7 +8,7 @@ describe VoucherAdjustmentsController, type: :request do let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) } let(:order_cycle) { create(:order_cycle, distributors: [distributor]) } let(:exchange) { order_cycle.exchanges.outgoing.first } - let(:order) do + let!(:order) do create( :order_with_line_items, line_items_count: 1, @@ -66,6 +66,29 @@ describe VoucherAdjustmentsController, type: :request do ) end end + + context "when the order has a payment and payment feed" do + let(:payment_method) { create(:payment_method, calculator: calculator) } + let(:calculator) do + ::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) + end + + before do + create(:payment, order: order, payment_method: payment_method, amount: order.total) + end + + it "removes existing payments" do + expect do + post "/voucher_adjustments", params: params + end.to change { order.reload.payments.count }.from(1).to(0) + end + + it "removes existing payment fees" do + expect do + post "/voucher_adjustments", params: params + end.to change { order.reload.all_adjustments.payment_fee.count }.from(1).to(0) + end + end end describe "DELETE voucher_adjustments/:id" do