| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/autofill/core/browser/payments/full_card_request.h" | 5 #include "components/autofill/core/browser/payments/full_card_request.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/time/time.h" |
| 10 #include "components/autofill/core/browser/autofill_client.h" | 11 #include "components/autofill/core/browser/autofill_client.h" |
| 11 #include "components/autofill/core/browser/autofill_metrics.h" | 12 #include "components/autofill/core/browser/autofill_metrics.h" |
| 12 #include "components/autofill/core/browser/credit_card.h" | 13 #include "components/autofill/core/browser/credit_card.h" |
| 13 #include "components/autofill/core/browser/personal_data_manager.h" | 14 #include "components/autofill/core/browser/personal_data_manager.h" |
| 14 | 15 |
| 15 namespace autofill { | 16 namespace autofill { |
| 16 namespace payments { | 17 namespace payments { |
| 17 | 18 |
| 18 FullCardRequest::FullCardRequest(AutofillClient* autofill_client, | 19 FullCardRequest::FullCardRequest(AutofillClient* autofill_client, |
| 19 payments::PaymentsClient* payments_client, | 20 payments::PaymentsClient* payments_client, |
| 20 PersonalDataManager* personal_data_manager) | 21 PersonalDataManager* personal_data_manager) |
| 21 : autofill_client_(autofill_client), | 22 : autofill_client_(autofill_client), |
| 22 payments_client_(payments_client), | 23 payments_client_(payments_client), |
| 23 personal_data_manager_(personal_data_manager), | 24 personal_data_manager_(personal_data_manager), |
| 24 delegate_(nullptr), | 25 delegate_(nullptr), |
| 26 should_unmask_card_(false), |
| 25 weak_ptr_factory_(this) { | 27 weak_ptr_factory_(this) { |
| 26 DCHECK(autofill_client_); | 28 DCHECK(autofill_client_); |
| 29 DCHECK(payments_client_); |
| 27 DCHECK(personal_data_manager_); | 30 DCHECK(personal_data_manager_); |
| 28 } | 31 } |
| 29 | 32 |
| 30 FullCardRequest::~FullCardRequest() {} | 33 FullCardRequest::~FullCardRequest() {} |
| 31 | 34 |
| 32 void FullCardRequest::GetFullCard(const CreditCard& card, | 35 void FullCardRequest::GetFullCard(const CreditCard& card, |
| 33 AutofillClient::UnmaskCardReason reason, | 36 AutofillClient::UnmaskCardReason reason, |
| 34 base::WeakPtr<Delegate> delegate) { | 37 base::WeakPtr<Delegate> delegate) { |
| 35 DCHECK(delegate); | 38 DCHECK(delegate); |
| 36 | 39 |
| 37 // Only request can be active a time. If the member variable |delegate_| is | 40 // Only request can be active a time. If the member variable |delegate_| is |
| 38 // already set, then immediately reject the new request through the method | 41 // already set, then immediately reject the new request through the method |
| 39 // parameter |delegate|. | 42 // parameter |delegate|. |
| 40 if (delegate_) { | 43 if (delegate_) { |
| 41 delegate->OnFullCardError(); | 44 delegate->OnFullCardError(); |
| 42 return; | 45 return; |
| 43 } | 46 } |
| 44 | 47 |
| 45 delegate_ = delegate; | 48 delegate_ = delegate; |
| 46 request_.reset(new payments::PaymentsClient::UnmaskRequestDetails); | 49 request_.reset(new payments::PaymentsClient::UnmaskRequestDetails); |
| 47 request_->card = card; | 50 request_->card = card; |
| 48 bool is_masked = card.record_type() == CreditCard::MASKED_SERVER_CARD; | 51 should_unmask_card_ = card.record_type() == CreditCard::MASKED_SERVER_CARD || |
| 49 if (is_masked) | 52 (card.record_type() == CreditCard::FULL_SERVER_CARD && |
| 53 card.ShouldUpdateExpiration(base::Time::Now())); |
| 54 if (should_unmask_card_) |
| 50 payments_client_->Prepare(); | 55 payments_client_->Prepare(); |
| 51 | 56 |
| 52 autofill_client_->ShowUnmaskPrompt(request_->card, reason, | 57 autofill_client_->ShowUnmaskPrompt(request_->card, reason, |
| 53 weak_ptr_factory_.GetWeakPtr()); | 58 weak_ptr_factory_.GetWeakPtr()); |
| 54 | 59 |
| 55 if (is_masked) { | 60 if (should_unmask_card_) { |
| 56 autofill_client_->LoadRiskData( | 61 autofill_client_->LoadRiskData( |
| 57 base::Bind(&FullCardRequest::OnDidGetUnmaskRiskData, | 62 base::Bind(&FullCardRequest::OnDidGetUnmaskRiskData, |
| 58 weak_ptr_factory_.GetWeakPtr())); | 63 weak_ptr_factory_.GetWeakPtr())); |
| 59 } | 64 } |
| 60 } | 65 } |
| 61 | 66 |
| 62 bool FullCardRequest::IsGettingFullCard() const { | 67 bool FullCardRequest::IsGettingFullCard() const { |
| 63 return !!request_; | 68 return !!request_; |
| 64 } | 69 } |
| 65 | 70 |
| 66 void FullCardRequest::OnUnmaskResponse(const UnmaskResponse& response) { | 71 void FullCardRequest::OnUnmaskResponse(const UnmaskResponse& response) { |
| 67 // TODO(rouslan): Update the expiration date of the card on disk. | |
| 68 // http://crbug.com/606008 | |
| 69 if (!response.exp_month.empty()) | 72 if (!response.exp_month.empty()) |
| 70 request_->card.SetRawInfo(CREDIT_CARD_EXP_MONTH, response.exp_month); | 73 request_->card.SetRawInfo(CREDIT_CARD_EXP_MONTH, response.exp_month); |
| 71 | 74 |
| 72 if (!response.exp_year.empty()) | 75 if (!response.exp_year.empty()) |
| 73 request_->card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, response.exp_year); | 76 request_->card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, response.exp_year); |
| 74 | 77 |
| 75 if (request_->card.record_type() != CreditCard::MASKED_SERVER_CARD) { | 78 if (request_->card.record_type() == CreditCard::LOCAL_CARD && |
| 79 (!response.exp_month.empty() || !response.exp_year.empty())) { |
| 80 personal_data_manager_->UpdateCreditCard(request_->card); |
| 81 } |
| 82 |
| 83 if (!should_unmask_card_) { |
| 76 if (delegate_) | 84 if (delegate_) |
| 77 delegate_->OnFullCardDetails(request_->card, response.cvc); | 85 delegate_->OnFullCardDetails(request_->card, response.cvc); |
| 78 delegate_ = nullptr; | 86 Reset(); |
| 79 autofill_client_->OnUnmaskVerificationResult(AutofillClient::SUCCESS); | 87 autofill_client_->OnUnmaskVerificationResult(AutofillClient::SUCCESS); |
| 80 request_.reset(); | |
| 81 return; | 88 return; |
| 82 } | 89 } |
| 83 | 90 |
| 84 request_->user_response = response; | 91 request_->user_response = response; |
| 85 if (!request_->risk_data.empty()) { | 92 if (!request_->risk_data.empty()) { |
| 86 real_pan_request_timestamp_ = base::Time::Now(); | 93 real_pan_request_timestamp_ = base::Time::Now(); |
| 87 payments_client_->UnmaskCard(*request_); | 94 payments_client_->UnmaskCard(*request_); |
| 88 } | 95 } |
| 89 } | 96 } |
| 90 | 97 |
| 91 void FullCardRequest::OnUnmaskPromptClosed() { | 98 void FullCardRequest::OnUnmaskPromptClosed() { |
| 92 if (delegate_) | 99 if (delegate_) |
| 93 delegate_->OnFullCardError(); | 100 delegate_->OnFullCardError(); |
| 94 | 101 |
| 95 delegate_ = nullptr; | 102 Reset(); |
| 96 payments_client_->CancelRequest(); | 103 payments_client_->CancelRequest(); |
| 97 request_.reset(); | |
| 98 } | 104 } |
| 99 | 105 |
| 100 void FullCardRequest::OnDidGetUnmaskRiskData(const std::string& risk_data) { | 106 void FullCardRequest::OnDidGetUnmaskRiskData(const std::string& risk_data) { |
| 101 request_->risk_data = risk_data; | 107 request_->risk_data = risk_data; |
| 102 if (!request_->user_response.cvc.empty()) { | 108 if (!request_->user_response.cvc.empty()) { |
| 103 real_pan_request_timestamp_ = base::Time::Now(); | 109 real_pan_request_timestamp_ = base::Time::Now(); |
| 104 payments_client_->UnmaskCard(*request_); | 110 payments_client_->UnmaskCard(*request_); |
| 105 } | 111 } |
| 106 } | 112 } |
| 107 | 113 |
| 108 void FullCardRequest::OnDidGetRealPan(AutofillClient::PaymentsRpcResult result, | 114 void FullCardRequest::OnDidGetRealPan(AutofillClient::PaymentsRpcResult result, |
| 109 const std::string& real_pan) { | 115 const std::string& real_pan) { |
| 110 AutofillMetrics::LogRealPanDuration( | 116 AutofillMetrics::LogRealPanDuration( |
| 111 base::Time::Now() - real_pan_request_timestamp_, result); | 117 base::Time::Now() - real_pan_request_timestamp_, result); |
| 112 | 118 |
| 113 if (!real_pan.empty()) { | 119 if (!real_pan.empty()) { |
| 114 DCHECK_EQ(AutofillClient::SUCCESS, result); | 120 DCHECK_EQ(AutofillClient::SUCCESS, result); |
| 115 request_->card.set_record_type(CreditCard::FULL_SERVER_CARD); | 121 request_->card.set_record_type(CreditCard::FULL_SERVER_CARD); |
| 116 request_->card.SetNumber(base::UTF8ToUTF16(real_pan)); | 122 request_->card.SetNumber(base::UTF8ToUTF16(real_pan)); |
| 123 request_->card.SetServerStatus(CreditCard::OK); |
| 117 | 124 |
| 118 if (request_->user_response.should_store_pan) | 125 if (request_->user_response.should_store_pan) |
| 119 personal_data_manager_->UpdateServerCreditCard(request_->card); | 126 personal_data_manager_->UpdateServerCreditCard(request_->card); |
| 120 | 127 |
| 121 if (delegate_) | 128 if (delegate_) |
| 122 delegate_->OnFullCardDetails(request_->card, request_->user_response.cvc); | 129 delegate_->OnFullCardDetails(request_->card, request_->user_response.cvc); |
| 123 } else { | 130 } else { |
| 124 if (delegate_) | 131 if (delegate_) |
| 125 delegate_->OnFullCardError(); | 132 delegate_->OnFullCardError(); |
| 126 } | 133 } |
| 127 | 134 |
| 135 Reset(); |
| 136 autofill_client_->OnUnmaskVerificationResult(result); |
| 137 } |
| 138 |
| 139 void FullCardRequest::Reset() { |
| 128 delegate_ = nullptr; | 140 delegate_ = nullptr; |
| 129 autofill_client_->OnUnmaskVerificationResult(result); | |
| 130 request_.reset(); | 141 request_.reset(); |
| 142 should_unmask_card_ = false; |
| 131 } | 143 } |
| 132 | 144 |
| 133 } // namespace payments | 145 } // namespace payments |
| 134 } // namespace autofill | 146 } // namespace autofill |
| OLD | NEW |