forked from Gregg/spree-pp-website-standard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pp_website_standard_extension.rb
88 lines (71 loc) · 2.76 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
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
# Uncomment this if you reference any of your controllers in activate
require_dependency 'application_controller'
=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
ApplicationController.class_eval do
# ADDED THIS METHOD SINCE IT WAS COMPLAINING IN PAYPAL PAYMENTS CONTROLLER (PP PAYMENT STANDARD EXTENSION)
def logged_in?
session = UserSession.find
session && session.record
end
end
# 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
# before_filter :find_order, :only => :create
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
# def find_order
# @order = Order.find_by_user_id(params[:current_user])
# if @order
# flash[:notice] = "You already got an order"
# redirect_back_or_default
# else
# @order.save
# flash[:notice] = "this is your first order"
# end
# end
end
# add new events and states to the FSM
fsm = Order.state_machines[:state]
new_event = StateMachine::Event.new(fsm, "fail_payment")
new_event.transition(:to => 'payment_failure', :from => ['in_progress', 'payment_pending'])
fsm.events << new_event
new_event = StateMachine::Event.new(fsm, "pend_payment")
new_event.transition(:to => 'payment_pending', :from => 'in_progress')
fsm.events << new_event
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