Skip to content

Commit

Permalink
Merge pull request #334 from solidusio/nebulab/rainerd+elia/add-payme…
Browse files Browse the repository at this point in the history
…nt-info-partial-handling

Add partial handling of payment information for payment provider custom integrations.
  • Loading branch information
rainerdema authored Mar 28, 2023
2 parents d554de2 + 402f545 commit 4e423d2
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
11 changes: 11 additions & 0 deletions templates/app/helpers/checkout_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module CheckoutHelper
def partial_name_with_fallback(prefix, partial_name, fallback_name = 'default')
if lookup_context.find_all("#{prefix}/_#{partial_name}").any?
"#{prefix}/#{partial_name}"
else
"#{prefix}/#{fallback_name}"
end
end
end
19 changes: 7 additions & 12 deletions templates/app/views/orders/_payment_info.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,14 @@
) unless @order.completed? %>

<% order.payments.valid.each do |payment| %>
<% source = payment.source %>

<ul class="payment-info__info">
<% if source.is_a?(Spree::CreditCard) %>
<li>
<% if source.last_digits %>
<%= t('spree.ending_in') %> <%= source.last_digits %>
<% end %>
</li>
<li><%= source.name %></li>
<% else %>
<li><%= payment.payment_method.name %></li>
<% end %>
<li>
<%= render partial_name_with_fallback(
'orders/payment_info',
payment.payment_method.partial_name,
'default'
), payment: payment %>
</li>
<li>
<strong><%= payment.display_amount %></strong>
(<%= t(payment.state, scope: 'spree.payment_states') %>)
Expand Down
1 change: 1 addition & 0 deletions templates/app/views/orders/payment_info/_check.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= t('spree.check') %>
1 change: 1 addition & 0 deletions templates/app/views/orders/payment_info/_default.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= payment.payment_method.name %>
5 changes: 5 additions & 0 deletions templates/app/views/orders/payment_info/_gateway.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% source = payment.source %>

<% if source.last_digits %>
<%= t('spree.ending_in') %> <%= source.last_digits %>
<% end %>
19 changes: 19 additions & 0 deletions templates/spec/helpers/checkout_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require 'solidus_starter_frontend_spec_helper'

RSpec.describe CheckoutHelper, type: :helper do
describe '#partial_name_with_fallback' do
it "uses the partial_name if it exists" do
expect(
partial_name_with_fallback('orders/payment_info', 'gateway', 'default')
).to eq('orders/payment_info/gateway')
end

it "uses the fallback_name if it's missing" do
expect(
partial_name_with_fallback('orders/payment_info', 'foo', 'default')
).to eq('orders/payment_info/default')
end
end
end

0 comments on commit 4e423d2

Please sign in to comment.