| OLD | NEW |
| (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/content/payment_request_spec.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/logging.h" |
| 10 |
| 11 namespace payments { |
| 12 |
| 13 namespace { |
| 14 // Identifier for the basic card payment method in the PaymentMethodData. |
| 15 static const char* const kBasicCardMethodName = "basic-card"; |
| 16 } // namespace |
| 17 |
| 18 PaymentRequestSpec::PaymentRequestSpec( |
| 19 mojom::PaymentOptionsPtr options, |
| 20 mojom::PaymentDetailsPtr details, |
| 21 std::vector<mojom::PaymentMethodDataPtr> method_data, |
| 22 Observer* observer) |
| 23 : options_(std::move(options)), details_(std::move(details)) { |
| 24 if (observer) |
| 25 AddObserver(observer); |
| 26 PopulateValidatedMethodData(method_data); |
| 27 } |
| 28 PaymentRequestSpec::~PaymentRequestSpec() {} |
| 29 |
| 30 void PaymentRequestSpec::AddObserver(Observer* observer) { |
| 31 CHECK(observer); |
| 32 observers_.AddObserver(observer); |
| 33 } |
| 34 |
| 35 void PaymentRequestSpec::RemoveObserver(Observer* observer) { |
| 36 observers_.RemoveObserver(observer); |
| 37 } |
| 38 |
| 39 void PaymentRequestSpec::PopulateValidatedMethodData( |
| 40 const std::vector<mojom::PaymentMethodDataPtr>& method_data) { |
| 41 if (method_data.empty()) { |
| 42 LOG(ERROR) << "Invalid payment methods or data"; |
| 43 NotifyOnInvalidSpecProvided(); |
| 44 return; |
| 45 } |
| 46 |
| 47 std::set<std::string> card_networks{"amex", "diners", "discover", |
| 48 "jcb", "mastercard", "mir", |
| 49 "unionpay", "visa"}; |
| 50 for (const mojom::PaymentMethodDataPtr& method_data_entry : method_data) { |
| 51 std::vector<std::string> supported_methods = |
| 52 method_data_entry->supported_methods; |
| 53 if (supported_methods.empty()) { |
| 54 LOG(ERROR) << "Invalid payment methods or data"; |
| 55 NotifyOnInvalidSpecProvided(); |
| 56 return; |
| 57 } |
| 58 |
| 59 for (const std::string& method : supported_methods) { |
| 60 if (method.empty()) |
| 61 continue; |
| 62 |
| 63 // If a card network is specified right in "supportedMethods", add it. |
| 64 auto card_it = card_networks.find(method); |
| 65 if (card_it != card_networks.end()) { |
| 66 supported_card_networks_.push_back(method); |
| 67 // |method| removed from |card_networks| so that it is not doubly added |
| 68 // to |supported_card_networks_| if "basic-card" is specified with no |
| 69 // supported networks. |
| 70 card_networks.erase(card_it); |
| 71 } else if (method == kBasicCardMethodName) { |
| 72 // For the "basic-card" method, check "supportedNetworks". |
| 73 if (method_data_entry->supported_networks.empty()) { |
| 74 // Empty |supported_networks| means all networks are supported. |
| 75 supported_card_networks_.insert(supported_card_networks_.end(), |
| 76 card_networks.begin(), |
| 77 card_networks.end()); |
| 78 // Clear the set so that no further networks are added to |
| 79 // |supported_card_networks_|. |
| 80 card_networks.clear(); |
| 81 } else { |
| 82 // The merchant has specified a few basic card supported networks. Use |
| 83 // the mapping to transform to known basic-card types. |
| 84 using mojom::BasicCardNetwork; |
| 85 std::unordered_map<BasicCardNetwork, std::string> networks = { |
| 86 {BasicCardNetwork::AMEX, "amex"}, |
| 87 {BasicCardNetwork::DINERS, "diners"}, |
| 88 {BasicCardNetwork::DISCOVER, "discover"}, |
| 89 {BasicCardNetwork::JCB, "jcb"}, |
| 90 {BasicCardNetwork::MASTERCARD, "mastercard"}, |
| 91 {BasicCardNetwork::MIR, "mir"}, |
| 92 {BasicCardNetwork::UNIONPAY, "unionpay"}, |
| 93 {BasicCardNetwork::VISA, "visa"}}; |
| 94 for (const BasicCardNetwork& supported_network : |
| 95 method_data_entry->supported_networks) { |
| 96 // Make sure that the network was not already added to |
| 97 // |supported_card_networks_|. |
| 98 auto card_it = card_networks.find(networks[supported_network]); |
| 99 if (card_it != card_networks.end()) { |
| 100 supported_card_networks_.push_back(networks[supported_network]); |
| 101 card_networks.erase(card_it); |
| 102 } |
| 103 } |
| 104 } |
| 105 } |
| 106 } |
| 107 } |
| 108 } |
| 109 |
| 110 void PaymentRequestSpec::NotifyOnInvalidSpecProvided() { |
| 111 for (auto& observer : observers_) |
| 112 observer.OnInvalidSpecProvided(); |
| 113 } |
| 114 |
| 115 } // namespace payments |
| OLD | NEW |