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

CheckoutV2: Add Amount Allocations object #5186

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions lib/active_merchant/billing/gateways/checkout_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")}",
Expand Down
28 changes: 28 additions & 0 deletions test/remote/gateways/remote_checkout_v2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ def setup
email: '[email protected]',
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,
Expand Down Expand Up @@ -1149,6 +1157,26 @@ def test_failed_verify
assert_match %r{request_invalid: card_number_invalid}, response.message
end

def test_successful_verify_with_ammount_allocations
response = @gateway.verify(@credit_card, @options_for_verify)
assert_success response
assert_match %r{Succeeded}, response.message
end

def test_successful_verify_via_oauth_with_ammount_allocations
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']
assert_not_nil response.responses.first.params['expires']
end

def test_failed_verify_with_ammount_allocations
response = @gateway.verify(@declined_card, @options_for_verify)
assert_failure response
assert_match %r{request_invalid: card_number_invalid}, response.message
end

def test_expired_card_returns_error_code
response = @gateway.purchase(@amount, @expired_card, @options)
assert_failure response
Expand Down
40 changes: 40 additions & 0 deletions test/unit/gateways/checkout_v2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
justingrobles marked this conversation as resolved.
Show resolved Hide resolved
{
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 = {
Expand Down