Close BNPL ToS dialog in the CreateBnplPaymentInstrument error case

In the error case, we must first close the BNPL ToS dialog (which must
be showing) before showing the error dialog and ending the flow.

Bug: 356443046
Change-Id: I101f9ca5e6d27a4872f1aa9143fb81c41dd69df3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6460453
Reviewed-by: Olivia Saul <jsaul@google.com>
Commit-Queue: Vinny Persky <vinnypersky@google.com>
Cr-Commit-Position: refs/heads/main@{#1447436}
diff --git a/components/autofill/core/browser/payments/bnpl_manager.cc b/components/autofill/core/browser/payments/bnpl_manager.cc
index 098a0ed2..c713f245 100644
--- a/components/autofill/core/browser/payments/bnpl_manager.cc
+++ b/components/autofill/core/browser/payments/bnpl_manager.cc
@@ -504,6 +504,7 @@
     ongoing_flow_state_->instrument_id = std::move(instrument_id);
     FetchRedirectUrl();
   } else {
+    payments_autofill_client().CloseBnplTos();
     payments_autofill_client().ShowAutofillErrorDialog(
         AutofillErrorDialogContext::WithBnplPermanentOrTemporaryError(
             /*is_permanent_error=*/ShouldShowPermanentErrorDialog(result)));
diff --git a/components/autofill/core/browser/payments/bnpl_manager_unittest.cc b/components/autofill/core/browser/payments/bnpl_manager_unittest.cc
index d6f4ce4b6..e4702c4 100644
--- a/components/autofill/core/browser/payments/bnpl_manager_unittest.cc
+++ b/components/autofill/core/browser/payments/bnpl_manager_unittest.cc
@@ -1400,6 +1400,7 @@
                   _))
       .WillOnce(base::test::RunOnceCallback<1>(
           PaymentsAutofillClient::PaymentsRpcResult::kPermanentFailure, ""));
+  EXPECT_CALL(GetPaymentsAutofillClient(), CloseBnplTos);
 
   test_api(*bnpl_manager_).CreateBnplPaymentInstrument();