OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/autofill/core/browser/payments/full_card_request.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_client.h" |
| 11 #include "components/autofill/core/browser/autofill_metrics.h" |
| 12 #include "components/autofill/core/browser/credit_card.h" |
| 13 #include "components/autofill/core/browser/personal_data_manager.h" |
| 14 |
| 15 namespace autofill { |
| 16 namespace payments { |
| 17 |
| 18 FullCardRequest::FullCardRequest(AutofillClient* autofill_client, |
| 19 payments::PaymentsClient* payments_client, |
| 20 PersonalDataManager* personal_data_manager) |
| 21 : autofill_client_(autofill_client), |
| 22 payments_client_(payments_client), |
| 23 personal_data_manager_(personal_data_manager), |
| 24 delegate_(nullptr), |
| 25 weak_ptr_factory_(this) { |
| 26 DCHECK(autofill_client_); |
| 27 DCHECK(personal_data_manager_); |
| 28 } |
| 29 |
| 30 FullCardRequest::~FullCardRequest() {} |
| 31 |
| 32 void FullCardRequest::GetFullCard(const CreditCard& card, |
| 33 AutofillClient::UnmaskCardReason reason, |
| 34 base::WeakPtr<Delegate> delegate) { |
| 35 DCHECK(delegate); |
| 36 |
| 37 // 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 |
| 39 // parameter |delegate|. |
| 40 if (delegate_) { |
| 41 delegate->OnFullCardError(); |
| 42 return; |
| 43 } |
| 44 |
| 45 delegate_ = delegate; |
| 46 request_.reset(new payments::PaymentsClient::UnmaskRequestDetails); |
| 47 request_->card = card; |
| 48 bool is_masked = card.record_type() == CreditCard::MASKED_SERVER_CARD; |
| 49 if (is_masked) |
| 50 payments_client_->Prepare(); |
| 51 |
| 52 autofill_client_->ShowUnmaskPrompt(request_->card, reason, |
| 53 weak_ptr_factory_.GetWeakPtr()); |
| 54 |
| 55 if (is_masked) { |
| 56 autofill_client_->LoadRiskData( |
| 57 base::Bind(&FullCardRequest::OnDidGetUnmaskRiskData, |
| 58 weak_ptr_factory_.GetWeakPtr())); |
| 59 } |
| 60 } |
| 61 |
| 62 bool FullCardRequest::IsGettingFullCard() const { |
| 63 return !!request_; |
| 64 } |
| 65 |
| 66 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()) |
| 70 request_->card.SetRawInfo(CREDIT_CARD_EXP_MONTH, response.exp_month); |
| 71 |
| 72 if (!response.exp_year.empty()) |
| 73 request_->card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, response.exp_year); |
| 74 |
| 75 if (request_->card.record_type() != CreditCard::MASKED_SERVER_CARD) { |
| 76 if (delegate_) |
| 77 delegate_->OnFullCardDetails(request_->card, response.cvc); |
| 78 delegate_ = nullptr; |
| 79 autofill_client_->OnUnmaskVerificationResult(AutofillClient::SUCCESS); |
| 80 request_.reset(); |
| 81 return; |
| 82 } |
| 83 |
| 84 request_->user_response = response; |
| 85 if (!request_->risk_data.empty()) { |
| 86 real_pan_request_timestamp_ = base::Time::Now(); |
| 87 payments_client_->UnmaskCard(*request_); |
| 88 } |
| 89 } |
| 90 |
| 91 void FullCardRequest::OnUnmaskPromptClosed() { |
| 92 if (delegate_) |
| 93 delegate_->OnFullCardError(); |
| 94 |
| 95 delegate_ = nullptr; |
| 96 payments_client_->CancelRequest(); |
| 97 request_.reset(); |
| 98 } |
| 99 |
| 100 void FullCardRequest::OnDidGetUnmaskRiskData(const std::string& risk_data) { |
| 101 request_->risk_data = risk_data; |
| 102 if (!request_->user_response.cvc.empty()) { |
| 103 real_pan_request_timestamp_ = base::Time::Now(); |
| 104 payments_client_->UnmaskCard(*request_); |
| 105 } |
| 106 } |
| 107 |
| 108 void FullCardRequest::OnDidGetRealPan(AutofillClient::PaymentsRpcResult result, |
| 109 const std::string& real_pan) { |
| 110 AutofillMetrics::LogRealPanDuration( |
| 111 base::Time::Now() - real_pan_request_timestamp_, result); |
| 112 |
| 113 if (!real_pan.empty()) { |
| 114 DCHECK_EQ(AutofillClient::SUCCESS, result); |
| 115 request_->card.set_record_type(CreditCard::FULL_SERVER_CARD); |
| 116 request_->card.SetNumber(base::UTF8ToUTF16(real_pan)); |
| 117 |
| 118 if (request_->user_response.should_store_pan) |
| 119 personal_data_manager_->UpdateServerCreditCard(request_->card); |
| 120 |
| 121 if (delegate_) |
| 122 delegate_->OnFullCardDetails(request_->card, request_->user_response.cvc); |
| 123 } else { |
| 124 if (delegate_) |
| 125 delegate_->OnFullCardError(); |
| 126 } |
| 127 |
| 128 delegate_ = nullptr; |
| 129 autofill_client_->OnUnmaskVerificationResult(result); |
| 130 request_.reset(); |
| 131 } |
| 132 |
| 133 } // namespace payments |
| 134 } // namespace autofill |
OLD | NEW |