Skip to content

Instantly share code, notes, and snippets.

@mxaly
Created September 17, 2013 10:14
Show Gist options
  • Save mxaly/6592473 to your computer and use it in GitHub Desktop.
Save mxaly/6592473 to your computer and use it in GitHub Desktop.
require 'digest/md5'
class Transaction < ActiveRecord::Base
serialize :details
belongs_to :user
money :amount
validates_presence_of :amount
validates_presence_of :source, :if => Proc.new { |transaction| transaction.is_a?(Transaction::Deposit) }
after_update :modify_balance_if_needed!
def modify_balance_if_needed!
if status_changed? and status_was != 'completed' and status == 'completed'
modify_balance!
end
end
def modify_balance!
user.balance ||= 0.to_money
user.balance += amount
user.save
end
def secret_id
# TODO - this also needs to be queryable, check deposits_controller#42
# It goes out to netcommercepay as their txtIndex, and comes back in as txtIndex too, where we query it.
# The rand BS is because netcommerce demands a unique transaction ID per request to "prevent duplicate transactions"
@secret_id ||= id.to_s + "_" + (rand * 100000).round.to_s
end
def purchase
binding.pry
Purchase.where(id: details[:purchase_id]).first
end
after_create :sync_to_capsule
def sync_to_capsule
if user.capsule_key? then
case type
when 'Transaction::Deposit'
note = Placebo::HistoryItem.new({
:party_id => user.capsule_key,
:note => "New deposit, ID#{self.id.to_s}, #{amount.to_s}, #{source}"
})
note.save
when 'Transaction::Withdrawal'
opp = Placebo::Opportunity.new({
:party_id => user.capsule_key,
:name => 'Withdrawal ID #' + self.id.to_s,
:milestone => 'withdrawal',
:description => "Amount #{amount.to_s}, Source #{source}",
:value => (-self.amount).to_s
})
opp.save
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment