Skip to content

Commit

Permalink
VisaNet Peru: Always include DSC_COD_ACCION
Browse files Browse the repository at this point in the history
Always include the action code description (DSC_COD_ACCION), when
available.

This also adds a test[1] to ensure the logic[2] is correct for our
stashing the message in the options hash for retrying a full refund if a
partial refund fails.

[1] #3174 (comment)

[2] #2772 (comment)

ECS-173

Unit:
15 tests, 75 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed

Remote:
18 tests, 27 assertions, 16 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
11.1111% passed

(Please note that most of the remote tests are failing because our test
credentials are invalid and we've not been able to get new/valid
credentials as of the time of this commit).

Closes #3174
  • Loading branch information
Zeb DeOs committed Apr 1, 2019
1 parent 077cba4 commit b16d656
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Adyen: Fix adding phone from billing address [curiousepic] #3179
* Fix partial or missing address exceptions [molbrown] #3180
* Adyen: Update to support normalized stored credential fields [molbrown] #3182
* VisaNet Peru: Always include DSC_COD_ACCION [bayprogrammer] #3174

== Version 1.91.0 (February 22, 2019)
* WorldPay: Pull CVC and AVS Result from Response [nfarve] #3106
Expand Down
32 changes: 22 additions & 10 deletions lib/active_merchant/billing/gateways/visanet_peru.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ def refund(amount, authorization, options={})
response = commit('cancelDeposit', params, options)
return response if response.success? || split_authorization(authorization).length == 1 || !options[:force_full_refund_if_unsettled]

# Attempt RefundSingleTransaction if unsettled
# Attempt RefundSingleTransaction if unsettled (and stash the original
# response message so it will be included it in the follow-up response
# message)
options[:error_message] = response.message
prepare_refund_data(params, authorization, options)
commit('refund', params, options)
end
Expand Down Expand Up @@ -197,15 +200,24 @@ def success_from(response)
end

def message_from(response, options, action)
if empty?(response['errorMessage']) || response['errorMessage'] == '[ ]'
action == 'refund' ? "#{response['data']['DSC_COD_ACCION']}, #{options[:error_message]}" : response['data']['DSC_COD_ACCION']
elsif action == 'refund'
message = "#{response['errorMessage']}, #{options[:error_message]}"
options[:error_message] = response['errorMessage']
message
else
response['errorMessage']
end
message_from_messages(
response['errorMessage'],
action_code_description(response),
options[:error_message]
)
end

def message_from_messages(*args)
args.reject { |m| error_message_empty?(m) }.join(' | ')
end

def action_code_description(response)
return nil unless response['data']
response['data']['DSC_COD_ACCION']
end

def error_message_empty?(error_message)
empty?(error_message) || error_message == '[ ]'
end

def response_error(raw_response, options, action)
Expand Down
8 changes: 6 additions & 2 deletions test/remote/gateways/remote_visanet_peru_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,21 @@ def test_successful_authorize_fractional_amount
assert_equal '1.99', response.params['data']['IMP_AUTORIZADO']
end

def test_failed_authorize
def test_failed_authorize_declined_card
response = @gateway.authorize(@amount, @declined_card, @options)
assert_failure response
assert_equal 400, response.error_code
assert_equal 'Operacion Denegada.', response.message
end

def test_failed_authorize_bad_email
@options[:email] = '[email protected]'
response = @gateway.authorize(@amount, @credit_card, @options)
assert_failure response
assert_equal 400, response.error_code
assert_equal 'REJECT', response.message

# this also exercises message joining for errorMessage and DSC_COD_ACCION when both are present
assert_equal 'REJECT | Operacion denegada', response.message
end

def test_failed_capture
Expand Down
75 changes: 74 additions & 1 deletion test/unit/gateways/visanet_peru_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_failed_authorize
response = @gateway.authorize(@amount, @credit_card, @options)
assert_failure response
assert_equal 400, response.error_code
assert_equal 'REJECT', response.message
assert_equal 'REJECT | Operacion denegada', response.message
end

def test_successful_capture
Expand Down Expand Up @@ -104,6 +104,27 @@ def test_failed_refund
assert_equal 400, response.error_code
end

def test_failed_full_refund_when_unsettled
@gateway.expects(:ssl_request).with(:put, any_parameters).returns(failed_refund_response)
@gateway.expects(:ssl_request).with(:post, any_parameters).returns(failed_refund_with_action_code_response)
response = @gateway.refund(@amount, '122333444|444333221', force_full_refund_if_unsettled: true)
assert_failure response
assert_equal("Operacion Denegada. | [ 'NUMORDEN 122333444 no se encuentra registrado', 'No se realizo la anulacion del deposito' ]", response.message)
assert_equal 400, response.error_code
end

def test_failed_full_refund_when_unsettled_additional_message_concatenation
@gateway.expects(:ssl_request).with(:put, any_parameters).returns(failed_refund_with_message_and_action_code_response)
@gateway.expects(:ssl_request).with(:post, any_parameters).returns(failed_refund_with_message_and_action_code_response_2)
first_msg = 'No se realizo la anulacion del deposito'
first_dsc = 'Operacion Denegada.'
second_msg = 'Mal funcionamiento de la inteligencia artificial'
second_dsc = 'Lo siento Dave, me temo que no puedo hacer eso.'

response = @gateway.refund(@amount, '122333444|444333221', force_full_refund_if_unsettled: true)
assert_equal("#{second_msg} | #{second_dsc} | #{first_msg} | #{first_dsc}", response.message)
end

def test_successful_void
@gateway.expects(:ssl_request).returns(successful_authorize_response)
response = @gateway.authorize(@amount, @credit_card, @options)
Expand Down Expand Up @@ -446,4 +467,56 @@ def failed_refund_response
}
RESPONSE
end

def failed_refund_with_action_code_response
<<-RESPONSE
{
"errorCode": 400,
"errorMessage": "[ ]",
"data": {
"ESTADO": "",
"RESPUESTA": "2",
"DSC_COD_ACCION": "Operacion Denegada."
},
"transactionLog": {
}
}
RESPONSE
end

def failed_refund_with_message_and_action_code_response
<<-RESPONSE
{
"errorCode": 400,
"errorMessage": "No se realizo la anulacion del deposito",
"data": {
"ESTADO": "",
"RESPUESTA": "2",
"DSC_COD_ACCION": "Operacion Denegada."
},
"transactionLog": {
}
}
RESPONSE
end

def failed_refund_with_message_and_action_code_response_2
<<-RESPONSE
{
"errorCode": 400,
"errorMessage": "Mal funcionamiento de la inteligencia artificial",
"data": {
"ESTADO": "",
"RESPUESTA": "2",
"DSC_COD_ACCION": "Lo siento Dave, me temo que no puedo hacer eso."
},
"transactionLog": {
}
}
RESPONSE
end

end

0 comments on commit b16d656

Please sign in to comment.