OLD | NEW |
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_data_util.h" | 10 #include "components/autofill/core/browser/autofill_data_util.h" |
11 #include "components/autofill/core/browser/autofill_type.h" | 11 #include "components/autofill/core/browser/autofill_type.h" |
12 #include "components/autofill/core/browser/field_types.h" | 12 #include "components/autofill/core/browser/field_types.h" |
| 13 #include "components/autofill/core/browser/validation.h" |
13 #include "components/autofill/core/common/autofill_clock.h" | 14 #include "components/autofill/core/common/autofill_clock.h" |
14 #include "components/payments/core/basic_card_response.h" | 15 #include "components/payments/core/basic_card_response.h" |
15 #include "components/payments/core/payment_request_data_util.h" | 16 #include "components/payments/core/payment_request_data_util.h" |
16 #include "components/payments/core/payment_request_delegate.h" | 17 #include "components/payments/core/payment_request_delegate.h" |
17 | 18 |
18 namespace payments { | 19 namespace payments { |
19 | 20 |
20 namespace { | |
21 | |
22 // Returns whether |card| has a non-empty number and cardholder name. Server | |
23 // cards will have a non-empty number. | |
24 bool CreditCardHasNumberAndName(const autofill::CreditCard& card, | |
25 const std::string& app_locale) { | |
26 return !card.number().empty() && | |
27 !card.GetInfo(autofill::AutofillType(autofill::CREDIT_CARD_NAME_FULL), | |
28 app_locale) | |
29 .empty(); | |
30 } | |
31 | |
32 } // namespace | |
33 | |
34 AutofillPaymentInstrument::AutofillPaymentInstrument( | 21 AutofillPaymentInstrument::AutofillPaymentInstrument( |
35 const std::string& method_name, | 22 const std::string& method_name, |
36 const autofill::CreditCard& card, | 23 const autofill::CreditCard& card, |
37 const std::vector<autofill::AutofillProfile*>& billing_profiles, | 24 const std::vector<autofill::AutofillProfile*>& billing_profiles, |
38 const std::string& app_locale, | 25 const std::string& app_locale, |
39 PaymentRequestDelegate* payment_request_delegate) | 26 PaymentRequestDelegate* payment_request_delegate) |
40 : PaymentInstrument( | 27 : PaymentInstrument( |
41 method_name, | 28 method_name, |
42 /* label= */ card.TypeAndLastFourDigits(), | 29 /* label= */ card.TypeAndLastFourDigits(), |
43 /* sublabel= */ | 30 /* sublabel= */ |
(...skipping 17 matching lines...) Expand all Loading... |
61 // not null, there's already an active request, which shouldn't happen. | 48 // not null, there's already an active request, which shouldn't happen. |
62 // |delegate_| is reset to nullptr when the request succeeds or fails. | 49 // |delegate_| is reset to nullptr when the request succeeds or fails. |
63 DCHECK(!delegate_); | 50 DCHECK(!delegate_); |
64 delegate_ = delegate; | 51 delegate_ = delegate; |
65 | 52 |
66 payment_request_delegate_->DoFullCardRequest(credit_card_, | 53 payment_request_delegate_->DoFullCardRequest(credit_card_, |
67 weak_ptr_factory_.GetWeakPtr()); | 54 weak_ptr_factory_.GetWeakPtr()); |
68 } | 55 } |
69 | 56 |
70 bool AutofillPaymentInstrument::IsCompleteForPayment() { | 57 bool AutofillPaymentInstrument::IsCompleteForPayment() { |
71 // A card is complete for payment if it's not expired, its number is not | 58 return autofill::GetCompletionStatusForCard(credit_card_, app_locale_) == |
72 // empty (a server card fills this condition) and there is a cardholder name. | 59 autofill::CREDIT_CARD_COMPLETE; |
73 // TODO(crbug.com/709776): Check for billing address association. | 60 } |
74 return !credit_card_.IsExpired(autofill::AutofillClock::Now()) && | 61 |
75 CreditCardHasNumberAndName(credit_card_, app_locale_); | 62 base::string16 AutofillPaymentInstrument::GetMissingInfoLabel() { |
| 63 return autofill::GetCompletionMessageForCard( |
| 64 autofill::GetCompletionStatusForCard(credit_card_, app_locale_)); |
76 } | 65 } |
77 | 66 |
78 bool AutofillPaymentInstrument::IsValidForCanMakePayment() { | 67 bool AutofillPaymentInstrument::IsValidForCanMakePayment() { |
79 // An expired card is still valid for the purposes of canMakePayment. | 68 autofill::CreditCardCompletionStatus status = |
80 return CreditCardHasNumberAndName(credit_card_, app_locale_); | 69 autofill::GetCompletionStatusForCard(credit_card_, app_locale_); |
| 70 // Card has to have a cardholder name and number for the purposes of |
| 71 // CanMakePayment. An expired card is still valid at this stage. |
| 72 return !(status & autofill::CREDIT_CARD_NO_CARDHOLDER || |
| 73 status & autofill::CREDIT_CARD_NO_NUMBER); |
81 } | 74 } |
82 | 75 |
83 void AutofillPaymentInstrument::OnFullCardRequestSucceeded( | 76 void AutofillPaymentInstrument::OnFullCardRequestSucceeded( |
84 const autofill::CreditCard& card, | 77 const autofill::CreditCard& card, |
85 const base::string16& cvc) { | 78 const base::string16& cvc) { |
86 DCHECK(delegate_); | 79 DCHECK(delegate_); |
87 credit_card_ = card; | 80 credit_card_ = card; |
88 std::unique_ptr<base::DictionaryValue> response_value = | 81 std::unique_ptr<base::DictionaryValue> response_value = |
89 payments::data_util::GetBasicCardResponseFromAutofillCreditCard( | 82 payments::data_util::GetBasicCardResponseFromAutofillCreditCard( |
90 credit_card_, cvc, billing_profiles_, app_locale_) | 83 credit_card_, cvc, billing_profiles_, app_locale_) |
91 .ToDictionaryValue(); | 84 .ToDictionaryValue(); |
92 std::string stringified_details; | 85 std::string stringified_details; |
93 base::JSONWriter::Write(*response_value, &stringified_details); | 86 base::JSONWriter::Write(*response_value, &stringified_details); |
94 delegate_->OnInstrumentDetailsReady(method_name(), stringified_details); | 87 delegate_->OnInstrumentDetailsReady(method_name(), stringified_details); |
95 delegate_ = nullptr; | 88 delegate_ = nullptr; |
96 } | 89 } |
97 | 90 |
98 void AutofillPaymentInstrument::OnFullCardRequestFailed() { | 91 void AutofillPaymentInstrument::OnFullCardRequestFailed() { |
99 // TODO(anthonyvd): Do something with the error. | 92 // TODO(anthonyvd): Do something with the error. |
100 delegate_ = nullptr; | 93 delegate_ = nullptr; |
101 } | 94 } |
102 | 95 |
103 } // namespace payments | 96 } // namespace payments |
OLD | NEW |