Skip to content

Commit 4882cc6

Browse files
committed
Streamline response handling in payment processing
Remove some indirection and unnecessary meta-programming. Have it return either true or false leaving the "success" state call as a responsibility of the caller (the "failure_state" was always the same). Also remove one nesting level in the implementation.
1 parent 1a4310d commit 4882cc6

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def authorize!
4545
source,
4646
gateway_options,
4747
)
48-
handle_response(response, :pend, :failure)
48+
pend! if handle_response(response)
4949
end
5050
end
5151

@@ -61,7 +61,7 @@ def purchase!
6161
source,
6262
gateway_options,
6363
)
64-
handle_response(response, :complete, :failure)
64+
complete! if handle_response(response)
6565
end
6666

6767
# This won't be called if `payment_method.purchase` raises a GatewayError
@@ -87,7 +87,7 @@ def capture!(capture_amount = nil)
8787
money = ::Money.new(capture_amount, currency)
8888
capture_events.create!(amount: money.to_d)
8989
update!(amount: captured_amount)
90-
handle_response(response, :complete, :failure)
90+
complete! if handle_response(response)
9191
end
9292
end
9393

@@ -182,24 +182,28 @@ def check_payment_preconditions!
182182
true
183183
end
184184

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

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)
190+
unless response.success?
191+
failure
201192
gateway_error(response)
193+
return false
194+
end
195+
196+
unless response.authorization.nil?
197+
self.response_code = response.authorization
198+
self.avs_response = response.avs_result['code']
199+
200+
if response.cvv_result
201+
self.cvv_response_code = response.cvv_result['code']
202+
self.cvv_response_message = response.cvv_result['message']
203+
end
202204
end
205+
206+
true
203207
end
204208

205209
def record_response(response)

0 commit comments

Comments
 (0)