mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 1 | // 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 | |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 8 | #include <map> |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 9 | #include <set> |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "base/macros.h" |
| 14 | #include "base/observer_list.h" |
mathp | eb8892ff | 2017-05-04 18:42:55 | [diff] [blame] | 15 | #include "base/strings/string16.h" |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame] | 16 | #include "components/autofill/core/browser/credit_card.h" |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 17 | #include "components/payments/core/currency_formatter.h" |
tmartino | 5f0912b8 | 2017-03-30 03:20:52 | [diff] [blame] | 18 | #include "components/payments/core/payment_options_provider.h" |
Rouslan Solomakhin | 2c88e23 | 2017-06-14 20:28:52 | [diff] [blame] | 19 | #include "third_party/WebKit/public/platform/modules/payments/payment_request.mojom.h" |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 20 | |
| 21 | namespace payments { |
| 22 | |
Anthony Vallee-Dubois | 059d59a | 2017-07-07 15:05:49 | [diff] [blame] | 23 | class PaymentInstrument; |
| 24 | |
mathp | 600bab5 | 2017-03-26 03:47:59 | [diff] [blame] | 25 | // Identifier for the basic card payment method in the PaymentMethodData. |
| 26 | extern const char kBasicCardMethodName[]; |
| 27 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 28 | // 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). |
tmartino | 5f0912b8 | 2017-03-30 03:20:52 | [diff] [blame] | 31 | class PaymentRequestSpec : public PaymentOptionsProvider { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 32 | public: |
anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 33 | // 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 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 41 | // Any class call add itself as Observer via AddObserver() and receive |
| 42 | // notification about spec events. |
| 43 | class Observer { |
| 44 | public: |
anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 45 | // 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 | |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 50 | // Called when the provided spec has changed. |
| 51 | virtual void OnSpecUpdated() = 0; |
| 52 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 53 | protected: |
| 54 | virtual ~Observer() {} |
| 55 | }; |
| 56 | |
| 57 | PaymentRequestSpec(mojom::PaymentOptionsPtr options, |
| 58 | mojom::PaymentDetailsPtr details, |
| 59 | std::vector<mojom::PaymentMethodDataPtr> method_data, |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 60 | PaymentRequestSpec::Observer* observer, |
| 61 | const std::string& app_locale); |
tmartino | 5f0912b8 | 2017-03-30 03:20:52 | [diff] [blame] | 62 | ~PaymentRequestSpec() override; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 63 | |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 64 | // Called when the merchant has new PaymentDetails. Will recompute every spec |
| 65 | // state that depends on |details|. |
| 66 | void UpdateWith(mojom::PaymentDetailsPtr details); |
| 67 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 68 | void AddObserver(Observer* observer); |
| 69 | void RemoveObserver(Observer* observer); |
| 70 | |
tmartino | 5f0912b8 | 2017-03-30 03:20:52 | [diff] [blame] | 71 | // 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; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 77 | |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 78 | const std::vector<std::string>& supported_card_networks() const { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 79 | return supported_card_networks_; |
| 80 | } |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 81 | const std::set<std::string>& supported_card_networks_set() const { |
| 82 | return supported_card_networks_set_; |
| 83 | } |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 84 | const std::map<std::string, std::set<std::string>>& stringified_method_data() |
| 85 | const { |
| 86 | return stringified_method_data_; |
| 87 | } |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame] | 88 | const std::set<autofill::CreditCard::CardType>& supported_card_types_set() |
| 89 | const { |
| 90 | return supported_card_types_set_; |
| 91 | } |
Randall Raymond | ec4e085 | 2017-07-14 01:30:45 | [diff] [blame] | 92 | const std::vector<std::string>& url_payment_method_identifiers() const { |
| 93 | return url_payment_method_identifiers_; |
| 94 | } |
mathp | 600bab5 | 2017-03-26 03:47:59 | [diff] [blame] | 95 | // 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); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 99 | |
Anthony Vallee-Dubois | 080d5b7 | 2017-05-11 22:34:04 | [diff] [blame] | 100 | // 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); |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 104 | |
Anthony Vallee-Dubois | 080d5b7 | 2017-05-11 22:34:04 | [diff] [blame] | 105 | // Uses CurrencyFormatter to get the formatted currency code for |
| 106 | // |currency_amount|'s currency. |
| 107 | std::string GetFormattedCurrencyCode( |
| 108 | const mojom::PaymentCurrencyAmountPtr& currency_amount); |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 109 | |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 110 | mojom::PaymentShippingOption* selected_shipping_option() const { |
| 111 | return selected_shipping_option_; |
| 112 | } |
Mathieu Perreault | 04b4c66 | 2017-06-02 13:35:13 | [diff] [blame] | 113 | // 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. |
mathp | eb8892ff | 2017-05-04 18:42:55 | [diff] [blame] | 116 | const base::string16& selected_shipping_option_error() const { |
| 117 | return selected_shipping_option_error_; |
| 118 | } |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 119 | |
anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 120 | void StartWaitingForUpdateWith(UpdateReason reason); |
Anthony Vallee-Dubois | 080d5b7 | 2017-05-11 22:34:04 | [diff] [blame] | 121 | bool IsMixedCurrency() const; |
| 122 | |
Anthony Vallee-Dubois | db030dd | 2017-05-19 18:04:51 | [diff] [blame] | 123 | UpdateReason current_update_reason() const { return current_update_reason_; } |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 124 | |
Anthony Vallee-Dubois | 059d59a | 2017-07-07 15:05:49 | [diff] [blame] | 125 | // 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-Dubois | db030dd | 2017-05-19 18:04:51 | [diff] [blame] | 137 | private: |
Anthony Vallee-Dubois | 059d59a | 2017-07-07 15:05:49 | [diff] [blame] | 138 | // 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(); } |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 144 | |
mathp | b77b873 | 2017-05-11 15:26:42 | [diff] [blame] | 145 | // Updates the |selected_shipping_option| based on the data passed to this |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 146 | // payment request by the website. This will set selected_shipping_option_ to |
mathp | b77b873 | 2017-05-11 15:26:42 | [diff] [blame] | 147 | // 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); |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 152 | |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 153 | // Will notify all observers that the spec has changed. |
| 154 | void NotifyOnSpecUpdated(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 155 | |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 156 | // 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 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 165 | mojom::PaymentOptionsPtr options_; |
| 166 | mojom::PaymentDetailsPtr details_; |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 167 | const std::string app_locale_; |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 168 | // The currently shipping option as specified by the merchant. |
| 169 | mojom::PaymentShippingOption* selected_shipping_option_; |
mathp | eb8892ff | 2017-05-04 18:42:55 | [diff] [blame] | 170 | base::string16 selected_shipping_option_error_; |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 171 | |
Anthony Vallee-Dubois | 080d5b7 | 2017-05-11 22:34:04 | [diff] [blame] | 172 | // One currency formatter is instantiated and cached per currency code. |
| 173 | std::map<std::string, CurrencyFormatter> currency_formatters_; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 174 | |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 175 | // 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. |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 178 | std::vector<std::string> supported_card_networks_; |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 179 | std::set<std::string> supported_card_networks_set_; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 180 | |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame] | 181 | std::set<autofill::CreditCard::CardType> supported_card_types_set_; |
| 182 | |
mathp | 600bab5 | 2017-03-26 03:47:59 | [diff] [blame] | 183 | // 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 Raymond | ec4e085 | 2017-07-14 01:30:45 | [diff] [blame] | 187 | // 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 | |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 193 | // 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-Dubois | db030dd | 2017-05-19 18:04:51 | [diff] [blame] | 197 | // The reason why this payment request is waiting for updateWith. |
| 198 | UpdateReason current_update_reason_; |
| 199 | |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 200 | // The |observer_for_testing_| will fire after all the |observers_| have been |
| 201 | // notified. |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 202 | base::ObserverList<Observer> observers_; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 203 | |
| 204 | DISALLOW_COPY_AND_ASSIGN(PaymentRequestSpec); |
| 205 | }; |
| 206 | |
| 207 | } // namespace payments |
| 208 | |
| 209 | #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_SPEC_H_ |