Skip to content

Commit a5ec43b

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 2b22cf0 commit a5ec43b

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
capture_events.create!(amount: amount)
@@ -86,7 +86,7 @@ def capture!(capture_amount = nil)
8686
money = ::Money.new(capture_amount, currency)
8787
capture_events.create!(amount: money.to_d)
8888
update!(amount: captured_amount)
89-
handle_response(response, :complete, :failure)
89+
complete! if handle_response(response)
9090
end
9191
end
9292

@@ -181,24 +181,28 @@ def check_payment_preconditions!
181181
true
182182
end
183183

184-
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)
185187
record_response(response)
186188

187-
if response.success?
188-
unless response.authorization.nil?
189-
self.response_code = response.authorization
190-
self.avs_response = response.avs_result['code']
191-
192-
if response.cvv_result
193-
self.cvv_response_code = response.cvv_result['code']
194-
self.cvv_response_message = response.cvv_result['message']
195-
end
196-
end
197-
send("#{success_state}!")
198-
else
199-
send(failure_state)
189+
unless response.success?
190+
failure
200191
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
201203
end
204+
205+
true
202206
end
203207

204208
def record_response(response)

0 commit comments

Comments
 (0)