mathp | f1a7a375 | 2017-03-15 11:23:37 | [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/content/payment_request_spec.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "base/logging.h" |
mathp | b65623a | 2017-04-06 02:01:54 | [diff] [blame^] | 10 | #include "components/payments/core/payment_method_data.h" |
| 11 | #include "components/payments/core/payment_request_data_util.h" |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 12 | |
| 13 | namespace payments { |
| 14 | |
mathp | b65623a | 2017-04-06 02:01:54 | [diff] [blame^] | 15 | namespace { |
| 16 | |
| 17 | // Returns the card network name associated with a given BasicCardNetwork. Names |
| 18 | // are inspired by https://www.w3.org/Payments/card-network-ids. |
| 19 | std::string GetBasicCardNetworkName(const mojom::BasicCardNetwork& network) { |
| 20 | switch (network) { |
| 21 | case mojom::BasicCardNetwork::AMEX: |
| 22 | return "amex"; |
| 23 | case mojom::BasicCardNetwork::DINERS: |
| 24 | return "diners"; |
| 25 | case mojom::BasicCardNetwork::DISCOVER: |
| 26 | return "discover"; |
| 27 | case mojom::BasicCardNetwork::JCB: |
| 28 | return "jcb"; |
| 29 | case mojom::BasicCardNetwork::MASTERCARD: |
| 30 | return "mastercard"; |
| 31 | case mojom::BasicCardNetwork::MIR: |
| 32 | return "mir"; |
| 33 | case mojom::BasicCardNetwork::UNIONPAY: |
| 34 | return "unionpay"; |
| 35 | case mojom::BasicCardNetwork::VISA: |
| 36 | return "visa"; |
| 37 | } |
| 38 | NOTREACHED(); |
| 39 | return std::string(); |
| 40 | } |
| 41 | |
| 42 | } // namespace |
| 43 | |
mathp | 600bab5 | 2017-03-26 03:47:59 | [diff] [blame] | 44 | const char kBasicCardMethodName[] = "basic-card"; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 45 | |
| 46 | PaymentRequestSpec::PaymentRequestSpec( |
| 47 | mojom::PaymentOptionsPtr options, |
| 48 | mojom::PaymentDetailsPtr details, |
| 49 | std::vector<mojom::PaymentMethodDataPtr> method_data, |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 50 | Observer* observer, |
| 51 | const std::string& app_locale) |
| 52 | : options_(std::move(options)), |
| 53 | details_(std::move(details)), |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 54 | app_locale_(app_locale), |
| 55 | selected_shipping_option_(nullptr), |
| 56 | observer_for_testing_(nullptr) { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 57 | if (observer) |
| 58 | AddObserver(observer); |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 59 | UpdateSelectedShippingOption(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 60 | PopulateValidatedMethodData(method_data); |
| 61 | } |
| 62 | PaymentRequestSpec::~PaymentRequestSpec() {} |
| 63 | |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 64 | void PaymentRequestSpec::UpdateWith(mojom::PaymentDetailsPtr details) { |
| 65 | details_ = std::move(details); |
| 66 | // We reparse the |details_| and update the observers. |
| 67 | UpdateSelectedShippingOption(); |
| 68 | NotifyOnSpecUpdated(); |
| 69 | } |
| 70 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 71 | void PaymentRequestSpec::AddObserver(Observer* observer) { |
| 72 | CHECK(observer); |
| 73 | observers_.AddObserver(observer); |
| 74 | } |
| 75 | |
| 76 | void PaymentRequestSpec::RemoveObserver(Observer* observer) { |
| 77 | observers_.RemoveObserver(observer); |
| 78 | } |
| 79 | |
tmartino | 5f0912b8 | 2017-03-30 03:20:52 | [diff] [blame] | 80 | bool PaymentRequestSpec::request_shipping() const { |
| 81 | return options_->request_shipping; |
| 82 | } |
| 83 | bool PaymentRequestSpec::request_payer_name() const { |
| 84 | return options_->request_payer_name; |
| 85 | } |
| 86 | bool PaymentRequestSpec::request_payer_phone() const { |
| 87 | return options_->request_payer_phone; |
| 88 | } |
| 89 | bool PaymentRequestSpec::request_payer_email() const { |
| 90 | return options_->request_payer_email; |
| 91 | } |
| 92 | |
| 93 | PaymentShippingType PaymentRequestSpec::shipping_type() const { |
| 94 | // Transform Mojo-specific enum into platform-agnostic equivalent. |
| 95 | switch (options_->shipping_type) { |
| 96 | case mojom::PaymentShippingType::DELIVERY: |
| 97 | return PaymentShippingType::DELIVERY; |
| 98 | case payments::mojom::PaymentShippingType::PICKUP: |
| 99 | return PaymentShippingType::PICKUP; |
| 100 | case payments::mojom::PaymentShippingType::SHIPPING: |
| 101 | return PaymentShippingType::SHIPPING; |
| 102 | default: |
| 103 | NOTREACHED(); |
| 104 | } |
| 105 | // Needed for compilation on some platforms. |
| 106 | return PaymentShippingType::SHIPPING; |
| 107 | } |
| 108 | |
mathp | 600bab5 | 2017-03-26 03:47:59 | [diff] [blame] | 109 | bool PaymentRequestSpec::IsMethodSupportedThroughBasicCard( |
| 110 | const std::string& method_name) { |
| 111 | return basic_card_specified_networks_.count(method_name) > 0; |
| 112 | } |
| 113 | |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 114 | base::string16 PaymentRequestSpec::GetFormattedCurrencyAmount( |
| 115 | const std::string& amount) { |
| 116 | CurrencyFormatter* formatter = GetOrCreateCurrencyFormatter( |
| 117 | details_->total->amount->currency, |
| 118 | details_->total->amount->currency_system, app_locale_); |
| 119 | return formatter->Format(amount); |
| 120 | } |
| 121 | |
| 122 | std::string PaymentRequestSpec::GetFormattedCurrencyCode() { |
| 123 | CurrencyFormatter* formatter = GetOrCreateCurrencyFormatter( |
| 124 | details_->total->amount->currency, |
| 125 | details_->total->amount->currency_system, app_locale_); |
| 126 | |
| 127 | return formatter->formatted_currency_code(); |
| 128 | } |
| 129 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 130 | void PaymentRequestSpec::PopulateValidatedMethodData( |
mathp | b65623a | 2017-04-06 02:01:54 | [diff] [blame^] | 131 | const std::vector<mojom::PaymentMethodDataPtr>& method_data_mojom) { |
| 132 | if (method_data_mojom.empty()) { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 133 | LOG(ERROR) << "Invalid payment methods or data"; |
| 134 | NotifyOnInvalidSpecProvided(); |
| 135 | return; |
| 136 | } |
| 137 | |
mathp | b65623a | 2017-04-06 02:01:54 | [diff] [blame^] | 138 | std::vector<PaymentMethodData> method_data_vector; |
| 139 | method_data_vector.reserve(method_data_mojom.size()); |
| 140 | for (const mojom::PaymentMethodDataPtr& method_data_entry : |
| 141 | method_data_mojom) { |
| 142 | PaymentMethodData method_data; |
| 143 | method_data.supported_methods = method_data_entry->supported_methods; |
| 144 | // Transfer the supported basic card networks. |
| 145 | std::vector<std::string> supported_networks; |
| 146 | for (const mojom::BasicCardNetwork& network : |
| 147 | method_data_entry->supported_networks) { |
| 148 | supported_networks.push_back(GetBasicCardNetworkName(network)); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 149 | } |
mathp | b65623a | 2017-04-06 02:01:54 | [diff] [blame^] | 150 | method_data.supported_networks = std::move(supported_networks); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 151 | |
mathp | b65623a | 2017-04-06 02:01:54 | [diff] [blame^] | 152 | // TODO(crbug.com/708603): Add browser-side support for |
| 153 | // |method_data.supported_types|. |
| 154 | method_data_vector.push_back(std::move(method_data)); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 155 | } |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 156 | |
mathp | b65623a | 2017-04-06 02:01:54 | [diff] [blame^] | 157 | if (!data_util::ParseBasicCardSupportedNetworks( |
| 158 | method_data_vector, &supported_card_networks_, |
| 159 | &basic_card_specified_networks_)) { |
| 160 | LOG(ERROR) << "Invalid payment methods or data"; |
| 161 | NotifyOnInvalidSpecProvided(); |
| 162 | return; |
| 163 | } |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 164 | supported_card_networks_set_.insert(supported_card_networks_.begin(), |
| 165 | supported_card_networks_.end()); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 166 | } |
| 167 | |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 168 | void PaymentRequestSpec::UpdateSelectedShippingOption() { |
| 169 | if (!request_shipping()) |
| 170 | return; |
| 171 | |
| 172 | // As per the spec, the selected shipping option should initially be the last |
| 173 | // one in the array that has its selected field set to true. |
| 174 | auto selected_shipping_option_it = std::find_if( |
| 175 | details().shipping_options.rbegin(), details().shipping_options.rend(), |
| 176 | [](const payments::mojom::PaymentShippingOptionPtr& element) { |
| 177 | return element->selected; |
| 178 | }); |
| 179 | if (selected_shipping_option_it != details().shipping_options.rend()) { |
| 180 | selected_shipping_option_ = selected_shipping_option_it->get(); |
| 181 | } |
| 182 | } |
| 183 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 184 | void PaymentRequestSpec::NotifyOnInvalidSpecProvided() { |
| 185 | for (auto& observer : observers_) |
| 186 | observer.OnInvalidSpecProvided(); |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 187 | if (observer_for_testing_) |
| 188 | observer_for_testing_->OnInvalidSpecProvided(); |
| 189 | } |
| 190 | |
| 191 | void PaymentRequestSpec::NotifyOnSpecUpdated() { |
| 192 | for (auto& observer : observers_) |
| 193 | observer.OnSpecUpdated(); |
| 194 | if (observer_for_testing_) |
| 195 | observer_for_testing_->OnSpecUpdated(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 196 | } |
| 197 | |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 198 | CurrencyFormatter* PaymentRequestSpec::GetOrCreateCurrencyFormatter( |
| 199 | const std::string& currency_code, |
| 200 | const std::string& currency_system, |
| 201 | const std::string& locale_name) { |
| 202 | if (!currency_formatter_) { |
| 203 | currency_formatter_.reset( |
| 204 | new CurrencyFormatter(currency_code, currency_system, locale_name)); |
| 205 | } |
| 206 | return currency_formatter_.get(); |
| 207 | } |
| 208 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 209 | } // namespace payments |