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