1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill/core/browser/payments/bnpl_manager.h"
#include <cstdint>
#include <optional>
#include <string>
#include <tuple>
#include <utility>
#include <variant>
#include "base/barrier_callback.h"
#include "base/check_deref.h"
#include "base/containers/contains.h"
#include "base/containers/to_vector.h"
#include "base/functional/bind.h"
#include "base/rand_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/data_model/payments/credit_card.h"
#include "components/autofill/core/browser/foundations/autofill_manager.h"
#include "components/autofill/core/browser/integrators/optimization_guide/autofill_optimization_guide.h"
#include "components/autofill/core/browser/metrics/form_events/credit_card_form_event_logger.h"
#include "components/autofill/core/browser/metrics/payments/bnpl_metrics.h"
#include "components/autofill/core/browser/payments/constants.h"
#include "components/autofill/core/browser/payments/payments_network_interface.h"
#include "components/autofill/core/browser/payments/payments_request_details.h"
#include "components/autofill/core/browser/payments/payments_util.h"
#include "components/autofill/core/browser/suggestions/payments/payments_suggestion_generator.h"
#include "components/autofill/core/browser/ui/payments/bnpl_tos_controller.h"
#include "components/autofill/core/browser/ui/payments/select_bnpl_issuer_dialog_controller_impl.h"
#include "components/autofill/core/common/autofill_payments_features.h"
namespace autofill::payments {
namespace {
// Returns true if the `extracted_amount_in_micros` is supported by
// `bnpl_issuer`.
bool ShouldShowBnplOptionForIssuer(const BnplIssuer& bnpl_issuer,
uint64_t extracted_amount_in_micros) {
// For MVP, BNPL will only target US users and support USD.
return bnpl_issuer.IsEligibleAmount(extracted_amount_in_micros,
/*currency=*/"USD");
}
bool ShouldShowPermanentErrorDialog(
PaymentsAutofillClient::PaymentsRpcResult result) {
return result == PaymentsAutofillClient::PaymentsRpcResult::
kVcnRetrievalPermanentFailure;
}
} // namespace
BnplManager::OngoingFlowState::OngoingFlowState() = default;
BnplManager::OngoingFlowState::~OngoingFlowState() = default;
BnplManager::BnplManager(BrowserAutofillManager* browser_autofill_manager)
: browser_autofill_manager_(CHECK_DEREF(browser_autofill_manager)) {}
BnplManager::~BnplManager() = default;
// static
const std::array<std::string_view, 2>&
BnplManager::GetSupportedBnplIssuerIds() {
// Calling `ConvertToBnplIssuerIdString` serves as a validation step,
// verifying that each supported Bnpl IssuerId enum value has a corresponding
// string representation. This helps maintain the invariant with
// `ConvertToBnplIssuerIdEnum`.
static const std::array<std::string_view, 2> kBnplIssuers = {
autofill::ConvertToBnplIssuerIdString(BnplIssuer::IssuerId::kBnplAffirm),
autofill::ConvertToBnplIssuerIdString(BnplIssuer::IssuerId::kBnplZip)};
return kBnplIssuers;
}
void BnplManager::OnDidAcceptBnplSuggestion(
uint64_t final_checkout_amount,
OnBnplVcnFetchedCallback on_bnpl_vcn_fetched_callback) {
ongoing_flow_state_ = std::make_unique<OngoingFlowState>();
ongoing_flow_state_->final_checkout_amount = final_checkout_amount;
ongoing_flow_state_->app_locale =
browser_autofill_manager_->client().GetAppLocale();
ongoing_flow_state_->billing_customer_number =
GetBillingCustomerId(payments_autofill_client().GetPaymentsDataManager());
ongoing_flow_state_->on_bnpl_vcn_fetched_callback =
std::move(on_bnpl_vcn_fetched_callback);
// Prefetch risk data to improve flow latency by reducing the need to fetch
// risk data later, as it can take several seconds in some rare cases.
payments_autofill_client().LoadRiskData(base::BindOnce(
&BnplManager::OnPrefetchedRiskDataLoaded, weak_factory_.GetWeakPtr()));
payments_autofill_client().ShowSelectBnplIssuerDialog(
GetSortedBnplIssuerContext(), ongoing_flow_state_->app_locale,
base::BindOnce(&BnplManager::OnIssuerSelected,
weak_factory_.GetWeakPtr()),
base::BindOnce(&BnplManager::Reset, weak_factory_.GetWeakPtr()));
browser_autofill_manager_->GetCreditCardFormEventLogger()
.OnDidAcceptBnplSuggestion();
}
void BnplManager::NotifyOfSuggestionGeneration(
const AutofillSuggestionTriggerSource trigger_source) {
if (!base::FeatureList::IsEnabled(features::kAutofillEnableBuyNowPayLater)) {
return;
}
update_suggestions_barrier_callback_ = base::BarrierCallback<
std::variant<SuggestionsShownResponse, std::optional<uint64_t>>>(
2U, base::BindOnce(&BnplManager::MaybeUpdateSuggestionsWithBnpl,
weak_factory_.GetWeakPtr(), trigger_source));
}
void BnplManager::OnSuggestionsShown(
base::span<const Suggestion> suggestions,
UpdateSuggestionsCallback update_suggestions_callback) {
if (!update_suggestions_barrier_callback_.has_value()) {
return;
}
// Do not proceed to calling the barrier callback, if the suggestion list
// already contains a buy-now-pay-later-entry (which is triggered after
// updating the original suggestion list).
if (base::Contains(suggestions, SuggestionType::kBnplEntry,
&Suggestion::type)) {
return;
}
if (update_suggestions_barrier_callback_.has_value()) {
update_suggestions_barrier_callback_->Run(SuggestionsShownResponse(
std::vector<Suggestion>(std::begin(suggestions), std::end(suggestions)),
std::move(update_suggestions_callback)));
}
}
void BnplManager::OnAmountExtractionReturned(
const std::optional<uint64_t>& extracted_amount) {
if (!update_suggestions_barrier_callback_.has_value()) {
return;
}
if (update_suggestions_barrier_callback_.has_value()) {
update_suggestions_barrier_callback_->Run(extracted_amount);
}
if (!extracted_amount && !has_logged_bnpl_suggestion_not_shown_reason_) {
LogBnplSuggestionNotShownReason(
autofill_metrics::BnplSuggestionNotShownReason::
kAmountExtractionFailure);
has_logged_bnpl_suggestion_not_shown_reason_ = true;
}
}
void BnplManager::FetchVcnDetails(GURL url) {
GetBnplPaymentInstrumentForFetchingVcnRequestDetails request_details;
request_details.billing_customer_number =
ongoing_flow_state_->billing_customer_number;
request_details.instrument_id = ongoing_flow_state_->instrument_id;
request_details.risk_data = ongoing_flow_state_->risk_data;
request_details.context_token = ongoing_flow_state_->context_token;
request_details.redirect_url = std::move(url);
request_details.issuer_id = autofill::ConvertToBnplIssuerIdString(
ongoing_flow_state_->issuer.issuer_id());
payments_autofill_client().ShowAutofillProgressDialog(
AutofillProgressDialogType::kBnplFetchVcnProgressDialog,
/*cancel_callback=*/base::BindOnce(
[](base::WeakPtr<BnplManager> manager) {
if (manager) {
// Note: Does not call
// `PaymentsAutofillClient::CloseAutofillProgressDialog()` as this
// is expected to be handled by dialog UI code.
manager->Reset();
}
},
weak_factory_.GetWeakPtr()));
payments_autofill_client()
.GetPaymentsNetworkInterface()
->GetBnplPaymentInstrumentForFetchingVcn(
std::move(request_details),
base::BindOnce(&BnplManager::OnVcnDetailsFetched,
weak_factory_.GetWeakPtr()));
}
void BnplManager::Reset() {
payments_autofill_client().GetPaymentsNetworkInterface()->CancelRequest();
ongoing_flow_state_.reset();
weak_factory_.InvalidateWeakPtrs();
}
void BnplManager::OnVcnDetailsFetched(
PaymentsAutofillClient::PaymentsRpcResult result,
const BnplFetchVcnResponseDetails& response_details) {
bool successful =
result == PaymentsAutofillClient::PaymentsRpcResult::kSuccess;
payments_autofill_client().CloseAutofillProgressDialog(
/*show_confirmation_before_closing=*/successful,
/*no_interactive_authentication_callback=*/base::OnceClosure());
if (successful) {
CHECK(ongoing_flow_state_);
CreditCard credit_card;
credit_card.SetRawInfo(autofill::CREDIT_CARD_NUMBER,
base::UTF8ToUTF16(response_details.pan));
credit_card.set_record_type(CreditCard::RecordType::kVirtualCard);
credit_card.SetRawInfo(autofill::CREDIT_CARD_NAME_FULL,
base::UTF8ToUTF16(response_details.cardholder_name));
credit_card.SetRawInfo(
autofill::CREDIT_CARD_EXP_MONTH,
base::UTF8ToUTF16(response_details.expiration_month));
credit_card.SetRawInfo(autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR,
base::UTF8ToUTF16(response_details.expiration_year));
credit_card.set_cvc(base::UTF8ToUTF16(response_details.cvv));
credit_card.set_issuer_id(autofill::ConvertToBnplIssuerIdString(
ongoing_flow_state_->issuer.issuer_id()));
credit_card.set_is_bnpl_card(true);
credit_card.SetNickname(ongoing_flow_state_->issuer.GetDisplayName());
std::move(ongoing_flow_state_->on_bnpl_vcn_fetched_callback)
.Run(credit_card);
} else {
payments_autofill_client().ShowAutofillErrorDialog(
AutofillErrorDialogContext::WithBnplPermanentOrTemporaryError(
/*is_permanent_error=*/ShouldShowPermanentErrorDialog(result)));
}
Reset();
}
void BnplManager::OnIssuerSelected(BnplIssuer selected_issuer) {
ongoing_flow_state_->issuer = std::move(selected_issuer);
if (ongoing_flow_state_->issuer.payment_instrument().has_value()) {
ongoing_flow_state_->instrument_id = base::NumberToString(
ongoing_flow_state_->issuer.payment_instrument()->instrument_id());
LoadRiskDataForFetchingRedirectUrl();
} else {
GetDetailsForCreateBnplPaymentInstrument();
}
}
void BnplManager::GetDetailsForCreateBnplPaymentInstrument() {
GetDetailsForCreateBnplPaymentInstrumentRequestDetails request_details;
request_details.app_locale = ongoing_flow_state_->app_locale;
request_details.billing_customer_number =
ongoing_flow_state_->billing_customer_number;
request_details.issuer_id = autofill::ConvertToBnplIssuerIdString(
ongoing_flow_state_->issuer.issuer_id());
payments_autofill_client()
.GetPaymentsNetworkInterface()
->GetDetailsForCreateBnplPaymentInstrument(
std::move(request_details),
base::BindOnce(
&BnplManager::OnDidGetDetailsForCreateBnplPaymentInstrument,
weak_factory_.GetWeakPtr()));
}
void BnplManager::OnDidGetDetailsForCreateBnplPaymentInstrument(
PaymentsAutofillClient::PaymentsRpcResult result,
std::string context_token,
LegalMessageLines legal_message) {
// Dismiss the loading throbber in the issuer selection dialog after the
// server call completion to show the next dialog.
payments_autofill_client().DismissSelectBnplIssuerDialog();
if (result == payments::PaymentsAutofillClient::PaymentsRpcResult::kSuccess) {
ongoing_flow_state_->context_token = std::move(context_token);
CHECK(!legal_message.empty());
BnplTosModel bnpl_tos_model;
bnpl_tos_model.legal_message_lines = std::move(legal_message);
bnpl_tos_model.issuer = ongoing_flow_state_->issuer;
payments_autofill_client().ShowBnplTos(
std::move(bnpl_tos_model),
base::BindOnce(&BnplManager::OnTosDialogAccepted,
weak_factory_.GetWeakPtr()),
base::BindOnce(&BnplManager::Reset, weak_factory_.GetWeakPtr()));
return;
}
payments_autofill_client().ShowAutofillErrorDialog(
AutofillErrorDialogContext::WithBnplPermanentOrTemporaryError(
/*is_permanent_error=*/ShouldShowPermanentErrorDialog(result)));
Reset();
}
void BnplManager::LoadRiskDataForFetchingRedirectUrl() {
if (ongoing_flow_state_->risk_data.empty()) {
payments_autofill_client().LoadRiskData(base::BindOnce(
&BnplManager::OnRiskDataLoadedAfterIssuerSelectionDialogAcceptance,
weak_factory_.GetWeakPtr()));
return;
}
FetchRedirectUrl();
}
void BnplManager::OnRiskDataLoadedAfterIssuerSelectionDialogAcceptance(
const std::string& risk_data) {
ongoing_flow_state_->risk_data = risk_data;
FetchRedirectUrl();
}
void BnplManager::FetchRedirectUrl() {
GetBnplPaymentInstrumentForFetchingUrlRequestDetails request_details;
request_details.billing_customer_number =
ongoing_flow_state_->billing_customer_number;
request_details.instrument_id = ongoing_flow_state_->instrument_id;
request_details.risk_data = ongoing_flow_state_->risk_data;
request_details.merchant_domain =
browser_autofill_manager_->client()
.GetLastCommittedPrimaryMainFrameOrigin()
.GetURL();
request_details.total_amount = ongoing_flow_state_->final_checkout_amount;
// Only `USD` is supported for MVP.
request_details.currency = "USD";
payments_autofill_client()
.GetPaymentsNetworkInterface()
->GetBnplPaymentInstrumentForFetchingUrl(
std::move(request_details),
base::BindOnce(&BnplManager::OnRedirectUrlFetched,
weak_factory_.GetWeakPtr()));
}
void BnplManager::OnRedirectUrlFetched(
PaymentsAutofillClient::PaymentsRpcResult result,
const BnplFetchUrlResponseDetails& response) {
if (ongoing_flow_state_->issuer.payment_instrument().has_value()) {
// If the BNPL issuer selected is linked, the issuer selection dialog must
// be showing, so close it.
payments_autofill_client().DismissSelectBnplIssuerDialog();
} else {
// If the BNPL issuer selected is not linked, the ToS dialog must be
// showing, so close it.
payments_autofill_client().CloseBnplTos();
}
if (result == payments::PaymentsAutofillClient::PaymentsRpcResult::kSuccess) {
ongoing_flow_state_->redirect_url = std::move(response.redirect_url);
ongoing_flow_state_->context_token = std::move(response.context_token);
PaymentsWindowManager::BnplContext payments_window_bnpl_context;
payments_window_bnpl_context.issuer_id =
ongoing_flow_state_->issuer.issuer_id();
payments_window_bnpl_context.initial_url =
ongoing_flow_state_->redirect_url;
payments_window_bnpl_context.success_url_prefix =
std::move(response.success_url_prefix);
payments_window_bnpl_context.failure_url_prefix =
std::move(response.failure_url_prefix);
payments_window_bnpl_context.completion_callback = base::BindOnce(
&BnplManager::OnPopupWindowCompleted, weak_factory_.GetWeakPtr());
payments_autofill_client().GetPaymentsWindowManager()->InitBnplFlow(
std::move(payments_window_bnpl_context));
} else {
payments_autofill_client().ShowAutofillErrorDialog(
AutofillErrorDialogContext::WithBnplPermanentOrTemporaryError(
/*is_permanent_error=*/ShouldShowPermanentErrorDialog(result)));
Reset();
}
}
void BnplManager::OnPopupWindowCompleted(
PaymentsWindowManager::BnplFlowResult result,
GURL url) {
switch (result) {
case PaymentsWindowManager::BnplFlowResult::kUserClosed:
Reset();
break;
case PaymentsWindowManager::BnplFlowResult::kSuccess:
FetchVcnDetails(std::move(url));
break;
case PaymentsWindowManager::BnplFlowResult::kFailure:
payments_autofill_client().ShowAutofillErrorDialog(
AutofillErrorDialogContext::WithBnplPermanentOrTemporaryError(
/*is_permanent_error=*/false));
Reset();
break;
}
}
void BnplManager::MaybeUpdateSuggestionsWithBnpl(
const AutofillSuggestionTriggerSource trigger_source,
std::vector<std::variant<SuggestionsShownResponse, std::optional<uint64_t>>>
responses) {
update_suggestions_barrier_callback_ = std::nullopt;
SuggestionsShownResponse* suggestions_shown_response = nullptr;
std::optional<uint64_t>* extracted_amount = nullptr;
for (auto& response : responses) {
if (std::holds_alternative<SuggestionsShownResponse>(response)) {
suggestions_shown_response =
std::get_if<SuggestionsShownResponse>(&response);
} else {
extracted_amount = std::get_if<std::optional<uint64_t>>(&response);
}
}
// TODO(crbug.com/392162610): Add protection so that this function will only
// be triggered after completion of suggestion shown and amount extraction.
// If `extracted_amount` here is a nullptr, it implies the amount extraction
// result is never received.
if (!suggestions_shown_response || !extracted_amount) {
// No need to update the suggestions if the function is called with partial
// input.
// This is not a common case and only happens when amount extraction is not
// completed and a second suggestion show is triggered without amount
// extraction.
return;
}
// If `extracted_amount` here is a nullopt, it implies an amount extraction
// result is received but the extraction is a failure.
if (!extracted_amount->has_value()) {
// No need to update the suggestions if the extracted amount is not in
// correct format or empty.
return;
}
const std::vector<BnplIssuer>& bnpl_issuers =
payments_autofill_client().GetPaymentsDataManager().GetBnplIssuers();
if (std::none_of(bnpl_issuers.begin(), bnpl_issuers.end(),
[extracted_amount](const BnplIssuer& bnpl_issuer) {
return ShouldShowBnplOptionForIssuer(
bnpl_issuer, extracted_amount->value());
})) {
// If the extracted amount is not supported by any issuer, no need to update
// the suggestion list.
if (!has_logged_bnpl_suggestion_not_shown_reason_) {
LogBnplSuggestionNotShownReason(
autofill_metrics::BnplSuggestionNotShownReason::
kCheckoutAmountNotSupported);
has_logged_bnpl_suggestion_not_shown_reason_ = true;
}
return;
}
// Append the BNPL suggestion at the end of the existing suggestion list
// (before footer items).
BnplSuggestionUpdateResult update_suggestions_result =
::autofill::MaybeUpdateSuggestionsWithBnpl(
/*current_suggestions=*/std::get<0>(*suggestions_shown_response),
bnpl_issuers, extracted_amount->value());
if (!update_suggestions_result.is_bnpl_suggestion_added) {
// No need to update the pop up, if no BNPL suggestion is added.
return;
}
// Update the pop up with BNPL suggestion entry added to the current shown
// suggestion list.
std::get<1>(*suggestions_shown_response)
.Run(update_suggestions_result.suggestions, trigger_source);
browser_autofill_manager_->GetCreditCardFormEventLogger()
.OnBnplSuggestionShown();
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \
BUILDFLAG(IS_CHROMEOS)
payments_autofill_client().GetPaymentsDataManager().SetAutofillHasSeenBnpl();
#endif // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) ||
// BUILDFLAG(IS_CHROMEOS)
}
void BnplManager::OnTosDialogAccepted() {
if (!ongoing_flow_state_->risk_data.empty()) {
CreateBnplPaymentInstrument();
return;
}
payments_autofill_client().LoadRiskData(
base::BindOnce(&BnplManager::OnRiskDataLoadedAfterTosDialogAcceptance,
weak_factory_.GetWeakPtr()));
}
void BnplManager::OnPrefetchedRiskDataLoaded(const std::string& risk_data) {
ongoing_flow_state_->risk_data = risk_data;
}
void BnplManager::OnRiskDataLoadedAfterTosDialogAcceptance(
const std::string& risk_data) {
ongoing_flow_state_->risk_data = risk_data;
CreateBnplPaymentInstrument();
}
void BnplManager::CreateBnplPaymentInstrument() {
CreateBnplPaymentInstrumentRequestDetails request_details;
request_details.app_locale = ongoing_flow_state_->app_locale;
request_details.billing_customer_number =
ongoing_flow_state_->billing_customer_number;
request_details.context_token = ongoing_flow_state_->context_token;
request_details.issuer_id = autofill::ConvertToBnplIssuerIdString(
ongoing_flow_state_->issuer.issuer_id());
request_details.risk_data = ongoing_flow_state_->risk_data;
payments_autofill_client()
.GetPaymentsNetworkInterface()
->CreateBnplPaymentInstrument(
std::move(request_details),
base::BindOnce(&BnplManager::OnBnplPaymentInstrumentCreated,
weak_factory_.GetWeakPtr()));
}
void BnplManager::OnBnplPaymentInstrumentCreated(
PaymentsAutofillClient::PaymentsRpcResult result,
std::string instrument_id) {
if (result == payments::PaymentsAutofillClient::PaymentsRpcResult::kSuccess) {
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)));
Reset();
}
}
std::vector<BnplIssuerContext> BnplManager::GetSortedBnplIssuerContext() {
AutofillOptimizationGuide* autofill_optimization_guide =
browser_autofill_manager_->client().GetAutofillOptimizationGuide();
const GURL& merchant_url = browser_autofill_manager_->client()
.GetLastCommittedPrimaryMainFrameOrigin()
.GetURL();
// Check BNPL issuer eligibility for the current page and save the
// eligibility with the corresponding issuer to the vector of
// `BnplIssuerContext`.
std::vector<BnplIssuerContext> result = base::ToVector(
payments_autofill_client().GetPaymentsDataManager().GetBnplIssuers(),
[this, &autofill_optimization_guide,
&merchant_url](const BnplIssuer& issuer) -> BnplIssuerContext {
// For MVP, BNPL will only target US users and support USD.
const base::optional_ref<const BnplIssuer::EligiblePriceRange>
price_range =
issuer.GetEligiblePriceRangeForCurrency(/*currency=*/"USD");
CHECK(price_range.has_value());
BnplIssuerEligibilityForPage eligibility;
if (!autofill_optimization_guide->IsUrlEligibleForBnplIssuer(
issuer.issuer_id(), merchant_url)) {
eligibility = BnplIssuerEligibilityForPage::
kNotEligibleIssuerDoesNotSupportMerchant;
} else if (ongoing_flow_state_->final_checkout_amount <
price_range->price_lower_bound) {
eligibility =
BnplIssuerEligibilityForPage::kNotEligibleCheckoutAmountTooLow;
} else if (ongoing_flow_state_->final_checkout_amount >
price_range->price_upper_bound) {
eligibility =
BnplIssuerEligibilityForPage::kNotEligibleCheckoutAmountTooHigh;
} else {
eligibility = BnplIssuerEligibilityForPage::kIsEligible;
}
return {issuer, eligibility};
});
// Shuffle `result` before sorting so that the order of two
// equivalently-sorted elements are randomized. This is to ensure there is no
// implicit preference towards any issuers.
base::RandomShuffle(result.begin(), result.end());
// Sort the `BnplIssuerContext` vector so that it follows below rules:
// 1. Eligible issuers should be in front of uneligible ones in a sorted
// vector.
// 2. Linked issuers must go before unlinked ones if they have the same
// eligibility.
// Note: If one issuer has a payment instrument and the other doesn't,
// then one is linked and the other is unlinked.
std::ranges::stable_sort(
result, [](const BnplIssuerContext& rhs, const BnplIssuerContext& lhs) {
// Lambda comparator which returns true if `rhs` should be in front of
// `lhs`.
// Note: Boolean value `false` is less than boolean value `true`.
return std::forward_as_tuple(
rhs.eligibility == BnplIssuerEligibilityForPage::kIsEligible,
rhs.issuer.payment_instrument().has_value()) >
std::forward_as_tuple(
lhs.eligibility == BnplIssuerEligibilityForPage::kIsEligible,
lhs.issuer.payment_instrument().has_value());
});
return result;
}
bool BnplManager::IsEligibleForBnpl() const {
AutofillOptimizationGuide* autofill_optimization_guide =
browser_autofill_manager_->client().GetAutofillOptimizationGuide();
if (!autofill_optimization_guide) {
return false;
}
const GURL& url =
browser_autofill_manager_->client().GetLastCommittedPrimaryMainFrameURL();
return std::ranges::any_of(
payments_autofill_client().GetPaymentsDataManager().GetBnplIssuers(),
[&autofill_optimization_guide, &url](const BnplIssuer& bnpl_issuer) {
return autofill_optimization_guide->IsUrlEligibleForBnplIssuer(
bnpl_issuer.issuer_id(), url);
});
}
} // namespace autofill::payments
|