-
Notifications
You must be signed in to change notification settings - Fork 7
/
pp_website_standard_extension.rb
60 lines (51 loc) · 2.16 KB
/
pp_website_standard_extension.rb
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
# Uncomment this if you reference any of your controllers in activate
require_dependency 'application'
=begin
unless RAILS_ENV == 'production'
PAYPAL_ACCOUNT = '[email protected]'
ActiveMerchant::Billing::Base.mode = :test
else
PAYPAL_ACCOUNT = '[email protected]'
end
=end
class PpWebsiteStandardExtension < Spree::Extension
version "0.6.x"
description "Describe your extension here"
url "http://github.com/Gregg/spree-pp-website-standard/tree/master"
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_payments, :conditions => ['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["fail_payment"].transition(:to => 'payment_failure', :from => ['in_progress', 'payment_pending'])
fsm.events["pend_payment"] = PluginAWeek::StateMachine::Event.new(fsm, "pend_payment")
fsm.events["pend_payment"].transition(:to => 'payment_pending', :from => 'in_progress')
fsm.after_transition :to => 'payment_pending', :do => lambda {|order| order.update_attribute(:checkout_complete, true)}
fsm.events["pay"].transition(:to => 'paid', :from => ['payment_pending', 'in_progress'])
Order.class_eval do
has_many :paypal_payments
end
end
def deactivate
# admin.tabs.remove "Spree Pp Website Standard"
end
end