blob: a3e8322571d6c762b967b06971c3d7a6fda8bb95 [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
9#include "base/logging.h"
mathpb65623a2017-04-06 02:01:5410#include "components/payments/core/payment_method_data.h"
11#include "components/payments/core/payment_request_data_util.h"
mathpf1a7a3752017-03-15 11:23:3712
13namespace payments {
14
mathpb65623a2017-04-06 02:01:5415namespace {
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.
19std::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
mathp600bab52017-03-26 03:47:5944const char kBasicCardMethodName[] = "basic-card";
mathpf1a7a3752017-03-15 11:23:3745
46PaymentRequestSpec::PaymentRequestSpec(
47 mojom::PaymentOptionsPtr options,
48 mojom::PaymentDetailsPtr details,
49 std::vector<mojom::PaymentMethodDataPtr> method_data,
mathpc0d616a2017-03-15 14:09:3350 Observer* observer,
51 const std::string& app_locale)
52 : options_(std::move(options)),
53 details_(std::move(details)),
mathp151bd31e2017-04-03 21:07:2454 app_locale_(app_locale),
55 selected_shipping_option_(nullptr),
56 observer_for_testing_(nullptr) {
mathpf1a7a3752017-03-15 11:23:3757 if (observer)
58 AddObserver(observer);
mathp151bd31e2017-04-03 21:07:2459 UpdateSelectedShippingOption();
mathpf1a7a3752017-03-15 11:23:3760 PopulateValidatedMethodData(method_data);
61}
62PaymentRequestSpec::~PaymentRequestSpec() {}
63
mathp151bd31e2017-04-03 21:07:2464void 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
mathpf1a7a3752017-03-15 11:23:3771void PaymentRequestSpec::AddObserver(Observer* observer) {
72 CHECK(observer);
73 observers_.AddObserver(observer);
74}
75
76void PaymentRequestSpec::RemoveObserver(Observer* observer) {
77 observers_.RemoveObserver(observer);
78}
79
tmartino5f0912b82017-03-30 03:20:5280bool PaymentRequestSpec::request_shipping() const {
81 return options_->request_shipping;
82}
83bool PaymentRequestSpec::request_payer_name() const {
84 return options_->request_payer_name;
85}
86bool PaymentRequestSpec::request_payer_phone() const {
87 return options_->request_payer_phone;
88}
89bool PaymentRequestSpec::request_payer_email() const {
90 return options_->request_payer_email;
91}
92
93PaymentShippingType 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
mathp600bab52017-03-26 03:47:59109bool PaymentRequestSpec::IsMethodSupportedThroughBasicCard(
110 const std::string& method_name) {
111 return basic_card_specified_networks_.count(method_name) > 0;
112}
113
mathpc0d616a2017-03-15 14:09:33114base::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
122std::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
mathpf1a7a3752017-03-15 11:23:37130void PaymentRequestSpec::PopulateValidatedMethodData(
mathpb65623a2017-04-06 02:01:54131 const std::vector<mojom::PaymentMethodDataPtr>& method_data_mojom) {
132 if (method_data_mojom.empty()) {
mathpf1a7a3752017-03-15 11:23:37133 LOG(ERROR) << "Invalid payment methods or data";
134 NotifyOnInvalidSpecProvided();
135 return;
136 }
137
mathpb65623a2017-04-06 02:01:54138 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));
mathpf1a7a3752017-03-15 11:23:37149 }
mathpb65623a2017-04-06 02:01:54150 method_data.supported_networks = std::move(supported_networks);
mathpf1a7a3752017-03-15 11:23:37151
mathpb65623a2017-04-06 02:01:54152 // TODO(crbug.com/708603): Add browser-side support for
153 // |method_data.supported_types|.
154 method_data_vector.push_back(std::move(method_data));
mathpf1a7a3752017-03-15 11:23:37155 }
mathp363735b2017-03-16 18:08:05156
mathpb65623a2017-04-06 02:01:54157 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 }
mathp363735b2017-03-16 18:08:05164 supported_card_networks_set_.insert(supported_card_networks_.begin(),
165 supported_card_networks_.end());
mathpf1a7a3752017-03-15 11:23:37166}
167
mathp151bd31e2017-04-03 21:07:24168void 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
mathpf1a7a3752017-03-15 11:23:37184void PaymentRequestSpec::NotifyOnInvalidSpecProvided() {
185 for (auto& observer : observers_)
186 observer.OnInvalidSpecProvided();
mathp151bd31e2017-04-03 21:07:24187 if (observer_for_testing_)
188 observer_for_testing_->OnInvalidSpecProvided();
189}
190
191void PaymentRequestSpec::NotifyOnSpecUpdated() {
192 for (auto& observer : observers_)
193 observer.OnSpecUpdated();
194 if (observer_for_testing_)
195 observer_for_testing_->OnSpecUpdated();
mathpf1a7a3752017-03-15 11:23:37196}
197
mathpc0d616a2017-03-15 14:09:33198CurrencyFormatter* 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
mathpf1a7a3752017-03-15 11:23:37209} // namespace payments