class Payment < ActiveRecord::Base

  belongs_to :element, :polymorphic => true

  before_create do
    generate_token
  end
  
  before_validation do
    self.iban = self.iban.to_s.gsub(/\s+/, "")
  end

  def generate_token
    
    if !self.token
      begin
        self.token = SecureRandom.hex(8)
      end while Payment.exists?(:token => self.token)
      
    end
  end

end
