Skip to content

Commit

Permalink
Braintree: Account for BraintreeError
Browse files Browse the repository at this point in the history
Take into account Braintree::ErrorResult for add_bank_account_to_customer
to prevent NoMethodError

Unit:
108 tests, 231 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed

Remote:
123 tests, 662 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
  • Loading branch information
Alma Malambo committed Dec 4, 2024
1 parent af2d1af commit 2fe6ccb
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
* Nuvei: Fix send savePM in false by default [javierpedrozaing] #5353
* Decidir and DecicirPlus: Add the wallet_id field [yunnydang] #5354
* Worldpay: Update where to pass shopperIPAddress [almalee24] #5348
* Braintree: Account for BraintreeError [almalee24] #5346

== Version 1.137.0 (August 2, 2024)
* Unlock dependency on `rexml` to allow fixing a CVE (#5181).
Expand Down
37 changes: 22 additions & 15 deletions lib/active_merchant/billing/gateways/braintree_blue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1061,26 +1061,33 @@ def add_bank_account_to_customer(payment_method, options)
}
)

verified = result.success? && result.payment_method&.verified
message = message_from_result(result)
message = not_verified_reason(result.payment_method) unless verified

success = bank_account_verified?(result)
resp_body = if result.respond_to?(:payment_method)
{
customer_vault_id: options[:customer],
bank_account_token: result.payment_method&.token,
verified: success
}
end
Response.new(
verified,
message,
{
customer_vault_id: options[:customer],
bank_account_token: result.payment_method&.token,
verified:
},
authorization: result.payment_method&.token
success,
message_from_bank_account_result(success, result),
resp_body || {},
authorization: (result.payment_method&.token if result.respond_to?(:payment_method))
)
end

def not_verified_reason(bank_account)
return unless bank_account.verifications.present?
def bank_account_verified?(result)
return false unless result.respond_to?(:payment_method)

result.success? && result.payment_method&.verified
end

def message_from_bank_account_result(success, response)
return message_from_result(response) if !response.respond_to?(:payment_method) || success
return unless response.payment_method.verifications.present?

verification = bank_account.verifications.first
verification = response.payment_method.verifications.first
"verification_status: [#{verification.status}], processor_response: [#{verification.processor_response_code}-#{verification.processor_response_text}]"
end

Expand Down
50 changes: 50 additions & 0 deletions test/unit/gateways/braintree_blue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,38 @@ def test_unsuccessful_transaction_returns_id_when_available
assert response.authorization.present?
end

def test_unsuccessful_adding_bank_account_to_customer
bank_account = check({ account_number: '1000000002', routing_number: '011000015' })
options = {
billing_address: {
address1: '1670',
address2: '1670 NW 82ND AVE',
city: 'Miami',
state: 'FL',
zip: '32191'
},
ach_mandate: 'ACH Mandate',
merchant_account_id: 'merchant_account_id'
}
customer = stub(
credit_cards: [stub_everything],
email: 'email',
phone: '321-654-0987',
first_name: 'John',
last_name: 'Smith'
)
customer.stubs(:id).returns('123')

Braintree::CustomerGateway.any_instance.expects(:create).returns(Braintree::SuccessfulResult.new(customer:))
Braintree::ClientTokenGateway.any_instance.expects(:generate).returns('IntcImNsaWVudF90b2tlblwiOlwiMTIzNFwifSI=')
ActiveMerchant::Billing::TokenNonce.any_instance.expects(:ssl_request).returns(JSON.generate(token_bank_response))
Braintree::PaymentMethodGateway.any_instance.expects(:create).returns(braintree_error_result(message: 'US bank account is not accepted by merchant account.'))

assert response = @gateway.store(bank_account, options)
refute response.success?
assert_equal response.message, 'US bank account is not accepted by merchant account.'
end

def test_unsuccessful_transaction_returns_message_when_available
Braintree::TransactionGateway.any_instance.
expects(:sale).
Expand Down Expand Up @@ -1658,6 +1690,24 @@ def standard_purchase_params
}
end

def token_bank_response
{
'data' => {
'tokenizeUsBankAccount' => {
'paymentMethod' => {
'id' => 'tokenusbankacct_bc_zrg45z_7wz95v_nscrks_q4zpjs_5m7',
'details' => {
'last4' => '0125'
}
}
}
},
'extensions' => {
'requestId' => '769b26d5-27e4-4602-b51d-face8b6ffdd5'
}
}
end

def success_create_token_nonce
<<-RESPONSE
[Braintree] <payment-method>
Expand Down

0 comments on commit 2fe6ccb

Please sign in to comment.