mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user