oddlyzen
2/10/2009 - 5:36 PM

gistfile1.txt

  # Cron jobs start here --------------------------------------------------------------
  def self.check_trial
    accounts = BillingAccount.find(:all, :include => :user, :conditions => ["kind = 'trial' and status = 'active'"])
    accounts.each do |a|
      if a.expires_at.to_s == Time.today.strftime("%Y-%m-%d") 
        pay_me = execute_payment(:customer_profile_id => a.customer_profile_id, :customer_payment_profile_id => a.customer_payment_profile_id, :user_id => a.user_id)
        if pay_me.success?
          a.kind = "full"
          a.expires_at = 1.year.from_now
          a.save!
          UserMailer.deliver_payment_receipt(a.user)
        else
          a.status = "canceled"
          a.save!
          UserMailer.deliver_payment_declined(a.user)
        end 
      elsif a.expires_at.to_s == 3.days.from_now.strftime("%Y-%m-%d")
        UserMailer.deliver_subscription_start_reminder(a.user)
      end
    end 
  end
  

  def self.check_expired 
    accounts = BillingAccount.find(:all, :include => :user, :conditions => ["kind = 'full' and status = 'active' and expires_at = ?", Time.today.strftime("%Y-%m-%d")])
    accounts.each do |a|
      pay_me = execute_payment(:customer_profile_id => a.customer_profile_id, :customer_payment_profile_id => a.customer_payment_profile_id, :user_id => a.user_id)
      if pay_me.success?
        a.expires_at = 1.year.from_now
        a.save!
        UserMailer.deliver_payment_receipt(a.user)
      else
        a.status = "canceled"
        a.save!
        UserMailer.deliver_payment_declined(a.user)
      end
    end
  end
  
  # cron jobs end here--------------------------------------------------------------