Index: components/payments/core/autofill_payment_instrument.cc |
diff --git a/components/payments/core/autofill_payment_instrument.cc b/components/payments/core/autofill_payment_instrument.cc |
index 04017e439f2e6ff936e4f0ad5631e5f5319798dc..e74b3892c589d144edd00bd205dbdfd57c92e12c 100644 |
--- a/components/payments/core/autofill_payment_instrument.cc |
+++ b/components/payments/core/autofill_payment_instrument.cc |
@@ -7,9 +7,11 @@ |
#include "base/json/json_writer.h" |
#include "base/strings/utf_string_conversions.h" |
#include "base/values.h" |
+#include "components/autofill/core/browser/autofill_country.h" |
#include "components/autofill/core/browser/autofill_data_util.h" |
#include "components/autofill/core/browser/autofill_type.h" |
#include "components/autofill/core/browser/field_types.h" |
+#include "components/autofill/core/browser/personal_data_manager.h" |
#include "components/autofill/core/browser/validation.h" |
#include "components/autofill/core/common/autofill_clock.h" |
#include "components/payments/core/basic_card_response.h" |
@@ -50,6 +52,31 @@ void AutofillPaymentInstrument::InvokePaymentApp( |
DCHECK(!delegate_); |
delegate_ = delegate; |
+ // Get the billing address. |
+ // TODO(crbug.com/709776): Make sure the billing address is valid before |
+ // getting here. |
+ if (!credit_card_.billing_address_id().empty()) { |
+ autofill::AutofillProfile* billing_address = |
+ autofill::PersonalDataManager::GetProfileFromProfilesByGUID( |
+ credit_card_.billing_address_id(), billing_profiles_); |
+ if (billing_address) |
+ billing_address_ = *billing_address; |
+ } |
+ |
+ is_waiting_for_billing_address_normalization_ = true; |
+ is_waiting_for_card_unmask_ = true; |
+ |
+ // Start the normalization of the billing address. |
+ // Use the country code from the profile if it is set, otherwise infer it |
+ // from the |app_locale_|. |
+ std::string country_code = base::UTF16ToUTF8( |
+ billing_address_.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)); |
+ if (!autofill::data_util::IsValidCountryCode(country_code)) { |
+ country_code = autofill::AutofillCountry::CountryCodeForLocale(app_locale_); |
+ } |
+ payment_request_delegate_->GetAddressNormalizer()->StartAddressNormalization( |
+ billing_address_, country_code, /*timeout_seconds=*/5, this); |
+ |
payment_request_delegate_->DoFullCardRequest(credit_card_, |
weak_ptr_factory_.GetWeakPtr()); |
} |
@@ -78,19 +105,51 @@ void AutofillPaymentInstrument::OnFullCardRequestSucceeded( |
const base::string16& cvc) { |
DCHECK(delegate_); |
credit_card_ = card; |
+ cvc_ = cvc; |
+ is_waiting_for_card_unmask_ = false; |
+ |
+ if (!is_waiting_for_billing_address_normalization_) |
+ GenerateBasicCardResponse(); |
+} |
+ |
+void AutofillPaymentInstrument::OnFullCardRequestFailed() { |
+ // TODO(anthonyvd): Do something with the error. |
+ delegate_ = nullptr; |
+} |
+ |
+void AutofillPaymentInstrument::OnAddressNormalized( |
+ const autofill::AutofillProfile& normalized_profile) { |
+ DCHECK(is_waiting_for_billing_address_normalization_); |
+ |
+ billing_address_ = normalized_profile; |
+ is_waiting_for_billing_address_normalization_ = false; |
+ |
+ if (!is_waiting_for_card_unmask_) |
+ GenerateBasicCardResponse(); |
+} |
+ |
+void AutofillPaymentInstrument::OnCouldNotNormalize( |
+ const autofill::AutofillProfile& profile) { |
+ // Since the phone number is formatted in either case, this profile should be |
+ // used. |
+ OnAddressNormalized(profile); |
+} |
+ |
+void AutofillPaymentInstrument::GenerateBasicCardResponse() { |
+ DCHECK(!is_waiting_for_billing_address_normalization_); |
+ DCHECK(!is_waiting_for_card_unmask_); |
+ DCHECK(delegate_); |
+ |
std::unique_ptr<base::DictionaryValue> response_value = |
payments::data_util::GetBasicCardResponseFromAutofillCreditCard( |
- credit_card_, cvc, billing_profiles_, app_locale_) |
+ credit_card_, cvc_, billing_address_, app_locale_) |
.ToDictionaryValue(); |
std::string stringified_details; |
base::JSONWriter::Write(*response_value, &stringified_details); |
delegate_->OnInstrumentDetailsReady(method_name(), stringified_details); |
- delegate_ = nullptr; |
-} |
-void AutofillPaymentInstrument::OnFullCardRequestFailed() { |
- // TODO(anthonyvd): Do something with the error. |
delegate_ = nullptr; |
+ cvc_ = base::UTF8ToUTF16(""); |
} |
} // namespace payments |