Skip to content

Commit

Permalink
Fix token nonce to support nil billing address
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Sanmartin authored and Gustavo Sanmartin committed Oct 31, 2023
1 parent 6a4afac commit 4a94068
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions test/unit/gateways/braintree_token_nonce_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4a94068

Please sign in to comment.