class CreditCard

Schema Information

Table name: credit_cards

id         :integer          not null, primary key
user_id    :integer
name       :string(255)
number     :string(255)
uri        :string(255)
active     :boolean          default(FALSE)
created_at :datetime
updated_at :datetime
university_id :integer

Attributes

cvv[RW]

paranoid

expiry_date[RW]

paranoid

Public Instance Methods

add_or_update_to_balanced_payments() click to toggle source

To add/upadte credit card details to balance payments.

# File app/models/credit_card.rb, line 34
def add_or_update_to_balanced_payments
  if self.new_record?
    begin
      balanced_student = user.balanced_student
      card = Balanced::Card.new(
        cvv: self.cvv,
        expiration_month: self.expiry_date.split('/').first,
        expiration_year: self.expiry_date.split('/').last,
        number: self.number,
        name: self.name,
        student: user.balanced_student_uri).save
      self.uri = card.href
      self.number = self.number.gsub!(/.(?=....)/, 'X')
    rescue => e
      logger.info e
      self.errors.add(:base, 'Invalid Card Details.')
      return false
    end
  end
end
balanced_card() click to toggle source

To find credit card for balanced payment.

# File app/models/credit_card.rb, line 63
def balanced_card
  if uri?
    Balanced::Card.find(uri)
  end
end
remove_from_balanced_payments() click to toggle source

To remove credit card from balance payments.

# File app/models/credit_card.rb, line 70
def remove_from_balanced_payments
  return false if active? or self.class.count == 1
  begin
    card = Balanced::Card.find(uri)
    card.unstore
  rescue => e
    logger.info e
    return false
  end
end
setup_cards_state() click to toggle source

To set the credit card state(active/deactive)

# File app/models/credit_card.rb, line 56
def setup_cards_state
  if active?
    self.user.credit_cards.where.not(id: id).update_all(active: false)
  end
end