Show last payment method in order confirmation

If we had multiple failed payments and then a successful payment, the
order confirmation was displaying the payment method of the first failed
payment. That was confusing and is now changed to the last payment
method.
This commit is contained in:
Maikel Linke
2020-02-13 16:30:31 +11:00
parent 7306d379a5
commit 2c2023df03
3 changed files with 34 additions and 2 deletions

View File

@@ -0,0 +1,10 @@
module OrdersHelper
def order_paid_via(order)
# `sort_by` avoids additional database queries when payments are loaded
# already. There is usually only one payment and this shouldn't cause
# any overhead compared to `order(:updated_at)`.
#
# Using `last` without sort is not deterministic.
order.payments.sort_by(&:updated_at).last.andand.payment_method
end
end

View File

@@ -13,9 +13,9 @@
.pad
.text-big
= t :order_payment
%strong= order.payments.first.andand.payment_method.andand.name
%strong= order_paid_via(order).andand.name
%p.text-small.text-skinny.pre-line
%em= order.payments.first.andand.payment_method.andand.description
%em= order_paid_via(order).andand.description
.order-summary.text-small
%strong

View File

@@ -29,4 +29,26 @@ describe "spree/shared/_order_details.html.haml" do
expect(rendered).to have_content("Paying via: Bar<script>evil</script>ter&rarr;ing")
end
it "shows the last used payment method" do
first_payment = order.payments.first
second_payment = create(
:payment,
order: order,
payment_method: create(:payment_method, name: "Cash")
)
third_payment = create(
:payment,
order: order,
payment_method: create(:payment_method, name: "Credit")
)
first_payment.update_column(:updated_at, 3.days.ago)
second_payment.update_column(:updated_at, 2.days.ago)
third_payment.update_column(:updated_at, 1.day.ago)
order.payments.reload
render
expect(rendered).to have_content("Paying via: Credit")
end
end