blob: 06ec2b4f46562699b0baa795d00e07d512754bd2 [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
anthonyvd045303a2017-01-17 22:19:197#include "components/autofill/core/browser/personal_data_manager.h"
mathpf709499d2017-01-09 20:48:368#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
14namespace payments {
15
16PaymentRequest::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
29PaymentRequest::~PaymentRequest() {}
30
31void 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();
tmartino8ce922852017-01-09 22:23:1041 client_.reset();
mathpf709499d2017-01-09 20:48:3642 return;
43 }
44 client_ = std::move(client);
45 details_ = std::move(details);
46}
47
48void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:1049 if (!client_.is_bound() || !binding_.is_bound()) {
50 OnError();
51 return;
52 }
mathpf709499d2017-01-09 20:48:3653 delegate_->ShowPaymentRequestDialog(this);
54}
55
56void PaymentRequest::Cancel() {
57 client_->OnError(payments::mojom::PaymentErrorReason::USER_CANCEL);
58}
59
60void PaymentRequest::OnError() {
61 binding_.Close();
62 manager_->DestroyRequest(this);
63}
64
mathp6758be032017-01-13 04:49:5065CurrencyFormatter* 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
anthonyvd045303a2017-01-17 22:19:1977autofill::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
mathpf709499d2017-01-09 20:48:3696} // namespace payments