blob: 0ad61671528eb8357d432730d7407a8fee1f7038 [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"
mathpf709499d2017-01-09 20:48:368#include "components/payments/payment_details_validation.h"
mathpf709499d2017-01-09 20:48:369#include "components/payments/payment_request_web_contents_manager.h"
10#include "content/public/browser/browser_thread.h"
11#include "content/public/browser/web_contents.h"
12
mathpc2d07f962017-02-17 18:33:5113using payments::mojom::BasicCardNetwork;
14
mathpf709499d2017-01-09 20:48:3615namespace payments {
16
mathpc2d07f962017-02-17 18:33:5117namespace {
18
19// Identifier for the basic card payment method in the PaymentMethodData.
20const char* const kBasicCardMethodName = "basic-card";
21
22} // namespace
23
mathpf709499d2017-01-09 20:48:3624PaymentRequest::PaymentRequest(
25 content::WebContents* web_contents,
26 std::unique_ptr<PaymentRequestDelegate> delegate,
27 PaymentRequestWebContentsManager* manager,
28 mojo::InterfaceRequest<payments::mojom::PaymentRequest> request)
29 : web_contents_(web_contents),
30 delegate_(std::move(delegate)),
31 manager_(manager),
tmartino36405622017-01-26 16:23:0332 binding_(this, std::move(request)),
33 selected_shipping_profile_(nullptr),
anthonyvd75bc4662017-02-22 22:04:4334 selected_contact_profile_(nullptr),
35 selected_credit_card_(nullptr) {
mathpf4bc50e2017-01-24 05:17:5036 // OnConnectionTerminated will be called when the Mojo pipe is closed. This
37 // will happen as a result of many renderer-side events (both successful and
38 // erroneous in nature).
39 // TODO(crbug.com/683636): Investigate using
40 // set_connection_error_with_reason_handler with Binding::CloseWithReason.
41 binding_.set_connection_error_handler(base::Bind(
42 &PaymentRequest::OnConnectionTerminated, base::Unretained(this)));
tmartino36405622017-01-26 16:23:0343
mathpf709499d2017-01-09 20:48:3644}
45
46PaymentRequest::~PaymentRequest() {}
47
48void PaymentRequest::Init(
49 payments::mojom::PaymentRequestClientPtr client,
mathpc2d07f962017-02-17 18:33:5150 std::vector<payments::mojom::PaymentMethodDataPtr> method_data,
mathpf709499d2017-01-09 20:48:3651 payments::mojom::PaymentDetailsPtr details,
52 payments::mojom::PaymentOptionsPtr options) {
53 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
54 std::string error;
55 if (!payments::validatePaymentDetails(details, &error)) {
56 LOG(ERROR) << error;
mathpf4bc50e2017-01-24 05:17:5057 OnConnectionTerminated();
mathpf709499d2017-01-09 20:48:3658 return;
59 }
60 client_ = std::move(client);
61 details_ = std::move(details);
mathpc2d07f962017-02-17 18:33:5162 PopulateValidatedMethodData(method_data);
tmartino36405622017-01-26 16:23:0363 PopulateProfileCache();
64 SetDefaultProfileSelections();
mathpf709499d2017-01-09 20:48:3665}
66
67void PaymentRequest::Show() {
tmartino8ce922852017-01-09 22:23:1068 if (!client_.is_bound() || !binding_.is_bound()) {
mathpf4bc50e2017-01-24 05:17:5069 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
70 OnConnectionTerminated();
tmartino8ce922852017-01-09 22:23:1071 return;
72 }
mathpf4bc50e2017-01-24 05:17:5073 delegate_->ShowDialog(this);
mathpf709499d2017-01-09 20:48:3674}
75
mathpf4bc50e2017-01-24 05:17:5076void PaymentRequest::Abort() {
77 // The API user has decided to abort. We return a successful abort message to
78 // the renderer, which closes the Mojo message pipe, which triggers
79 // PaymentRequest::OnConnectionTerminated, which destroys this object.
80 if (client_.is_bound())
81 client_->OnAbort(true /* aborted_successfully */);
82}
83
84void PaymentRequest::UserCancelled() {
85 // If |client_| is not bound, then the object is already being destroyed as
86 // a result of a renderer event.
87 if (!client_.is_bound())
88 return;
89
90 // This sends an error to the renderer, which informs the API user.
mathpf709499d2017-01-09 20:48:3691 client_->OnError(payments::mojom::PaymentErrorReason::USER_CANCEL);
mathpf4bc50e2017-01-24 05:17:5092
93 // We close all bindings and ask to be destroyed.
94 client_.reset();
95 binding_.Close();
96 manager_->DestroyRequest(this);
mathpf709499d2017-01-09 20:48:3697}
98
mathpf4bc50e2017-01-24 05:17:5099void PaymentRequest::OnConnectionTerminated() {
100 // We are here because of a browser-side error, or likely as a result of the
101 // connection_error_handler on |binding_|, which can mean that the renderer
102 // has decided to close the pipe for various reasons (see all uses of
103 // PaymentRequest::clearResolversAndCloseMojoConnection() in Blink). We close
104 // the binding and the dialog, and ask to be deleted.
105 client_.reset();
mathpf709499d2017-01-09 20:48:36106 binding_.Close();
mathpf4bc50e2017-01-24 05:17:50107 delegate_->CloseDialog();
mathpf709499d2017-01-09 20:48:36108 manager_->DestroyRequest(this);
109}
110
mathp6758be032017-01-13 04:49:50111CurrencyFormatter* PaymentRequest::GetOrCreateCurrencyFormatter(
112 const std::string& currency_code,
jinho.bang42764542017-01-24 14:42:56113 const std::string& currency_system,
mathp6758be032017-01-13 04:49:50114 const std::string& locale_name) {
115 if (!currency_formatter_) {
116 currency_formatter_.reset(
117 new CurrencyFormatter(currency_code, currency_system, locale_name));
118 }
mathp6758be032017-01-13 04:49:50119 return currency_formatter_.get();
120}
121
tmartino36405622017-01-26 16:23:03122const std::vector<autofill::AutofillProfile*>&
123 PaymentRequest::shipping_profiles() {
124 return shipping_profiles_;
125}
126
127const std::vector<autofill::AutofillProfile*>&
128 PaymentRequest::contact_profiles() {
129 return contact_profiles_;
tmartino68c0a272017-01-19 17:44:08130}
131
anthonyvd75bc4662017-02-22 22:04:43132const std::vector<autofill::CreditCard*>& PaymentRequest::credit_cards() {
133 return credit_cards_;
anthonyvd045303a2017-01-17 22:19:19134}
135
tmartino36405622017-01-26 16:23:03136void PaymentRequest::PopulateProfileCache() {
tmartino36405622017-01-26 16:23:03137 std::vector<autofill::AutofillProfile*> profiles =
mathpd4cfd8f2017-02-09 21:16:53138 personal_data_manager()->GetProfilesToSuggest();
tmartino36405622017-01-26 16:23:03139
140 // PaymentRequest may outlive the Profiles returned by the Data Manager.
141 // Thus, we store copies, and return a vector of pointers to these copies
anthonyvd75bc4662017-02-22 22:04:43142 // whenever Profiles are requested. The same is true for credit cards.
tmartino36405622017-01-26 16:23:03143 for (size_t i = 0; i < profiles.size(); i++) {
144 profile_cache_.push_back(
145 base::MakeUnique<autofill::AutofillProfile>(*profiles[i]));
146
147 // TODO(tmartino): Implement deduplication rules specific to shipping and
148 // contact profiles.
149 shipping_profiles_.push_back(profile_cache_[i].get());
150 contact_profiles_.push_back(profile_cache_[i].get());
151 }
anthonyvd75bc4662017-02-22 22:04:43152
153 const std::vector<autofill::CreditCard*>& cards =
154 personal_data_manager()->GetCreditCardsToSuggest();
155 for (autofill::CreditCard* card : cards) {
156 card_cache_.push_back(base::MakeUnique<autofill::CreditCard>(*card));
157 credit_cards_.push_back(card_cache_.back().get());
158 }
tmartino36405622017-01-26 16:23:03159}
160
161void PaymentRequest::SetDefaultProfileSelections() {
162 if (!shipping_profiles().empty())
163 set_selected_shipping_profile(shipping_profiles()[0]);
164
165 if (!contact_profiles().empty())
166 set_selected_contact_profile(contact_profiles()[0]);
anthonyvd75bc4662017-02-22 22:04:43167
168 // TODO(anthonyvd): Change this code to prioritize server cards and implement
169 // a way to modify this function's return value.
170 const std::vector<autofill::CreditCard*> cards = credit_cards();
171 auto first_complete_card =
172 std::find_if(cards.begin(), cards.end(),
173 [](autofill::CreditCard* card) { return card->IsValid(); });
174
175 selected_credit_card_ =
176 first_complete_card == cards.end() ? nullptr : *first_complete_card;
tmartino36405622017-01-26 16:23:03177}
178
mathpc2d07f962017-02-17 18:33:51179void PaymentRequest::PopulateValidatedMethodData(
180 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data) {
181 if (method_data.empty()) {
182 LOG(ERROR) << "Invalid payment methods or data";
183 OnConnectionTerminated();
184 return;
185 }
186
187 std::set<std::string> card_networks{"amex", "diners", "discover",
188 "jcb", "mastercard", "mir",
189 "unionpay", "visa"};
190 for (const payments::mojom::PaymentMethodDataPtr& method_data_entry :
191 method_data) {
192 std::vector<std::string> supported_methods =
193 method_data_entry->supported_methods;
194 if (supported_methods.empty()) {
195 LOG(ERROR) << "Invalid payment methods or data";
196 OnConnectionTerminated();
197 return;
198 }
199
200 for (const std::string& method : supported_methods) {
201 if (method.empty())
202 continue;
203
204 // If a card network is specified right in "supportedMethods", add it.
205 auto card_it = card_networks.find(method);
206 if (card_it != card_networks.end()) {
207 supported_card_networks_.push_back(method);
208 // |method| removed from |card_networks| so that it is not doubly added
209 // to |supported_card_networks_| if "basic-card" is specified with no
210 // supported networks.
211 card_networks.erase(card_it);
212 } else if (method == kBasicCardMethodName) {
213 // For the "basic-card" method, check "supportedNetworks".
214 if (method_data_entry->supported_networks.empty()) {
215 // Empty |supported_networks| means all networks are supported.
216 supported_card_networks_.insert(supported_card_networks_.end(),
217 card_networks.begin(),
218 card_networks.end());
219 // Clear the set so that no further networks are added to
220 // |supported_card_networks_|.
221 card_networks.clear();
222 } else {
223 // The merchant has specified a few basic card supported networks. Use
224 // the mapping to transform to known basic-card types.
225 std::unordered_map<BasicCardNetwork, std::string> networks = {
226 {BasicCardNetwork::AMEX, "amex"},
227 {BasicCardNetwork::DINERS, "diners"},
228 {BasicCardNetwork::DISCOVER, "discover"},
229 {BasicCardNetwork::JCB, "jcb"},
230 {BasicCardNetwork::MASTERCARD, "mastercard"},
231 {BasicCardNetwork::MIR, "mir"},
232 {BasicCardNetwork::UNIONPAY, "unionpay"},
233 {BasicCardNetwork::VISA, "visa"}};
234 for (const BasicCardNetwork& supported_network :
235 method_data_entry->supported_networks) {
236 // Make sure that the network was not already added to
237 // |supported_card_networks_|.
238 auto card_it = card_networks.find(networks[supported_network]);
239 if (card_it != card_networks.end()) {
240 supported_card_networks_.push_back(networks[supported_network]);
241 card_networks.erase(card_it);
242 }
243 }
244 }
245 }
246 }
247 }
248}
249
mathpf709499d2017-01-09 20:48:36250} // namespace payments