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 | |
tmartino | 68c0a27 | 2017-01-19 17:44:08 | [diff] [blame] | 7 | #include "base/memory/ptr_util.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 8 | #include "components/payments/payment_details_validation.h" |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 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 | |
mathp | c2d07f96 | 2017-02-17 18:33:51 | [diff] [blame] | 13 | using payments::mojom::BasicCardNetwork; |
| 14 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 15 | namespace payments { |
| 16 | |
mathp | c2d07f96 | 2017-02-17 18:33:51 | [diff] [blame] | 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 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 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), |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 32 | binding_(this, std::move(request)), |
| 33 | selected_shipping_profile_(nullptr), |
anthonyvd | 75bc466 | 2017-02-22 22:04:43 | [diff] [blame^] | 34 | selected_contact_profile_(nullptr), |
| 35 | selected_credit_card_(nullptr) { |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 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))); |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 43 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | PaymentRequest::~PaymentRequest() {} |
| 47 | |
| 48 | void PaymentRequest::Init( |
| 49 | payments::mojom::PaymentRequestClientPtr client, |
mathp | c2d07f96 | 2017-02-17 18:33:51 | [diff] [blame] | 50 | std::vector<payments::mojom::PaymentMethodDataPtr> method_data, |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 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; |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 57 | OnConnectionTerminated(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 58 | return; |
| 59 | } |
| 60 | client_ = std::move(client); |
| 61 | details_ = std::move(details); |
mathp | c2d07f96 | 2017-02-17 18:33:51 | [diff] [blame] | 62 | PopulateValidatedMethodData(method_data); |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 63 | PopulateProfileCache(); |
| 64 | SetDefaultProfileSelections(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void PaymentRequest::Show() { |
tmartino | 8ce92285 | 2017-01-09 22:23:10 | [diff] [blame] | 68 | if (!client_.is_bound() || !binding_.is_bound()) { |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 69 | LOG(ERROR) << "Attempted Show(), but binding(s) missing."; |
| 70 | OnConnectionTerminated(); |
tmartino | 8ce92285 | 2017-01-09 22:23:10 | [diff] [blame] | 71 | return; |
| 72 | } |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 73 | delegate_->ShowDialog(this); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 74 | } |
| 75 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 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. |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 91 | client_->OnError(payments::mojom::PaymentErrorReason::USER_CANCEL); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 92 | |
| 93 | // We close all bindings and ask to be destroyed. |
| 94 | client_.reset(); |
| 95 | binding_.Close(); |
| 96 | manager_->DestroyRequest(this); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 97 | } |
| 98 | |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 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(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 106 | binding_.Close(); |
mathp | f4bc50e | 2017-01-24 05:17:50 | [diff] [blame] | 107 | delegate_->CloseDialog(); |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 108 | manager_->DestroyRequest(this); |
| 109 | } |
| 110 | |
mathp | 6758be03 | 2017-01-13 04:49:50 | [diff] [blame] | 111 | CurrencyFormatter* PaymentRequest::GetOrCreateCurrencyFormatter( |
| 112 | const std::string& currency_code, |
jinho.bang | 4276454 | 2017-01-24 14:42:56 | [diff] [blame] | 113 | const std::string& currency_system, |
mathp | 6758be03 | 2017-01-13 04:49:50 | [diff] [blame] | 114 | const std::string& locale_name) { |
| 115 | if (!currency_formatter_) { |
| 116 | currency_formatter_.reset( |
| 117 | new CurrencyFormatter(currency_code, currency_system, locale_name)); |
| 118 | } |
mathp | 6758be03 | 2017-01-13 04:49:50 | [diff] [blame] | 119 | return currency_formatter_.get(); |
| 120 | } |
| 121 | |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 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_; |
tmartino | 68c0a27 | 2017-01-19 17:44:08 | [diff] [blame] | 130 | } |
| 131 | |
anthonyvd | 75bc466 | 2017-02-22 22:04:43 | [diff] [blame^] | 132 | const std::vector<autofill::CreditCard*>& PaymentRequest::credit_cards() { |
| 133 | return credit_cards_; |
anthonyvd | 045303a | 2017-01-17 22:19:19 | [diff] [blame] | 134 | } |
| 135 | |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 136 | void PaymentRequest::PopulateProfileCache() { |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 137 | std::vector<autofill::AutofillProfile*> profiles = |
mathp | d4cfd8f | 2017-02-09 21:16:53 | [diff] [blame] | 138 | personal_data_manager()->GetProfilesToSuggest(); |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 139 | |
| 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 |
anthonyvd | 75bc466 | 2017-02-22 22:04:43 | [diff] [blame^] | 142 | // whenever Profiles are requested. The same is true for credit cards. |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 143 | 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 | } |
anthonyvd | 75bc466 | 2017-02-22 22:04:43 | [diff] [blame^] | 152 | |
| 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 | } |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void 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]); |
anthonyvd | 75bc466 | 2017-02-22 22:04:43 | [diff] [blame^] | 167 | |
| 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; |
tmartino | 3640562 | 2017-01-26 16:23:03 | [diff] [blame] | 177 | } |
| 178 | |
mathp | c2d07f96 | 2017-02-17 18:33:51 | [diff] [blame] | 179 | void 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 | |
mathp | f709499d | 2017-01-09 20:48:36 | [diff] [blame] | 250 | } // namespace payments |