blob: 00e8ff76110759424ab968344ae49fb330c3f9d7 [file] [log] [blame]
mathpf1a7a3752017-03-15 11:23:371// 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
Mathieu Perreault51339b82017-07-20 17:06:059#include "base/feature_list.h"
mathpf1a7a3752017-03-15 11:23:3710#include "base/logging.h"
mathpeb8892ff2017-05-04 18:42:5511#include "base/strings/utf_string_conversions.h"
Mohamad Ahmadif5544bb2017-09-01 21:48:2212#include "components/payments/content/payment_request_converter.h"
Mathieu Perreault51339b82017-07-20 17:06:0513#include "components/payments/core/features.h"
Anthony Vallee-Dubois059d59a2017-07-07 15:05:4914#include "components/payments/core/payment_instrument.h"
mathpb65623a2017-04-06 02:01:5415#include "components/payments/core/payment_method_data.h"
16#include "components/payments/core/payment_request_data_util.h"
mathpeb8892ff2017-05-04 18:42:5517#include "components/strings/grit/components_strings.h"
18#include "ui/base/l10n/l10n_util.h"
mathpf1a7a3752017-03-15 11:23:3719
20namespace payments {
21
mathpb65623a2017-04-06 02:01:5422namespace {
23
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5124// Validates the |method_data| and fills the output parameters.
Anthony Vallee-Dubois059d59a2017-07-07 15:05:4925void PopulateValidatedMethodData(
26 const std::vector<PaymentMethodData>& method_data_vector,
27 std::vector<std::string>* supported_card_networks,
28 std::set<std::string>* basic_card_specified_networks,
29 std::set<std::string>* supported_card_networks_set,
30 std::set<autofill::CreditCard::CardType>* supported_card_types_set,
Randall Raymond6a85ba0ab2017-08-04 23:11:5431 std::vector<GURL>* url_payment_method_identifiers,
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5132 std::set<std::string>* payment_method_identifiers_set,
Anthony Vallee-Dubois059d59a2017-07-07 15:05:4933 std::map<std::string, std::set<std::string>>* stringified_method_data) {
Randall Raymondec4e0852017-07-14 01:30:4534 data_util::ParseSupportedMethods(method_data_vector, supported_card_networks,
35 basic_card_specified_networks,
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5136 url_payment_method_identifiers,
37 payment_method_identifiers_set);
Anthony Vallee-Dubois059d59a2017-07-07 15:05:4938 supported_card_networks_set->insert(supported_card_networks->begin(),
39 supported_card_networks->end());
40
41 data_util::ParseSupportedCardTypes(method_data_vector,
42 supported_card_types_set);
43}
44
45void PopulateValidatedMethodData(
46 const std::vector<mojom::PaymentMethodDataPtr>& method_data_mojom,
47 std::vector<std::string>* supported_card_networks,
48 std::set<std::string>* basic_card_specified_networks,
49 std::set<std::string>* supported_card_networks_set,
50 std::set<autofill::CreditCard::CardType>* supported_card_types_set,
Randall Raymond6a85ba0ab2017-08-04 23:11:5451 std::vector<GURL>* url_payment_method_identifiers,
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5152 std::set<std::string>* payment_method_identifiers_set,
Anthony Vallee-Dubois059d59a2017-07-07 15:05:4953 std::map<std::string, std::set<std::string>>* stringified_method_data) {
54 std::vector<PaymentMethodData> method_data_vector;
55 method_data_vector.reserve(method_data_mojom.size());
56 for (const mojom::PaymentMethodDataPtr& method_data_entry :
57 method_data_mojom) {
Jinho Bangd252ebf5b12018-07-18 00:00:5958 (*stringified_method_data)[method_data_entry->supported_method].insert(
59 method_data_entry->stringified_data);
Anthony Vallee-Dubois059d59a2017-07-07 15:05:4960
Mohamad Ahmadif5544bb2017-09-01 21:48:2261 method_data_vector.push_back(ConvertPaymentMethodData(method_data_entry));
Anthony Vallee-Dubois059d59a2017-07-07 15:05:4962 }
63
64 PopulateValidatedMethodData(
65 method_data_vector, supported_card_networks,
66 basic_card_specified_networks, supported_card_networks_set,
Randall Raymondec4e0852017-07-14 01:30:4567 supported_card_types_set, url_payment_method_identifiers,
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5168 payment_method_identifiers_set, stringified_method_data);
Anthony Vallee-Dubois059d59a2017-07-07 15:05:4969}
70
mathpb65623a2017-04-06 02:01:5471} // namespace
72
mathp600bab52017-03-26 03:47:5973const char kBasicCardMethodName[] = "basic-card";
Mathieu Perreault627b97c2017-08-12 00:44:2274const char kGooglePayMethodName[] = "https://google.com/pay";
75const char kAndroidPayMethodName[] = "https://android.com/pay";
mathpf1a7a3752017-03-15 11:23:3776
77PaymentRequestSpec::PaymentRequestSpec(
78 mojom::PaymentOptionsPtr options,
79 mojom::PaymentDetailsPtr details,
80 std::vector<mojom::PaymentMethodDataPtr> method_data,
mathpc0d616a2017-03-15 14:09:3381 Observer* observer,
82 const std::string& app_locale)
83 : options_(std::move(options)),
84 details_(std::move(details)),
gogerald7a0cc3e2017-09-19 03:35:4885 method_data_(std::move(method_data)),
mathp151bd31e2017-04-03 21:07:2486 app_locale_(app_locale),
Anthony Vallee-Duboisdb030dd2017-05-19 18:04:5187 selected_shipping_option_(nullptr) {
mathpf1a7a3752017-03-15 11:23:3788 if (observer)
89 AddObserver(observer);
Jinho Bang6f591c72018-12-05 09:26:4390 if (!details_->shipping_options)
91 details_->shipping_options = std::vector<mojom::PaymentShippingOptionPtr>();
mathpb77b8732017-05-11 15:26:4292 UpdateSelectedShippingOption(/*after_update=*/false);
Anthony Vallee-Dubois059d59a2017-07-07 15:05:4993 PopulateValidatedMethodData(
gogerald7a0cc3e2017-09-19 03:35:4894 method_data_, &supported_card_networks_, &basic_card_specified_networks_,
Anthony Vallee-Dubois059d59a2017-07-07 15:05:4995 &supported_card_networks_set_, &supported_card_types_set_,
Rouslan Solomakhin4eea9bc22017-10-10 15:18:5196 &url_payment_method_identifiers_, &payment_method_identifiers_set_,
97 &stringified_method_data_);
mathpf1a7a3752017-03-15 11:23:3798}
99PaymentRequestSpec::~PaymentRequestSpec() {}
100
mathp151bd31e2017-04-03 21:07:24101void PaymentRequestSpec::UpdateWith(mojom::PaymentDetailsPtr details) {
Jinho Bang6f591c72018-12-05 09:26:43102 DCHECK(details_);
103 if (details->total)
104 details_->total = std::move(details->total);
Jinho Bang94ca2b102019-02-21 05:55:57105 if (!details->display_items.empty())
106 details_->display_items = std::move(details->display_items);
Jinho Bang6f591c72018-12-05 09:26:43107 if (details->shipping_options)
108 details_->shipping_options = std::move(details->shipping_options);
Jinho Bang2a2035182019-02-21 07:57:56109 if (!details->modifiers.empty())
110 details_->modifiers = std::move(details->modifiers);
Jinho Bang6f591c72018-12-05 09:26:43111 details_->error = std::move(details->error);
112 if (details->shipping_address_errors)
113 details_->shipping_address_errors =
114 std::move(details->shipping_address_errors);
115 if (details->id)
116 details_->id = std::move(details->id);
Rouslan Solomakhina9ff9282017-10-31 21:58:05117 RecomputeSpecForDetails();
118}
119
Jinho Bang745898b2019-03-25 14:00:03120void PaymentRequestSpec::Retry(
121 mojom::PaymentValidationErrorsPtr validation_errors) {
122 if (!validation_errors)
Jinho Bang092e7162018-09-06 23:41:19123 return;
Jinho Bangcac8d9a02018-08-23 19:47:22124
Jinho Bang745898b2019-03-25 14:00:03125 retry_error_message_ =
126 validation_errors->error.empty()
127 ? l10n_util::GetStringUTF16(IDS_PAYMENTS_ERROR_MESSAGE)
128 : base::UTF8ToUTF16(std::move(validation_errors->error));
129 details_->shipping_address_errors =
130 std::move(validation_errors->shipping_address);
131 payer_errors_ = std::move(validation_errors->payer);
Jinho Bangcac8d9a02018-08-23 19:47:22132 current_update_reason_ = UpdateReason::RETRY;
133 NotifyOnSpecUpdated();
134 current_update_reason_ = UpdateReason::NONE;
135}
136
137base::string16 PaymentRequestSpec::GetShippingAddressError(
138 autofill::ServerFieldType type) {
Jinho Bang47531aa2018-12-14 10:43:55139 if (!details_->shipping_address_errors)
Jinho Bangcac8d9a02018-08-23 19:47:22140 return base::string16();
141
142 if (type == autofill::ADDRESS_HOME_STREET_ADDRESS)
Jinho Bang47531aa2018-12-14 10:43:55143 return base::UTF8ToUTF16(details_->shipping_address_errors->address_line);
Jinho Bangcac8d9a02018-08-23 19:47:22144
145 if (type == autofill::ADDRESS_HOME_CITY)
Jinho Bang47531aa2018-12-14 10:43:55146 return base::UTF8ToUTF16(details_->shipping_address_errors->city);
Jinho Bangcac8d9a02018-08-23 19:47:22147
148 if (type == autofill::ADDRESS_HOME_COUNTRY)
Jinho Bang47531aa2018-12-14 10:43:55149 return base::UTF8ToUTF16(details_->shipping_address_errors->country);
Jinho Bangcac8d9a02018-08-23 19:47:22150
151 if (type == autofill::ADDRESS_HOME_DEPENDENT_LOCALITY)
Jinho Bang47531aa2018-12-14 10:43:55152 return base::UTF8ToUTF16(
153 details_->shipping_address_errors->dependent_locality);
Jinho Bangcac8d9a02018-08-23 19:47:22154
155 if (type == autofill::COMPANY_NAME)
Jinho Bang47531aa2018-12-14 10:43:55156 return base::UTF8ToUTF16(details_->shipping_address_errors->organization);
Jinho Bangcac8d9a02018-08-23 19:47:22157
158 if (type == autofill::PHONE_HOME_WHOLE_NUMBER)
Jinho Bang47531aa2018-12-14 10:43:55159 return base::UTF8ToUTF16(details_->shipping_address_errors->phone);
Jinho Bangcac8d9a02018-08-23 19:47:22160
161 if (type == autofill::ADDRESS_HOME_ZIP)
Jinho Bang47531aa2018-12-14 10:43:55162 return base::UTF8ToUTF16(details_->shipping_address_errors->postal_code);
Jinho Bangcac8d9a02018-08-23 19:47:22163
164 if (type == autofill::NAME_FULL)
Jinho Bang47531aa2018-12-14 10:43:55165 return base::UTF8ToUTF16(details_->shipping_address_errors->recipient);
Jinho Bangcac8d9a02018-08-23 19:47:22166
167 if (type == autofill::ADDRESS_HOME_STATE)
Jinho Bang47531aa2018-12-14 10:43:55168 return base::UTF8ToUTF16(details_->shipping_address_errors->region);
Jinho Bangcac8d9a02018-08-23 19:47:22169
170 if (type == autofill::ADDRESS_HOME_SORTING_CODE)
Jinho Bang47531aa2018-12-14 10:43:55171 return base::UTF8ToUTF16(details_->shipping_address_errors->sorting_code);
Jinho Bangcac8d9a02018-08-23 19:47:22172
173 return base::string16();
174}
175
176base::string16 PaymentRequestSpec::GetPayerError(
177 autofill::ServerFieldType type) {
178 if (!payer_errors_)
179 return base::string16();
180
181 if (type == autofill::EMAIL_ADDRESS)
182 return base::UTF8ToUTF16(payer_errors_->email);
183
184 if (type == autofill::NAME_FULL)
185 return base::UTF8ToUTF16(payer_errors_->name);
186
187 if (type == autofill::PHONE_HOME_WHOLE_NUMBER)
188 return base::UTF8ToUTF16(payer_errors_->phone);
189
190 return base::string16();
191}
192
193bool PaymentRequestSpec::has_shipping_address_error() const {
Jinho Bang47531aa2018-12-14 10:43:55194 return details_->shipping_address_errors && request_shipping() &&
195 !(details_->shipping_address_errors->address_line.empty() &&
196 details_->shipping_address_errors->city.empty() &&
197 details_->shipping_address_errors->country.empty() &&
198 details_->shipping_address_errors->dependent_locality.empty() &&
199 details_->shipping_address_errors->organization.empty() &&
200 details_->shipping_address_errors->phone.empty() &&
201 details_->shipping_address_errors->postal_code.empty() &&
202 details_->shipping_address_errors->recipient.empty() &&
203 details_->shipping_address_errors->region.empty() &&
Jinho Bang47531aa2018-12-14 10:43:55204 details_->shipping_address_errors->sorting_code.empty());
Jinho Bangcac8d9a02018-08-23 19:47:22205}
206
207bool PaymentRequestSpec::has_payer_error() const {
208 return payer_errors_ &&
Jinho Bang7f19d892018-08-27 18:23:00209 (request_payer_email() || request_payer_name() ||
210 request_payer_phone()) &&
Jinho Bangcac8d9a02018-08-23 19:47:22211 !(payer_errors_->email.empty() && payer_errors_->name.empty() &&
212 payer_errors_->phone.empty());
213}
214
Rouslan Solomakhina9ff9282017-10-31 21:58:05215void PaymentRequestSpec::RecomputeSpecForDetails() {
Jinho Bang092e7162018-09-06 23:41:19216 // Reparse the |details_| and update the observers.
217 UpdateSelectedShippingOption(/*after_update=*/true);
218
mathp151bd31e2017-04-03 21:07:24219 NotifyOnSpecUpdated();
Anthony Vallee-Duboisdb030dd2017-05-19 18:04:51220 current_update_reason_ = UpdateReason::NONE;
mathp151bd31e2017-04-03 21:07:24221}
222
mathpf1a7a3752017-03-15 11:23:37223void PaymentRequestSpec::AddObserver(Observer* observer) {
224 CHECK(observer);
225 observers_.AddObserver(observer);
226}
227
228void PaymentRequestSpec::RemoveObserver(Observer* observer) {
229 observers_.RemoveObserver(observer);
230}
231
tmartino5f0912b82017-03-30 03:20:52232bool PaymentRequestSpec::request_shipping() const {
233 return options_->request_shipping;
234}
235bool PaymentRequestSpec::request_payer_name() const {
236 return options_->request_payer_name;
237}
238bool PaymentRequestSpec::request_payer_phone() const {
239 return options_->request_payer_phone;
240}
241bool PaymentRequestSpec::request_payer_email() const {
242 return options_->request_payer_email;
243}
244
245PaymentShippingType PaymentRequestSpec::shipping_type() const {
246 // Transform Mojo-specific enum into platform-agnostic equivalent.
247 switch (options_->shipping_type) {
248 case mojom::PaymentShippingType::DELIVERY:
249 return PaymentShippingType::DELIVERY;
250 case payments::mojom::PaymentShippingType::PICKUP:
251 return PaymentShippingType::PICKUP;
252 case payments::mojom::PaymentShippingType::SHIPPING:
253 return PaymentShippingType::SHIPPING;
254 default:
255 NOTREACHED();
256 }
257 // Needed for compilation on some platforms.
258 return PaymentShippingType::SHIPPING;
259}
260
mathp600bab52017-03-26 03:47:59261bool PaymentRequestSpec::IsMethodSupportedThroughBasicCard(
262 const std::string& method_name) {
263 return basic_card_specified_networks_.count(method_name) > 0;
264}
265
mathpc0d616a2017-03-15 14:09:33266base::string16 PaymentRequestSpec::GetFormattedCurrencyAmount(
Anthony Vallee-Dubois080d5b72017-05-11 22:34:04267 const mojom::PaymentCurrencyAmountPtr& currency_amount) {
Jinho Bang26b0ea42018-05-24 01:09:04268 CurrencyFormatter* formatter =
269 GetOrCreateCurrencyFormatter(currency_amount->currency, app_locale_);
Anthony Vallee-Dubois080d5b72017-05-11 22:34:04270 return formatter->Format(currency_amount->value);
mathpc0d616a2017-03-15 14:09:33271}
272
Anthony Vallee-Dubois080d5b72017-05-11 22:34:04273std::string PaymentRequestSpec::GetFormattedCurrencyCode(
274 const mojom::PaymentCurrencyAmountPtr& currency_amount) {
Jinho Bang26b0ea42018-05-24 01:09:04275 CurrencyFormatter* formatter =
276 GetOrCreateCurrencyFormatter(currency_amount->currency, app_locale_);
mathpc0d616a2017-03-15 14:09:33277
278 return formatter->formatted_currency_code();
279}
280
anthonyvd2f30baa12017-04-13 22:30:50281void PaymentRequestSpec::StartWaitingForUpdateWith(
282 PaymentRequestSpec::UpdateReason reason) {
Anthony Vallee-Duboisdb030dd2017-05-19 18:04:51283 current_update_reason_ = reason;
anthonyvd2f30baa12017-04-13 22:30:50284 for (auto& observer : observers_) {
285 observer.OnStartUpdating(reason);
286 }
287}
288
Anthony Vallee-Dubois080d5b72017-05-11 22:34:04289bool PaymentRequestSpec::IsMixedCurrency() const {
290 const std::string& total_currency = details_->total->amount->currency;
291 return std::any_of(details_->display_items.begin(),
292 details_->display_items.end(),
293 [&total_currency](const mojom::PaymentItemPtr& item) {
294 return item->amount->currency != total_currency;
295 });
296}
297
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49298const mojom::PaymentItemPtr& PaymentRequestSpec::GetTotal(
299 PaymentInstrument* selected_instrument) const {
300 const mojom::PaymentDetailsModifierPtr* modifier =
301 GetApplicableModifier(selected_instrument);
Rouslan Solomakhin4ad48942018-02-26 17:21:15302 return modifier && (*modifier)->total ? (*modifier)->total : details_->total;
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49303}
rouslan690997682017-05-09 18:07:39304
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49305std::vector<const mojom::PaymentItemPtr*> PaymentRequestSpec::GetDisplayItems(
306 PaymentInstrument* selected_instrument) const {
307 std::vector<const mojom::PaymentItemPtr*> display_items;
308 const mojom::PaymentDetailsModifierPtr* modifier =
309 GetApplicableModifier(selected_instrument);
gogerald7a0cc3e2017-09-19 03:35:48310 for (const auto& item : details_->display_items) {
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49311 display_items.push_back(&item);
mathpf1a7a3752017-03-15 11:23:37312 }
mathp363735b2017-03-16 18:08:05313
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49314 if (modifier) {
315 for (const auto& additional_item : (*modifier)->additional_display_items) {
316 display_items.push_back(&additional_item);
317 }
318 }
319 return display_items;
320}
Rouslan Solomakhin25d708b2017-06-23 17:12:03321
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49322const std::vector<mojom::PaymentShippingOptionPtr>&
323PaymentRequestSpec::GetShippingOptions() const {
Jinho Bang6f591c72018-12-05 09:26:43324 DCHECK(details_->shipping_options);
325 return *details_->shipping_options;
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49326}
327
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49328const mojom::PaymentDetailsModifierPtr*
329PaymentRequestSpec::GetApplicableModifier(
330 PaymentInstrument* selected_instrument) const {
Mathieu Perreault51339b82017-07-20 17:06:05331 if (!selected_instrument ||
332 !base::FeatureList::IsEnabled(features::kWebPaymentsModifiers))
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49333 return nullptr;
334
gogerald7a0cc3e2017-09-19 03:35:48335 for (const auto& modifier : details_->modifiers) {
gogerald6cf12fe2017-11-08 21:29:55336 std::set<std::string> supported_card_networks_set;
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49337 std::set<autofill::CreditCard::CardType> supported_types;
Randall Raymondec4e0852017-07-14 01:30:45338 // The following 4 are unused but required by PopulateValidatedMethodData.
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49339 std::set<std::string> basic_card_specified_networks;
gogerald6cf12fe2017-11-08 21:29:55340 std::vector<std::string> supported_networks;
Randall Raymond6a85ba0ab2017-08-04 23:11:54341 std::vector<GURL> url_payment_method_identifiers;
Rouslan Solomakhin4eea9bc22017-10-10 15:18:51342 std::set<std::string> payment_method_identifiers_set;
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49343 std::map<std::string, std::set<std::string>> stringified_method_data;
344 PopulateValidatedMethodData(
Mohamad Ahmadif5544bb2017-09-01 21:48:22345 {ConvertPaymentMethodData(modifier->method_data)}, &supported_networks,
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49346 &basic_card_specified_networks, &supported_card_networks_set,
Randall Raymondec4e0852017-07-14 01:30:45347 &supported_types, &url_payment_method_identifiers,
Rouslan Solomakhin4eea9bc22017-10-10 15:18:51348 &payment_method_identifiers_set, &stringified_method_data);
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49349
350 if (selected_instrument->IsValidForModifier(
Jinho Bangd252ebf5b12018-07-18 00:00:59351 modifier->method_data->supported_method,
gogerald6cf12fe2017-11-08 21:29:55352 !modifier->method_data->supported_networks.empty(),
353 supported_card_networks_set,
354 !modifier->method_data->supported_types.empty(), supported_types)) {
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49355 return &modifier;
356 }
357 }
358 return nullptr;
mathpf1a7a3752017-03-15 11:23:37359}
360
mathpb77b8732017-05-11 15:26:42361void PaymentRequestSpec::UpdateSelectedShippingOption(bool after_update) {
Jinho Bang6f591c72018-12-05 09:26:43362 if (!request_shipping() || !details_->shipping_options)
mathp151bd31e2017-04-03 21:07:24363 return;
364
mathpb77b8732017-05-11 15:26:42365 selected_shipping_option_ = nullptr;
mathpeb8892ff2017-05-04 18:42:55366 selected_shipping_option_error_.clear();
Rouslan Solomakhindfcf1e7dc2019-02-28 18:43:23367 if (details_->shipping_options->empty() || !details_->error.empty()) {
mathpb77b8732017-05-11 15:26:42368 // No options are provided by the merchant.
369 if (after_update) {
Mathieu Perreault2c1f3192017-05-18 14:45:28370 // This is after an update, which means that the selected address is not
mathpb77b8732017-05-11 15:26:42371 // supported. The merchant may have customized the error string, or a
372 // generic one is used.
gogerald7a0cc3e2017-09-19 03:35:48373 if (!details_->error.empty()) {
374 selected_shipping_option_error_ = base::UTF8ToUTF16(details_->error);
mathpb77b8732017-05-11 15:26:42375 } else {
Mathieu Perreault2c1f3192017-05-18 14:45:28376 // The generic error string depends on the shipping type.
377 switch (shipping_type()) {
378 case PaymentShippingType::DELIVERY:
379 selected_shipping_option_error_ = l10n_util::GetStringUTF16(
380 IDS_PAYMENTS_UNSUPPORTED_DELIVERY_ADDRESS);
381 break;
382 case PaymentShippingType::PICKUP:
383 selected_shipping_option_error_ = l10n_util::GetStringUTF16(
384 IDS_PAYMENTS_UNSUPPORTED_PICKUP_ADDRESS);
385 break;
386 case PaymentShippingType::SHIPPING:
387 selected_shipping_option_error_ = l10n_util::GetStringUTF16(
388 IDS_PAYMENTS_UNSUPPORTED_SHIPPING_ADDRESS);
389 break;
390 }
mathpb77b8732017-05-11 15:26:42391 }
mathpb77b8732017-05-11 15:26:42392 }
393 return;
394 }
395
mathp151bd31e2017-04-03 21:07:24396 // As per the spec, the selected shipping option should initially be the last
mathpb77b8732017-05-11 15:26:42397 // one in the array that has its selected field set to true. If none are
398 // selected by the merchant, |selected_shipping_option_| stays nullptr.
mathp151bd31e2017-04-03 21:07:24399 auto selected_shipping_option_it = std::find_if(
Jinho Bang6f591c72018-12-05 09:26:43400 details_->shipping_options->rbegin(), details_->shipping_options->rend(),
mathp151bd31e2017-04-03 21:07:24401 [](const payments::mojom::PaymentShippingOptionPtr& element) {
402 return element->selected;
403 });
Jinho Bang6f591c72018-12-05 09:26:43404 if (selected_shipping_option_it != details_->shipping_options->rend()) {
mathp151bd31e2017-04-03 21:07:24405 selected_shipping_option_ = selected_shipping_option_it->get();
406 }
407}
408
mathp151bd31e2017-04-03 21:07:24409void PaymentRequestSpec::NotifyOnSpecUpdated() {
410 for (auto& observer : observers_)
411 observer.OnSpecUpdated();
mathpf1a7a3752017-03-15 11:23:37412}
413
mathpc0d616a2017-03-15 14:09:33414CurrencyFormatter* PaymentRequestSpec::GetOrCreateCurrencyFormatter(
415 const std::string& currency_code,
mathpc0d616a2017-03-15 14:09:33416 const std::string& locale_name) {
Anthony Vallee-Dubois080d5b72017-05-11 22:34:04417 // Create a currency formatter for |currency_code|, or if already created
418 // return the cached version.
419 std::pair<std::map<std::string, CurrencyFormatter>::iterator, bool>
420 emplace_result = currency_formatters_.emplace(
421 std::piecewise_construct, std::forward_as_tuple(currency_code),
Jinho Bang26b0ea42018-05-24 01:09:04422 std::forward_as_tuple(currency_code, locale_name));
Anthony Vallee-Dubois080d5b72017-05-11 22:34:04423
424 return &(emplace_result.first->second);
mathpc0d616a2017-03-15 14:09:33425}
426
mathpf1a7a3752017-03-15 11:23:37427} // namespace payments