Sha256: 76b29fd3fc3c47657cb6764e75ee8c6cc0492a858f2e563e8747ef5e7965700d

Contents?: true

Size: 833 Bytes

Versions: 5

Compression:

Stored size: 833 Bytes

Contents

class Payment < ActiveRecord::Base
  before_validation :set_status_pending, :on => :create
  before_update :charge_subscription
  validate :valid_amount
  validates_presence_of :subscription_id
  validates_presence_of :amount, :message => "can't be blank"
  validates_numericality_of :amount
  has_statuses "pending", "paid", "failed"
  belongs_to :subscription
  
  
  
  
  def valid_amount
    if amount < 0
      errors[:amount] = "cannot be less than 0"
    end
    if amount > subscription.balance
      errors[:amount] = "cannot be more than your balance"
    end
    if amount == 0
      errors[:amount] = "cannot be zero"
    end
  end
  
  
  private
  
  def charge_subscription

    if subscription.bill!(amount)
      self.status = "paid"
    end
    
  end
  def set_status_pending
    self.status = "pending"
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
saasaparilla-0.2.2 app/models/payment.rb
saasaparilla-0.2.1 app/models/payment.rb
saasaparilla-0.1.8 app/models/payment.rb
saasaparilla-0.1.7 app/models/payment.rb
saasaparilla-0.1.6 app/models/payment.rb