Daniel W
Daniel W

Reputation: 73

Credit Card Type Images with Stripe Rails 4

I've been searching through Stripes documentation for a fast solution to the problem of showing credit card logos for currently subscribed users.

<%= current_user.card_type %> shows the credit card name, but I'm unable to add the logo in it's place. Do you have an approach that I can take that won't cause the instance method to fail?

HTML Helper Method that I created with the help of Tyler

  def stripe_card_img(card_type)
    case current_user.card_type
      when 'Visa'
        content_tag(:span,'', class: 'fa fa-cc-visa fa-2x', style: 'color: #1a1f71;')
      when 'American Express'
        content_tag(:span, '', class: 'fa-cc-amex fa-2x', style: 'color: #355EB7;')
      when 'Discover'
        content_tag(:span, '', class: 'fa-cc-discover fa-2x', style: 'color: orange')
      when 'JCB'
        content_tag(:span, '', class: 'fa-cc-jcb fa-2x', style: 'green')
      when 'Diners Club'
        content_tag(:span, '', class: 'fa-cc-diners-club fa-2x', style: '#87CEFA')
    end
  end

Upvotes: 0

Views: 469

Answers (1)

TylerJPresley
TylerJPresley

Reputation: 489

It's verbose, but it works. Using font awesome:

<% if current_user.card_type == 'Visa' %><span class="fa fa-cc-visa"></span><% end %>
<% if current_user.card_type == 'American Express' %><span class="fa fa-cc-amex"></span><% end %>
<% if current_user.card_type == 'MasterCard' %><span class="fa fa-cc-mastercard"></span><% end %>
<% if current_user.card_type == 'Discover' %><span class="fa fa-cc-discover"></span><% end %>
<% if current_user.card_type == 'JCB' %><span class="fa fa-cc-jcb"></span><% end %>
<% if current_user.card_type == 'Diners Club' %><span class="fa fa-cc-diners-club"></span><% end %>

case

<% case current_user.card_type 
   when 'Visa' %><span class="fa fa-cc-visa"></span>
<% when 'American Express' %><span class="fa fa-cc-amex"></span>
<% when 'MasterCard' %><span class="fa fa-cc-discover"></span>
...
<% end %>

helper

def stripe_name(card_type)
  [...INSERT CASE/CONDITIONAL...]
end 

<%= stripe_name(current_user.card_type) %>

Upvotes: 1

Related Questions