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
This commit is contained in:
Gaetan Craig-Riou
2023-07-31 12:05:52 +10:00
parent 33ef8def08
commit 089d2b9c84
2 changed files with 28 additions and 1 deletions

View File

@@ -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

View File

@@ -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