Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(513)

Unified Diff: components/payments/core/autofill_payment_instrument.cc

Issue 2842463002: [Payments] Normalize billing address for response on Desktop. (Closed)
Patch Set: Keep the old version for iOS Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..590fc74d5590a310968eb3731a4324d4a2fb962a 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/602666): Ensure we reach here only if the card has a billing
Mathieu 2017/04/24 19:42:09 TODO(crbug.com/709776): Make sure the billing addr
sebsg 2017/04/25 17:36:43 Done.
+ // address. Then add DCHECK(!card->billing_address_id().empty()).
+ if (!credit_card_.billing_address_id().empty()) {
+ autofill::AutofillProfile* billing_address =
+ autofill::PersonalDataManager::GetProfileFromProfilesByGUID(
+ credit_card_.billing_address_id(), billing_profiles_);
+ DCHECK(billing_address);
Mathieu 2017/04/24 19:42:09 as discussed this morning let's avoid a crash and
sebsg 2017/04/25 17:36:44 Done.
+ 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,53 @@ 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) {
+ if (is_waiting_for_billing_address_normalization_) {
Mathieu 2017/04/24 19:42:09 DCHECK?
sebsg 2017/04/25 17:36:43 Done.
+ 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

Powered by Google App Engine
This is Rietveld 408576698