/
processing.rb
180 lines (151 loc) 路 5.81 KB
/
processing.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
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
module Spree
class Payment < ActiveRecord::Base
module Processing
def process!
if payment_method && payment_method.source_required?
if source
if !processing?
if Spree::Config[:auto_capture]
purchase!
else
authorize!
end
end
else
raise Core::GatewayError.new(I18n.t(:payment_processing_failed))
end
end
end
def authorize!
started_processing!
gateway_action(source, :authorize, :pend)
end
def purchase!
started_processing!
gateway_action(source, :purchase, :complete)
end
def capture!
protect_from_connection_error do
check_environment
if payment_method.payment_profiles_supported?
# Gateways supporting payment profiles will need access to creditcard object because this stores the payment profile information
# so supply the authorization itself as well as the creditcard, rather than just the authorization code
response = payment_method.capture(self, source, gateway_options)
else
# Standard ActiveMerchant capture usage
response = payment_method.capture((amount * 100).round,
response_code,
gateway_options)
end
handle_response(response, :complete, :failure)
end
end
def void_transaction!
protect_from_connection_error do
check_environment
response = payment_method.void(self.response_code, gateway_options)
record_response(response)
if response.success?
self.response_code = response.authorization
self.void
else
gateway_error(response)
end
end
end
end
def credit!(credit_amount=nil)
protect_from_connection_error do
check_environment
credit_amount ||= credit_allowed >= order.outstanding_balance.abs ? order.outstanding_balance.abs : credit_allowed.abs
credit_amount = credit_amount.to_f
if payment_method.payment_profiles_supported?
response = payment_method.credit((credit_amount * 100).round, source, response_code, gateway_options)
else
response = payment_method.credit((credit_amount * 100).round, response_code, gateway_options)
end
record_response(response)
if response.success?
self.class.create({ :order => order,
:source => self,
:payment_method => payment_method,
:amount => credit_amount.abs * -1,
:response_code => response.authorization,
:state => 'completed' }, :without_protection => true)
else
gateway_error(response)
end
end
end
def partial_credit(amount)
return if amount > credit_allowed
started_processing!
credit!(amount)
end
private
def gateway_action(source, action, success_state)
protect_from_connection_error do
check_environment
response = payment_method.send(action, (amount * 100).round,
source,
gateway_options)
handle_response(response, success_state, :failure)
end
end
def handle_response(response, success_state, failure_state)
record_response(response)
if response.success?
self.response_code = response.authorization
self.avs_response = response.avs_result['code']
self.send("#{success_state}!")
else
self.send(failure_state)
gateway_error(response)
end
end
def record_response(response)
log_entries.create({:details => response.to_yaml}, :without_protection => true)
end
def gateway_options
options = { :email => order.email,
:customer => order.email,
:ip => '192.168.1.100', # TODO: Use an actual IP
:order_id => order.number }
options.merge!({ :shipping => order.ship_total * 100,
:tax => order.tax_total * 100,
:subtotal => order.item_total * 100 })
options.merge!({ :currency => payment_method.preferences[:currency_code] }) if payment_method && payment_method.preferences[:currency_code]
options.merge({ :billing_address => order.bill_address.try(:active_merchant_hash),
:shipping_address => order.ship_address.try(:active_merchant_hash) })
end
def protect_from_connection_error
begin
yield
rescue ActiveMerchant::ConnectionError => e
gateway_error(e)
end
end
def record_log(response)
log_entries.create({:details => response.to_yaml}, :without_protection => true)
end
def gateway_error(error)
if error.is_a? ActiveMerchant::Billing::Response
text = error.params['message'] || error.params['response_reason_text'] || error.message
elsif error.is_a? ActiveMerchant::ConnectionError
text = I18n.t(:unable_to_connect_to_gateway)
else
text = error.to_s
end
logger.error(I18n.t(:gateway_error))
logger.error(" #{error.to_yaml}")
raise Core::GatewayError.new(text)
end
# Saftey check to make sure we're not accidentally performing operations on a live gateway.
# Ex. When testing in staging environment with a copy of production data.
def check_environment
return if payment_method.environment == Rails.env
message = I18n.t(:gateway_config_unavailable) + " - #{Rails.env}"
raise Core::GatewayError.new(message)
end
end
end