From d9606a57ec9d0a2b7b4a255f7bc8af8f44dff8c4 Mon Sep 17 00:00:00 2001 From: Alma Malambo Date: Mon, 25 Nov 2024 15:49:37 -0600 Subject: [PATCH] Braintree: Account for BraintreeError MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../billing/gateways/braintree_blue.rb | 37 ++++++++------ test/unit/gateways/braintree_blue_test.rb | 50 +++++++++++++++++++ 2 files changed, 72 insertions(+), 15 deletions(-) diff --git a/lib/active_merchant/billing/gateways/braintree_blue.rb b/lib/active_merchant/billing/gateways/braintree_blue.rb index c7611c4ddac..bdc2955a486 100644 --- a/lib/active_merchant/billing/gateways/braintree_blue.rb +++ b/lib/active_merchant/billing/gateways/braintree_blue.rb @@ -1051,26 +1051,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 diff --git a/test/unit/gateways/braintree_blue_test.rb b/test/unit/gateways/braintree_blue_test.rb index 32dfee503ee..1db754d100a 100644 --- a/test/unit/gateways/braintree_blue_test.rb +++ b/test/unit/gateways/braintree_blue_test.rb @@ -1137,6 +1137,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). @@ -1622,6 +1654,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]