Skip to content

Commit 36340a8

Browse files
authored
Merge pull request #4823 from solidusio/elia/payment-processing-refactor
Spree::Payment::Processing refactor
2 parents c6050d7 + a5ec43b commit 36340a8

1 file changed

Lines changed: 58 additions & 55 deletions

File tree

core/app/models/spree/payment/processing.rb

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,36 @@ def process!
3535
end
3636

3737
def authorize!
38-
handle_payment_preconditions { process_authorization }
38+
return unless check_payment_preconditions!
39+
40+
started_processing!
41+
42+
protect_from_connection_error do
43+
response = payment_method.authorize(
44+
money.money.cents,
45+
source,
46+
gateway_options,
47+
)
48+
pend! if handle_response(response)
49+
end
3950
end
4051

4152
# Captures the entire amount of a payment.
4253
def purchase!
43-
handle_payment_preconditions { process_purchase }
54+
return unless check_payment_preconditions!
55+
56+
started_processing!
57+
58+
protect_from_connection_error do
59+
response = payment_method.purchase(
60+
money.money.cents,
61+
source,
62+
gateway_options,
63+
)
64+
complete! if handle_response(response)
65+
end
66+
67+
capture_events.create!(amount: amount)
4468
end
4569

4670
# Takes the amount in cents to capture.
@@ -62,7 +86,7 @@ def capture!(capture_amount = nil)
6286
money = ::Money.new(capture_amount, currency)
6387
capture_events.create!(amount: money.to_d)
6488
update!(amount: captured_amount)
65-
handle_response(response, :complete, :failure)
89+
complete! if handle_response(response)
6690
end
6791
end
6892

@@ -139,77 +163,56 @@ def handle_void_response(response)
139163

140164
private
141165

142-
def process_authorization
143-
started_processing!
144-
gateway_action(source, :authorize, :pend)
145-
end
166+
# @raises Spree::Core::GatewayError
167+
def check_payment_preconditions!
168+
return if processing?
169+
return unless payment_method
170+
return unless payment_method.source_required?
146171

147-
def process_purchase
148-
started_processing!
149-
gateway_action(source, :purchase, :complete)
150-
# This won't be called if gateway_action raises a GatewayError
151-
capture_events.create!(amount: amount)
152-
end
153-
154-
def handle_payment_preconditions(&_block)
155-
unless block_given?
156-
raise ArgumentError.new("handle_payment_preconditions must be called with a block")
172+
unless source
173+
gateway_error(I18n.t('spree.payment_processing_failed'))
157174
end
158175

159-
return if payment_method.nil?
160-
return if !payment_method.source_required?
161-
162-
if source
163-
if !processing?
164-
if payment_method.supports?(source)
165-
yield
166-
else
167-
invalidate!
168-
raise Core::GatewayError.new(I18n.t('spree.payment_method_not_supported'))
169-
end
170-
end
171-
else
172-
raise Core::GatewayError.new(I18n.t('spree.payment_processing_failed'))
176+
unless payment_method.supports?(source)
177+
invalidate!
178+
gateway_error(I18n.t('spree.payment_method_not_supported'))
173179
end
174-
end
175180

176-
def gateway_action(source, action, success_state)
177-
protect_from_connection_error do
178-
response = payment_method.send(action, money.money.cents,
179-
source,
180-
gateway_options)
181-
handle_response(response, success_state, :failure)
182-
end
181+
true
183182
end
184183

185-
def handle_response(response, success_state, failure_state)
184+
# @returns true if the response is successful
185+
# @returns false (and calls #failure) if the response is not successful
186+
def handle_response(response)
186187
record_response(response)
187188

188-
if response.success?
189-
unless response.authorization.nil?
190-
self.response_code = response.authorization
191-
self.avs_response = response.avs_result['code']
192-
193-
if response.cvv_result
194-
self.cvv_response_code = response.cvv_result['code']
195-
self.cvv_response_message = response.cvv_result['message']
196-
end
197-
end
198-
send("#{success_state}!")
199-
else
200-
send(failure_state)
189+
unless response.success?
190+
failure
201191
gateway_error(response)
192+
return false
193+
end
194+
195+
unless response.authorization.nil?
196+
self.response_code = response.authorization
197+
self.avs_response = response.avs_result['code']
198+
199+
if response.cvv_result
200+
self.cvv_response_code = response.cvv_result['code']
201+
self.cvv_response_message = response.cvv_result['message']
202+
end
202203
end
204+
205+
true
203206
end
204207

205208
def record_response(response)
206209
log_entries.create!(details: response.to_yaml)
207210
end
208211

209212
def protect_from_connection_error
210-
yield
213+
yield
211214
rescue ActiveMerchant::ConnectionError => error
212-
gateway_error(error)
215+
gateway_error(error)
213216
end
214217

215218
def gateway_error(error)

0 commit comments

Comments
 (0)