Fix payment_total calculation

For payment that complete during the checkout (Paypal, Stripe) the
amount was recorded twice against `order.payment_total`. This is because
the `payment_total` gets updated in an afer_save Payment callback when a
payment is completed, and then once more when we process payment in
`Spree::Order#process_each_payment`.
This is an existing  issue on master, but it was hidden by the
`update_shipping_fees!` callback, it trigerred an update of the order's
total, which then updated `order.payment_total` with the correct value.
Now that we removed the callback, the bug showed up.

Note, I updated the stripe specs for consistency even though they are
currently disabled.
This commit is contained in:
Gaetan Craig-Riou
2025-01-20 16:07:32 +11:00
parent 1afa7fe5c0
commit c71ae35685
5 changed files with 4 additions and 14 deletions

View File

@@ -673,10 +673,6 @@ module Spree
break if payment_total >= total
yield payment
if payment.completed?
self.payment_total += payment.amount
end
end
end

View File

@@ -29,7 +29,7 @@
%td.text-right{colspan: "3"}
%strong
= t :order_amount_paid
%td.text-right.total
%td.text-right.total{id: "amount-paid"}
%strong
= order.display_payment_total.to_html
- if order.outstanding_balance.positive?

View File

@@ -294,15 +294,6 @@ RSpec.describe Spree::Order do
expect(payment).to receive(:process!)
expect(order.process_payments!).to be_truthy
end
it "stores the payment total on the order" do
allow(payment).to receive(:process!)
allow(payment).to receive(:completed?).and_return(true)
order.process_payments!
expect(order.payment_total).to eq(payment.amount)
end
end
context "when a payment raises a GatewayError" do

View File

@@ -69,6 +69,7 @@ RSpec.describe "Check out with Paypal" do
click_on "Complete order"
expect(page).to have_content "Your order has been processed successfully"
expect(page.find("#amount-paid").text).to have_content "$19.99"
expect(order.reload.state).to eq "complete"
expect(order.payments.count).to eq 1

View File

@@ -71,6 +71,8 @@ RSpec.describe "Check out with Stripe" do
checkout_with_stripe
expect(page).to have_content "Confirmed"
expect(page.find("#amount-paid").text).to have_content "$19.99"
expect(order.reload.completed?).to eq true
expect(order.payments.first.state).to eq "completed"
end