| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ios/chrome/browser/payments/payment_request.h" | 5 #include "ios/chrome/browser/payments/payment_request.h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 8 |
| 7 #include "base/containers/adapters.h" | 9 #include "base/containers/adapters.h" |
| 8 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_data_util.h" | 12 #include "components/autofill/core/browser/autofill_data_util.h" |
| 11 #include "components/autofill/core/browser/autofill_profile.h" | 13 #include "components/autofill/core/browser/autofill_profile.h" |
| 12 #include "components/autofill/core/browser/credit_card.h" | 14 #include "components/autofill/core/browser/credit_card.h" |
| 13 #include "components/autofill/core/browser/personal_data_manager.h" | 15 #include "components/autofill/core/browser/personal_data_manager.h" |
| 14 #include "components/autofill/core/browser/region_data_loader_impl.h" | 16 #include "components/autofill/core/browser/region_data_loader_impl.h" |
| 17 #include "components/autofill/core/browser/validation.h" |
| 15 #include "components/payments/core/currency_formatter.h" | 18 #include "components/payments/core/currency_formatter.h" |
| 16 #include "components/payments/core/payment_request_data_util.h" | 19 #include "components/payments/core/payment_request_data_util.h" |
| 17 #include "ios/chrome/browser/application_context.h" | 20 #include "ios/chrome/browser/application_context.h" |
| 18 #include "ios/chrome/browser/autofill/validation_rules_storage_factory.h" | 21 #include "ios/chrome/browser/autofill/validation_rules_storage_factory.h" |
| 22 #import "ios/chrome/browser/payments/payment_request_util.h" |
| 19 #include "ios/web/public/payments/payment_request.h" | 23 #include "ios/web/public/payments/payment_request.h" |
| 20 #include "third_party/libaddressinput/chromium/chrome_metadata_source.h" | 24 #include "third_party/libaddressinput/chromium/chrome_metadata_source.h" |
| 21 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" | 25 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" |
| 22 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" | 26 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" |
| 23 | 27 |
| 24 #if !defined(__has_feature) || !__has_feature(objc_arc) | 28 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 25 #error "This file requires ARC support." | 29 #error "This file requires ARC support." |
| 26 #endif | 30 #endif |
| 27 | 31 |
| 28 namespace { | 32 namespace { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 selected_shipping_profile_ = shipping_profiles_[0]; | 70 selected_shipping_profile_ = shipping_profiles_[0]; |
| 67 } | 71 } |
| 68 | 72 |
| 69 // If the highest-ranking contact profile is usable, select it. Otherwise, | 73 // If the highest-ranking contact profile is usable, select it. Otherwise, |
| 70 // select none. | 74 // select none. |
| 71 if (!contact_profiles_.empty() && | 75 if (!contact_profiles_.empty() && |
| 72 profile_comparator_.IsContactInfoComplete(contact_profiles_[0])) { | 76 profile_comparator_.IsContactInfoComplete(contact_profiles_[0])) { |
| 73 selected_contact_profile_ = contact_profiles_[0]; | 77 selected_contact_profile_ = contact_profiles_[0]; |
| 74 } | 78 } |
| 75 | 79 |
| 76 // Select the highest ranking credit card. | 80 // TODO(crbug.com/702063): Change this code to prioritize credit cards by use |
| 77 if (!credit_cards_.empty()) | 81 // count and other means. |
| 78 selected_credit_card_ = credit_cards_[0]; | 82 auto first_complete_credit_card = std::find_if( |
| 83 credit_cards_.begin(), credit_cards_.end(), |
| 84 [this](const autofill::CreditCard* credit_card) { |
| 85 DCHECK(credit_card); |
| 86 return payment_request_util::IsCreditCardCompleteForPayment( |
| 87 *credit_card, billing_profiles()); |
| 88 }); |
| 89 if (first_complete_credit_card != credit_cards_.end()) |
| 90 selected_credit_card_ = *first_complete_credit_card; |
| 79 } | 91 } |
| 80 | 92 |
| 81 PaymentRequest::~PaymentRequest() {} | 93 PaymentRequest::~PaymentRequest() {} |
| 82 | 94 |
| 83 void PaymentRequest::UpdatePaymentDetails(const web::PaymentDetails& details) { | 95 void PaymentRequest::UpdatePaymentDetails(const web::PaymentDetails& details) { |
| 84 web_payment_request_.details = details; | 96 web_payment_request_.details = details; |
| 85 PopulateAvailableShippingOptions(); | 97 PopulateAvailableShippingOptions(); |
| 86 SetSelectedShippingOption(); | 98 SetSelectedShippingOption(); |
| 87 } | 99 } |
| 88 | 100 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 PopulateAvailableCreditCards(); | 191 PopulateAvailableCreditCards(); |
| 180 | 192 |
| 181 return credit_card_cache_.back().get(); | 193 return credit_card_cache_.back().get(); |
| 182 } | 194 } |
| 183 | 195 |
| 184 payments::PaymentsProfileComparator* PaymentRequest::profile_comparator() { | 196 payments::PaymentsProfileComparator* PaymentRequest::profile_comparator() { |
| 185 return &profile_comparator_; | 197 return &profile_comparator_; |
| 186 } | 198 } |
| 187 | 199 |
| 188 bool PaymentRequest::CanMakePayment() const { | 200 bool PaymentRequest::CanMakePayment() const { |
| 189 return !credit_cards_.empty(); | 201 for (const autofill::CreditCard* credit_card : credit_cards_) { |
| 202 DCHECK(credit_card); |
| 203 autofill::CreditCardCompletionStatus status = |
| 204 autofill::GetCompletionStatusForCard( |
| 205 *credit_card, GetApplicationContext()->GetApplicationLocale(), |
| 206 billing_profiles()); |
| 207 // A card only has to have a cardholder name and a number for the purposes |
| 208 // of CanMakePayment. An expired card or one without a billing address is |
| 209 // valid for this purpose. |
| 210 return !(status & autofill::CREDIT_CARD_NO_CARDHOLDER || |
| 211 status & autofill::CREDIT_CARD_NO_NUMBER); |
| 212 } |
| 213 return false; |
| 190 } | 214 } |
| 191 | 215 |
| 192 void PaymentRequest::PopulateCreditCardCache() { | 216 void PaymentRequest::PopulateCreditCardCache() { |
| 193 // TODO(crbug.com/709036): Validate method data. | 217 // TODO(crbug.com/709036): Validate method data. |
| 194 payments::data_util::ParseBasicCardSupportedNetworks( | 218 payments::data_util::ParseBasicCardSupportedNetworks( |
| 195 web_payment_request_.method_data, &supported_card_networks_, | 219 web_payment_request_.method_data, &supported_card_networks_, |
| 196 &basic_card_specified_networks_); | 220 &basic_card_specified_networks_); |
| 197 | 221 |
| 198 const std::vector<autofill::CreditCard*>& credit_cards_to_suggest = | 222 const std::vector<autofill::CreditCard*>& credit_cards_to_suggest = |
| 199 personal_data_manager_->GetCreditCardsToSuggest(); | 223 personal_data_manager_->GetCreditCardsToSuggest(); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 void PaymentRequest::SetSelectedShippingOption() { | 270 void PaymentRequest::SetSelectedShippingOption() { |
| 247 // If more than one option has |selected| set, the last one in the sequence | 271 // If more than one option has |selected| set, the last one in the sequence |
| 248 // should be treated as the selected item. | 272 // should be treated as the selected item. |
| 249 for (auto* shipping_option : base::Reversed(shipping_options_)) { | 273 for (auto* shipping_option : base::Reversed(shipping_options_)) { |
| 250 if (shipping_option->selected) { | 274 if (shipping_option->selected) { |
| 251 selected_shipping_option_ = shipping_option; | 275 selected_shipping_option_ = shipping_option; |
| 252 break; | 276 break; |
| 253 } | 277 } |
| 254 } | 278 } |
| 255 } | 279 } |
| OLD | NEW |