| 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/common/autofill_clock.h" |
| 11 #include "components/payments/core/basic_card_response.h" | 12 #include "components/payments/core/basic_card_response.h" |
| 12 #include "components/payments/core/payment_request_data_util.h" | 13 #include "components/payments/core/payment_request_data_util.h" |
| 14 #include "components/payments/core/payment_request_delegate.h" |
| 13 | 15 |
| 14 namespace payments { | 16 namespace payments { |
| 15 | 17 |
| 16 AutofillPaymentInstrument::AutofillPaymentInstrument( | 18 AutofillPaymentInstrument::AutofillPaymentInstrument( |
| 17 const std::string& method_name, | 19 const std::string& method_name, |
| 18 const autofill::CreditCard& card, | 20 const autofill::CreditCard& card, |
| 19 const std::vector<autofill::AutofillProfile*>& billing_profiles, | 21 const std::vector<autofill::AutofillProfile*>& billing_profiles, |
| 20 const std::string& app_locale) | 22 const std::string& app_locale, |
| 23 PaymentRequestDelegate* payment_request_delegate) |
| 21 : PaymentInstrument( | 24 : PaymentInstrument( |
| 22 method_name, | 25 method_name, |
| 23 /* label= */ card.TypeAndLastFourDigits(), | 26 /* label= */ card.TypeAndLastFourDigits(), |
| 24 /* sublabel= */ | 27 /* sublabel= */ |
| 25 card.GetInfo(autofill::AutofillType(autofill::CREDIT_CARD_NAME_FULL), | 28 card.GetInfo(autofill::AutofillType(autofill::CREDIT_CARD_NAME_FULL), |
| 26 app_locale), | 29 app_locale), |
| 27 autofill::data_util::GetPaymentRequestData(card.type()) | 30 autofill::data_util::GetPaymentRequestData(card.type()) |
| 28 .icon_resource_id), | 31 .icon_resource_id), |
| 29 credit_card_(card), | 32 credit_card_(card), |
| 30 billing_profiles_(billing_profiles), | 33 billing_profiles_(billing_profiles), |
| 31 app_locale_(app_locale) {} | 34 app_locale_(app_locale), |
| 35 delegate_(nullptr), |
| 36 payment_request_delegate_(payment_request_delegate), |
| 37 weak_ptr_factory_(this) {} |
| 32 AutofillPaymentInstrument::~AutofillPaymentInstrument() {} | 38 AutofillPaymentInstrument::~AutofillPaymentInstrument() {} |
| 33 | 39 |
| 34 void AutofillPaymentInstrument::InvokePaymentApp( | 40 void AutofillPaymentInstrument::InvokePaymentApp( |
| 35 PaymentInstrument::Delegate* delegate) { | 41 PaymentInstrument::Delegate* delegate) { |
| 36 DCHECK(delegate); | 42 DCHECK(delegate); |
| 37 std::string stringified_details; | 43 // There can be only one FullCardRequest going on at a time. If |delegate_| is |
| 38 // TODO(mathp): Show the CVC dialog and use the data instead of the | 44 // not null, there's already an active request, which shouldn't happen. |
| 39 // placeholder string. | 45 // |delegate_| is reset to nullptr when the request succeeds or fails. |
| 40 std::unique_ptr<base::DictionaryValue> response_value = | 46 DCHECK(!delegate_); |
| 41 payments::data_util::GetBasicCardResponseFromAutofillCreditCard( | 47 delegate_ = delegate; |
| 42 credit_card_, base::ASCIIToUTF16("123"), billing_profiles_, | 48 |
| 43 app_locale_) | 49 payment_request_delegate_->DoFullCardRequest(credit_card_, |
| 44 .ToDictionaryValue(); | 50 weak_ptr_factory_.GetWeakPtr()); |
| 45 base::JSONWriter::Write(*response_value, &stringified_details); | |
| 46 delegate->OnInstrumentDetailsReady(method_name(), stringified_details); | |
| 47 } | 51 } |
| 48 | 52 |
| 49 bool AutofillPaymentInstrument::IsValid() { | 53 bool AutofillPaymentInstrument::IsValid() { |
| 50 return credit_card_.IsValid(); | 54 return !credit_card_.IsExpired(autofill::AutofillClock::Now()); |
| 55 } |
| 56 |
| 57 void AutofillPaymentInstrument::OnFullCardRequestSucceeded( |
| 58 const autofill::CreditCard& card, |
| 59 const base::string16& cvc) { |
| 60 DCHECK(delegate_); |
| 61 credit_card_ = card; |
| 62 std::unique_ptr<base::DictionaryValue> response_value = |
| 63 payments::data_util::GetBasicCardResponseFromAutofillCreditCard( |
| 64 credit_card_, cvc, billing_profiles_, app_locale_) |
| 65 .ToDictionaryValue(); |
| 66 std::string stringified_details; |
| 67 base::JSONWriter::Write(*response_value, &stringified_details); |
| 68 delegate_->OnInstrumentDetailsReady(method_name(), stringified_details); |
| 69 delegate_ = nullptr; |
| 70 } |
| 71 |
| 72 void AutofillPaymentInstrument::OnFullCardRequestFailed() { |
| 73 // TODO(anthonyvd): Do something with the error. |
| 74 delegate_ = nullptr; |
| 51 } | 75 } |
| 52 | 76 |
| 53 } // namespace payments | 77 } // namespace payments |
| OLD | NEW |