diff --git a/lib/active_merchant/billing/gateways/braintree/token_nonce.rb b/lib/active_merchant/billing/gateways/braintree/token_nonce.rb index eeaee734fc2..dc9a3e0bc90 100644 --- a/lib/active_merchant/billing/gateways/braintree/token_nonce.rb +++ b/lib/active_merchant/billing/gateways/braintree/token_nonce.rb @@ -92,7 +92,7 @@ def billing_address_from_options def build_nonce_credit_card_request(payment_method) billing_address = billing_address_from_options key_replacements = { city: :locality, state: :region, zipCode: :postalCode } - billing_address.transform_keys! { |key| key_replacements[key] || key } + billing_address&.transform_keys! { |key| key_replacements[key] || key } { creditCard: { number: payment_method.number, diff --git a/test/unit/gateways/braintree_token_nonce_test.rb b/test/unit/gateways/braintree_token_nonce_test.rb index d75ff5c405e..89aac7612c8 100644 --- a/test/unit/gateways/braintree_token_nonce_test.rb +++ b/test/unit/gateways/braintree_token_nonce_test.rb @@ -25,6 +25,7 @@ def setup ach_mandate: 'ach_mandate' } @generator = TokenNonce.new(@braintree_backend, @options) + @no_address_generator = TokenNonce.new(@braintree_backend, { ach_mandate: 'ach_mandate' }) end def test_build_nonce_request_for_credit_card @@ -66,6 +67,23 @@ def test_build_nonce_request_for_bank_account assert_equal bank_account_input['individualOwner']['lastName'], bank_account.last_name end + def test_build_nonce_request_for_credit_card_without_address + credit_card = credit_card('4111111111111111') + response = @no_address_generator.send(:build_nonce_request, credit_card) + parse_response = JSON.parse response + assert_client_sdk_metadata(parse_response) + assert_equal normalize_graph(parse_response['query']), normalize_graph(credit_card_query) + assert_includes parse_response['variables']['input'], 'creditCard' + + credit_card_input = parse_response['variables']['input']['creditCard'] + + assert_equal credit_card_input['number'], credit_card.number + assert_equal credit_card_input['expirationYear'], credit_card.year.to_s + assert_equal credit_card_input['expirationMonth'], credit_card.month.to_s.rjust(2, '0') + assert_equal credit_card_input['cvv'], credit_card.verification_value + assert_equal credit_card_input['cardholderName'], credit_card.name + end + def test_token_from credit_card = credit_card(number: 4111111111111111) c_token = @generator.send(:token_from, credit_card, token_credit_response)