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