blob: 282b3fa38afad3d8ef83f6db57f9f05c532ca0ad [file] [log] [blame]
mathpf709499d2017-01-09 20:48:361// 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
tmartino68c0a272017-01-19 17:44:087#include "base/memory/ptr_util.h"
anthonyvd045303a2017-01-17 22:19:198#include "components/autofill/core/browser/personal_data_manager.h"
mathpf709499d2017-01-09 20:48:369#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
15namespace payments {
16
17PaymentRequest::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
30PaymentRequest::~PaymentRequest() {}
31
32void 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();
tmartino8ce922852017-01-09 22:23:1042 client_.reset();
mathpf709499d2017-01-09 20:48:3643 return;
44 }
45 client_ = std::move(client);
46 details_ = std::move(details);
47}
48
49void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:1050 if (!client_.is_bound() || !binding_.is_bound()) {
51 OnError();
52 return;
53 }
mathpf709499d2017-01-09 20:48:3654 delegate_->ShowPaymentRequestDialog(this);
55}
56
57void PaymentRequest::Cancel() {
58 client_->OnError(payments::mojom::PaymentErrorReason::USER_CANCEL);
59}
60
61void PaymentRequest::OnError() {
62 binding_.Close();
63 manager_->DestroyRequest(this);
64}
65
mathp6758be032017-01-13 04:49:5066CurrencyFormatter* 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
tmartino68c0a272017-01-19 17:44:0878autofill::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
anthonyvd045303a2017-01-17 22:19:1991autofill::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
mathpf709499d2017-01-09 20:48:36110} // namespace payments