Sha256: d4496d930e3bc3087c140d8c00c42ad1777ae8dd986c4876633fbdfae081a74d

Contents?: true

Size: 1.18 KB

Versions: 12

Compression:

Stored size: 1.18 KB

Contents

module Pay
  class PaymentMethod < Pay::ApplicationRecord
    self.inheritance_column = nil

    belongs_to :customer

    store_accessor :data, :brand # Visa, Mastercard, Discover, PayPal
    store_accessor :data, :last4
    store_accessor :data, :exp_month
    store_accessor :data, :exp_year
    store_accessor :data, :email # PayPal email, etc
    store_accessor :data, :username
    store_accessor :data, :bank

    # Aliases to share PaymentMethodAttributes
    alias_attribute :payment_method_type, :type

    validates :processor_id, presence: true, uniqueness: {scope: :customer_id, case_sensitive: true}

    def self.find_by_processor_and_id(processor, processor_id)
      joins(:customer).find_by(processor_id: processor_id, pay_customers: {processor: processor})
    end

    def self.pay_processor_for(name)
      "Pay::#{name.to_s.classify}::PaymentMethod".constantize
    end

    def payment_processor
      @payment_processor ||= self.class.pay_processor_for(customer.processor).new(self)
    end

    def make_default!
      return if default?

      payment_processor.make_default!

      customer.payment_methods.update_all(default: false)
      update!(default: true)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pay-3.0.12 app/models/pay/payment_method.rb
pay-3.0.11 app/models/pay/payment_method.rb
pay-3.0.10 app/models/pay/payment_method.rb
pay-3.0.9 app/models/pay/payment_method.rb
pay-3.0.8 app/models/pay/payment_method.rb
pay-3.0.7 app/models/pay/payment_method.rb
pay-3.0.6 app/models/pay/payment_method.rb
pay-3.0.5 app/models/pay/payment_method.rb
pay-3.0.4 app/models/pay/payment_method.rb
pay-3.0.2 app/models/pay/payment_method.rb
pay-3.0.1 app/models/pay/payment_method.rb
pay-3.0.0 app/models/pay/payment_method.rb