mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 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/payments/payment_request.h" |
| 6 | |
tmartino | 68c0a27 | 2017-01-19 17:44:08 | [diff] [blame] | 7 | #include "base/memory/ptr_util.h" |
anthonyvd | 045303a | 2017-01-17 22:19:19 | [diff] [blame] | 8 | #include "components/autofill/core/browser/personal_data_manager.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 9 | #include "components/payments/payment_details_validation.h" |
| 10 | #include "components/payments/payment_request_delegate.h" |
| 11 | #include "components/payments/payment_request_web_contents_manager.h" |
| 12 | #include "content/public/browser/browser_thread.h" |
| 13 | #include "content/public/browser/web_contents.h" |
| 14 | |
| 15 | namespace payments { |
| 16 | |
| 17 | PaymentRequest::PaymentRequest( |
| 18 | content::WebContents* web_contents, |
| 19 | std::unique_ptr<PaymentRequestDelegate> delegate, |
| 20 | PaymentRequestWebContentsManager* manager, |
| 21 | mojo::InterfaceRequest<payments::mojom::PaymentRequest> request) |
| 22 | : web_contents_(web_contents), |
| 23 | delegate_(std::move(delegate)), |
| 24 | manager_(manager), |
| 25 | binding_(this, std::move(request)) { |
| 26 | binding_.set_connection_error_handler( |
| 27 | base::Bind(&PaymentRequest::OnError, base::Unretained(this))); |
| 28 | } |
| 29 | |
| 30 | PaymentRequest::~PaymentRequest() {} |
| 31 | |
| 32 | void PaymentRequest::Init( |
| 33 | payments::mojom::PaymentRequestClientPtr client, |
| 34 | std::vector<payments::mojom::PaymentMethodDataPtr> methodData, |
| 35 | payments::mojom::PaymentDetailsPtr details, |
| 36 | payments::mojom::PaymentOptionsPtr options) { |
| 37 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 38 | std::string error; |
| 39 | if (!payments::validatePaymentDetails(details, &error)) { |
| 40 | LOG(ERROR) << error; |
| 41 | OnError(); |
tmartino | 8ce92285 | 2017-01-09 22:23:10 | [diff] [blame] | 42 | client_.reset(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 43 | return; |
| 44 | } |
| 45 | client_ = std::move(client); |
| 46 | details_ = std::move(details); |
| 47 | } |
| 48 | |
| 49 | void PaymentRequest::Show() { |
tmartino | 8ce92285 | 2017-01-09 22:23:10 | [diff] [blame] | 50 | if (!client_.is_bound() || !binding_.is_bound()) { |
| 51 | OnError(); |
| 52 | return; |
| 53 | } |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 54 | delegate_->ShowPaymentRequestDialog(this); |
| 55 | } |
| 56 | |
| 57 | void PaymentRequest::Cancel() { |
| 58 | client_->OnError(payments::mojom::PaymentErrorReason::USER_CANCEL); |
| 59 | } |
| 60 | |
| 61 | void PaymentRequest::OnError() { |
| 62 | binding_.Close(); |
| 63 | manager_->DestroyRequest(this); |
| 64 | } |
| 65 | |
mathp | 6758be03 | 2017-01-13 04:49:50 | [diff] [blame] | 66 | CurrencyFormatter* PaymentRequest::GetOrCreateCurrencyFormatter( |
| 67 | const std::string& currency_code, |
| 68 | const base::Optional<std::string> currency_system, |
| 69 | const std::string& locale_name) { |
| 70 | if (!currency_formatter_) { |
| 71 | currency_formatter_.reset( |
| 72 | new CurrencyFormatter(currency_code, currency_system, locale_name)); |
| 73 | } |
| 74 | |
| 75 | return currency_formatter_.get(); |
| 76 | } |
| 77 | |
tmartino | 68c0a27 | 2017-01-19 17:44:08 | [diff] [blame] | 78 | autofill::AutofillProfile* PaymentRequest::GetCurrentlySelectedProfile() { |
| 79 | // TODO(tmartino): Implement more sophisticated algorithm for populating |
| 80 | // this when it starts empty. |
| 81 | if (!profile_) { |
| 82 | autofill::PersonalDataManager* data_manager = |
| 83 | delegate_->GetPersonalDataManager(); |
| 84 | auto profiles = data_manager->GetProfiles(); |
| 85 | if (!profiles.empty()) |
| 86 | profile_ = base::MakeUnique<autofill::AutofillProfile>(*profiles[0]); |
| 87 | } |
| 88 | return profile_ ? profile_.get() : nullptr; |
| 89 | } |
| 90 | |
anthonyvd | 045303a | 2017-01-17 22:19:19 | [diff] [blame] | 91 | autofill::CreditCard* PaymentRequest::GetCurrentlySelectedCreditCard() { |
| 92 | // TODO(anthonyvd): Change this code to prioritize server cards and implement |
| 93 | // a way to modify this function's return value. |
| 94 | autofill::PersonalDataManager* data_manager = |
| 95 | delegate_->GetPersonalDataManager(); |
| 96 | |
| 97 | const std::vector<autofill::CreditCard*> cards = |
| 98 | data_manager->GetCreditCardsToSuggest(); |
| 99 | |
| 100 | auto first_complete_card = std::find_if( |
| 101 | cards.begin(), |
| 102 | cards.end(), |
| 103 | [] (autofill::CreditCard* card) { |
| 104 | return card->IsValid(); |
| 105 | }); |
| 106 | |
| 107 | return first_complete_card == cards.end() ? nullptr : *first_complete_card; |
| 108 | } |
| 109 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 110 | } // namespace payments |