public
Description: Paypal Website Payments Standard Extension for Spree, the open source shopping cart
Homepage:
Clone URL: git://github.com/Gregg/spree-pp-website-standard.git
schof (author)
Wed Oct 29 19:01:18 -0700 2008
commit  b53f60b2a5dea07c0a32c6a9553da6b6a5849b63
tree    58bafbcc04bf5ea8d8a8c4206f01f2bc9057f28f
parent  7548b59337be586c2d132dda5005c04881b0f675
spree-pp-website-standard / pp_website_standard_extension.rb
100644 67 lines (56 sloc) 2.397 kb
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
# Uncomment this if you reference any of your controllers in activate
require_dependency 'application'
 
unless RAILS_ENV == 'production'
  PAYPAL_ACCOUNT = 'joe@bidness.com'
  ActiveMerchant::Billing::Base.mode = :test
else
  PAYPAL_ACCOUNT = 'Gregg@railsenvy.com'
end
 
class PpWebsiteStandardExtension < Spree::Extension
  version "1.1"
  description "Describe your extension here"
  url "http://yourwebsite.com/spree_pp_website_standard"
 
  define_routes do |map|
    map.resources :orders do |order|
      # we're kind of abusing the notion of a restful collection here but we're in the weird position of
      # not being able to create the payment before sending the request to paypal
      order.resource :paypal_payment, :collection => {:successful => :post}
    end
  end
  
  def activate
 
    # Add a partial for PaypalPayment txns
    Admin::OrdersController.class_eval do
      before_filter :add_pp_standard_txns, :only => :show
      def add_pp_standard_txns
        @txn_partials << 'pp_standard_txns'
      end
    end
    
    # Add a filter to the OrdersController so that if user is reaching us from an email link we can
    # associate the order with the user (once they log in)
    OrdersController.class_eval do
      before_filter :associate_order, :only => :show
      private
      def associate_order
        return unless payer_id = params[:payer_id]
        orders = Order.find(:all, :include => :paypal_payment, :conditions => ['paypal_payments.payer_id = ? AND orders.user_id is null', payer_id])
        orders.each do |order|
          order.update_attribute("user", current_user)
        end
      end
    end
 
    # add new events and states to the FSM
    fsm = Order.state_machines['state']
    fsm.events["fail_payment"] = PluginAWeek::StateMachine::Event.new(fsm, "fail_payment")
    fsm.events["pend_payment"] = PluginAWeek::StateMachine::Event.new(fsm, "pend_payment")
    fsm.events["fail_payment"].transition(:to => 'payment_failure')
    fsm.events["pend_payment"].transition(:to => 'payment_pending')
    fsm.after_transition :to => 'payment_pending', :do => lambda {|order| order.update_attribute(:checkout_complete, true)}
    
    # add a PaypalPayment association to the Order model
    Order.class_eval do
      has_one :paypal_payment
    end
  
  end
  
  def deactivate
    # admin.tabs.remove "Spree Pp Website Standard"
  end
  
end