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 | |
mathp | 600bab5 | 2017-03-26 03:47:59 | [diff] [blame] | 23 | // Identifier for the basic card payment method in the PaymentMethodData. |
| 24 | extern const char kBasicCardMethodName[]; |
| 25 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 26 | // The spec contains all the options that the merchant has specified about this |
| 27 | // Payment Request. It's a (mostly) read-only view, which can be updated in |
| 28 | // certain occasions by the merchant (see API). |
tmartino | 5f0912b8 | 2017-03-30 03:20:52 | [diff] [blame] | 29 | class PaymentRequestSpec : public PaymentOptionsProvider { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 30 | public: |
anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 31 | // This enum represents which bit of information was changed to trigger an |
| 32 | // update roundtrip with the website. |
| 33 | enum class UpdateReason { |
| 34 | NONE, |
| 35 | SHIPPING_OPTION, |
| 36 | SHIPPING_ADDRESS, |
| 37 | }; |
| 38 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 39 | // Any class call add itself as Observer via AddObserver() and receive |
| 40 | // notification about spec events. |
| 41 | class Observer { |
| 42 | public: |
anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 43 | // Called when the website is notified that the user selected shipping |
| 44 | // options or a shipping address. This will be followed by a call to |
| 45 | // OnSpecUpdated or the PaymentRequest being aborted due to a timeout. |
| 46 | virtual void OnStartUpdating(UpdateReason reason) {} |
| 47 | |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 48 | // Called when the provided spec has changed. |
| 49 | virtual void OnSpecUpdated() = 0; |
| 50 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 51 | protected: |
| 52 | virtual ~Observer() {} |
| 53 | }; |
| 54 | |
| 55 | PaymentRequestSpec(mojom::PaymentOptionsPtr options, |
| 56 | mojom::PaymentDetailsPtr details, |
| 57 | std::vector<mojom::PaymentMethodDataPtr> method_data, |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 58 | PaymentRequestSpec::Observer* observer, |
| 59 | const std::string& app_locale); |
tmartino | 5f0912b8 | 2017-03-30 03:20:52 | [diff] [blame] | 60 | ~PaymentRequestSpec() override; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 61 | |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 62 | // Called when the merchant has new PaymentDetails. Will recompute every spec |
| 63 | // state that depends on |details|. |
| 64 | void UpdateWith(mojom::PaymentDetailsPtr details); |
| 65 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 66 | void AddObserver(Observer* observer); |
| 67 | void RemoveObserver(Observer* observer); |
| 68 | |
tmartino | 5f0912b8 | 2017-03-30 03:20:52 | [diff] [blame] | 69 | // PaymentOptionsProvider: |
| 70 | bool request_shipping() const override; |
| 71 | bool request_payer_name() const override; |
| 72 | bool request_payer_phone() const override; |
| 73 | bool request_payer_email() const override; |
| 74 | PaymentShippingType shipping_type() const override; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 75 | |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 76 | const std::vector<std::string>& supported_card_networks() const { |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 77 | return supported_card_networks_; |
| 78 | } |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 79 | const std::set<std::string>& supported_card_networks_set() const { |
| 80 | return supported_card_networks_set_; |
| 81 | } |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 82 | const std::map<std::string, std::set<std::string>>& stringified_method_data() |
| 83 | const { |
| 84 | return stringified_method_data_; |
| 85 | } |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame^] | 86 | const std::set<autofill::CreditCard::CardType>& supported_card_types_set() |
| 87 | const { |
| 88 | return supported_card_types_set_; |
| 89 | } |
mathp | 600bab5 | 2017-03-26 03:47:59 | [diff] [blame] | 90 | // Returns whether the |method_name| was specified as supported through the |
| 91 | // "basic-card" payment method. If false, it means either the |method_name| is |
| 92 | // not supported at all, or specified directly in supportedMethods. |
| 93 | bool IsMethodSupportedThroughBasicCard(const std::string& method_name); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 94 | |
Anthony Vallee-Dubois | 080d5b7 | 2017-05-11 22:34:04 | [diff] [blame] | 95 | // Uses CurrencyFormatter to format the value of |currency_amount| with the |
| 96 | // currency symbol for its currency. |
| 97 | base::string16 GetFormattedCurrencyAmount( |
| 98 | const mojom::PaymentCurrencyAmountPtr& currency_amount); |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 99 | |
Anthony Vallee-Dubois | 080d5b7 | 2017-05-11 22:34:04 | [diff] [blame] | 100 | // Uses CurrencyFormatter to get the formatted currency code for |
| 101 | // |currency_amount|'s currency. |
| 102 | std::string GetFormattedCurrencyCode( |
| 103 | const mojom::PaymentCurrencyAmountPtr& currency_amount); |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 104 | |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 105 | mojom::PaymentShippingOption* selected_shipping_option() const { |
| 106 | return selected_shipping_option_; |
| 107 | } |
Mathieu Perreault | 04b4c66 | 2017-06-02 13:35:13 | [diff] [blame] | 108 | // This may contain a non-empty error returned by the merchant. In this case |
| 109 | // PaymentRequestState::selected_shipping_option_error_profile() will contain |
| 110 | // the profile related to the error. |
mathp | eb8892ff | 2017-05-04 18:42:55 | [diff] [blame] | 111 | const base::string16& selected_shipping_option_error() const { |
| 112 | return selected_shipping_option_error_; |
| 113 | } |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 114 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 115 | const mojom::PaymentDetails& details() const { return *details_.get(); } |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 116 | |
anthonyvd | 2f30baa1 | 2017-04-13 22:30:50 | [diff] [blame] | 117 | void StartWaitingForUpdateWith(UpdateReason reason); |
| 118 | |
Anthony Vallee-Dubois | 080d5b7 | 2017-05-11 22:34:04 | [diff] [blame] | 119 | bool IsMixedCurrency() const; |
| 120 | |
Anthony Vallee-Dubois | db030dd | 2017-05-19 18:04:51 | [diff] [blame] | 121 | UpdateReason current_update_reason() const { return current_update_reason_; } |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 122 | |
Anthony Vallee-Dubois | db030dd | 2017-05-19 18:04:51 | [diff] [blame] | 123 | private: |
mathp | b65623a | 2017-04-06 02:01:54 | [diff] [blame] | 124 | // Validates the |method_data| and fills |supported_card_networks_|, |
| 125 | // |supported_card_networks_set_| and |basic_card_specified_networks_|. |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 126 | void PopulateValidatedMethodData( |
| 127 | const std::vector<mojom::PaymentMethodDataPtr>& method_data); |
| 128 | |
mathp | b77b873 | 2017-05-11 15:26:42 | [diff] [blame] | 129 | // Updates the |selected_shipping_option| based on the data passed to this |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 130 | // payment request by the website. This will set selected_shipping_option_ to |
mathp | b77b873 | 2017-05-11 15:26:42 | [diff] [blame] | 131 | // the last option marked selected in the options array. If no options are |
| 132 | // provided and this method is called |after_update|, it means the merchant |
| 133 | // doesn't ship to this location. In this case, |
| 134 | // |selected_shipping_option_error_| will be set. |
| 135 | void UpdateSelectedShippingOption(bool after_update); |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 136 | |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 137 | // Will notify all observers that the spec has changed. |
| 138 | void NotifyOnSpecUpdated(); |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 139 | |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 140 | // Returns the CurrencyFormatter instance for this PaymentRequest. |
| 141 | // |locale_name| should be the result of the browser's GetApplicationLocale(). |
| 142 | // Note: Having multiple currencies per PaymentRequest is not supported; hence |
| 143 | // the CurrencyFormatter is cached here. |
| 144 | CurrencyFormatter* GetOrCreateCurrencyFormatter( |
| 145 | const std::string& currency_code, |
| 146 | const std::string& currency_system, |
| 147 | const std::string& locale_name); |
| 148 | |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 149 | mojom::PaymentOptionsPtr options_; |
| 150 | mojom::PaymentDetailsPtr details_; |
mathp | c0d616a | 2017-03-15 14:09:33 | [diff] [blame] | 151 | const std::string app_locale_; |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 152 | // The currently shipping option as specified by the merchant. |
| 153 | mojom::PaymentShippingOption* selected_shipping_option_; |
mathp | eb8892ff | 2017-05-04 18:42:55 | [diff] [blame] | 154 | base::string16 selected_shipping_option_error_; |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 155 | |
Anthony Vallee-Dubois | 080d5b7 | 2017-05-11 22:34:04 | [diff] [blame] | 156 | // One currency formatter is instantiated and cached per currency code. |
| 157 | std::map<std::string, CurrencyFormatter> currency_formatters_; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 158 | |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 159 | // A list/set of supported basic card networks. The list is used to keep the |
| 160 | // order in which they were specified by the merchant. The set is used for |
| 161 | // fast lookup of supported methods. |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 162 | std::vector<std::string> supported_card_networks_; |
mathp | 363735b | 2017-03-16 18:08:05 | [diff] [blame] | 163 | std::set<std::string> supported_card_networks_set_; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 164 | |
Rouslan Solomakhin | 25d708b | 2017-06-23 17:12:03 | [diff] [blame^] | 165 | std::set<autofill::CreditCard::CardType> supported_card_types_set_; |
| 166 | |
mathp | 600bab5 | 2017-03-26 03:47:59 | [diff] [blame] | 167 | // Only the set of basic-card specified networks. NOTE: callers should use |
| 168 | // |supported_card_networks_set_| to check merchant support. |
| 169 | std::set<std::string> basic_card_specified_networks_; |
| 170 | |
rouslan | 69099768 | 2017-05-09 18:07:39 | [diff] [blame] | 171 | // A mapping of the payment method names to the corresponding JSON-stringified |
| 172 | // payment method specific data. |
| 173 | std::map<std::string, std::set<std::string>> stringified_method_data_; |
| 174 | |
Anthony Vallee-Dubois | db030dd | 2017-05-19 18:04:51 | [diff] [blame] | 175 | // The reason why this payment request is waiting for updateWith. |
| 176 | UpdateReason current_update_reason_; |
| 177 | |
mathp | 151bd31e | 2017-04-03 21:07:24 | [diff] [blame] | 178 | // The |observer_for_testing_| will fire after all the |observers_| have been |
| 179 | // notified. |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 180 | base::ObserverList<Observer> observers_; |
mathp | f1a7a375 | 2017-03-15 11:23:37 | [diff] [blame] | 181 | |
| 182 | DISALLOW_COPY_AND_ASSIGN(PaymentRequestSpec); |
| 183 | }; |
| 184 | |
| 185 | } // namespace payments |
| 186 | |
| 187 | #endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_SPEC_H_ |