github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

spot-us / spot-us

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 157
    • 25
  • Source
  • Commits
  • Network (25)
  • Issues (5)
  • Downloads (0)
  • Wiki (4)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (12)
    • blogs
    • donate_and_login
    • ideal
    • master ✓
    • networks
    • new_design
    • new_design_merged
    • paypal_cart
    • paypal_response
    • pitch_approval
    • production
    • spotus_v_1_0
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Community Funded Reporting — Read more

  cancel

http://www.spot.us/

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

merged design tweaks 
spherop (author)
Tue Feb 09 15:40:36 -0800 2010
commit  393a939bea05af32d5b09f7462df7367b03820c8
tree    1b730a558c725d21db83c869af44232f3faece2b
parent  71b93bd59d1f38a0e375f2e71932dd99a7f34677 parent  f2853932148b77ab6297f10ea48032cb08e344d0
spot-us / app / models / purchase.rb app/models/purchase.rb
100644 188 lines (151 sloc) 5.659 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# == Schema Information
# Schema version: 20090218144012
#
# Table name: purchases
#
# id :integer(4) not null, primary key
# first_name :string(255)
# last_name :string(255)
# credit_card_number_ending :string(255)
# address1 :string(255)
# address2 :string(255)
# city :string(255)
# state :string(255)
# zip :string(255)
# user_id :integer(4)
# created_at :datetime
# updated_at :datetime
# total_amount :decimal(15, 2)
#
 
class Purchase < ActiveRecord::Base
  class GatewayError < RuntimeError; end
 
  attr_accessor :credit_card_number, :credit_card_year, :credit_card_month,
    :credit_card_type, :verification_value
  attr_reader :credit_card
 
  cattr_accessor :gateway
 
  after_create :associate_donations, :associate_spotus_donations, :associate_credits
  before_create :bill_credit_card, :unless => lambda {|p| p.paypal_transaction? }
 
  before_validation_on_create :build_credit_card, :set_credit_card_number_ending, :unless => lambda {|p| p.paypal_transaction? }
  before_validation_on_create :set_total_amount
  validates_presence_of :first_name, :last_name, :credit_card_number_ending,
    :address1, :city, :state, :zip, :user_id, :unless => lambda {|p| p.credit_covers_total? || p.paypal_transaction? }
 
  validates_presence_of :credit_card_number, :credit_card_year,
    :credit_card_type, :credit_card_month, :verification_value,
    :on => :create, :unless => lambda {|p| p.credit_covers_total? || p.paypal_transaction? }
 
  validate :validate_credit_card, :on => :create, :unless => lambda {|p| p.credit_covers_total? || p.paypal_transaction? }
 
  belongs_to :user
  has_many :donations, :conditions => {:donation_type => "payment"}
  has_many :credit_pitches, :class_name => "Donation", :conditions => {:donation_type => "credit"}
  has_one :spotus_donation
 
  def credit_covers_total?
    self.total_amount == 0
  end
 
  def credit_covers_partial?
    !credit_covers_total? && credit_to_apply > 0
  end
 
  def paypal_transaction?
    !paypal_transaction_id.blank?
  end
 
  def donations=(donations)
    @new_donations = donations
  end
  
  def credit_pitches=(credit_pitches)
    @new_credit_pitches = credit_pitches
  end
 
  def total_amount
    return self[:total_amount] unless self[:total_amount].blank?
    donations_sum - credit_to_apply
  end
 
  def self.valid_donations_for_user?(user, donations)
    donations.all? {|d| d.user == user && d.unpaid? }
  end
 
  protected
 
  # total of all donations
  def donations_sum
    amount = 0
    amount += donations.map(&:amount).sum unless donations.blank?
    amount += @new_donations.map(&:amount).sum unless @new_donations.blank?
    amount += spotus_donation[:amount] unless spotus_donation.nil? || spotus_donation[:amount].nil?
    amount
  end
 
  def set_total_amount
    self[:total_amount] = total_amount
  end
 
  def build_credit_card
    @credit_card = ActiveMerchant::Billing::CreditCard.new(credit_card_hash)
  end
 
  def validate_credit_card
    unless credit_card.valid?
      credit_card.errors.each do |field, messages|
        messages.each do |message|
          errors.add(:"credit_card_#{field}", message)
        end
      end
    end
  end
 
  def associate_donations
    (@new_donations || []).each do |donation|
      donation.purchase = self
      donation.pay!
      
    end
  end
  
  def associate_credits
    transaction do
      credit_pitch_ids = user.credit_pitches.unpaid.map{|credit_pitch| [credit_pitch.pitch.id]}.join(", ")
      credit = Credit.create(:user => user, :description => "Applied to Pitches (#{credit_pitch_ids})",
                      :amount => (0 - user.allocated_credits))
      user.credit_pitches.unpaid.each do |credit_pitch|
        credit_pitch.credit_id = credit.id
        credit_pitch.status = "deducted"
        credit_pitch.save(false)
      end
    end
  end
 
  def associate_spotus_donations
    user.unpaid_spotus_donation.update_attribute(:purchase_id, self.id) if user.unpaid_spotus_donation
  end
 
 
  def set_credit_card_number_ending
    if credit_card_number
      self.credit_card_number_ending ||= credit_card_number.last(4)
    end
  end
 
  def credit_to_apply
    return 0 if user.nil?
    [user.allocated_credits, donations_sum].min
  end
 
  # the gateway expects the total amount to be an integer or a money obj
  def total_amount_for_gateway
    (total_amount.to_f * 100).to_i
  end
 
  def bill_credit_card
    return true if credit_covers_total?
    response = gateway.purchase(total_amount_for_gateway,
                                credit_card,
                                billing_hash)
    unless response.success?
      raise GatewayError, response.message
    end
  end
 
  private
 
  def billing_hash
    { :billing_address => { :address1 => address1,
                            :address2 => address2,
                            :city => city,
                            :state => state,
                            :zip => zip,
                            :country => 'US',
                            :email => email } }
  end
 
 
  def credit_card_hash
    { :first_name => first_name,
      :last_name => last_name,
      :number => credit_card_number,
      :month => credit_card_month,
      :year => credit_card_year,
      :verification_value => verification_value,
      :type => credit_card_type }
  end
 
  def email
    user.nil? ? nil : user.email
  end
 
end
 
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server