Search code examples
ruby-on-rails-5spree

Why are some reasons that payments.valid.first.source return nil for Spree::Order?


Spree eCommerce 3.3.0 Rails 5.1 Ruby 2.5.7

I am trying to pull the payment_selection made for an order. In Spree::OrdersController, I am using:

payments.valid.first&.source

Just to be clear, payments is an array of Spree::Payment::CreditCard--not Spree::Payment::Check.


Solution

  • It all depends on the payment gateway you're using. Even if it's a CC based payment gateway there are some that don't persist CC as a source in Spree.

    Each Payment Method in Spree has a method called source_required?: https://github.com/spree/spree/blob/a01ffd29b73f75cb234fcf5d368fb23553acf4d1/core/app/models/spree/payment_method.rb#L49

    You should check if the one you are using returns true.