Skip to content

Commit

Permalink
CheckoutV2: Add metadata to Credit
Browse files Browse the repository at this point in the history
Add metadata to Credit and pending as a field for response.
Set pending to true for CheckoutV2 if a Credit returns
status of Pending.

Only add response['error_codes'] to the message if it exist.
Remote
113 tests, 282 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed

Unit:
67 tests, 420 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
  • Loading branch information
Alma Malambo committed Nov 11, 2024
1 parent efa27d7 commit b658ecd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
14 changes: 12 additions & 2 deletions lib/active_merchant/billing/gateways/checkout_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def credit(amount, payment, options = {})
add_instruction_data(post, options)
add_payout_sender_data(post, options)
add_payout_destination_data(post, options)
add_metadata(post, options)

commit(:credit, post, options)
end
Expand Down Expand Up @@ -580,10 +581,17 @@ def response(action, succeeded, response, options = {}, source_id = nil)
error_code: error_code_from(succeeded, body, options),
test: test?,
avs_result: avs_result(response),
cvv_result: cvv_result(response)
cvv_result: cvv_result(response),
pending: pending_result(response, action)
)
end

def pending_result(response, action)
return unless action == :credit

response['status'] == 'Pending'
end

def headers(action, options)
auth_token = @options[:access_token] ? "Bearer #{@options[:access_token]}" : @options[:secret_key]
auth_token = @options[:public_key] if action == :tokens
Expand Down Expand Up @@ -664,7 +672,9 @@ def message_from(succeeded, response, options)
if succeeded
'Succeeded'
elsif response['error_type']
response['error_type'] + ': ' + response['error_codes'].first
return response['error_type'] unless response['error_codes']

"#{response['error_type']}: #{response['error_codes'].first}"
else
response_summary = response['response_summary'] || response.dig('actions', 0, 'response_summary')
response_summary || response['response_code'] || response['status'] || response['message'] || 'Unable to read error message'
Expand Down
3 changes: 2 additions & 1 deletion lib/active_merchant/billing/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Error < ActiveMerchantError #:nodoc:
end

class Response
attr_reader :params, :message, :test, :authorization, :avs_result, :cvv_result, :error_code, :emv_authorization, :network_transaction_id
attr_reader :params, :message, :test, :authorization, :avs_result, :cvv_result, :error_code, :emv_authorization, :network_transaction_id, :pending

def success?
@success
Expand All @@ -30,6 +30,7 @@ def initialize(success, message, params = {}, options = {})
@error_code = options[:error_code]
@emv_authorization = options[:emv_authorization]
@network_transaction_id = options[:network_transaction_id]
@pending = options[:pending] || false

@avs_result = if options[:avs_result].kind_of?(AVSResult)
options[:avs_result].to_hash
Expand Down
2 changes: 2 additions & 0 deletions test/remote/gateways/remote_checkout_v2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ def test_successful_credit
response = @gateway_oauth.credit(@amount, @credit_card, @options.merge({ source_type: 'currency_account', source_id: 'ca_spwmped4qmqenai7hcghquqle4', account_holder_type: 'individual' }))
assert_success response
assert_equal 'Succeeded', response.message
assert_equal true, response.primary_response.pending
end

def test_successful_money_transfer_payout_via_credit_individual_account_holder_type
Expand All @@ -912,6 +913,7 @@ def test_successful_money_transfer_payout_via_credit_individual_account_holder_t
response = @gateway_oauth.credit(@amount, @credit_card, @payout_options.merge(account_holder_type: 'individual', payout: true))
assert_success response
assert_equal 'Succeeded', response.message
assert_equal true, response.primary_response.pending
end

def test_successful_money_transfer_payout_via_credit_corporate_account_holder_type
Expand Down
22 changes: 21 additions & 1 deletion test/unit/gateways/checkout_v2_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'test_helper'

class CheckoutV2Test < Test::Unit::TestCase
include CommStub

Expand Down Expand Up @@ -795,7 +796,8 @@ def test_successfully_passes_fund_type_and_fields
funds_transfer_type: 'FD',
source_type: 'currency_account',
source_id: 'ca_spwmped4qmqenai7hcghquqle4',
account_holder_type: 'individual'
account_holder_type: 'individual',
metadata: { transaction_token: '123' }
}
response = stub_comms(@gateway, :ssl_request) do
@gateway.credit(@amount, @credit_card, options)
Expand All @@ -807,6 +809,7 @@ def test_successfully_passes_fund_type_and_fields
assert_equal request['destination']['account_holder']['type'], options[:account_holder_type]
assert_equal request['destination']['account_holder']['first_name'], @credit_card.first_name
assert_equal request['destination']['account_holder']['last_name'], @credit_card.last_name
assert_equal request['metadata']['transaction_token'], '123'
end.respond_with(successful_credit_response)
assert_success response
end
Expand Down Expand Up @@ -1034,6 +1037,15 @@ def test_error_code_returned
assert_match(/request_invalid: card_expired/, response.error_code)
end

def test_error_type_without_error_code_returned
response = stub_comms(@gateway, :ssl_request) do
@gateway.purchase(@amount, @credit_card)
end.respond_with(error_type_without_error_codes_response)

assert_failure response
assert_match(/request_invalid/, response.error_code)
end

def test_4xx_error_message
@gateway.expects(:ssl_request).raises(error_4xx_response)

Expand Down Expand Up @@ -1467,6 +1479,14 @@ def error_code_response
)
end

def error_type_without_error_codes_response
%(
{
"request_id": "e5a3ce6f-a4e9-4445-9ec7-e5975e9a6213","error_type": "request_invalid"
}
)
end

def error_4xx_response
mock_response = Net::HTTPUnauthorized.new('1.1', '401', 'Unauthorized')
mock_response.stubs(:body).returns('')
Expand Down

0 comments on commit b658ecd

Please sign in to comment.