Adding IPNs

This commit is contained in:
2013-08-24 02:18:37 -07:00
parent 75e4907a9c
commit ec4cf4dea9
23 changed files with 379 additions and 5 deletions

50
app/models/ipn.rb Normal file
View File

@@ -0,0 +1,50 @@
class Ipn < ActiveRecord::Base
attr_accessible :data
belongs_to :payment
after_create :create_payment
def self.new_from_dynamic_params(params)
ipn = Ipn.new()
ipn.attributes.each do |c|
unless params[c.first.to_sym].nil?
ipn[c.first.to_sym] = params[c.first.to_sym]
end
end
return ipn
end
def link_payment
create_payment
end
private
def create_payment
# find user by email, then by payee
user = User.find_by_email(self.payer_email)
user = User.find_by_payee(self.payer_email) if user.nil? && self.payer_email.present?
# Only create payments if the amount matches a member level
if user.present?
if User.member_levels[self.payment_gross.to_i].present?
payment = Payment.new
payment.date = self.payment_date
payment.user_id = user.id
payment.amount = self.payment_gross
if payment.save
self.payment_id = payment.id
self.save!
else
return [false, "Unable to link payment. Payment error: #{payment.errors.full_messages.first}"]
end
else
return [false, "Unable to link payment. Couldn't find membership level '#{self.payment_gross.to_i}'."]
end
else
return [false, "Unable to link payment. Couldn't find user/payee '#{self.payer_email}'."]
end
end
end

View File

@@ -1,10 +1,12 @@
class Payment < ActiveRecord::Base
belongs_to :user
attr_accessible :date, :user_id, :created_by
has_one :ipn
attr_accessible :date, :user_id, :created_by, :amount
validates_presence_of :user_id, :date, :created_by
validates_presence_of :user_id, :date, :amount # not created_by
validates_uniqueness_of :date, :scope => :user_id, :message => ' of payment already exists for this user.'
def human_date
if date.year < DateTime.now.year
date.strftime("%b %e, %y")

View File

@@ -85,6 +85,10 @@ class User < ActiveRecord::Base
end
end
def self.member_levels
{25 => "Associate", 50 => "Basic", 100 => "Plus"}
end
def member_status
case self.member_level.to_i
when 0