Skip to content

Commit

Permalink
MercadoPago: Adding 3DS gateway specific fields
Browse files Browse the repository at this point in the history
Summary:
------------------------------
MercadoPago adding needed fields to mark a transaction to
request 3DS flow.

[SER-1226](https://spreedly.atlassian.net/browse/SER-1226)

Remote Test:
------------------------------
Finished in 79.263845 seconds.
20 tests, 56 assertions, 0 failures, 0 errors, 0 pendings,
1 omissions, 0 notifications
100% passed

Unit Tests:
------------------------------
Finished in 43.253318 seconds.
5981 tests, 80141 assertions, 0 failures, 0 errors, 0 pendings,
0 omissions, 0 notifications
100% passed

RuboCop:
------------------------------
798 files inspected, no offenses detected
  • Loading branch information
Heavyblade committed Aug 15, 2024
1 parent edf22c3 commit 107c1d4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
12 changes: 10 additions & 2 deletions lib/active_merchant/billing/gateways/mercado_pago.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ def purchase_request(money, payment, options = {})
add_net_amount(post, options)
add_taxes(post, options)
add_notification_url(post, options)
post[:binary_mode] = (options[:binary_mode].nil? ? true : options[:binary_mode])
add_3ds(post, options)
post[:binary_mode] = options.fetch(:binary_mode, true) unless options[:execute_threed]
post
end

Expand Down Expand Up @@ -287,7 +288,7 @@ def success_from(action, response)
if action == 'refund'
response['status'] != 404 && response['error'].nil?
else
%w[active approved authorized cancelled in_process].include?(response['status'])
%w[active approved authorized cancelled in_process pending].include?(response['status'])
end
end

Expand Down Expand Up @@ -322,6 +323,13 @@ def error_code_from(action, response)
end
end

def add_3ds(post, options)
return unless options[:execute_threed]

post[:three_d_secure_mode] = options[:three_ds_mode] == 'mandatory' ? 'mandatory' : 'optional'
post[:notification_url] = options[:notification_url] if options[:notification_url]
end

def url(action)
full_url = (test? ? test_url : live_url)
full_url + "/#{action}?access_token=#{CGI.escape(@options[:access_token])}"
Expand Down
15 changes: 14 additions & 1 deletion test/remote/gateways/remote_mercado_pago_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def setup
@options = {
billing_address: address,
shipping_address: address,
email: 'user+br@example.com',
email: 'test_user_1390220683@testuser.com',
description: 'Store Purchase'
}
@processing_options = {
Expand Down Expand Up @@ -363,4 +363,17 @@ def test_transcript_scrubbing
assert_scrubbed(@credit_card.verification_value, transcript)
assert_scrubbed(@gateway.options[:access_token], transcript)
end

def test_successful_purchase_with_3ds
three_ds_cc = credit_card('5483928164574623', verification_value: '123', month: 11, year: 2025)
@options[:execute_threed] = true

response = @gateway.purchase(290, three_ds_cc, @options)

assert_success response
assert_equal 'pending_challenge', response.message
assert_include response.params, 'three_ds_info'
assert_equal response.params['three_ds_info']['external_resource_url'], 'https://api.mercadopago.com/cardholder_authenticator/v2/prod/browser-challenges'
assert_include response.params['three_ds_info'], 'creq'
end
end
17 changes: 15 additions & 2 deletions test/unit/gateways/mercado_pago_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ def setup
end

def test_successful_purchase
@gateway.expects(:ssl_post).at_most(2).returns(successful_purchase_response)
response = stub_comms do
@gateway.purchase(@amount, @credit_card, @options)
end.check_request do |endpoint, data, _headers|
request = JSON.parse(data)
assert_equal true, request['binary_mode'] if /payments/.match?(endpoint)
end.respond_with(successful_purchase_response)

response = @gateway.purchase(@amount, @credit_card, @options)
assert_success response

assert_equal '4141491|1.0', response.authorization
Expand Down Expand Up @@ -499,6 +503,15 @@ def test_invalid_taxes_shape
end
end

def test_set_binary_mode_to_nil_when_request_is_3ds
stub_comms do
@gateway.authorize(@amount, @credit_card, @options.merge(execute_threed: true))
end.check_request do |endpoint, data, _headers|
request = JSON.parse(data)
assert_nil request['binary_mode'] if /payments/.match?(endpoint)
end.respond_with(successful_authorize_response)
end

private

def pre_scrubbed
Expand Down

0 comments on commit 107c1d4

Please sign in to comment.