blob: 9170efaf382947675f4a3704cf0121ae4fda6508 [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
mathp600bab52017-03-26 03:47:5923// Identifier for the basic card payment method in the PaymentMethodData.
24extern const char kBasicCardMethodName[];
25
mathpf1a7a3752017-03-15 11:23:3726// 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).
tmartino5f0912b82017-03-30 03:20:5229class PaymentRequestSpec : public PaymentOptionsProvider {
mathpf1a7a3752017-03-15 11:23:3730 public:
anthonyvd2f30baa12017-04-13 22:30:5031 // 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
mathpf1a7a3752017-03-15 11:23:3739 // Any class call add itself as Observer via AddObserver() and receive
40 // notification about spec events.
41 class Observer {
42 public:
anthonyvd2f30baa12017-04-13 22:30:5043 // 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
mathp151bd31e2017-04-03 21:07:2448 // Called when the provided spec has changed.
49 virtual void OnSpecUpdated() = 0;
50
mathpf1a7a3752017-03-15 11:23:3751 protected:
52 virtual ~Observer() {}
53 };
54
55 PaymentRequestSpec(mojom::PaymentOptionsPtr options,
56 mojom::PaymentDetailsPtr details,
57 std::vector<mojom::PaymentMethodDataPtr> method_data,
mathpc0d616a2017-03-15 14:09:3358 PaymentRequestSpec::Observer* observer,
59 const std::string& app_locale);
tmartino5f0912b82017-03-30 03:20:5260 ~PaymentRequestSpec() override;
mathpf1a7a3752017-03-15 11:23:3761
mathp151bd31e2017-04-03 21:07:2462 // Called when the merchant has new PaymentDetails. Will recompute every spec
63 // state that depends on |details|.
64 void UpdateWith(mojom::PaymentDetailsPtr details);
65
mathpf1a7a3752017-03-15 11:23:3766 void AddObserver(Observer* observer);
67 void RemoveObserver(Observer* observer);
68
tmartino5f0912b82017-03-30 03:20:5269 // 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;
mathpf1a7a3752017-03-15 11:23:3775
mathp363735b2017-03-16 18:08:0576 const std::vector<std::string>& supported_card_networks() const {
mathpf1a7a3752017-03-15 11:23:3777 return supported_card_networks_;
78 }
mathp363735b2017-03-16 18:08:0579 const std::set<std::string>& supported_card_networks_set() const {
80 return supported_card_networks_set_;
81 }
rouslan690997682017-05-09 18:07:3982 const std::map<std::string, std::set<std::string>>& stringified_method_data()
83 const {
84 return stringified_method_data_;
85 }
Rouslan Solomakhin25d708b2017-06-23 17:12:0386 const std::set<autofill::CreditCard::CardType>& supported_card_types_set()
87 const {
88 return supported_card_types_set_;
89 }
mathp600bab52017-03-26 03:47:5990 // 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);
mathpf1a7a3752017-03-15 11:23:3794
Anthony Vallee-Dubois080d5b72017-05-11 22:34:0495 // 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);
mathpc0d616a2017-03-15 14:09:3399
Anthony Vallee-Dubois080d5b72017-05-11 22:34:04100 // Uses CurrencyFormatter to get the formatted currency code for
101 // |currency_amount|'s currency.
102 std::string GetFormattedCurrencyCode(
103 const mojom::PaymentCurrencyAmountPtr& currency_amount);
mathpc0d616a2017-03-15 14:09:33104
mathp151bd31e2017-04-03 21:07:24105 mojom::PaymentShippingOption* selected_shipping_option() const {
106 return selected_shipping_option_;
107 }
Mathieu Perreault04b4c662017-06-02 13:35:13108 // 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.
mathpeb8892ff2017-05-04 18:42:55111 const base::string16& selected_shipping_option_error() const {
112 return selected_shipping_option_error_;
113 }
mathp151bd31e2017-04-03 21:07:24114
mathpf1a7a3752017-03-15 11:23:37115 const mojom::PaymentDetails& details() const { return *details_.get(); }
mathpf1a7a3752017-03-15 11:23:37116
anthonyvd2f30baa12017-04-13 22:30:50117 void StartWaitingForUpdateWith(UpdateReason reason);
118
Anthony Vallee-Dubois080d5b72017-05-11 22:34:04119 bool IsMixedCurrency() const;
120
Anthony Vallee-Duboisdb030dd2017-05-19 18:04:51121 UpdateReason current_update_reason() const { return current_update_reason_; }
mathp151bd31e2017-04-03 21:07:24122
Anthony Vallee-Duboisdb030dd2017-05-19 18:04:51123 private:
mathpb65623a2017-04-06 02:01:54124 // Validates the |method_data| and fills |supported_card_networks_|,
125 // |supported_card_networks_set_| and |basic_card_specified_networks_|.
mathpf1a7a3752017-03-15 11:23:37126 void PopulateValidatedMethodData(
127 const std::vector<mojom::PaymentMethodDataPtr>& method_data);
128
mathpb77b8732017-05-11 15:26:42129 // Updates the |selected_shipping_option| based on the data passed to this
mathp151bd31e2017-04-03 21:07:24130 // payment request by the website. This will set selected_shipping_option_ to
mathpb77b8732017-05-11 15:26:42131 // 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);
mathp151bd31e2017-04-03 21:07:24136
mathp151bd31e2017-04-03 21:07:24137 // Will notify all observers that the spec has changed.
138 void NotifyOnSpecUpdated();
mathpf1a7a3752017-03-15 11:23:37139
mathpc0d616a2017-03-15 14:09:33140 // 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
mathpf1a7a3752017-03-15 11:23:37149 mojom::PaymentOptionsPtr options_;
150 mojom::PaymentDetailsPtr details_;
mathpc0d616a2017-03-15 14:09:33151 const std::string app_locale_;
mathp151bd31e2017-04-03 21:07:24152 // The currently shipping option as specified by the merchant.
153 mojom::PaymentShippingOption* selected_shipping_option_;
mathpeb8892ff2017-05-04 18:42:55154 base::string16 selected_shipping_option_error_;
mathp151bd31e2017-04-03 21:07:24155
Anthony Vallee-Dubois080d5b72017-05-11 22:34:04156 // One currency formatter is instantiated and cached per currency code.
157 std::map<std::string, CurrencyFormatter> currency_formatters_;
mathpf1a7a3752017-03-15 11:23:37158
mathp363735b2017-03-16 18:08:05159 // 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.
mathpf1a7a3752017-03-15 11:23:37162 std::vector<std::string> supported_card_networks_;
mathp363735b2017-03-16 18:08:05163 std::set<std::string> supported_card_networks_set_;
mathpf1a7a3752017-03-15 11:23:37164
Rouslan Solomakhin25d708b2017-06-23 17:12:03165 std::set<autofill::CreditCard::CardType> supported_card_types_set_;
166
mathp600bab52017-03-26 03:47:59167 // 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
rouslan690997682017-05-09 18:07:39171 // 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-Duboisdb030dd2017-05-19 18:04:51175 // The reason why this payment request is waiting for updateWith.
176 UpdateReason current_update_reason_;
177
mathp151bd31e2017-04-03 21:07:24178 // The |observer_for_testing_| will fire after all the |observers_| have been
179 // notified.
mathpf1a7a3752017-03-15 11:23:37180 base::ObserverList<Observer> observers_;
mathpf1a7a3752017-03-15 11:23:37181
182 DISALLOW_COPY_AND_ASSIGN(PaymentRequestSpec);
183};
184
185} // namespace payments
186
187#endif // COMPONENTS_PAYMENTS_CONTENT_PAYMENT_REQUEST_SPEC_H_