Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: components/payments/payment_request.cc

Issue 2713033004: Layered component for web payments (Closed)
Patch Set: Rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/payments/payment_request.h ('k') | components/payments/payment_request.mojom » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
7 #include "base/memory/ptr_util.h"
8 #include "components/payments/payment_details_validation.h"
9 #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
13 using payments::mojom::BasicCardNetwork;
14
15 namespace payments {
16
17 namespace {
18
19 // Identifier for the basic card payment method in the PaymentMethodData.
20 const char* const kBasicCardMethodName = "basic-card";
21
22 } // namespace
23
24 PaymentRequest::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),
32 binding_(this, std::move(request)),
33 selected_shipping_profile_(nullptr),
34 selected_contact_profile_(nullptr),
35 selected_credit_card_(nullptr) {
36 // 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)));
43
44 }
45
46 PaymentRequest::~PaymentRequest() {}
47
48 void PaymentRequest::Init(
49 payments::mojom::PaymentRequestClientPtr client,
50 std::vector<payments::mojom::PaymentMethodDataPtr> method_data,
51 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;
57 OnConnectionTerminated();
58 return;
59 }
60 client_ = std::move(client);
61 details_ = std::move(details);
62 PopulateValidatedMethodData(method_data);
63 PopulateProfileCache();
64 SetDefaultProfileSelections();
65 }
66
67 void PaymentRequest::Show() {
68 if (!client_.is_bound() || !binding_.is_bound()) {
69 LOG(ERROR) << "Attempted Show(), but binding(s) missing.";
70 OnConnectionTerminated();
71 return;
72 }
73 delegate_->ShowDialog(this);
74 }
75
76 void 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
84 void 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.
91 client_->OnError(payments::mojom::PaymentErrorReason::USER_CANCEL);
92
93 // We close all bindings and ask to be destroyed.
94 client_.reset();
95 binding_.Close();
96 manager_->DestroyRequest(this);
97 }
98
99 void 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();
106 binding_.Close();
107 delegate_->CloseDialog();
108 manager_->DestroyRequest(this);
109 }
110
111 CurrencyFormatter* PaymentRequest::GetOrCreateCurrencyFormatter(
112 const std::string& currency_code,
113 const std::string& currency_system,
114 const std::string& locale_name) {
115 if (!currency_formatter_) {
116 currency_formatter_.reset(
117 new CurrencyFormatter(currency_code, currency_system, locale_name));
118 }
119 return currency_formatter_.get();
120 }
121
122 const std::vector<autofill::AutofillProfile*>&
123 PaymentRequest::shipping_profiles() {
124 return shipping_profiles_;
125 }
126
127 const std::vector<autofill::AutofillProfile*>&
128 PaymentRequest::contact_profiles() {
129 return contact_profiles_;
130 }
131
132 void PaymentRequest::PopulateProfileCache() {
133 std::vector<autofill::AutofillProfile*> profiles =
134 personal_data_manager()->GetProfilesToSuggest();
135
136 // PaymentRequest may outlive the Profiles returned by the Data Manager.
137 // Thus, we store copies, and return a vector of pointers to these copies
138 // whenever Profiles are requested. The same is true for credit cards.
139 for (size_t i = 0; i < profiles.size(); i++) {
140 profile_cache_.push_back(
141 base::MakeUnique<autofill::AutofillProfile>(*profiles[i]));
142
143 // TODO(tmartino): Implement deduplication rules specific to shipping and
144 // contact profiles.
145 shipping_profiles_.push_back(profile_cache_[i].get());
146 contact_profiles_.push_back(profile_cache_[i].get());
147 }
148
149 const std::vector<autofill::CreditCard*>& cards =
150 personal_data_manager()->GetCreditCardsToSuggest();
151 for (autofill::CreditCard* card : cards) {
152 card_cache_.push_back(base::MakeUnique<autofill::CreditCard>(*card));
153 credit_cards_.push_back(card_cache_.back().get());
154 }
155 }
156
157 void PaymentRequest::SetDefaultProfileSelections() {
158 if (!shipping_profiles().empty())
159 set_selected_shipping_profile(shipping_profiles()[0]);
160
161 if (!contact_profiles().empty())
162 set_selected_contact_profile(contact_profiles()[0]);
163
164 // TODO(anthonyvd): Change this code to prioritize server cards and implement
165 // a way to modify this function's return value.
166 const std::vector<autofill::CreditCard*> cards = credit_cards();
167 auto first_complete_card =
168 std::find_if(cards.begin(), cards.end(),
169 [](autofill::CreditCard* card) { return card->IsValid(); });
170
171 selected_credit_card_ =
172 first_complete_card == cards.end() ? nullptr : *first_complete_card;
173 }
174
175 void PaymentRequest::PopulateValidatedMethodData(
176 const std::vector<payments::mojom::PaymentMethodDataPtr>& method_data) {
177 if (method_data.empty()) {
178 LOG(ERROR) << "Invalid payment methods or data";
179 OnConnectionTerminated();
180 return;
181 }
182
183 std::set<std::string> card_networks{"amex", "diners", "discover",
184 "jcb", "mastercard", "mir",
185 "unionpay", "visa"};
186 for (const payments::mojom::PaymentMethodDataPtr& method_data_entry :
187 method_data) {
188 std::vector<std::string> supported_methods =
189 method_data_entry->supported_methods;
190 if (supported_methods.empty()) {
191 LOG(ERROR) << "Invalid payment methods or data";
192 OnConnectionTerminated();
193 return;
194 }
195
196 for (const std::string& method : supported_methods) {
197 if (method.empty())
198 continue;
199
200 // If a card network is specified right in "supportedMethods", add it.
201 auto card_it = card_networks.find(method);
202 if (card_it != card_networks.end()) {
203 supported_card_networks_.push_back(method);
204 // |method| removed from |card_networks| so that it is not doubly added
205 // to |supported_card_networks_| if "basic-card" is specified with no
206 // supported networks.
207 card_networks.erase(card_it);
208 } else if (method == kBasicCardMethodName) {
209 // For the "basic-card" method, check "supportedNetworks".
210 if (method_data_entry->supported_networks.empty()) {
211 // Empty |supported_networks| means all networks are supported.
212 supported_card_networks_.insert(supported_card_networks_.end(),
213 card_networks.begin(),
214 card_networks.end());
215 // Clear the set so that no further networks are added to
216 // |supported_card_networks_|.
217 card_networks.clear();
218 } else {
219 // The merchant has specified a few basic card supported networks. Use
220 // the mapping to transform to known basic-card types.
221 std::unordered_map<BasicCardNetwork, std::string> networks = {
222 {BasicCardNetwork::AMEX, "amex"},
223 {BasicCardNetwork::DINERS, "diners"},
224 {BasicCardNetwork::DISCOVER, "discover"},
225 {BasicCardNetwork::JCB, "jcb"},
226 {BasicCardNetwork::MASTERCARD, "mastercard"},
227 {BasicCardNetwork::MIR, "mir"},
228 {BasicCardNetwork::UNIONPAY, "unionpay"},
229 {BasicCardNetwork::VISA, "visa"}};
230 for (const BasicCardNetwork& supported_network :
231 method_data_entry->supported_networks) {
232 // Make sure that the network was not already added to
233 // |supported_card_networks_|.
234 auto card_it = card_networks.find(networks[supported_network]);
235 if (card_it != card_networks.end()) {
236 supported_card_networks_.push_back(networks[supported_network]);
237 card_networks.erase(card_it);
238 }
239 }
240 }
241 }
242 }
243 }
244 }
245
246 } // namespace payments
OLDNEW
« no previous file with comments | « components/payments/payment_request.h ('k') | components/payments/payment_request.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698