| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "components/payments/content/payment_request_spec.h" | 5 #include "components/payments/content/payment_request_spec.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 | 10 |
| 11 namespace payments { | 11 namespace payments { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 // Identifier for the basic card payment method in the PaymentMethodData. | 14 // Identifier for the basic card payment method in the PaymentMethodData. |
| 15 static const char* const kBasicCardMethodName = "basic-card"; | 15 static const char* const kBasicCardMethodName = "basic-card"; |
| 16 } // namespace | 16 } // namespace |
| 17 | 17 |
| 18 PaymentRequestSpec::PaymentRequestSpec( | 18 PaymentRequestSpec::PaymentRequestSpec( |
| 19 mojom::PaymentOptionsPtr options, | 19 mojom::PaymentOptionsPtr options, |
| 20 mojom::PaymentDetailsPtr details, | 20 mojom::PaymentDetailsPtr details, |
| 21 std::vector<mojom::PaymentMethodDataPtr> method_data, | 21 std::vector<mojom::PaymentMethodDataPtr> method_data, |
| 22 Observer* observer) | 22 Observer* observer, |
| 23 : options_(std::move(options)), details_(std::move(details)) { | 23 const std::string& app_locale) |
| 24 : options_(std::move(options)), |
| 25 details_(std::move(details)), |
| 26 app_locale_(app_locale) { |
| 24 if (observer) | 27 if (observer) |
| 25 AddObserver(observer); | 28 AddObserver(observer); |
| 26 PopulateValidatedMethodData(method_data); | 29 PopulateValidatedMethodData(method_data); |
| 27 } | 30 } |
| 28 PaymentRequestSpec::~PaymentRequestSpec() {} | 31 PaymentRequestSpec::~PaymentRequestSpec() {} |
| 29 | 32 |
| 30 void PaymentRequestSpec::AddObserver(Observer* observer) { | 33 void PaymentRequestSpec::AddObserver(Observer* observer) { |
| 31 CHECK(observer); | 34 CHECK(observer); |
| 32 observers_.AddObserver(observer); | 35 observers_.AddObserver(observer); |
| 33 } | 36 } |
| 34 | 37 |
| 35 void PaymentRequestSpec::RemoveObserver(Observer* observer) { | 38 void PaymentRequestSpec::RemoveObserver(Observer* observer) { |
| 36 observers_.RemoveObserver(observer); | 39 observers_.RemoveObserver(observer); |
| 37 } | 40 } |
| 38 | 41 |
| 42 base::string16 PaymentRequestSpec::GetFormattedCurrencyAmount( |
| 43 const std::string& amount) { |
| 44 CurrencyFormatter* formatter = GetOrCreateCurrencyFormatter( |
| 45 details_->total->amount->currency, |
| 46 details_->total->amount->currency_system, app_locale_); |
| 47 return formatter->Format(amount); |
| 48 } |
| 49 |
| 50 std::string PaymentRequestSpec::GetFormattedCurrencyCode() { |
| 51 CurrencyFormatter* formatter = GetOrCreateCurrencyFormatter( |
| 52 details_->total->amount->currency, |
| 53 details_->total->amount->currency_system, app_locale_); |
| 54 |
| 55 return formatter->formatted_currency_code(); |
| 56 } |
| 57 |
| 39 void PaymentRequestSpec::PopulateValidatedMethodData( | 58 void PaymentRequestSpec::PopulateValidatedMethodData( |
| 40 const std::vector<mojom::PaymentMethodDataPtr>& method_data) { | 59 const std::vector<mojom::PaymentMethodDataPtr>& method_data) { |
| 41 if (method_data.empty()) { | 60 if (method_data.empty()) { |
| 42 LOG(ERROR) << "Invalid payment methods or data"; | 61 LOG(ERROR) << "Invalid payment methods or data"; |
| 43 NotifyOnInvalidSpecProvided(); | 62 NotifyOnInvalidSpecProvided(); |
| 44 return; | 63 return; |
| 45 } | 64 } |
| 46 | 65 |
| 47 std::set<std::string> card_networks{"amex", "diners", "discover", | 66 std::set<std::string> card_networks{"amex", "diners", "discover", |
| 48 "jcb", "mastercard", "mir", | 67 "jcb", "mastercard", "mir", |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 } | 124 } |
| 106 } | 125 } |
| 107 } | 126 } |
| 108 } | 127 } |
| 109 | 128 |
| 110 void PaymentRequestSpec::NotifyOnInvalidSpecProvided() { | 129 void PaymentRequestSpec::NotifyOnInvalidSpecProvided() { |
| 111 for (auto& observer : observers_) | 130 for (auto& observer : observers_) |
| 112 observer.OnInvalidSpecProvided(); | 131 observer.OnInvalidSpecProvided(); |
| 113 } | 132 } |
| 114 | 133 |
| 134 CurrencyFormatter* PaymentRequestSpec::GetOrCreateCurrencyFormatter( |
| 135 const std::string& currency_code, |
| 136 const std::string& currency_system, |
| 137 const std::string& locale_name) { |
| 138 if (!currency_formatter_) { |
| 139 currency_formatter_.reset( |
| 140 new CurrencyFormatter(currency_code, currency_system, locale_name)); |
| 141 } |
| 142 return currency_formatter_.get(); |
| 143 } |
| 144 |
| 115 } // namespace payments | 145 } // namespace payments |
| OLD | NEW |