mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
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:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user