| 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 16 matching lines...) Expand all Loading... |
| 60 // There can be only one FullCardRequest going on at a time. If |delegate_| is | 47 // There can be only one FullCardRequest going on at a time. If |delegate_| is |
| 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 base::string16* missing_info) { |
| 72 // empty (a server card fills this condition) and there is a cardholder name. | 59 return autofill::IsCompleteForPaymentRequest(credit_card_, app_locale_, |
| 73 // TODO(crbug.com/709776): Check for billing address association. | 60 missing_info); |
| 74 return !credit_card_.IsExpired(autofill::AutofillClock::Now()) && | |
| 75 CreditCardHasNumberAndName(credit_card_, app_locale_); | |
| 76 } | 61 } |
| 77 | 62 |
| 78 bool AutofillPaymentInstrument::IsValidForCanMakePayment() { | 63 bool AutofillPaymentInstrument::IsValidForCanMakePayment() { |
| 79 // An expired card is still valid for the purposes of canMakePayment. | 64 // An expired card is still valid for the purposes of canMakePayment. |
| 80 return CreditCardHasNumberAndName(credit_card_, app_locale_); | 65 return autofill::CreditCardHasNumberAndName(credit_card_, app_locale_); |
| 81 } | 66 } |
| 82 | 67 |
| 83 void AutofillPaymentInstrument::OnFullCardRequestSucceeded( | 68 void AutofillPaymentInstrument::OnFullCardRequestSucceeded( |
| 84 const autofill::CreditCard& card, | 69 const autofill::CreditCard& card, |
| 85 const base::string16& cvc) { | 70 const base::string16& cvc) { |
| 86 DCHECK(delegate_); | 71 DCHECK(delegate_); |
| 87 credit_card_ = card; | 72 credit_card_ = card; |
| 88 std::unique_ptr<base::DictionaryValue> response_value = | 73 std::unique_ptr<base::DictionaryValue> response_value = |
| 89 payments::data_util::GetBasicCardResponseFromAutofillCreditCard( | 74 payments::data_util::GetBasicCardResponseFromAutofillCreditCard( |
| 90 credit_card_, cvc, billing_profiles_, app_locale_) | 75 credit_card_, cvc, billing_profiles_, app_locale_) |
| 91 .ToDictionaryValue(); | 76 .ToDictionaryValue(); |
| 92 std::string stringified_details; | 77 std::string stringified_details; |
| 93 base::JSONWriter::Write(*response_value, &stringified_details); | 78 base::JSONWriter::Write(*response_value, &stringified_details); |
| 94 delegate_->OnInstrumentDetailsReady(method_name(), stringified_details); | 79 delegate_->OnInstrumentDetailsReady(method_name(), stringified_details); |
| 95 delegate_ = nullptr; | 80 delegate_ = nullptr; |
| 96 } | 81 } |
| 97 | 82 |
| 98 void AutofillPaymentInstrument::OnFullCardRequestFailed() { | 83 void AutofillPaymentInstrument::OnFullCardRequestFailed() { |
| 99 // TODO(anthonyvd): Do something with the error. | 84 // TODO(anthonyvd): Do something with the error. |
| 100 delegate_ = nullptr; | 85 delegate_ = nullptr; |
| 101 } | 86 } |
| 102 | 87 |
| 103 } // namespace payments | 88 } // namespace payments |
| OLD | NEW |