Skip to content
Browse files

Purchases can be paid via PayPal

  • Loading branch information...
1 parent b77866d commit ed0a52c52197eee55e42247c32c034e0936da81c GIT_AUTHOR_NAME committed with veezus Apr 15, 2009
View
16 app/models/purchase.rb
@@ -28,16 +28,18 @@ class GatewayError < RuntimeError; end
cattr_accessor :gateway
after_create :associate_donations, :apply_credits, :associate_spotus_donations
- before_create :bill_credit_card
- before_validation_on_create :build_credit_card, :set_credit_card_number_ending, :set_total_amount
+ 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? }
+ :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? }
+ :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? }
+ validate :validate_credit_card, :on => :create, :unless => lambda {|p| p.credit_covers_total? || p.paypal_transaction? }
belongs_to :user
has_many :donations
@@ -47,6 +49,10 @@ def credit_covers_total?
self.total_amount == 0
end
+ def paypal_transaction?
+ !paypal_transaction_id.blank?
+ end
+
def donations=(donations)
@new_donations = donations
end
View
9 db/migrate/20090415193608_add_paypal_transaction_id_to_purchases.rb
@@ -0,0 +1,9 @@
+class AddPaypalTransactionIdToPurchases < ActiveRecord::Migration
+ def self.up
+ add_column :purchases, :paypal_transaction_id, :string
+ end
+
+ def self.down
+ remove_column :purchases, :paypal_transaction_id
+ end
+end
View
14 spec/factories.rb
@@ -158,13 +158,27 @@
purchase.zip '02141'
purchase.association :user
+
purchase.credit_card_number '1'
purchase.credit_card_month '1'
purchase.credit_card_type 'bogus'
purchase.credit_card_year { Time.now.year + 1 }
purchase.verification_value '111'
end
+Factory.define :paypal_purchase, :class => Purchase do |purchase|
+ purchase.first_name 'John'
+ purchase.last_name 'User'
+ purchase.address1 '100 Happy Lane'
+ purchase.address2 'Apt. 2'
+ purchase.city 'Boston'
+ purchase.state 'MA'
+ purchase.zip '02141'
+ purchase.paypal_transaction_id '28C98632UU123291R'
+
+ purchase.association :user
+end
+
Factory.define :credit_card, :class => ActiveMerchant::Billing::CreditCard do |cc|
cc.first_name 'Billy'
cc.last_name 'Joel'
View
21 spec/models/purchase_spec.rb
@@ -111,6 +111,27 @@ def do_validate
end
end
+ describe "when a paypal transaction" do
+ before do
+ @donation = Factory.build(:donation)
+ @purchase = Factory.build(:paypal_purchase, :user => Factory(:user), :donations => [@donation])
+ @purchase.paypal_transaction_id = '28C98632UU123291R'
+ end
+ it "knows it is a paypal transaction" do
+ @purchase.should be_paypal_transaction
+ end
+ it "should not have credit card callbacks" do
+ @purchase.should_receive(:bill_credit_card).never
+ @purchase.should_receive(:build_credit_card).never
+ @purchase.should_receive(:set_credit_card_number_ending).never
+ @purchase.should_receive(:validate_credit_card).never
+ @purchase.save
+ end
+ it "is valid without credit card information" do
+ @purchase.should be_valid
+ end
+ end
+
it "should raise a gateway error when the gateway does not return a success response" do
@donation = Factory(:donation, :user => Factory(:user), :amount => 25)
lambda { Factory(:purchase, :credit_card_number => '2', :donations => [@donation]) }.

0 comments on commit ed0a52c

Please sign in to comment.
Something went wrong with that request. Please try again.