Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<table class='index' id='listing_log_entries'>
<colgroup>
<col style="width: 30%">
<col style="width: 60%">
<col style="width: 25%">
<col style="width: 15%">
<col style="width: 50%">
<col style="width: 10%">
</colgroup>
<thead>
<tr>
<th><%= Spree::LogEntry.human_attribute_name(:created_at) %></th>
<th><%= Spree::LogEntry.human_attribute_name(:source) %></th>
<th><%= Spree::LogEntry.human_attribute_name(:details) %></th>
<th></th>
</tr>
Expand All @@ -17,6 +19,9 @@
<td>
<%= pretty_time(entry.created_at) %>
</td>
<td>
<%= entry.source_type.to_s.demodulize %>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use I18n to display the human readable name and not the cryptic class name.

</td>
<td>
<pre><%= entry.parsed_details.message %></pre>
</td>
Expand Down
5 changes: 3 additions & 2 deletions backend/app/views/spree/admin/payments/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
</fieldset>
<% end %>

<% if @payment.log_entries.any? %>
<% log_entries = @payment.all_log_entries %>
<% if log_entries.any? %>
<fieldset class="no-border-bottom">
<legend><%= plural_resource_name(Spree::LogEntry) %></legend>
<%= render 'spree/admin/payments/log_entries', log_entries: @payment.log_entries %>
<%= render 'spree/admin/payments/log_entries', log_entries: log_entries %>
</fieldset>
<% end %>
25 changes: 25 additions & 0 deletions backend/spec/features/admin/orders/log_entries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,29 @@
end
end
end

context "with a log entry belonging to a refund of the payment" do
let!(:payment) { create(:payment, amount: 100) }
let!(:refund) { create(:refund, payment: payment, amount: 10) }

before do
response = ActiveMerchant::Billing::Response.new(
true,
"Refund processed",
transid: "REFUND-1"
)

refund.log_entries.create!(details: response.to_yaml)
end

it "shows the refund entry on the payment page with 'Refund' as its source" do
visit spree.admin_order_payments_path(payment.order)
click_on payment.number

within("#listing_log_entries") do
expect(page).to have_content("Refund processed")
expect(page).to have_content("Refund")
end
end
end
end
9 changes: 9 additions & 0 deletions core/app/models/spree/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ def credit_allowed
amount - refunds.sum(:amount)
end

# @return [ActiveRecord::Relation<Spree::LogEntry>] log entries for this
# payment and its refunds, ordered by creation time
def all_log_entries
Spree::LogEntry
.where(source: self)
.or(Spree::LogEntry.where(source: refunds))
.order(:created_at)
end

# @return [Boolean] true when this payment can be credited
def can_credit?
credit_allowed > 0
Expand Down
33 changes: 33 additions & 0 deletions core/spec/models/spree/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,39 @@
end
end

describe "#all_log_entries" do
let(:payment) { create(:payment, amount: 100) }

it "is empty when neither the payment nor any refund has log entries" do
expect(payment.all_log_entries).to be_empty
end

it "includes log entries whose source is the payment itself" do
entry = payment.log_entries.create!(details: "a".to_yaml)

expect(payment.all_log_entries).to contain_exactly(entry)
end

it "includes log entries from every refund on the payment" do
refund_a = create(:refund, payment: payment, reason: refund_reason, amount: 10)
refund_b = create(:refund, payment: payment, reason: refund_reason, amount: 20)
refund_a_entry = refund_a.log_entries.create!(details: "a".to_yaml)
refund_b_entry = refund_b.log_entries.create!(details: "b".to_yaml)

expect(payment.all_log_entries).to contain_exactly(refund_a_entry, refund_b_entry)
end

it "merges payment and refund entries and orders them chronologically" do
refund = create(:refund, payment: payment, reason: refund_reason, amount: 10)

first = payment.log_entries.create!(details: "first".to_yaml, created_at: 2.minutes.ago)
second = refund.log_entries.create!(details: "second".to_yaml, created_at: 1.minute.ago)
third = payment.log_entries.create!(details: "third".to_yaml, created_at: Time.current)

expect(payment.all_log_entries.to_a).to eq([first, second, third])
end
end

context "validations" do
it "returns useful error messages when source is invalid" do
payment.source = Spree::CreditCard.new
Expand Down
Loading