ruby-on-railsbraintreebraintree-rails

How to find payment method type when querying Braintree in Rails


I'm trying to query the payment method associated with a Braintree::Customer. I would like to display the type of the payment method(s) attached to the account and additional information (last 4 digits of card number if it's a credit card; email if it's a Paypal account).

I cannot find any documentation on how to retrieve the type of the Braintree::PaymentMethod object (i.e. whether it's a credit card or a Paypal account).

This is important because I need to access attributes that are exclusive to either types.


Solution

  • So I completely forgot about Ruby's native methods and how to compare objects...

    You can do a simple comparison by using the .class method

    e.g.

    if Braintree::PaymentMethod.find(token).class == Braintree::PayPalAccount
       payment_type = 'Paypal'
    elsif Braintree::PaymentMethod.find(token).class == Braintree::CreditCard
       payment_type = 'Credit Card'
    end