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

Side by Side Diff: components/payments/core/autofill_payment_instrument.cc

Issue 2842463002: [Payments] Normalize billing address for response on Desktop. (Closed)
Patch Set: Addressed moe's comments Created 3 years, 7 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 unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/payments/core/autofill_payment_instrument.h" 5 #include "components/payments/core/autofill_payment_instrument.h"
6 6
7 #include "base/json/json_writer.h" 7 #include "base/json/json_writer.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "components/autofill/core/browser/autofill_country.h"
10 #include "components/autofill/core/browser/autofill_data_util.h" 11 #include "components/autofill/core/browser/autofill_data_util.h"
11 #include "components/autofill/core/browser/autofill_type.h" 12 #include "components/autofill/core/browser/autofill_type.h"
12 #include "components/autofill/core/browser/field_types.h" 13 #include "components/autofill/core/browser/field_types.h"
14 #include "components/autofill/core/browser/personal_data_manager.h"
13 #include "components/autofill/core/browser/validation.h" 15 #include "components/autofill/core/browser/validation.h"
14 #include "components/autofill/core/common/autofill_clock.h" 16 #include "components/autofill/core/common/autofill_clock.h"
15 #include "components/payments/core/basic_card_response.h" 17 #include "components/payments/core/basic_card_response.h"
16 #include "components/payments/core/payment_request_data_util.h" 18 #include "components/payments/core/payment_request_data_util.h"
17 #include "components/payments/core/payment_request_delegate.h" 19 #include "components/payments/core/payment_request_delegate.h"
18 20
19 namespace payments { 21 namespace payments {
20 22
21 AutofillPaymentInstrument::AutofillPaymentInstrument( 23 AutofillPaymentInstrument::AutofillPaymentInstrument(
22 const std::string& method_name, 24 const std::string& method_name,
(...skipping 20 matching lines...) Expand all
43 45
44 void AutofillPaymentInstrument::InvokePaymentApp( 46 void AutofillPaymentInstrument::InvokePaymentApp(
45 PaymentInstrument::Delegate* delegate) { 47 PaymentInstrument::Delegate* delegate) {
46 DCHECK(delegate); 48 DCHECK(delegate);
47 // There can be only one FullCardRequest going on at a time. If |delegate_| is 49 // There can be only one FullCardRequest going on at a time. If |delegate_| is
48 // not null, there's already an active request, which shouldn't happen. 50 // not null, there's already an active request, which shouldn't happen.
49 // |delegate_| is reset to nullptr when the request succeeds or fails. 51 // |delegate_| is reset to nullptr when the request succeeds or fails.
50 DCHECK(!delegate_); 52 DCHECK(!delegate_);
51 delegate_ = delegate; 53 delegate_ = delegate;
52 54
55 // Get the billing address.
56 // TODO(crbug.com/709776): Make sure the billing address is valid before
57 // getting here.
58 if (!credit_card_.billing_address_id().empty()) {
59 autofill::AutofillProfile* billing_address =
60 autofill::PersonalDataManager::GetProfileFromProfilesByGUID(
61 credit_card_.billing_address_id(), billing_profiles_);
62 if (billing_address)
63 billing_address_ = *billing_address;
64 }
65
66 is_waiting_for_billing_address_normalization_ = true;
67 is_waiting_for_card_unmask_ = true;
68
69 // Start the normalization of the billing address.
70 // Use the country code from the profile if it is set, otherwise infer it
71 // from the |app_locale_|.
72 std::string country_code = base::UTF16ToUTF8(
73 billing_address_.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY));
74 if (!autofill::data_util::IsValidCountryCode(country_code)) {
75 country_code = autofill::AutofillCountry::CountryCodeForLocale(app_locale_);
76 }
77 payment_request_delegate_->GetAddressNormalizer()->StartAddressNormalization(
78 billing_address_, country_code, /*timeout_seconds=*/5, this);
79
53 payment_request_delegate_->DoFullCardRequest(credit_card_, 80 payment_request_delegate_->DoFullCardRequest(credit_card_,
54 weak_ptr_factory_.GetWeakPtr()); 81 weak_ptr_factory_.GetWeakPtr());
55 } 82 }
56 83
57 bool AutofillPaymentInstrument::IsCompleteForPayment() { 84 bool AutofillPaymentInstrument::IsCompleteForPayment() {
58 return autofill::GetCompletionStatusForCard(credit_card_, app_locale_) == 85 return autofill::GetCompletionStatusForCard(credit_card_, app_locale_) ==
59 autofill::CREDIT_CARD_COMPLETE; 86 autofill::CREDIT_CARD_COMPLETE;
60 } 87 }
61 88
62 base::string16 AutofillPaymentInstrument::GetMissingInfoLabel() { 89 base::string16 AutofillPaymentInstrument::GetMissingInfoLabel() {
63 return autofill::GetCompletionMessageForCard( 90 return autofill::GetCompletionMessageForCard(
64 autofill::GetCompletionStatusForCard(credit_card_, app_locale_)); 91 autofill::GetCompletionStatusForCard(credit_card_, app_locale_));
65 } 92 }
66 93
67 bool AutofillPaymentInstrument::IsValidForCanMakePayment() { 94 bool AutofillPaymentInstrument::IsValidForCanMakePayment() {
68 autofill::CreditCardCompletionStatus status = 95 autofill::CreditCardCompletionStatus status =
69 autofill::GetCompletionStatusForCard(credit_card_, app_locale_); 96 autofill::GetCompletionStatusForCard(credit_card_, app_locale_);
70 // Card has to have a cardholder name and number for the purposes of 97 // Card has to have a cardholder name and number for the purposes of
71 // CanMakePayment. An expired card is still valid at this stage. 98 // CanMakePayment. An expired card is still valid at this stage.
72 return !(status & autofill::CREDIT_CARD_NO_CARDHOLDER || 99 return !(status & autofill::CREDIT_CARD_NO_CARDHOLDER ||
73 status & autofill::CREDIT_CARD_NO_NUMBER); 100 status & autofill::CREDIT_CARD_NO_NUMBER);
74 } 101 }
75 102
76 void AutofillPaymentInstrument::OnFullCardRequestSucceeded( 103 void AutofillPaymentInstrument::OnFullCardRequestSucceeded(
77 const autofill::CreditCard& card, 104 const autofill::CreditCard& card,
78 const base::string16& cvc) { 105 const base::string16& cvc) {
79 DCHECK(delegate_); 106 DCHECK(delegate_);
80 credit_card_ = card; 107 credit_card_ = card;
81 std::unique_ptr<base::DictionaryValue> response_value = 108 cvc_ = cvc;
82 payments::data_util::GetBasicCardResponseFromAutofillCreditCard( 109 is_waiting_for_card_unmask_ = false;
83 credit_card_, cvc, billing_profiles_, app_locale_) 110
84 .ToDictionaryValue(); 111 if (!is_waiting_for_billing_address_normalization_)
85 std::string stringified_details; 112 GenerateBasicCardResponse();
86 base::JSONWriter::Write(*response_value, &stringified_details);
87 delegate_->OnInstrumentDetailsReady(method_name(), stringified_details);
88 delegate_ = nullptr;
89 } 113 }
90 114
91 void AutofillPaymentInstrument::OnFullCardRequestFailed() { 115 void AutofillPaymentInstrument::OnFullCardRequestFailed() {
92 // TODO(anthonyvd): Do something with the error. 116 // TODO(anthonyvd): Do something with the error.
93 delegate_ = nullptr; 117 delegate_ = nullptr;
94 } 118 }
95 119
120 void AutofillPaymentInstrument::OnAddressNormalized(
121 const autofill::AutofillProfile& normalized_profile) {
122 DCHECK(is_waiting_for_billing_address_normalization_);
123
124 billing_address_ = normalized_profile;
125 is_waiting_for_billing_address_normalization_ = false;
126
127 if (!is_waiting_for_card_unmask_)
128 GenerateBasicCardResponse();
129 }
130
131 void AutofillPaymentInstrument::OnCouldNotNormalize(
132 const autofill::AutofillProfile& profile) {
133 // Since the phone number is formatted in either case, this profile should be
134 // used.
135 OnAddressNormalized(profile);
136 }
137
138 void AutofillPaymentInstrument::GenerateBasicCardResponse() {
139 DCHECK(!is_waiting_for_billing_address_normalization_);
140 DCHECK(!is_waiting_for_card_unmask_);
141 DCHECK(delegate_);
142
143 std::unique_ptr<base::DictionaryValue> response_value =
144 payments::data_util::GetBasicCardResponseFromAutofillCreditCard(
145 credit_card_, cvc_, billing_address_, app_locale_)
146 .ToDictionaryValue();
147 std::string stringified_details;
148 base::JSONWriter::Write(*response_value, &stringified_details);
149 delegate_->OnInstrumentDetailsReady(method_name(), stringified_details);
150
151 delegate_ = nullptr;
152 cvc_ = base::UTF8ToUTF16("");
153 }
154
96 } // namespace payments 155 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698