Skip to content

Commit

Permalink
Streamline response handling in payment processing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
elia committed Jan 20, 2023
1 parent 2b22cf0 commit a5ec43b
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions core/app/models/spree/payment/processing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def authorize!
source,
gateway_options,
)
handle_response(response, :pend, :failure)
pend! if handle_response(response)
end
end

Expand All @@ -61,7 +61,7 @@ def purchase!
source,
gateway_options,
)
handle_response(response, :complete, :failure)
complete! if handle_response(response)
end

capture_events.create!(amount: amount)
Expand All @@ -86,7 +86,7 @@ def capture!(capture_amount = nil)
money = ::Money.new(capture_amount, currency)
capture_events.create!(amount: money.to_d)
update!(amount: captured_amount)
handle_response(response, :complete, :failure)
complete! if handle_response(response)
end
end

Expand Down Expand Up @@ -181,24 +181,28 @@ def check_payment_preconditions!
true
end

def handle_response(response, success_state, failure_state)
# @returns true if the response is successful
# @returns false (and calls #failure) if the response is not successful
def handle_response(response)
record_response(response)

if response.success?
unless response.authorization.nil?
self.response_code = response.authorization
self.avs_response = response.avs_result['code']

if response.cvv_result
self.cvv_response_code = response.cvv_result['code']
self.cvv_response_message = response.cvv_result['message']
end
end
send("#{success_state}!")
else
send(failure_state)
unless response.success?
failure
gateway_error(response)
return false
end

unless response.authorization.nil?
self.response_code = response.authorization
self.avs_response = response.avs_result['code']

if response.cvv_result
self.cvv_response_code = response.cvv_result['code']
self.cvv_response_message = response.cvv_result['message']
end
end

true
end

def record_response(response)
Expand Down

0 comments on commit a5ec43b

Please sign in to comment.