From 3ee6d1350e59e440bbb5d852bf66627c7b045d12 Mon Sep 17 00:00:00 2001 From: Justin Robles Date: Wed, 12 Jun 2024 14:44:26 +0800 Subject: [PATCH] Use Amount Allocations object as Marketplace API is deprecated --- .../billing/gateways/checkout_v2.rb | 9 +++++ .../gateways/remote_checkout_v2_test.rb | 14 +++++-- test/unit/gateways/checkout_v2_test.rb | 40 +++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/lib/active_merchant/billing/gateways/checkout_v2.rb b/lib/active_merchant/billing/gateways/checkout_v2.rb index 29908275cbd..a1f6c14073d 100644 --- a/lib/active_merchant/billing/gateways/checkout_v2.rb +++ b/lib/active_merchant/billing/gateways/checkout_v2.rb @@ -145,6 +145,7 @@ def build_auth_or_purchase(post, amount, payment_method, options) add_3ds(post, options) add_metadata(post, options, payment_method) add_processing_channel(post, options) + add_amount_allocations_data(post, options) add_marketplace_data(post, options) add_recipient_data(post, options) add_processing_data(post, options) @@ -503,6 +504,14 @@ def add_marketplace_data(post, options) end end + def add_amount_allocations_data(post, options) + return unless options[:amount_allocations]&.is_a?(Array) + + post[:amount_allocations] = options[:amount_allocations].select do |v| + v[:id].present? && v[:amount].present? + end + end + def access_token_header { 'Authorization' => "Basic #{Base64.encode64("#{@options[:client_id]}:#{@options[:client_secret]}").delete("\n")}", diff --git a/test/remote/gateways/remote_checkout_v2_test.rb b/test/remote/gateways/remote_checkout_v2_test.rb index 142a1d898d6..9e6dbccdadc 100644 --- a/test/remote/gateways/remote_checkout_v2_test.rb +++ b/test/remote/gateways/remote_checkout_v2_test.rb @@ -85,6 +85,14 @@ def setup email: 'longbob.longsen@example.com', processing_channel_id: 'pc_lxgl7aqahkzubkundd2l546hdm' } + @options_for_verify = @options.merge({ + amount_allocations: [ + { + id: 'ent_123', + amount: 0 + } + ] + }) @additional_options = @options.merge( card_on_file: true, transaction_indicator: 2, @@ -1130,13 +1138,13 @@ def test_failed_void_via_oauth end def test_successful_verify - response = @gateway.verify(@credit_card, @options) + response = @gateway.verify(@credit_card, @options_for_verify) assert_success response assert_match %r{Succeeded}, response.message end def test_successful_verify_via_oauth - response = @gateway_oauth.verify(@credit_card, @options) + response = @gateway_oauth.verify(@credit_card, @options_for_verify) assert_success response assert_match %r{Succeeded}, response.message assert_not_nil response.responses.first.params['access_token'] @@ -1144,7 +1152,7 @@ def test_successful_verify_via_oauth end def test_failed_verify - response = @gateway.verify(@declined_card, @options) + response = @gateway.verify(@declined_card, @options_for_verify) assert_failure response assert_match %r{request_invalid: card_number_invalid}, response.message end diff --git a/test/unit/gateways/checkout_v2_test.rb b/test/unit/gateways/checkout_v2_test.rb index 220ad5a70c9..e22a9ac3f23 100644 --- a/test/unit/gateways/checkout_v2_test.rb +++ b/test/unit/gateways/checkout_v2_test.rb @@ -482,6 +482,46 @@ def test_successful_authorize_and_capture_with_additional_options assert_success capture end + def test_successful_authorize_and_capture_with_additional_options_ammount_allocation + response = stub_comms(@gateway, :ssl_request) do + options = { + card_on_file: true, + transaction_indicator: 2, + previous_charge_id: 'pay_123', + processing_channel_id: 'pc_123', + amount_allocations: [ + { + id: 'ent_123', + amount: 49 + }, + { + id: 'ent_456', + amount: 51 + }, + { + id: 'ent_789' + } + ] + } + @gateway.authorize(@amount, @credit_card, options) + end.check_request do |_method, _endpoint, data, _headers| + assert_match(%r{"stored":"true"}, data) + assert_match(%r{"payment_type":"Recurring"}, data) + assert_match(%r{"previous_payment_id":"pay_123"}, data) + assert_match(%r{"processing_channel_id":"pc_123"}, data) + assert_match(/"amount_allocations\":\[{\"id\":\"ent_123\",\"amount\":49},{\"id\":\"ent_456\",\"amount\":51}\]/, data) + end.respond_with(successful_authorize_response) + + assert_success response + assert_equal 'pay_fj3xswqe3emuxckocjx6td73ni', response.authorization + + capture = stub_comms(@gateway, :ssl_request) do + @gateway.capture(@amount, response.authorization) + end.respond_with(successful_capture_response) + + assert_success capture + end + def test_successful_purchase_with_stored_credentials initial_response = stub_comms(@gateway, :ssl_request) do initial_options = {