Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reach: refactor to prevent symbols on response #4650

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/active_merchant/billing/gateways/reach.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ def sign_body(body)
end

def parse(body)
hash_response = URI.decode_www_form(body).to_h.transform_keys!(&:to_sym)
hash_response[:response] = JSON.parse(hash_response[:response], symbolize_names: true)
hash_response = URI.decode_www_form(body).to_h
hash_response['response'] = JSON.parse(hash_response['response'])

hash_response
end
Expand All @@ -224,12 +224,12 @@ def format_and_sign(post)
end

def get_network_payment_reference(response)
parameters = { request: { MerchantId: @options[:merchant_id], OrderId: response.params['response'][:OrderId] } }
parameters = { request: { MerchantId: @options[:merchant_id], OrderId: response.params['response']['OrderId'] } }
body = post_data format_and_sign(parameters)

raw_response = ssl_request :post, url('query'), body, {}
response = parse(raw_response)
message = response.dig(:response, :Payment, :NetworkPaymentReference)
message = response.dig('response', 'Payment', 'NetworkPaymentReference')
Response.new(true, message, {})
end

Expand All @@ -242,7 +242,7 @@ def commit(action, parameters)
success_from(response),
message_from(response) || '',
response,
authorization: authorization_from(response[:response]),
authorization: authorization_from(response['response']),
# avs_result: AVSResult.new(code: response['some_avs_response_key']),
# cvv_result: CVVResult.new(response['some_cvv_response_key']),
test: test?,
Expand All @@ -253,23 +253,23 @@ def commit(action, parameters)
end

def success_from(response)
response.dig(:response, :Error).blank?
response.dig('response', 'Error').blank?
end

def message_from(response)
success_from(response) ? '' : response.dig(:response, :Error, :ReasonCode)
success_from(response) ? '' : response.dig('response', 'Error', 'ReasonCode')
end

def authorization_from(response)
response[:OrderId]
response['OrderId']
end

def post_data(params)
params.map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
end

def error_code_from(response)
response[:response][:Error][:Code] unless success_from(response)
response['response']['Error']['Code'] unless success_from(response)
end

def url(action)
Expand Down
52 changes: 26 additions & 26 deletions test/remote/gateways/remote_reach_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def test_successful_authorize
response = @gateway.authorize(@amount, @credit_card, @options)

assert_success response
assert response.params['response'][:Authorized]
assert response.params['response'][:OrderId]
assert response.params['response']['Authorized']
assert response.params['response']['OrderId']
end

def test_failed_authorize
Expand All @@ -49,8 +49,8 @@ def test_successful_purchase
response = @gateway.purchase(@amount, @credit_card, @options)

assert_success response
assert response.params['response'][:Authorized]
assert response.params['response'][:OrderId]
assert response.params['response']['Authorized']
assert response.params['response']['OrderId']
end

def test_failed_purchase
Expand All @@ -64,8 +64,8 @@ def test_successful_purchase_with_fingerprint
response = @gateway.purchase(@amount, @credit_card, @options)

assert_success response
assert response.params['response'][:Authorized]
assert response.params['response'][:OrderId]
assert response.params['response']['Authorized']
assert response.params['response']['OrderId']
end

def test_successful_purchase_with_shipping_data
Expand All @@ -81,8 +81,8 @@ def test_successful_purchase_with_shipping_data
response = @gateway.purchase(@amount, @credit_card, @options)

assert_success response
assert response.params['response'][:Authorized]
assert response.params['response'][:OrderId]
assert response.params['response']['Authorized']
assert response.params['response']['OrderId']
end

def test_failed_purchase_with_incomplete_shipping_data
Expand Down Expand Up @@ -128,8 +128,8 @@ def test_successful_purchase_with_items
response = @gateway.purchase(@amount, @credit_card, @options)

assert_success response
assert response.params['response'][:Authorized]
assert response.params['response'][:OrderId]
assert response.params['response']['Authorized']
assert response.params['response']['OrderId']
end

# The Complete flag in the response returns false when capture is
Expand All @@ -148,8 +148,8 @@ def test_successful_purchase_with_store_credentials

assert_success response

assert response.params['response'][:Authorized]
assert response.params['response'][:OrderId]
assert response.params['response']['Authorized']
assert response.params['response']['OrderId']
end

def test_successful_purchase_with_store_credentials_mit
Expand All @@ -158,8 +158,8 @@ def test_successful_purchase_with_store_credentials_mit

assert_success response

assert response.params['response'][:Authorized]
assert response.params['response'][:OrderId]
assert response.params['response']['Authorized']
assert response.params['response']['OrderId']
end

def test_successful_purchase_with_store_credentials_mit_and_network_transaction_id
Expand All @@ -171,8 +171,8 @@ def test_successful_purchase_with_store_credentials_mit_and_network_transaction_

assert_success response

assert response.params['response'][:Authorized]
assert response.params['response'][:OrderId]
assert response.params['response']['Authorized']
assert response.params['response']['OrderId']
end

def test_failed_purchase_with_store_credentials_mit_and_network_transaction_id
Expand All @@ -199,7 +199,7 @@ def test_successful_refund
)

assert_success response
assert response.params.symbolize_keys[:response][:RefundId].present?
assert response.params['response']['RefundId'].present?
end

def test_failed_refund
Expand All @@ -208,15 +208,15 @@ def test_failed_refund

assert_failure response
assert_equal 'OrderStateInvalid', response.error_code
assert response.params.symbolize_keys[:response][:OrderId].present?
assert response.params['response']['OrderId'].present?
end

def test_successful_void
authorize = @gateway.authorize(@amount, @credit_card, @options)
response = @gateway.void(authorize.authorization, @options)

assert_success response
assert response.params.symbolize_keys[:response][:OrderId].present?
assert response.params['response']['OrderId'].present?
end

def test_failed_void
Expand All @@ -232,44 +232,44 @@ def test_successful_partial_void
response = @gateway.void(authorize.authorization, @options)

assert_success response
assert response.params.symbolize_keys[:response][:OrderId].present?
assert response.params['response']['OrderId'].present?
end

def test_successful_void_higher_amount
authorize = @gateway.authorize(@amount * 2, @credit_card, @options)
response = @gateway.void(authorize.authorization, @options)

assert_success response
assert response.params.symbolize_keys[:response][:OrderId].present?
assert response.params['response']['OrderId'].present?
end

def test_successful_double_void_and_idempotent
authorize = @gateway.authorize(@amount, @credit_card, @options)
response = @gateway.void(authorize.authorization, @options)

assert_success response
assert response.params.symbolize_keys[:response][:OrderId].present?
assert response.params['response']['OrderId'].present?

second_void_response = @gateway.void(authorize.authorization, @options)

assert_success second_void_response
assert second_void_response.params.symbolize_keys[:response][:OrderId].present?
assert second_void_response.params['response']['OrderId'].present?

assert_equal response.params.symbolize_keys[:response][:OrderId], second_void_response.params.symbolize_keys[:response][:OrderId]
assert_equal response.params['response']['OrderId'], second_void_response.params['response']['OrderId']
end

def test_successful_verify
response = @gateway.verify(@credit_card, @options)

assert_success response
assert response.params.symbolize_keys[:response][:OrderId].present?
assert response.params['response']['OrderId'].present?
end

def test_failed_verify
response = @gateway.verify(@declined_card, @options)

assert_failure response
assert response.params.symbolize_keys[:response][:OrderId].present?
assert response.params['response']['OrderId'].present?
assert_equal 'PaymentAuthorizationFailed', response.error_code
end

Expand Down
8 changes: 4 additions & 4 deletions test/unit/gateways/reach_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,27 @@ def test_sending_item_sku_and_item_price
end

def test_successfull_retrieve_error_message
response = { response: { Error: { ReasonCode: 'is an error' } } }
response = { 'response' => { 'Error' => { 'ReasonCode' => 'is an error' } } }

message = @gateway.send(:message_from, response)
assert_equal 'is an error', message
end

def test_safe_retrieve_error_message
response = { response: { Error: { Code: 'is an error' } } }
response = { 'response' => { 'Error' => { 'Code' => 'is an error' } } }

message = @gateway.send(:message_from, response)
assert_nil message
end

def test_sucess_from_on_sucess_result
response = { response: { OrderId: '' } }
response = { 'response' => { OrderId: '' } }

assert @gateway.send(:success_from, response)
end

def test_sucess_from_on_failure
response = { response: { Error: 'is an error' } }
response = { 'response' => { 'Error' => 'is an error' } }

refute @gateway.send(:success_from, response)
end
Expand Down