blob: 798e31dd360e7e6bdf8fb725792a523308bd6497 [file] [log] [blame]
mathpf1a7a3752017-03-15 11:23:371// Copyright 2017 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#ifndef COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_SPEC_H_
6#define COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_SPEC_H_
7
rouslan690997682017-05-09 18:07:398#include <map>
mathp363735b2017-03-16 18:08:059#include <set>
mathpf1a7a3752017-03-15 11:23:3710#include <string>
11#include <vector>
12
13#include "base/macros.h"
14#include "base/observer_list.h"
mathpeb8892ff2017-05-04 18:42:5515#include "base/strings/string16.h"
Rouslan Solomakhin25d708b2017-06-23 17:12:0316#include "components/autofill/core/browser/credit_card.h"
mathpc0d616a2017-03-15 14:09:3317#include "components/payments/core/currency_formatter.h"
tmartino5f0912b82017-03-30 03:20:5218#include "components/payments/core/payment_options_provider.h"
Rouslan Solomakhin2c88e232017-06-14 20:28:5219#include "third_party/WebKit/public/platform/modules/payments/payment_request.mojom.h"
mathpf1a7a3752017-03-15 11:23:3720
21namespace payments {
22
Anthony Vallee-Dubois059d59a2017-07-07 15:05:4923class PaymentInstrument;
24
mathp600bab52017-03-26 03:47:5925// Identifier for the basic card payment method in the PaymentMethodData.
26extern const char kBasicCardMethodName[];
27
mathpf1a7a3752017-03-15 11:23:3728// The spec contains all the options that the merchant has specified about this
29// Payment Request. It's a (mostly) read-only view, which can be updated in
30// certain occasions by the merchant (see API).
tmartino5f0912b82017-03-30 03:20:5231class PaymentRequestSpec : public PaymentOptionsProvider {
mathpf1a7a3752017-03-15 11:23:3732 public:
anthonyvd2f30baa12017-04-13 22:30:5033 // This enum represents which bit of information was changed to trigger an
34 // update roundtrip with the website.
35 enum class UpdateReason {
36 NONE,
37 SHIPPING_OPTION,
38 SHIPPING_ADDRESS,
39 };
40
mathpf1a7a3752017-03-15 11:23:3741 // Any class call add itself as Observer via AddObserver() and receive
42 // notification about spec events.
43 class Observer {
44 public:
anthonyvd2f30baa12017-04-13 22:30:5045 // Called when the website is notified that the user selected shipping
46 // options or a shipping address. This will be followed by a call to
47 // OnSpecUpdated or the PaymentRequest being aborted due to a timeout.
48 virtual void OnStartUpdating(UpdateReason reason) {}
49
mathp151bd31e2017-04-03 21:07:2450 // Called when the provided spec has changed.
51 virtual void OnSpecUpdated() = 0;
52
mathpf1a7a3752017-03-15 11:23:3753 protected:
54 virtual ~Observer() {}
55 };
56
57 PaymentRequestSpec(mojom::PaymentOptionsPtr options,
58 mojom::PaymentDetailsPtr details,
59 std::vector<mojom::PaymentMethodDataPtr> method_data,
mathpc0d616a2017-03-15 14:09:3360 PaymentRequestSpec::Observer* observer,
61 const std::string& app_locale);
tmartino5f0912b82017-03-30 03:20:5262 ~PaymentRequestSpec() override;
mathpf1a7a3752017-03-15 11:23:3763
mathp151bd31e2017-04-03 21:07:2464 // Called when the merchant has new PaymentDetails. Will recompute every spec
65 // state that depends on |details|.
66 void UpdateWith(mojom::PaymentDetailsPtr details);
67
mathpf1a7a3752017-03-15 11:23:3768 void AddObserver(Observer* observer);
69 void RemoveObserver(Observer* observer);
70
tmartino5f0912b82017-03-30 03:20:5271 // PaymentOptionsProvider:
72 bool request_shipping() const override;
73 bool request_payer_name() const override;
74 bool request_payer_phone() const override;
75 bool request_payer_email() const override;
76 PaymentShippingType shipping_type() const override;
mathpf1a7a3752017-03-15 11:23:3777
mathp363735b2017-03-16 18:08:0578 const std::vector<std::string>& supported_card_networks() const {
mathpf1a7a3752017-03-15 11:23:3779 return supported_card_networks_;
80 }
mathp363735b2017-03-16 18:08:0581 const std::set<std::string>& supported_card_networks_set() const {
82 return supported_card_networks_set_;
83 }
rouslan690997682017-05-09 18:07:3984 const std::map<std::string, std::set<std::string>>& stringified_method_data()
85 const {
86 return stringified_method_data_;
87 }
Rouslan Solomakhin25d708b2017-06-23 17:12:0388 const std::set<autofill::CreditCard::CardType>& supported_card_types_set()
89 const {
90 return supported_card_types_set_;
91 }
Randall Raymondec4e0852017-07-14 01:30:4592 const std::vector<std::string>& url_payment_method_identifiers() const {
93 return url_payment_method_identifiers_;
94 }
mathp600bab52017-03-26 03:47:5995 // Returns whether the |method_name| was specified as supported through the
96 // "basic-card" payment method. If false, it means either the |method_name| is
97 // not supported at all, or specified directly in supportedMethods.
98 bool IsMethodSupportedThroughBasicCard(const std::string& method_name);
mathpf1a7a3752017-03-15 11:23:3799
Anthony Vallee-Dubois080d5b72017-05-11 22:34:04100 // Uses CurrencyFormatter to format the value of |currency_amount| with the
101 // currency symbol for its currency.
102 base::string16 GetFormattedCurrencyAmount(
103 const mojom::PaymentCurrencyAmountPtr& currency_amount);
mathpc0d616a2017-03-15 14:09:33104
Anthony Vallee-Dubois080d5b72017-05-11 22:34:04105 // Uses CurrencyFormatter to get the formatted currency code for
106 // |currency_amount|'s currency.
107 std::string GetFormattedCurrencyCode(
108 const mojom::PaymentCurrencyAmountPtr& currency_amount);
mathpc0d616a2017-03-15 14:09:33109
mathp151bd31e2017-04-03 21:07:24110 mojom::PaymentShippingOption* selected_shipping_option() const {
111 return selected_shipping_option_;
112 }
Mathieu Perreault04b4c662017-06-02 13:35:13113 // This may contain a non-empty error returned by the merchant. In this case
114 // PaymentRequestState::selected_shipping_option_error_profile() will contain
115 // the profile related to the error.
mathpeb8892ff2017-05-04 18:42:55116 const base::string16& selected_shipping_option_error() const {
117 return selected_shipping_option_error_;
118 }
mathp151bd31e2017-04-03 21:07:24119
anthonyvd2f30baa12017-04-13 22:30:50120 void StartWaitingForUpdateWith(UpdateReason reason);
Anthony Vallee-Dubois080d5b72017-05-11 22:34:04121 bool IsMixedCurrency() const;
122
Anthony Vallee-Duboisdb030dd2017-05-19 18:04:51123 UpdateReason current_update_reason() const { return current_update_reason_; }
mathp151bd31e2017-04-03 21:07:24124
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49125 // Returns the total object of this payment request, taking into account the
126 // applicable modifier for |selected_instrument| if any.
127 const mojom::PaymentItemPtr& GetTotal(
128 PaymentInstrument* selected_instrument) const;
129 // Returns the display items for this payment request, taking into account the
130 // applicable modifier for |selected_instrument| if any.
131 std::vector<const mojom::PaymentItemPtr*> GetDisplayItems(
132 PaymentInstrument* selected_instrument) const;
133
134 const std::vector<mojom::PaymentShippingOptionPtr>& GetShippingOptions()
135 const;
136
Anthony Vallee-Duboisdb030dd2017-05-19 18:04:51137 private:
Anthony Vallee-Dubois059d59a2017-07-07 15:05:49138 // Returns the first applicable modifier in the Payment Request for the
139 // |selected_instrument|.
140 const mojom::PaymentDetailsModifierPtr* GetApplicableModifier(
141 PaymentInstrument* selected_instrument) const;
142
143 const mojom::PaymentDetails& details() const { return *details_.get(); }
mathpf1a7a3752017-03-15 11:23:37144
mathpb77b8732017-05-11 15:26:42145 // Updates the |selected_shipping_option| based on the data passed to this
mathp151bd31e2017-04-03 21:07:24146 // payment request by the website. This will set selected_shipping_option_ to
mathpb77b8732017-05-11 15:26:42147 // the last option marked selected in the options array. If no options are
148 // provided and this method is called |after_update|, it means the merchant
149 // doesn't ship to this location. In this case,
150 // |selected_shipping_option_error_| will be set.
151 void UpdateSelectedShippingOption(bool after_update);
mathp151bd31e2017-04-03 21:07:24152
mathp151bd31e2017-04-03 21:07:24153 // Will notify all observers that the spec has changed.
154 void NotifyOnSpecUpdated();
mathpf1a7a3752017-03-15 11:23:37155
mathpc0d616a2017-03-15 14:09:33156 // Returns the CurrencyFormatter instance for this PaymentRequest.
157 // |locale_name| should be the result of the browser's GetApplicationLocale().
158 // Note: Having multiple currencies per PaymentRequest is not supported; hence
159 // the CurrencyFormatter is cached here.
160 CurrencyFormatter* GetOrCreateCurrencyFormatter(
161 const std::string& currency_code,
162 const std::string& currency_system,
163 const std::string& locale_name);
164
mathpf1a7a3752017-03-15 11:23:37165 mojom::PaymentOptionsPtr options_;
166 mojom::PaymentDetailsPtr details_;
mathpc0d616a2017-03-15 14:09:33167 const std::string app_locale_;
mathp151bd31e2017-04-03 21:07:24168 // The currently shipping option as specified by the merchant.
169 mojom::PaymentShippingOption* selected_shipping_option_;
mathpeb8892ff2017-05-04 18:42:55170 base::string16 selected_shipping_option_error_;
mathp151bd31e2017-04-03 21:07:24171
Anthony Vallee-Dubois080d5b72017-05-11 22:34:04172 // One currency formatter is instantiated and cached per currency code.
173 std::map<std::string, CurrencyFormatter> currency_formatters_;
mathpf1a7a3752017-03-15 11:23:37174
mathp363735b2017-03-16 18:08:05175 // A list/set of supported basic card networks. The list is used to keep the
176 // order in which they were specified by the merchant. The set is used for
177 // fast lookup of supported methods.
mathpf1a7a3752017-03-15 11:23:37178 std::vector<std::string> supported_card_networks_;
mathp363735b2017-03-16 18:08:05179 std::set<std::string> supported_card_networks_set_;
mathpf1a7a3752017-03-15 11:23:37180
Rouslan Solomakhin25d708b2017-06-23 17:12:03181 std::set<autofill::CreditCard::CardType> supported_card_types_set_;
182
mathp600bab52017-03-26 03:47:59183 // Only the set of basic-card specified networks. NOTE: callers should use
184 // |supported_card_networks_set_| to check merchant support.
185 std::set<std::string> basic_card_specified_networks_;
186
Randall Raymondec4e0852017-07-14 01:30:45187 // A list of supported url-based payment method identifers specified by the
188 // merchant. This encompasses one of the two types of payment method
189 // identifers, the other being standardized payment method identifiers i.e.,
190 // basic-card.
191 std::vector<std::string> url_payment_method_identifiers_;
192
rouslan690997682017-05-09 18:07:39193 // A mapping of the payment method names to the corresponding JSON-stringified
194 // payment method specific data.
195 std::map<std::string, std::set<std::string>> stringified_method_data_;
196
Anthony Vallee-Duboisdb030dd2017-05-19 18:04:51197 // The reason why this payment request is waiting for updateWith.
198 UpdateReason current_update_reason_;
199
mathp151bd31e2017-04-03 21:07:24200 // The |observer_for_testing_| will fire after all the |observers_| have been
201 // notified.
mathpf1a7a3752017-03-15 11:23:37202 base::ObserverList<Observer> observers_;
mathpf1a7a3752017-03-15 11:23:37203
204 DISALLOW_COPY_AND_ASSIGN(PaymentRequestSpec);
205};
206
207} // namespace payments
208
209#endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_SPEC_H_