Fix order updater to update payment fees

The order updater did not take into account payment fees on pending
payment.
This commit is contained in:
Gaetan Craig-Riou
2024-09-30 16:15:59 +10:00
parent fafd86a2db
commit 03dbd54b25
3 changed files with 35 additions and 5 deletions

View File

@@ -240,7 +240,24 @@ module OrderManagement
return unless order.state.in? ["payment", "confirmation", "complete"]
return unless order.pending_payments.any?
order.pending_payments.first.update_attribute :amount, order.total
# Update payment tax fees if needed
payment = order.pending_payments.first
new_amount = payment.payment_method.compute_amount(payment)
if new_amount != payment.adjustment.amount
update_payment_adjustment(payment.adjustment, new_amount)
end
# Update payment with correct amount
payment.update_attribute :amount, order.total
end
def update_payment_adjustment(adjustment, amount)
adjustment.update_attribute(:amount, amount)
# Update order total to take into account updated payment fees
update_adjustment_total
update_order_total
persist_totals
end
end
end

View File

@@ -121,7 +121,7 @@ module OrderManagement
updater.update
end
context "whith pending payments" do
context "with pending payments" do
let(:order) { create(:completed_order_with_totals) }
it "updates pending payments" do
@@ -183,6 +183,19 @@ module OrderManagement
expect { updater.update }.to change { payment.reload.amount }.from(10).to(20)
end
it "updates pending payments fees" do
calculator = build(:calculator_flat_percent_per_item, preferred_flat_percent: 10)
payment_method = create(:payment_method, name: "Percentage cash", calculator:)
payment = create(:payment, payment_method:, order:, amount: order.total)
# update order so the order total will change
update_order_quantity(order)
order.payments.reload
expect { updater.update }.to change { payment.reload.amount }.from(10).to(22)
.and change { payment.reload.adjustment.amount }.from(1).to(2)
end
end
context "with order in cart" do

View File

@@ -320,12 +320,11 @@ RSpec.describe "As a consumer, I want to checkout my order" do
:payment_method,
distributors: [distributor],
name: "Payment with Fee", description: "Payment with fee",
calculator: Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 0.1)
calculator: Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10)
)
}
it "calculated the correct order total" do
pending
visit checkout_step_path(:payment)
expect(page).to have_checked_field "Payment with Fee"
@@ -348,7 +347,8 @@ RSpec.describe "As a consumer, I want to checkout my order" do
# Check summary page total
expect(page).to have_title "Checkout Summary - Open Food Network"
expect(page).to have_selector("#order_total", text: 20.02)
expect(page).to have_selector("#order_total", text: 22.00)
expect(order.reload.payments.first.amount).to eql(22.00)
end
end